diff options
author | Preston Pan <preston@nullring.xyz> | 2024-01-28 14:06:09 -0800 |
---|---|---|
committer | Preston Pan <preston@nullring.xyz> | 2024-01-28 14:06:09 -0800 |
commit | f0c5408f486741a092be0b984ca5bdeb410fae5b (patch) | |
tree | 01e99293a9f1527b1db58749acc0a301af762fc1 | |
parent | a6664e5cca5fb7d681789f22457f24a9ab6c38b5 (diff) |
add uncurry
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | src/builtins.c | 69 |
2 files changed, 70 insertions, 1 deletions
@@ -25,7 +25,7 @@ clean: install: cp $(TARGET) /usr/local/bin/ mkdir -p /usr/local/lib/share/stem/ - cp -r stemlib/ /usr/local/share/stem/stdlib/ + cp -r stemlib/ /usr/local/share/stem/ doc: doxygen diff --git a/src/builtins.c b/src/builtins.c index 0bb3662..bf8618a 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -1327,6 +1327,73 @@ void tostr(value_t *v) { } array_append(STACK, v1); } + +void include(value_t *v) { + value_t *v1 = array_pop(STACK); + if (v1 == NULL) { + eval_error("EMPTY STACK"); + return; + } + if (v1->type != VSTR) { + array_append(STACK, v1); + eval_error("EMPTY STACK"); + return; + } + char *val = ""; + size_t len = 0; + string_t *strval = init_string("/usr/local/share/stem/stemlib/"); + string_concat(strval, v1->str_word); + FILE *fp = fopen(strval->value, "rb"); + string_free(strval); + if (!fp) { + array_append(STACK, v1); + eval_error("FREAD ERROR"); + return; + } + ssize_t bytes_read = getdelim(&val, &len, '\0', fp); + fclose(fp); + + value_t *retval = init_value(VQUOTE); + retval->quote = init_array(10); + parser_t *p = parser_pp(val); + value_t *cur; + while (1) { + cur = parser_get_next(p); + if (cur == NULL) + break; + array_append(retval->quote, cur); + } + array_append(STACK, retval); + value_free(v1); + free(p->source); + free(p); +} + +void uncurry(value_t *v) { + value_t *v1 = array_pop(STACK); + if (v1 == NULL) { + eval_error("EMPTY STACK"); + return; + } + if (v1->type != VQUOTE) { + array_append(STACK, v1); + eval_error("EMPTY STACK"); + return; + } + if (v1->quote->size <= 0) { + array_append(STACK, v1); + eval_error("INDEX ERROR"); + return; + } + value_t *retval = v1->quote->items[0]; + for (int i = 1; i < v1->quote->size; i++) { + v1->quote->items[i - 1] = v1->quote->items[i]; + } + v1->quote->size--; + array_append(STACK, v1); + array_append(STACK, retval); +} + void add_objs() {} void add_funcs() { @@ -1384,4 +1451,6 @@ void add_funcs() { add_func(FLIT, unglue, "unglue"); add_func(FLIT, stemand, "and"); add_func(FLIT, stemor, "or"); + add_func(FLIT, include, "include"); + add_func(FLIT, uncurry, "uncurry"); } |