diff --git a/src/interpreter.c b/src/interpreter.c index 8498e29..2b27c01 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -148,9 +148,16 @@ GroundVariable* findVariable(GroundVariable* head, const char *id) { } void deleteVariable(GroundVariable** head, GroundVariable *item) { + if (item == NULL) { + return; + } if (strcmp(item->id, "self") == 0) { return; } + if (item->freed) { + return; + } + item->freed = true; if (item->value.type == CUSTOM) { GroundObjectField* destructor = findField(*item->value.data.customVal, "destructor"); if (destructor != NULL) { @@ -175,6 +182,7 @@ void deleteVariable(GroundVariable** head, GroundVariable *item) { } } } + freeGroundValue(&item->value); HASH_DEL(*head, item); free(item); } @@ -182,12 +190,12 @@ void deleteVariable(GroundVariable** head, GroundVariable *item) { void addVariable(GroundVariable **head, const char *id, GroundValue data) { GroundVariable* variable = findVariable(*head, id); if (variable) { - freeGroundValue(&variable->value); deleteVariable(head, variable); } GroundVariable* item = malloc(sizeof(GroundVariable)); snprintf(item->id, MAX_ID_LEN, "%s", id); item->value = copyGroundValue(&data); + item->freed = false; HASH_ADD_STR(*head, id, item); } @@ -1904,7 +1912,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction); } } else { - if (function->args[i].type != ANY || !checkFnTypes(&in->args.args[i + 1].value.value, &function->args[i])) { + if (function->args[i].type != ANY && !checkFnTypes(&in->args.args[i + 1].value.value, &function->args[i])) { runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction); } } @@ -1934,7 +1942,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop if (in->args.args[i + 1].type != VALUE) { runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value", in, currentInstruction); } - if (function->args[i].type != ANY || !checkFnTypes(&in->args.args[i + 1].value.value, &function->args[i])) { + if (function->args[i].type != ANY && !checkFnTypes(&in->args.args[i + 1].value.value, &function->args[i])) { runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction); } addVariable(newScope.variables, function->args[i].name, in->args.args[i + 1].value.value); @@ -2008,7 +2016,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction); } } else { - if (function->args[i].type != ANY || !checkFnTypes(&in->args.args[i + 2].value.value, &function->args[i])) { + if (function->args[i].type != ANY && !checkFnTypes(&in->args.args[i + 2].value.value, &function->args[i])) { runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction); } } @@ -2063,7 +2071,7 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop runtimeError(ARG_TYPE_MISMATCH, "Expecting a Value", in, currentInstruction); } //if (in->args.args[i + 1].value.value.type != function->args[i].type) { - if (function->args[i].type != ANY || !checkFnTypes(&in->args.args[i + 2].value.value, &function->args[i])) { + if (function->args[i].type != ANY && !checkFnTypes(&in->args.args[i + 2].value.value, &function->args[i])) { runtimeError(ARG_TYPE_MISMATCH, "Mismatched function argument types", in, currentInstruction); } addVariable(newScope.variables, function->args[i].name, in->args.args[i + 2].value.value); diff --git a/src/types.h b/src/types.h index 8c96cd4..c75fee9 100644 --- a/src/types.h +++ b/src/types.h @@ -107,6 +107,7 @@ typedef struct GroundVariable { char id[MAX_ID_LEN]; GroundValue value; UT_hash_handle hh; + bool freed; } GroundVariable; typedef struct GroundScope {