diff options
| -rw-r--r-- | examples/recursion.pcdt | 2 | ||||
| -rw-r--r-- | src/eval.c | 14 | ||||
| -rw-r--r-- | src/hashtable.c | 3 |
3 files changed, 15 insertions, 4 deletions
diff --git a/examples/recursion.pcdt b/examples/recursion.pcdt new file mode 100644 index 0000000..88b81f5 --- /dev/null +++ b/examples/recursion.pcdt @@ -0,0 +1,2 @@ +store( (print(arg0()) incFunc(sum(1; arg0())) ) incFunc;) +incFunc(0;) @@ -19,7 +19,7 @@ sym_t* eval(sym_t* symbol, hashtable_t* parentScope) { else if (symbol->province == KEY_REF) { sym_t* returnV = NULL; /* entering new scope, so create a new table */ - hashtable_t* newScope = init_hashtable(500, parentScope); + hashtable_t* newScope = init_hashtable(50, parentScope); sym_t* ir = try_eval_internal(symbol, newScope); if (ir != NULL) { returnV = ir; @@ -48,7 +48,7 @@ sym_t* eval(sym_t* symbol, hashtable_t* parentScope) { } } /* clear new scope, then return */ - destroy_hashtable(newScope); + /* destroy_hashtable(newScope); */ return returnV; } else { @@ -59,7 +59,7 @@ sym_t* eval(sym_t* symbol, hashtable_t* parentScope) { void exec_all(sym_t** symbols, int count, hashtable_t* symbolScope) { for (int i = 0; i < count; i++) { eval(symbols[i], symbolScope); - destroy_sym(symbols[i]); + /* destroy_sym(symbols[i]); */ } } /* internal functions? @@ -123,6 +123,14 @@ sym_t* try_eval_internal(sym_t* symbol, hashtable_t* symbolScope) { int b = symtoi(v); return b ? &falseSym : &trueSym; } + else if (hash == get_hash("sum")) { + int* sum = calloc(1, sizeof(int)); + for (int i = 0; i < symbol->argCount; i++) { + sym_t* v = eval(symbol->args[i], symbolScope); + *sum += symtoi(v); + } + return init_sym(LITERAL, INT, sum); + } else if (hash == get_hash("print")) { sym_t* v = eval(symbol->args[0], symbolScope); switch (v->litType) { diff --git a/src/hashtable.c b/src/hashtable.c index 2f4ca64..2506c5b 100644 --- a/src/hashtable.c +++ b/src/hashtable.c @@ -1,4 +1,5 @@ #include <stdlib.h> +#include <string.h> #include <stdio.h> #include "hashtable.h" @@ -40,7 +41,7 @@ unsigned int hashtable_add_pair(hashtable_t* hashtable, pair_t pair) { } pair_t hashtable_get_pair(hashtable_t* hashtable, char* key) { unsigned int hash = get_hash(key); - return hashtable->pairs[hash % hashtable->capacity]; + return hashtable->pairs[(hash) % hashtable->capacity]; } pair_t hashtable_get_pair_recurse(hashtable_t* origin, char* key) { pair_t pair = hashtable_get_pair(origin, key); |
