From: Elias Fleckenstein Date: Thu, 30 Dec 2021 19:01:13 +0000 (+0100) Subject: Implement nolambda:flow X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=34b269f811896fd033ce72fcfea63bfe541038b0;p=uwu-nolambda.git Implement nolambda:flow --- diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e64b357 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +all: flow.so + +uwu_include_path=../uwulang/ + +%.so: %.c + gcc -g -I${uwu_include_path} -shared -fpic $< -o $@ -D_GNU_SOURCE + +clean: + rm -rf *.so diff --git a/README.md b/README.md index 993b89c..bc71ab5 100644 --- a/README.md +++ b/README.md @@ -6,35 +6,35 @@ nolambda is a collection of native [uwu](https://github.com/EliasFleckenstein03/ ### `nolambda:flow` - `nolambda:flow:linear`: Accepts an arbitrary number of arguments of arbitrary type, but at least one and evaulates all of them in order. Returns the last argument. -- `nolambda:flow:error`: Accepts a string as $0, prints the string to stderr and exits the program with failure. Returns `:nil:nil` in theory. +- `nolambda:flow:error`: Accepts an arbitrary value as $0, prints it to stderr appending a newline and exits the program with failure. Returns `:nil:nil` in theory. ### `nolambda:io` -- `nolambda:io:print`: Accepts a string as $0 and prints it to stdout, followed by a newline. Returns $0. +- `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:fs` Note: all file paths are relative to the _directory the program was started from_. -- `nolambda:fs:read`: Accepts a file name (string) as $0 and returns it's contents as a string. Causes an error if the file does not exist. -- `nolambda:fs:write`: Accepts a file name (string) as $0 and overwrites it with the contents in $1 (string). Causes an error if the file could not be written. Returns `:bool:true` if the file was freshly created, `:bool:false` if it was overwritten. -- `nolambda:fs:remove`: Accepts a file name (string) as $0 and removes it from the file system. Causes an error if the file could not be removed, but does not cause an error if the file does not exist. Returns `:bool:true` if the file existed and was removed, `:bool:false` otherwise. -- `nolambda:fs:exists`: Accepts a file name (string) as $0 and returns `:bool:true` if it exists, `:bool:fase` else. +- `nolambda:fs:read`: Accepts a file name (arbirary value, used as string) as $0 and returns it's contents as a string. Causes an error if the file does not exist. +- `nolambda:fs:write`: Accepts a file name (arbirary value, used as string) as $0 and overwrites it with the contents in $1 (arbirary value, used as string). Causes an error if the file could not be written. Returns `:bool:true` if the file was freshly created, `:bool:false` if it was overwritten. +- `nolambda:fs:remove`: Accepts a file name (arbirary value, used as string) as $0 and removes it from the file system. Causes an error if the file could not be removed, but does not cause an error if the file does not exist. Returns `:bool:true` if the file existed and was removed, `:bool:false` otherwise. +- `nolambda:fs:exists`: Accepts a file name (arbirary value, used as string) as $0 and returns `:bool:true` if it exists, `:bool:fase` else. ### `nolambda:os` - `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 (string) as $0 and executes it as a shell command. Returns the exit code of the command as an integer. +- `nolambda:os:execute`: Takes a command (arbirary value, used as 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:global` -- `nolambda:global:set`: Creates or overwrites a global variable named $0 (string) and puts the contents of $1 (arbitrary type) into it. Returns `:bool:false` if the variable existed previously and was updated, `:bool:true` if it was created. -- `nolambda:global:get`: Returns the global variable named $0 (string). Causes an error if the variable does not exist. -- `nolambda:global:exists`: Returns `:bool:true` if the global variable named $0 (string) exists, `:bool:false` else. -- `nolambda:global:delete`: Deletes the global variable named $0 (string). Returns `:bool:true` if the variable existed previously, `:bool:false` else. +- `nolambda:global:set`: Creates or overwrites a global variable named $0 (arbirary value, used as string) and puts the contents of $1 (arbitrary type) into it. Returns `:bool:false` if the variable existed previously and was updated, `:bool:true` if it was created. +- `nolambda:global:get`: Returns the global variable named $0 (arbirary value, used as string). Causes an error if the variable does not exist. +- `nolambda:global:exists`: Returns `:bool:true` if the global variable named $0 (arbirary value, used as string) exists, `:bool:false` else. +- `nolambda:global:delete`: Deletes the global variable named $0 (arbirary value, used as string). Returns `:bool:true` if the variable existed previously, `:bool:false` else. ### `nolambda:random` diff --git a/flow.c b/flow.c new file mode 100644 index 0000000..ecf380c --- /dev/null +++ b/flow.c @@ -0,0 +1,33 @@ +#include +#include +#include "common/err.h" +#include "api/vm.h" +#include "api/nil.h" +#include "api/str.h" + +UwUVMValue uwu_linear(UwUVMArgs *args) +{ + if (args->num < 1) + error("error: nolambda:flow:linear requires at least one argument"); + + size_t return_arg = args->num - 1; + + for (size_t i = 0; i < return_arg; i++) + uwuvm_get_arg(args, i); + + return uwuvm_clone_value(uwuvm_get_arg(args, return_arg)); +} + +UwUVMValue uwu_error(UwUVMArgs *args) +{ + if (args->num != 1) + error("error: nolambda:flow:error exactly one argument"); + + char *err = uwustr_get(uwuvm_get_arg(args, 0)); + fprintf(stderr, "%s\n", err); + free(err); + + exit(1); + + return uwunil_create(); +}