From 04e4389fcf3c531c491022ca93a69b8edb9d35a8 Mon Sep 17 00:00:00 2001 From: Michael Bozhilov Date: Wed, 15 Nov 2023 20:26:53 +0200 Subject: [PATCH] Avoid errors bubbling up in recursive evaluation --- nulascript/eval/eval.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/nulascript/eval/eval.cc b/nulascript/eval/eval.cc index 7e900d7..dc85866 100644 --- a/nulascript/eval/eval.cc +++ b/nulascript/eval/eval.cc @@ -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(leftExpression); @@ -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); @@ -169,7 +175,11 @@ Storage* evaluate(Node* node) { } else if (checkBase(node, typeid(Infix))) { auto infix = dynamic_cast(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(node);