diff --git a/makefile b/makefile index 1058471..b094b13 100644 --- a/makefile +++ b/makefile @@ -6,7 +6,7 @@ # make uninstall ## C compiler -CC=tcc # much faster compilation than gcc +CC=gcc # much faster compilation than gcc COMPILER_FLAGS=#-g3 -Wall -Wextra -Wconversion -Wdouble-promotion -Wno-unused-parameter -Wno-unused-function -Wno-sign-conversion -fsanitize=undefined # exclude: -fsanitize-trap, because I'm using an old version of gcc and couldn't bother getting a new one. ## ^ from @@ -35,7 +35,7 @@ STYLE_BLUEPRINT=webkit FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT) build: $(SRC) - $(CC) $(COMPILER_FLAGS) $(INCS) $(SRC) $(MPC) -o mumble $(LIBS) + $(CC) $(COMPILER_FLAGS) $(INCS) $(SRC) $(MPC) -o mumble $(LIBS) $(DEBUG) format: $(SRC) $(FORMATTER) $(SRC) diff --git a/mumble b/mumble index 27e3fda..8d1d120 100755 Binary files a/mumble and b/mumble differ diff --git a/src/mumble.c b/src/mumble.c index 686e886..c416153 100644 --- a/src/mumble.c +++ b/src/mumble.c @@ -5,7 +5,7 @@ #include #include "mpc/mpc.h" -#define VERBOSE 0 +#define VERBOSE 1 #define LISPVAL_ASSERT(cond, err) \ if (!(cond)) { \ return lispval_err(err); \ @@ -122,8 +122,12 @@ lispval* lispval_qexpr(void) } // Destructor +void print_lispval_tree(lispval* v, int indent_level); void delete_lispval(lispval* v) { + if(v == NULL) return; + printf("\n Freeing:"); + print_lispval_tree(v, 2); switch (v->type) { case LISPVAL_NUM: if(VERBOSE) printf("\nFreed num"); @@ -652,7 +656,7 @@ lispval* evaluate_lispval(lispval* l, lispenv* env) // Check if this is a symbol if(l->type == LISPVAL_SYM){ - get_from_lispenv(l->sym, env); + return get_from_lispenv(l->sym, env); } // Evaluate the children if needed @@ -662,16 +666,32 @@ lispval* evaluate_lispval(lispval* l, lispenv* env) } } // Check if any are errors. + // lispval* error = NULL; for (int i = 0; i < l->count; i++) { if (l->cell[i]->type == LISPVAL_ERR) { - return clone_lispval(l->cell[i]); + return l->cell[i]; + // error = clone_lispval(l->cell[i]); } } + // Now the problem is that evlauate_lispval is recursive + // and it also allocates new memory. + // This means that I want to delete the allocated memory. + // But the problem is that it could delete the original object l + // leading to a double-free error + // To mitigate this, I've made the delete_lispval function + // a bit more robust, to be able to handle cases where it had + // already been deleted + // But the compromise here is to not care about destruction to + // the original object in case of an error. + // if(error!=NULL){ + // delete_lispval(l); + // return error; + // } // Check if the first element is an operation. if (l->count >= 2 && ((l->cell[0])->type == LISPVAL_FUNC)) { lispval* temp = clone_lispval(l->cell[0]); - lispval* f = pop_lispval(l, 0); + lispval* f = pop_lispval(temp, 0); lispval* operands = temp; // lispval* operation = clone_lispval(l->cell[0]); // lispval* operands = lispval_sexpr(); @@ -754,6 +774,7 @@ int main(int argc, char** argv) print_lispval_parenthesis(answer); printf("\n"); } + delete_lispval(l); delete_lispval(answer); } else {