aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPreston Pan <preston@nullring.xyz>2024-01-22 18:34:19 -0800
committerPreston Pan <preston@nullring.xyz>2024-01-22 18:34:19 -0800
commit2750e54a2ad5afeabb7093fc10e9fb686b6f6059 (patch)
tree114b0e79cc9bb92d8be32fbb30100cab507a15e0 /src
parentab4eaabe940c1593ac060d91c652fcdb587786c0 (diff)
add unglue
Diffstat (limited to 'src')
-rw-r--r--src/builtins.c74
-rw-r--r--src/stem.c6
2 files changed, 75 insertions, 5 deletions
diff --git a/src/builtins.c b/src/builtins.c
index 4714c23..f3497b3 100644
--- a/src/builtins.c
+++ b/src/builtins.c
@@ -332,7 +332,6 @@ void stemfloor(value_t *v) {
void stemeval(value_t *v) {
value_t *v1 = array_pop(STACK);
if (v1 == NULL) {
- array_append(STACK, v1);
eval_error("EMPTY STACK");
return;
}
@@ -350,6 +349,28 @@ void stemeval(value_t *v) {
}
}
+void unglue(value_t *v) {
+ value_t *v1 = array_pop(STACK);
+ if (v1 == NULL) {
+ eval_error("EMPTY STACK");
+ return;
+ }
+ if (v1->type != VWORD) {
+ array_append(STACK, v1);
+ eval_error("EMPTY STACK");
+ return;
+ }
+ value_t *value = ht_get(WORD_TABLE, v1->str_word);
+ if (!value) {
+ array_append(STACK, v1);
+ eval_error("UNBOUND WORD");
+ return;
+ }
+ value = value_copy(value);
+ array_append(STACK, value);
+ value_free(v1);
+}
+
void strquote(value_t *v) {
value_t *v1 = array_pop(STACK);
if (v1 == NULL) {
@@ -399,7 +420,54 @@ void curry(value_t *v) {
return;
}
- array_curry(v2->quote, v1);
+ array_add(v2->quote, v1, 0);
+ array_append(STACK, v2);
+}
+
+void steminsert(value_t *v) {
+ value_t *v3 = array_pop(STACK);
+ if (v3 == NULL) {
+ eval_error("EMPTY STACK");
+ return;
+ }
+ value_t *v2 = array_pop(STACK);
+ if (v2 == NULL) {
+ array_append(STACK, v3);
+ eval_error("EMPTY STACK");
+ return;
+ }
+ value_t *v1 = array_pop(STACK);
+ if (v1 == NULL) {
+ array_append(STACK, v2);
+ array_append(STACK, v3);
+ eval_error("EMTPY STACK");
+ return;
+ }
+
+ if (v1->type != VINT) {
+ array_append(STACK, v1);
+ array_append(STACK, v2);
+ array_append(STACK, v3);
+ eval_error("INCORRECT TYPE ARGUMENT");
+ return;
+ }
+
+ if (v3->type != VQUOTE) {
+ array_append(STACK, v1);
+ array_append(STACK, v2);
+ array_append(STACK, v3);
+ eval_error("INCORRECT TYPE ARGUMENT");
+ return;
+ }
+ if (v1->int_float < 0 || v1->int_float >= v3->quote->size) {
+ array_append(STACK, v1);
+ array_append(STACK, v2);
+ array_append(STACK, v3);
+ eval_error("INDEX ERROR");
+ return;
+ }
+
+ array_add(v3->quote, v2, (int)v1->int_float);
array_append(STACK, v2);
}
@@ -1253,4 +1321,6 @@ void add_funcs() {
add_func(FLIT, undef, "undef");
add_func(FLIT, tostr, "tostr");
add_func(FLIT, stemcut, "cut");
+ add_func(FLIT, steminsert, "insert");
+ add_func(FLIT, unglue, "unglue");
}
diff --git a/src/stem.c b/src/stem.c
index 5c2e379..f5a13ad 100644
--- a/src/stem.c
+++ b/src/stem.c
@@ -39,15 +39,15 @@ void array_append(array_t *a, value_t *v) {
a->size++;
}
-void array_curry(array_t *a, value_t *v) {
+void array_add(array_t *a, value_t *v, int index) {
if (a->size >= a->capacity - 3) {
a->capacity = a->capacity * 2;
a->items = realloc(a->items, a->capacity * sizeof(value_t *));
}
- for (int i = a->size - 1; i >= 0; i--) {
+ for (int i = a->size - 1; i >= index; i--) {
a->items[i + 1] = a->items[i];
}
- a->items[0] = v;
+ a->items[index] = v;
a->size++;
}