diff options
author | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-12-30 20:49:24 +0100 |
---|---|---|
committer | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-12-30 20:49:24 +0100 |
commit | 4f3d72475537b66e870894cdd4c6abcbb102ecc1 (patch) | |
tree | dc2c46c79803f244a5e5a9793e2be6956f930ef7 | |
parent | 34b269f811896fd033ce72fcfea63bfe541038b0 (diff) | |
download | uwu-nolambda-4f3d72475537b66e870894cdd4c6abcbb102ecc1.tar.xz |
Implement :nolambda:io module
-rw-r--r-- | .gitmodules | 3 | ||||
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | flow.c | 4 | ||||
-rw-r--r-- | io.c | 43 | ||||
m--------- | linenoise | 0 | ||||
-rw-r--r-- | test.uwu | 9 |
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 @@ -1,4 +1,4 @@ -all: flow.so +all: flow.so io.so uwu_include_path=../uwulang/ @@ -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` @@ -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); @@ -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" +) |