diff options
-rw-r--r-- | README.md | 11 | ||||
-rw-r--r-- | src/builtins.c | 11 | ||||
m--------- | stemlib | 0 |
3 files changed, 13 insertions, 9 deletions
@@ -12,6 +12,9 @@ about memory allocation while the implementation remains extremely simple. install `doxygen` as an optional dependency. If you are on a BSD or MacOS, you must use `gmake`. +# Documentation +Builtin function definitions and the C API in general are documented [here](https://stemdoc.nullring.xyz). + # Quickstart Because this is a stack based language, all operations are done in reverse polish. For example, to add two numbers together: ``` @@ -26,12 +29,12 @@ to the action of adding two numbers when called. Let's look at a real example of a REPL implementation in this language: ``` -repl [ "> " . read strquote eval repl ] func +repl [ "> " . read strquote eval repl ] def repl ``` `repl` is a word, which means it is a literal, and everything that is a literal gets pushed onto the stack. -Everything between the `[` and `]` is an element in a quote. Then, we see the `func` word. If a word is already bound to a function, -the function gets called instead of getting pushed to the stack, so the `func` function gets called, which takes the top two +Everything between the `[` and `]` is an element in a quote. Then, we see the `def` word. If a word is already bound to a function, +the function gets called instead of getting pushed to the stack, so the `def` function gets called, which takes the top two elements off the stack, and creates a function called `repl` where now every time `repl` is called in the future, the quote is evaluated instead. @@ -46,5 +49,5 @@ does, and then finally `repl` gets called again at the end so we can loop foreve ## Factorial Let's take a closer look at the factorial function: ``` - +factorial [ dup 0 <= [ 1 + ] [ dup 1 - factorial * ] if ] def ``` diff --git a/src/builtins.c b/src/builtins.c index 8d985fe..475306d 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -401,12 +401,12 @@ void stemfread(value_t *v) { eval_error("EMPTY STACK"); return; } - char *val = NULL; - size_t len; + char *val = ""; + size_t len = 0; FILE *fp = fopen(v1->str_word->value, "rb"); if (!fp) { array_append(STACK, v1); - eval_error("INCORRECT TYPE ARGUMENT"); + eval_error("FREAD ERROR"); return; } ssize_t bytes_read = getdelim(&val, &len, '\0', fp); @@ -593,8 +593,8 @@ void dip(value_t *v) { return; } + array_append(EVAL_STACK, v1); if (v2->type == VQUOTE) { - 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])); @@ -951,6 +951,7 @@ void compose(value_t *v) { } else if (v2->type == VQUOTE && v1->type == VQUOTE) { retval = v1; array_extend(v1->quote, v2->quote); + free(v2->quote->items); free(v2->quote); free(v2); } else { @@ -1169,7 +1170,7 @@ void stemcut(value_t *v) { for (int i = v1->int_float; i < v2->quote->size; i++) { array_append(r2->quote, v2->quote->items[i]); } - // [ a b c ] 1 cut => [ a ] [ b c ] + /* [ a b c ] 1 cut => [ a ] [ b c ] */ break; default: array_append(STACK, v2); diff --git a/stemlib b/stemlib -Subproject 3e2c7374c36a3439069348f27d818ac23797a82 +Subproject 2c9840a0a469a3ddd223a060e276c630a493acb |