diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2023-02-16 21:58:47 -0800 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2023-02-16 21:58:47 -0800 |
| commit | e34f5492b0df9ae22de0aaa537018efa2a191d2d (patch) | |
| tree | 637348c444c2773bc4db2aba47fa791ba6906002 /src | |
| parent | cd4a69bf4b219e2f96de2d86d8ea41ab01d0f5e3 (diff) | |
changed some stuff to get recursion working, made some more memory leaks
Diffstat (limited to 'src')
| -rw-r--r-- | src/eval.c | 14 | ||||
| -rw-r--r-- | src/hashtable.c | 3 |
2 files changed, 13 insertions, 4 deletions
@@ -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); |
