From 4f3d72475537b66e870894cdd4c6abcbb102ecc1 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Thu, 30 Dec 2021 20:49:24 +0100 Subject: [PATCH] Implement :nolambda:io module --- .gitmodules | 3 +++ Makefile | 2 +- README.md | 2 +- flow.c | 4 ++-- io.c | 43 +++++++++++++++++++++++++++++++++++++++++++ linenoise | 1 + test.uwu | 9 +++++++++ 7 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 .gitmodules create mode 100644 io.c create mode 160000 linenoise create mode 100644 test.uwu 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 +#include +#include +#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 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 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" +) -- 2.44.0