Skip to content

Commit

Permalink
Avoid errors bubbling up in recursive evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
asynchroza committed Nov 15, 2023
1 parent d148138 commit 04e4389
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions nulascript/eval/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ BooleanStorage* getBooleanReference(bool val) {
return val ? trueStorage : falseStorage;
}

bool isErrorStorage(Storage* storage) {
return checkBase(storage, typeid(ErrorStorage));
}

Storage* evaluateIntegerInfix(std::string op, Storage* leftExpression,
Storage* rightExpression) {
auto left = dynamic_cast<IntegerStorage*>(leftExpression);
Expand Down Expand Up @@ -117,6 +121,8 @@ Storage* evaluateInfix(std::string op, Storage* leftExpression,

Storage* evaluateIf(Conditional* expression) {
auto condition = evaluate(expression->condition);
if (isErrorStorage(condition))
return condition;

if (checkTruthiness(condition)) {
return evaluate(expression->currentBlock);
Expand Down Expand Up @@ -169,7 +175,11 @@ Storage* evaluate(Node* node) {
} else if (checkBase(node, typeid(Infix))) {
auto infix = dynamic_cast<Infix*>(node);
auto leftExpression = evaluate(infix->left);
if (isErrorStorage(leftExpression))
return leftExpression;
auto rightExpression = evaluate(infix->right);
if (isErrorStorage(rightExpression))
return rightExpression;
return evaluateInfix(infix->op, leftExpression, rightExpression);
} else if (checkBase(node, typeid(BlockStatement))) {
auto block = dynamic_cast<BlockStatement*>(node);
Expand Down

0 comments on commit 04e4389

Please sign in to comment.