Add catch support

This commit is contained in:
2026-04-15 10:37:14 +10:00
parent ca85550c92
commit e4b5aafe35
4 changed files with 36 additions and 13 deletions

View File

@@ -758,7 +758,18 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
instructionsToPause --;
int ci = currentInstruction;
GroundValue gv = interpretGroundInstruction(in->instructions[i], &scope);
if (gv.type != NONE) {
if (gv.type == ERROR) {
GroundCatch* catch = findCatch(*scope.catches, gv.data.errorVal.type);
if (catch != NULL) {
currentInstruction = catch->label->lineNum;
} else {
if (scope.isMainScope) {
throwError(&gv.data.errorVal);
} else {
return gv;
}
}
} else if (gv.type != NONE) {
return gv;
}
if (ci != currentInstruction) {
@@ -1964,16 +1975,6 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
returnValue.data.errorVal.line = currentInstruction;
returnValue.data.errorVal.hasLine = true;
returnValue.data.errorVal.where = in;
GroundCatch* catch = findCatch(*scope->catches, returnValue.data.errorVal.type);
if (catch != NULL) {
// Jump to label where catch points to
currentInstruction = catch->label->lineNum;
}
if (scope->isMainScope) {
throwError(&returnValue.data.errorVal);
}
return returnValue;
}
if (returnValue.type != ANY && returnValue.type != function->returnType) {
@@ -2451,8 +2452,8 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
return createErrorGroundValue(
createGroundError(
in->args.args[0].value.value.data.stringVal,
in->args.args[1].value.value.data.stringVal,
in->args.args[0].value.value.data.stringVal,
in,
(size_t*)&currentInstruction
)
@@ -2478,8 +2479,9 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
}
GroundCatch catch = {
.label = label
.label = label,
};
snprintf(catch.id, sizeof(catch.id), "%s", in->args.args[0].value.value.data.stringVal);
HASH_ADD_STR(*scope->catches, id, &catch);
break;