forked from ground/ground
Catching errors across scopes
This commit is contained in:
22
src/main.cpp
22
src/main.cpp
@@ -279,6 +279,7 @@ struct Struct {
|
|||||||
struct Error {
|
struct Error {
|
||||||
string code;
|
string code;
|
||||||
int pops;
|
int pops;
|
||||||
|
string reporter;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -413,6 +414,7 @@ Literal error(string in, string errCode = "syntaxError", int exitc = 1) {
|
|||||||
Literal tmpLit;
|
Literal tmpLit;
|
||||||
tmpLit.val = false;
|
tmpLit.val = false;
|
||||||
setVal(catches.top()[errCode].varName, tmpLit);
|
setVal(catches.top()[errCode].varName, tmpLit);
|
||||||
|
retError.reporter = catches.top()[errCode].varName;
|
||||||
Literal tmpLit2;
|
Literal tmpLit2;
|
||||||
tmpLit2.val = retError;
|
tmpLit2.val = retError;
|
||||||
return tmpLit2;
|
return tmpLit2;
|
||||||
@@ -2383,6 +2385,19 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
|
|||||||
// Call the function
|
// Call the function
|
||||||
Literal retVal = exec(fnToExec.instructions, true);
|
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 we were executing a function in a struct, add back values
|
||||||
if (inStruct) {
|
if (inStruct) {
|
||||||
for (auto &[key, value] : structVal.values) {
|
for (auto &[key, value] : structVal.values) {
|
||||||
@@ -2394,6 +2409,13 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
|
|||||||
variables = scopeBackup;
|
variables = scopeBackup;
|
||||||
labelStack.pop();
|
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
|
// Add back the struct values
|
||||||
variables[structName].val = structVal;
|
variables[structName].val = structVal;
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user