diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2023-02-17 15:18:28 -0800 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2023-02-17 15:18:28 -0800 |
| commit | 5bb8f72816e7bbcffdc5053f9e7dd3d819fcd840 (patch) | |
| tree | beda48e0fba4e4d2b3134b393f990f6719ddf46c /src/eval.c | |
| parent | cb4abc221844aa22f11447914d6d5dedd71a054c (diff) | |
added more frees I think
Diffstat (limited to 'src/eval.c')
| -rw-r--r-- | src/eval.c | 89 |
1 files changed, 60 insertions, 29 deletions
@@ -5,16 +5,21 @@ #include "hashtable.h" #include "parse.h" -const int trueVal = 1; -sym_t trueSym = {LITERAL, INT, (void*)&trueVal, NULL, 0}; -const int falseVal = 0; -sym_t falseSym = {LITERAL, INT, (void*)&falseVal, NULL, 0}; - -/* TODO: read base hashtable from file to allow modularity -pair value value? */ sym_t* eval(sym_t* symbol, hashtable_t* parentScope) { if (symbol->province == LITERAL) { - return(symbol); + if (symbol->litType == INT) { + return init_sym_int(*((int*)symbol->name)); + } + else if (symbol->litType == FLOAT) { + /* TODO: float */ + return NULL; + } + else if (symbol->litType == STRING) { + return init_sym_str((char*)symbol->name); + } + else { + return NULL; + } } else if (symbol->province == KEY_REF) { sym_t* returnV = NULL; @@ -48,7 +53,7 @@ sym_t* eval(sym_t* symbol, hashtable_t* parentScope) { } } /* clear new scope, then return */ - /* destroy_hashtable(newScope); */ + destroy_hashtable(newScope); return returnV; } else { @@ -58,8 +63,8 @@ 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]); */ + sym_t* sym = eval(symbols[i], symbolScope); + destroy_sym(sym); } } /* internal functions? @@ -71,6 +76,7 @@ sym_t* try_eval_internal(sym_t* symbol, hashtable_t* symbolScope) { /* return last arg, that's the function return line */ exec_all(symbol->args, symbol->argCount - 1, symbolScope); return eval(symbol->args[symbol->argCount - 1], symbolScope); + return init_sym_int(0); } /* TODO: rename storel */ else if (hash == get_hash("store")) { @@ -80,56 +86,80 @@ sym_t* try_eval_internal(sym_t* symbol, hashtable_t* symbolScope) { sym_t* v = symbol->args[0]; pair_t p = init_pair(k, (void*)v); hashtable_add_pair(symbolScope->parent, p); - return &trueSym; + return init_sym_int(1); } /* TODO: release()/free() method? */ else if (hash == get_hash("eq")) { litType_t type = NO_TYPE; - void* lastVal = NULL; + int firstInt = 0; + float firstFloat = 0; + char* firstString; for (int i = 0; i < symbol->argCount; i++) { sym_t* v = eval(symbol->args[i], symbolScope); /* initialize */ if (type == NO_TYPE) { type = v->litType; - lastVal = v->name; + if (type == INT) { + firstInt = symtoi(v); + destroy_sym(v); + } + else if (type == FLOAT) { + } + else if (type == STRING) { + firstString = symtos(v); + free(v); + } } else { /* interpret type */ if (type == INT) { - if (symtoi(v) != *((int*)lastVal)) { - return &falseSym; + int si = symtoi(v); + destroy_sym(v); + if (si != firstInt) { + return init_sym_int(0); } } - if (type == STRING) { - if (strcmp(symtos(v), (char*)lastVal) != 0) { - return &falseSym; + /* TODO: float */ + else if (type == FLOAT) { + + } + else if (type == STRING) { + char* ss = symtos(v); + if (strcmp(ss, firstString) != 0) { + destroy_sym(v); + return init_sym_int(0); } + destroy_sym(v); } - /* TODO: float */ } } - return &trueSym; + return init_sym_int(1); } else if (hash == get_hash("if")) { sym_t* v = eval(symbol->args[0], symbolScope); - if (symtoi(v) != 0) { + int si = symtoi(v); + destroy_sym(v); + if (si != 0) { exec_all(symbol->args + 1, symbol->argCount - 1, symbolScope); - return &trueSym; + return init_sym_int(1); } - return &falseSym; + /* TODO: free other args if condition fails */ + return init_sym_int(0); } else if (hash == get_hash("not")) { sym_t* v = eval(symbol->args[0], symbolScope); int b = symtoi(v); - return b ? &falseSym : &trueSym; + destroy_sym(v); + return init_sym_int(!b); } else if (hash == get_hash("sum")) { - int* sum = calloc(1, sizeof(int)); + int sum = 0; for (int i = 0; i < symbol->argCount; i++) { sym_t* v = eval(symbol->args[i], symbolScope); - *sum += symtoi(v); + sum += symtoi(v); + destroy_sym(v); } - return init_sym(LITERAL, INT, sum); + return init_sym_int(sum); } else if (hash == get_hash("print")) { sym_t* v = eval(symbol->args[0], symbolScope); @@ -145,7 +175,8 @@ sym_t* try_eval_internal(sym_t* symbol, hashtable_t* symbolScope) { printf("%s\n", symtos(v)); break; } - return &trueSym; + destroy_sym(v); + return init_sym_int(1); } else { return NULL; |
