Destructor

This commit is contained in:
2026-04-11 18:53:17 +10:00
parent e0bc9261a3
commit 5b01784011

View File

@@ -148,6 +148,33 @@ GroundVariable* findVariable(GroundVariable* head, const char *id) {
}
void deleteVariable(GroundVariable** head, GroundVariable *item) {
if (strcmp(item->id, "self") == 0) {
return;
}
if (item->value.type == CUSTOM) {
GroundObjectField* destructor = findField(*item->value.data.customVal, "destructor");
if (destructor != NULL) {
if (destructor->value.type == FUNCTION) {
GroundScope scope = {
.labels = NULL,
.variables = malloc(sizeof(GroundVariable*)),
.isMainScope = false
};
if (scope.variables == NULL) {
runtimeError(FIXME, "Failed to allocate memory for scope variables", NULL, -1);
}
*scope.variables = NULL;
addVariable(scope.variables, "self", item->value);
GroundInstruction instruction = createGroundInstruction(CALLMETHOD);
addArgToInstruction(&instruction, createRefGroundArg(DIRREF, "self"));
addArgToInstruction(&instruction, createRefGroundArg(FNREF, "destructor"));
addArgToInstruction(&instruction, createRefGroundArg(DIRREF, ""));
interpretGroundInstruction(instruction, &scope);
} else {
runtimeError(ARG_TYPE_MISMATCH, "Custom type destructor must be a function", NULL, -1);
}
}
}
HASH_DEL(*head, item);
free(item);
}
@@ -1922,6 +1949,13 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
if (returnValue.type != function->returnType) {
runtimeError(RETURN_TYPE_MISMATCH, "Unexpected return value type from function", in, currentInstruction);
}
GroundVariable* var, *tmp;
HASH_ITER(hh, *newScope.variables, var, tmp) {
deleteVariable(newScope.variables, var);
}
addVariable(scope->variables, in->args.args[in->args.length - 1].value.refName, returnValue);
currentInstruction = currentCurrentInstruction;
}