Start work on error handling
This commit is contained in:
@@ -11,6 +11,8 @@
|
||||
|
||||
int currentInstruction = 0;
|
||||
|
||||
bool isMainScopeGlobal = true;
|
||||
|
||||
[[noreturn]] void runtimeError(GroundRuntimeError error, char* what, GroundInstruction* where, int whereLine) {
|
||||
printf("Ground runtime error:\n ErrorType: ");
|
||||
switch (error) {
|
||||
@@ -78,6 +80,22 @@ int currentInstruction = 0;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
[[noreturn]] void throwError(GroundError* error) {
|
||||
printf("Uncaught Ground runtime error:\n ErrorType: %s\n", error->type);
|
||||
if (error->what != NULL) {
|
||||
printf(" ErrorContext: %s\n", error->what);
|
||||
}
|
||||
if (error->where != NULL) {
|
||||
printf(" ErrorInstruction: ");
|
||||
printGroundInstruction(error->where);
|
||||
printf("\n");
|
||||
}
|
||||
if (error->line > -1) {
|
||||
printf(" ErrorLine: %zu\n", error->line + 1);
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
|
||||
GroundLabel* findLabel(GroundLabel* head, const char *id) {
|
||||
GroundLabel *item;
|
||||
HASH_FIND_STR(head, id, item);
|
||||
@@ -438,6 +456,8 @@ GroundValue interpretGroundProgram(GroundProgram* in, GroundScope* inScope) {
|
||||
scope.labels = &labels;
|
||||
scope.variables = &variables;
|
||||
}
|
||||
scope.isMainScope = isMainScopeGlobal;
|
||||
isMainScopeGlobal = false;
|
||||
// Preprocess all labels, structs and functions
|
||||
for (size_t i = 0; i < in->size; i++) {
|
||||
if (in->instructions[i].type == CREATELABEL) {
|
||||
@@ -872,6 +892,10 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("none"));
|
||||
break;
|
||||
}
|
||||
case ERROR: {
|
||||
addVariable(scope->variables, in->args.args[1].value.refName, createStringGroundValue("error"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -1737,6 +1761,12 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
||||
|
||||
if (function->nativeFn) {
|
||||
GroundValue returnValue = function->nativeFn(scope, argsList);
|
||||
if (returnValue.type == ERROR) {
|
||||
if (scope->isMainScope) {
|
||||
throwError(&returnValue.data.errorVal);
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
if (returnValue.type != function->returnType) {
|
||||
runtimeError(RETURN_TYPE_MISMATCH, "Unexpected return value type from native function", in, currentInstruction);
|
||||
}
|
||||
@@ -1756,6 +1786,9 @@ GroundValue interpretGroundInstruction(GroundInstruction inst, GroundScope* scop
|
||||
size_t currentCurrentInstruction = currentInstruction;
|
||||
currentInstruction = function->startLine;
|
||||
GroundValue returnValue = interpretGroundProgram(&function->program, &newScope);
|
||||
if (returnValue.type == ERROR) {
|
||||
return returnValue;
|
||||
}
|
||||
if (returnValue.type != function->returnType) {
|
||||
runtimeError(RETURN_TYPE_MISMATCH, "Unexpected return value type from function", in, currentInstruction);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user