Catching errors across scopes

This commit is contained in:
2025-09-21 14:10:09 +10:00
parent bfbcd376df
commit c4ebca9ed9
2 changed files with 45 additions and 1 deletions

View File

@@ -279,6 +279,7 @@ struct Struct {
struct Error {
string code;
int pops;
string reporter;
};
/*
@@ -413,6 +414,7 @@ Literal error(string in, string errCode = "syntaxError", int exitc = 1) {
Literal tmpLit;
tmpLit.val = false;
setVal(catches.top()[errCode].varName, tmpLit);
retError.reporter = catches.top()[errCode].varName;
Literal tmpLit2;
tmpLit2.val = retError;
return tmpLit2;
@@ -2383,6 +2385,19 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
// Call the function
Literal retVal = exec(fnToExec.instructions, true);
string errPreserveValue;
if (holds_alternative<Error>(retVal.val)) {
Error err = get<Error>(retVal.val);
if (err.pops > 1) {
err.pops --;
Literal tmpLit;
tmpLit.val = err;
return tmpLit;
}
errPreserveValue = err.reporter;
}
// If we were executing a function in a struct, add back values
if (inStruct) {
for (auto &[key, value] : structVal.values) {
@@ -2394,6 +2409,13 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
variables = scopeBackup;
labelStack.pop();
// Add back the false for any errors
if (!errPreserveValue.empty()) {
Literal errLit;
errLit.val = false;
setVal(errPreserveValue, errLit);
}
// Add back the struct values
variables[structName].val = structVal;

View File

@@ -1 +1,23 @@
error "Hello, world!" "sillyError"
fun -int !divten -int &divisor
divide 10 $divisor &out
# Skip the rest of the function because we have an error
println "aw yeag we devided by zero"
return $out
endfun
fun -string !wrapperFn -int &dingle
pusharg $dingle
!divten &result
# Skip the rest of the function because we have an error
println "big error incoming"
tostring $result &out
return $out
endfun
catch "divisionByZeroError" &success
pusharg 0
!wrapperFn &result
# There's a catch in this scope, stop here and continue execution
println $success