Skip to content

Commit

Permalink
Add std.system()
Browse files Browse the repository at this point in the history
  • Loading branch information
vtereshkov committed Dec 29, 2021
1 parent 628eec9 commit 555af45
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions import_embed/std.um
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,5 @@ fn getenv*(name: str): str {
return "" + rtlgetenv(name)
}

fn rtlsystem(command: str): int
fn system*(command: str): int {return rtlsystem(command)}
2 changes: 2 additions & 0 deletions import_embed/umka_runtime_src.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ static const char *rtlSrc =
" return \"\" + rtlgetenv(name)\n"
"}\n"
"\n"
"fn rtlsystem(command: str): int\n"
"fn system*(command: str): int {return rtlsystem(command)}\n"
" ";

#endif // UMKA_RUNTIME_SRC_H_INCLUDED
8 changes: 8 additions & 0 deletions spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,8 @@ Returns the number of seconds since the start of the program.

### Command line and environment

#### Functions

```
fn argc*(): int
```
Expand All @@ -1399,6 +1401,12 @@ fn getenv*(name: str): str

Returns the environment variable with the specified `name`.

```
fn system*(command: str): int
```

Invokes the command processor to execute a `command`. Returns a platform-specific result.

## Embedding API

The Umka interpreter is a shared library that provides the API for embedding into a C/C++ host application.
Expand Down
1 change: 1 addition & 0 deletions src/umka_compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ static void compilerDeclareExternalFuncs(Compiler *comp)
externalAdd(&comp->externals, "rtltime", &rtltime);
externalAdd(&comp->externals, "rtlclock", &rtlclock);
externalAdd(&comp->externals, "rtlgetenv", &rtlgetenv);
externalAdd(&comp->externals, "rtlsystem", &rtlsystem);
}


Expand Down
6 changes: 6 additions & 0 deletions src/umka_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,9 @@ void rtlgetenv(Slot *params, Slot *result)
result->ptrVal = val;
}


void rtlsystem(Slot *params, Slot *result)
{
const char *command = (const char *)params[0].ptrVal;
result->intVal = system(command);
}
1 change: 1 addition & 0 deletions src/umka_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ void rtlfeof (Slot *params, Slot *result);
void rtltime (Slot *params, Slot *result);
void rtlclock (Slot *params, Slot *result);
void rtlgetenv (Slot *params, Slot *result);
void rtlsystem (Slot *params, Slot *result);

#endif // UMKA_RUNTIME_H_INCLUDED

0 comments on commit 555af45

Please sign in to comment.