aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElias Fleckenstein <eliasfleckenstein@web.de>2021-12-30 20:49:24 +0100
committerElias Fleckenstein <eliasfleckenstein@web.de>2021-12-30 20:49:24 +0100
commit4f3d72475537b66e870894cdd4c6abcbb102ecc1 (patch)
treedc2c46c79803f244a5e5a9793e2be6956f930ef7
parent34b269f811896fd033ce72fcfea63bfe541038b0 (diff)
downloaduwu-nolambda-4f3d72475537b66e870894cdd4c6abcbb102ecc1.tar.xz
Implement :nolambda:io module
-rw-r--r--.gitmodules3
-rw-r--r--Makefile2
-rw-r--r--README.md2
-rw-r--r--flow.c4
-rw-r--r--io.c43
m---------linenoise0
-rw-r--r--test.uwu9
7 files changed, 59 insertions, 4 deletions
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..ccb526b
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "linenoise"]
+ path = linenoise
+ url = https://github.com/antirez/linenoise
diff --git a/Makefile b/Makefile
index e64b357..bd4f504 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-all: flow.so
+all: flow.so io.so
uwu_include_path=../uwulang/
diff --git a/README.md b/README.md
index bc71ab5..fa218f2 100644
--- 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 ecf380c..40ea012 100644
--- 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
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
+Subproject 97d2850af13c339369093b78abe5265845d7822
diff --git a/test.uwu b/test.uwu
new file mode 100644
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"
+)