From 22eb9a274386c7fbc5cdee2b86f621c44fd823e2 Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Sun, 7 Jan 2024 22:48:35 -0800 Subject: forgot what i did lol --- Makefile | 3 +++ examples/repl.stem | 5 ++++- examples/stdlib.stem | 2 ++ main.c | 1 + parser.c | 33 ++++++++++++++++++++++++++++----- 5 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 examples/stdlib.stem diff --git a/Makefile b/Makefile index 9dec505..b5991f6 100644 --- a/Makefile +++ b/Makefile @@ -9,4 +9,7 @@ all: clean: rm stem + +install: + cp stem /usr/local/bin/ # end diff --git a/examples/repl.stem b/examples/repl.stem index 7f2010f..1468835 100644 --- a/examples/repl.stem +++ b/examples/repl.stem @@ -1 +1,4 @@ -loop [ "> " . read strquote eval loop ] func loop +"./stdlib.stem" fread strquote eval +"./math.stem" include + +repl [ "> " . read strquote eval repl ] func repl diff --git a/examples/stdlib.stem b/examples/stdlib.stem new file mode 100644 index 0000000..eca48c8 --- /dev/null +++ b/examples/stdlib.stem @@ -0,0 +1,2 @@ +evalstr [ strquote eval ] func +include [ fread evalstr ] func diff --git a/main.c b/main.c index 4c058b4..afa2e65 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,7 @@ #include #include #include +#include extern ht_t *WORD_TABLE; extern array_t *STACK; diff --git a/parser.c b/parser.c index 32b0a94..1bbf793 100644 --- a/parser.c +++ b/parser.c @@ -1012,10 +1012,6 @@ bool eval_builtins(value_t *v) { array_append(v2->quote, v1); array_append(STACK, v2); - } else if (strcmp(str, "clear") == 0) { - for (int i = 0; i < STACK->size; i++) { - value_free(array_pop(STACK)); - } } else if (strcmp(str, "del") == 0) { v2 = array_pop(STACK); if (v2 == NULL) { @@ -1071,6 +1067,33 @@ bool eval_builtins(value_t *v) { eval(v1); } array_append(STACK, v1); + } else if (strcmp(str, "dip") == 0) { + v2 = array_pop(STACK); + if (v2 == NULL) { + value_free(v); + return eval_error(); + } + v1 = array_pop(STACK); + if (v1 == NULL) { + value_free(v); + array_append(STACK, v2); + return eval_error(); + } + + if (v2->type == VQUOTE) { + array_append(EVAL_STACK, v); + array_append(EVAL_STACK, v1); + array_append(EVAL_STACK, v2); + for (int i = 0; i < v2->quote->size; i++) { + eval(value_copy(v2->quote->items[i])); + } + value_free(array_pop(EVAL_STACK)); + value_free(array_pop(EVAL_STACK)); + array_pop(EVAL_STACK); + } else { + eval(v1); + } + array_append(STACK, v1); } else if (strcmp(str, "len") == 0) { v1 = array_pop(STACK); if (v1 == NULL) { @@ -1182,7 +1205,7 @@ bool eval_builtins(value_t *v) { array_append(STACK, v1); return eval_error(); } - char *val; + char *val = NULL; size_t len; FILE *fp = fopen(v1->str_word->value, "rb"); if (!fp) { -- cgit