diff options
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | examples/repl.stem | 5 | ||||
-rw-r--r-- | examples/stdlib.stem | 2 | ||||
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | parser.c | 33 |
5 files changed, 38 insertions, 6 deletions
@@ -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 @@ -2,6 +2,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> extern ht_t *WORD_TABLE; extern array_t *STACK; @@ -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) { |