]> git.lizzy.rs Git - uwu-nolambda.git/commitdiff
Implement :nolambda:io module
authorElias Fleckenstein <eliasfleckenstein@web.de>
Thu, 30 Dec 2021 19:49:24 +0000 (20:49 +0100)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Thu, 30 Dec 2021 19:49:24 +0000 (20:49 +0100)
.gitmodules [new file with mode: 0644]
Makefile
README.md
flow.c
io.c [new file with mode: 0644]
linenoise [new submodule]
test.uwu [new file with mode: 0644]

diff --git a/.gitmodules b/.gitmodules
new file mode 100644 (file)
index 0000000..ccb526b
--- /dev/null
@@ -0,0 +1,3 @@
+[submodule "linenoise"]
+       path = linenoise
+       url = https://github.com/antirez/linenoise
index e64b357578bf8eb70c1ebc3e0b1e3c6ae24dcab9..bd4f504a4453b8e4df5d2cfaff8385f8d36d2637 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-all: flow.so
+all: flow.so io.so
 
 uwu_include_path=../uwulang/
 
index bc71ab5cbba6c938b923d092f5f275f06f120493..fa218f25ae9d68b41c5212cfb6217c1bcb6c485b 100644 (file)
--- a/README.md
+++ b/README.md
@@ -11,7 +11,7 @@ nolambda is a collection of native [uwu](https://github.com/EliasFleckenstein03/
 ### `nolambda:io`
 
 - `nolambda:io:print`: Accepts an arbitrary value as $0 and prints it to stdout, followed by a newline. Returns $0.
-- `nolambda:io:scan`: Reads a line from stdin and returns it as a string, without the newline character at the end. This is interally using readline.
+- `nolambda:io:scan`: Reads a line from stdin and returns it as a string, without the newline character at the end. If $0 is given, it is used as a prompt (after converting to string).
 
 ### `nolambda:fs`
 
diff --git a/flow.c b/flow.c
index ecf380c3a62f6c6c8386ceda5c14a485d7831a97..40ea0128f532da0908b5b550ba04922fd50fe668 100644 (file)
--- a/flow.c
+++ b/flow.c
@@ -8,7 +8,7 @@
 UwUVMValue uwu_linear(UwUVMArgs *args)
 {
        if (args->num < 1)
-               error("error: nolambda:flow:linear requires at least one argument");
+               error("error: nolambda:flow:linear requires at least one argument\n");
 
        size_t return_arg = args->num - 1;
 
@@ -21,7 +21,7 @@ UwUVMValue uwu_linear(UwUVMArgs *args)
 UwUVMValue uwu_error(UwUVMArgs *args)
 {
        if (args->num != 1)
-               error("error: nolambda:flow:error exactly one argument");
+               error("error: nolambda:flow:error requires exactly one argument\n");
 
        char *err = uwustr_get(uwuvm_get_arg(args, 0));
        fprintf(stderr, "%s\n", err);
diff --git a/io.c b/io.c
new file mode 100644 (file)
index 0000000..dbfda7c
--- /dev/null
+++ b/io.c
@@ -0,0 +1,43 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "linenoise/linenoise.h"
+#include "common/err.h"
+#include "api/vm.h"
+#include "api/str.h"
+
+UwUVMValue uwu_print(UwUVMArgs *args)
+{
+       if (args->num != 1)
+               error("error: nolambda:io:print requires exactly one argument\n");
+
+       UwUVMValue value = uwuvm_get_arg(args, 0);
+
+       char *str = uwustr_get(value);
+       printf("%s\n", str);
+       free(str);
+
+       return uwuvm_clone_value(value);
+}
+
+UwUVMValue uwu_scan(UwUVMArgs *args)
+{
+       char *prompt = NULL;
+
+       if (args->num == 0)
+               prompt = strdup("");
+       else if (args->num == 1)
+               prompt = uwustr_get(uwuvm_get_arg(args, 0));
+       else
+               error("error: nolambda:io:scan requires exactly one or zero arguments\n");
+
+       char *return_string = linenoise(prompt);
+       UwUVMValue return_value = uwustr_create(return_string);
+
+       linenoiseFree(return_string);
+       free(prompt);
+
+       return return_value;
+}
+
+#include "linenoise/linenoise.c"
diff --git a/linenoise b/linenoise
new file mode 160000 (submodule)
index 0000000..97d2850
--- /dev/null
+++ b/linenoise
@@ -0,0 +1 @@
+Subproject commit 97d2850af13c339369093b78abe5265845d78220
diff --git a/test.uwu b/test.uwu
new file mode 100644 (file)
index 0000000..a061a99
--- /dev/null
+++ b/test.uwu
@@ -0,0 +1,9 @@
+main flow:linear(
+       io:print("please enter something:"),
+       io:print(io:scan),
+       io:print(:str:cat(
+               "your input: ",
+               io:scan("please enter something else: ")
+       )),
+       "success"
+)