aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--include/stem.h3
-rw-r--r--src/builtins.c34
-rw-r--r--src/stem.c12
4 files changed, 20 insertions, 31 deletions
diff --git a/README.md b/README.md
index 850b2a5..a4206f4 100644
--- a/README.md
+++ b/README.md
@@ -51,3 +51,5 @@ Let's take a closer look at the factorial function:
```
factorial [ dup 0 <= [ 1 + ] [ dup 1 - factorial * ] if ] def
```
+we check if the input is less than or equal to zero, and if it is, we add one to the input and it is our output.
+Otherwise, we multiply that input by itself minus one.
diff --git a/include/stem.h b/include/stem.h
index 381b0ad..f252694 100644
--- a/include/stem.h
+++ b/include/stem.h
@@ -111,6 +111,9 @@ array_t *init_array(size_t size);
/*! appends element to back of array */
void array_append(array_t *a, value_t *v);
+/*! appends element to front of array */
+void array_curry(array_t *a, value_t *v);
+
/*! pops last element off of array */
value_t *array_pop(array_t *a);
diff --git a/src/builtins.c b/src/builtins.c
index bb18bb1..4714c23 100644
--- a/src/builtins.c
+++ b/src/builtins.c
@@ -25,9 +25,8 @@ extern parser_t *PARSER;
extern ht_t *FLIT;
extern ht_t *OBJ_TABLE;
-/* TODO: rotr, rotl, dip, map, filter, errstr, join (for strings), switch
- * (for quotes), split (split array, string, word into two), del (deleting
- * entries from quotes, strings, words) */
+/* TODO: switch (for quotes), del (deleting entries from quotes, strings, words)
+ */
/* taken from stackoverflow */
char *get_line(FILE *f) {
@@ -400,7 +399,7 @@ void curry(value_t *v) {
return;
}
- array_append(v2->quote, v1);
+ array_curry(v2->quote, v1);
array_append(STACK, v2);
}
@@ -567,33 +566,6 @@ void stemlen(value_t *v) {
array_append(STACK, retval);
}
-/* void dip(value_t *v) { */
-/* value_t *v2 = array_pop(STACK); */
-/* if (v2 == NULL) { */
-/* eval_error("EMPTY STACK"); */
-/* return; */
-/* } */
-/* value_t *v1 = array_pop(STACK); */
-/* if (v1 == NULL) { */
-/* array_append(STACK, v2); */
-/* eval_error("EMPTY STACK"); */
-/* return; */
-/* } */
-
-/* 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])); */
-/* } */
-/* value_free(array_pop(EVAL_STACK)); */
-/* array_pop(EVAL_STACK); */
-/* } else { */
-/* eval(v2); */
-/* } */
-/* array_append(STACK, v1); */
-/* } */
-
void dip(value_t *v) {
value_t *v2 = array_pop(STACK);
if (v2 == NULL) {
diff --git a/src/stem.c b/src/stem.c
index 57905cb..5c2e379 100644
--- a/src/stem.c
+++ b/src/stem.c
@@ -39,6 +39,18 @@ void array_append(array_t *a, value_t *v) {
a->size++;
}
+void array_curry(array_t *a, value_t *v) {
+ 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--) {
+ a->items[i + 1] = a->items[i];
+ }
+ a->items[0] = v;
+ a->size++;
+}
+
value_t *array_pop(array_t *a) {
if (a->size > 0) {
value_t *v = a->items[a->size - 1];