]> git.lizzy.rs Git - uwu-nolambda.git/commitdiff
Implement nolambda:os module
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 1 Jan 2022 13:52:47 +0000 (14:52 +0100)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 1 Jan 2022 13:52:47 +0000 (14:52 +0100)
Makefile
README.md
os.c [new file with mode: 0644]
test.uwu

index 7e163ae8cdf90d426e8b7462b7866cc4ef39e77a..bdf5dedd6d9cd1205b50317fcd3ace4e0f483b78 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,9 +1,9 @@
-all: flow.so io.so fs.so
+all: flow.so io.so fs.so os.so
 
 uwu_include_path=../uwulang/
 
 %.so: %.c
-       gcc -g -I${uwu_include_path} -shared -fpic $< -o $@ -D_GNU_SOURCE
+       gcc -g -I${uwu_include_path} -shared -fpic $< -o $@ -D_GNU_SOURCE -Wall -Wextra
 
 clean:
        rm -rf *.so
index 05f7d177219499de827c52c49e45496f428573b5..8dbdb0a4a6498b1115388574534cc2acbcd0fad1 100644 (file)
--- a/README.md
+++ b/README.md
@@ -17,7 +17,7 @@ make uwu_include_path=/path/to/uwulang/repo
 
 ## Modules
 
-The following modules are implemented: `nolambda:flow`, `nolambda:io`, `nolambda:fs`. The modules `nolambda:os`, `nolambda:global`, and `nolambda:random` are ToDo.
+The following modules are implemented: `nolambda:flow`, `nolambda:io`, `nolambda:fs`, `nolambda:os`. The modules `nolambda:global`, and `nolambda:random` are ToDo.
 
 ### `nolambda:flow`
 
@@ -43,7 +43,7 @@ Note: all file paths are relative to the _directory the program was started from
 - `nolambda:os:exit`: Optionally takes an exit code (integer) as $0 and exits the program with the given exit code, or 0 if no exit code was given. Returns `:nil:nil` in theory.
 - `nolambda:os:sleep`: Takes an integer as $0 and pauses the execution of the program for $1 milliseconds. Returns `:nil:nil`.
 - `nolambda:os:execute`: Takes a command (arbirary value, converted to string) as $0 and executes it as a shell command. Returns the exit code of the command as an integer.
-- `nolambda:os:time`: Returns the current unix time as an integer.
+- `nolambda:os:time`: Returns the current unix millis as an integer.
 
 ### `nolambda:global`
 
diff --git a/os.c b/os.c
new file mode 100644 (file)
index 0000000..c147bab
--- /dev/null
+++ b/os.c
@@ -0,0 +1,69 @@
+#include <time.h>
+#include <stdlib.h>
+#include <errno.h>
+#include "api/vm.h"
+#include "api/util.h"
+#include "api/int.h"
+#include "api/nil.h"
+#include "api/str.h"
+#include "common/err.h"
+
+UwUVMValue uwu_exit(UwUVMArgs *args)
+{
+       uwuutil_require_max("nolambda:os:exit", args, 1);
+
+       long exit_code = 0;
+
+       if (args->num == 1)
+               exit_code = uwuint_get(uwuvm_get_arg(args, 0));
+
+       exit(exit_code);
+       return uwunil_create();
+}
+
+UwUVMValue uwu_sleep(UwUVMArgs *args)
+{
+       uwuutil_require_exact("nolamda:os:sleep", args, 1);
+
+       UwUVMValue value = uwuvm_get_arg(args, 0);
+
+       if (value.type != &uwuint_type)
+               error("type error: nolamda:os:sleep requires an integer as $0\n");
+
+       long millis = uwuint_get(value);
+
+       if (millis < 0)
+               error("type error: nolamda:os:sleep requires a positive value as $0\n");
+
+       struct timespec ts = {
+               .tv_sec = millis / 1000,
+               .tv_nsec = 1000000 * (millis % 1000),
+       };
+
+       while (nanosleep(&ts, &ts) != 0)
+               if (errno != EINTR)
+                       syserror("nanosleep", NULL);
+
+       return uwunil_create();
+}
+
+UwUVMValue uwu_execute(UwUVMArgs *args)
+{
+       uwuutil_require_exact("nolambda:os:execute", args, 1);
+
+       char *command = uwustr_get(uwuvm_get_arg(args, 0));
+       int ret = system(command);
+       free(command);
+
+       return uwuint_create(ret);
+}
+
+UwUVMValue uwu_time(UwUVMArgs *args)
+{
+       uwuutil_require_exact("nolamda:os:time", args, 0);
+
+       struct timespec ts;
+       clock_gettime(CLOCK_REALTIME, &ts);
+
+       return uwuint_create(ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
+}
index a0e0681a33e26be285e60e84c6076d85083cb73a..8e7d32b52455ddce7ee3afb5697b93f9c3916320 100644 (file)
--- a/test.uwu
+++ b/test.uwu
@@ -27,5 +27,11 @@ main flow:linear(
                io:print("successfully removed file")
        ),
 
+       io:print(os:time),
+       os:sleep(500),
+       io:print(os:time),
+
+       os:execute("echo hello world"),
+
        "success"
 )