diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | doc/internal_functions_dictionary | 1 | ||||
| -rw-r--r-- | src/eval.c | 48 | ||||
| -rw-r--r-- | src/parse.c | 35 | ||||
| -rw-r--r-- | src/parse.h | 1 | ||||
| -rw-r--r-- | src/precedent.c | 5 | ||||
| -rw-r--r-- | src/scope.c | 3 | ||||
| -rw-r--r-- | src/scope.h | 5 |
8 files changed, 47 insertions, 53 deletions
@@ -5,7 +5,7 @@ PREFIX ?= /usr BINDIR ?= ${PREFIX}/bin generate: - $(CC) -o precedent src/precedent.c src/hashtable.c src/scope.c src/parse.c src/eval.c $(CFLAGS) + $(CC) -o precedent src/precedent.c src/hashtable.c src/parse.c src/eval.c $(CFLAGS) clean: rm precedent diff --git a/doc/internal_functions_dictionary b/doc/internal_functions_dictionary index b7fe7d6..3b85c71 100644 --- a/doc/internal_functions_dictionary +++ b/doc/internal_functions_dictionary @@ -3,4 +3,5 @@ store() -- stores a pointer to the first argument under the name specified by th eq() -- returns 1 if the eval of all the arguments is the same, 0 if different. accepts any number of arguments if() -- if the eval of the first argument is 1, executes all the other arguments not() -- if the eval of the first argument is 1, returns 0, and vice versa +sum() -- returns the sum of the eval of all the arguments print() -- prints the eval of the first argument to the console, and a newline character @@ -37,16 +37,25 @@ sym_t* eval(sym_t* symbol, hashtable_t* parentScope) { if (p.key != NULL) { sym_t* nextSym = (sym_t*)p.value; /* add params */ + sym_t** addedArgs = malloc(100 * sizeof(sym_t*)); for (int i = 0; i < symbol->argCount && i < 100; i++) { char* keyName = malloc(6 * sizeof(char)); strcpy(keyName, "arg"); char index[3]; snprintf(index, 3, "%d", i); strcat(keyName, index); - pair_t argPair = init_pair(keyName, eval(symbol->args[i], parentScope)); + sym_t* argV = eval(symbol->args[i], parentScope); + addedArgs[i] = argV; + pair_t argPair = init_pair(keyName, argV); + free(keyName); hashtable_add_pair(newScope, argPair); } returnV = eval(nextSym, newScope); + /* clean up args */ + for (int i = 0; i < symbol->argCount && i < 100; i++) { + destroy_sym(addedArgs[i]); + } + free(addedArgs); } else { printf("no matching pair in hashtable\n"); @@ -76,15 +85,17 @@ 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")) { /* first arg is value, remaining args are names */ /* TODO: recursion for multiple pairs */ - char* k = symtos(eval(symbol->args[1], symbolScope)); - sym_t* v = symbol->args[0]; - pair_t p = init_pair(k, (void*)v); + sym_t* v = eval(symbol->args[1], symbolScope); + char* k = symtos(v); + destroy_sym(v); + sym_t* value = symbol->args[0]; + pair_t p = init_pair(k, (void*)value); + free(k); hashtable_add_pair(symbolScope->parent, p); return init_sym_int(1); } @@ -101,14 +112,13 @@ sym_t* try_eval_internal(sym_t* symbol, hashtable_t* symbolScope) { type = v->litType; if (type == INT) { firstInt = symtoi(v); - destroy_sym(v); } else if (type == FLOAT) { } else if (type == STRING) { firstString = symtos(v); - free(v); } + destroy_sym(v); } else { /* interpret type */ @@ -127,12 +137,16 @@ sym_t* try_eval_internal(sym_t* symbol, hashtable_t* symbolScope) { char* ss = symtos(v); if (strcmp(ss, firstString) != 0) { destroy_sym(v); + free(firstString); return init_sym_int(0); } destroy_sym(v); } } } + if (type == STRING) { + free(firstString); + } return init_sym_int(1); } else if (hash == get_hash("if")) { @@ -163,17 +177,15 @@ sym_t* try_eval_internal(sym_t* symbol, hashtable_t* symbolScope) { } else if (hash == get_hash("print")) { sym_t* v = eval(symbol->args[0], symbolScope); - switch (v->litType) { - case NO_TYPE: - break; - case INT: - printf("%d\n", symtoi(v)); - break; - case FLOAT: - break; - case STRING: - printf("%s\n", symtos(v)); - break; + if (v->litType == INT) { + printf("%d\n", symtoi(v)); + } + else if (v->litType == FLOAT) { + } + else if (v->litType == STRING) { + char* s = symtos(v); + printf("%s\n", s); + free(s); } destroy_sym(v); return init_sym_int(1); diff --git a/src/parse.c b/src/parse.c index 4aafc32..0144f44 100644 --- a/src/parse.c +++ b/src/parse.c @@ -36,18 +36,10 @@ void destroy_sym(sym_t* symbol) { free(symbol); } void destroy_sym_tree(sym_t* origin) { - if (origin->province == KEY_REF) { - /* doesn't follow user-defined refs */ - if (is_internal((char*)origin->name)) { - for (int i = 0; i < origin->argCount; i++) { - destroy_sym_tree(origin->args[i]); - } - destroy_sym(origin); - } - } - else if (origin->province == LITERAL) { - destroy_sym(origin); + for (int i = 0; i < origin->argCount; i++) { + destroy_sym_tree(origin->args[i]); } + destroy_sym(origin); } int symtoi(sym_t* symbol) { return *((int*)symbol->name); @@ -56,7 +48,9 @@ float symtof(sym_t* symbol) { return *((float*)symbol->name); } char* symtos(sym_t* symbol) { - return (char*)symbol->name; + char* vp = malloc((strlen(symbol->name) + 1) * sizeof(char)); + strcpy(vp, symbol->name); + return vp; } void symbol_add_arg(sym_t* symbol, sym_t* arg) { symbol->argCount += 1; @@ -130,6 +124,10 @@ symset_t* parse_symbols(char** str) { for (int s = 0; s < symbols->count; s++) { symbol_add_arg(symbol, symbols->symbols[s]); } + if (symbols->count > 0) { + free(symbols->symbols); + } + free(symbols); symset_add_symbol(symbolSet, symbol); break; } @@ -148,19 +146,10 @@ sym_t* parse_symbol_final(char* str) { for (int s = 0; s < finalSymset->count; s++) { symbol_add_arg(root, finalSymset->symbols[s]); } + free(finalSymset->symbols); + free(finalSymset); return root; } -/* TODO: improve this system */ -int is_internal(char* name) { - unsigned int hash = get_hash(name); - if (hash == get_hash("root") || hash == get_hash("store") - || hash == get_hash("eq") || hash == get_hash("if") - || hash == get_hash("not") || hash == get_hash("sum") - || hash == get_hash("print")) { - return 1; - } - return 0; -} void write_symbol_visual(sym_t* symbol, int offset) { for (int o = 0; o < offset; o++) { printf(" "); diff --git a/src/parse.h b/src/parse.h index b25e719..7d3b85b 100644 --- a/src/parse.h +++ b/src/parse.h @@ -43,7 +43,6 @@ void symbol_add_arg(sym_t* symbol, sym_t* arg); void symset_add_symbol(symset_t* symbolSet, sym_t* symbol); symset_t* parse_symbols(char** str); sym_t* parse_symbol_final(char* str); -int is_internal(char* name); void write_symbol_visual(sym_t* symbol, int offset); #endif diff --git a/src/precedent.c b/src/precedent.c index c623234..5b60e3d 100644 --- a/src/precedent.c +++ b/src/precedent.c @@ -63,8 +63,8 @@ int main(int argc, char** argv) { printf("time taken to set up environment: %.5f secs\n", (float)(setupTime - initTime) / (float)CLOCKS_PER_SEC); printf("time taken to execute program: %.5f secs\n", ((float)(evalTime - setupTime) - initTime) / (float)CLOCKS_PER_SEC); } - destroy_sym(sym); destroy_sym_tree(finalSymbol); + destroy_sym(sym); destroy_hashtable(rootTable); return 0; } @@ -109,9 +109,10 @@ char* preprocess(char* str, char* baseDir) { char* newStr = malloc((strlen(toInclude) + strlen(str) + 1) * sizeof(char)); strcpy(newStr, toInclude); strcat(newStr, str); - str = newStr; + free(str); free(includeName); free(path); free(toInclude); + str = newStr; } } diff --git a/src/scope.c b/src/scope.c deleted file mode 100644 index f2b9f79..0000000 --- a/src/scope.c +++ /dev/null @@ -1,3 +0,0 @@ -#include <stdlib.h> -#include "scope.h" -#include "hashtable.h" diff --git a/src/scope.h b/src/scope.h deleted file mode 100644 index a687015..0000000 --- a/src/scope.h +++ /dev/null @@ -1,5 +0,0 @@ -#include "hashtable.h" -#ifndef SCOPE -#define SCOPE - -#endif |
