From cbf5417c5eceef00b8683a09781fbd91cf955b5d Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Thu, 17 Feb 2022 10:04:52 +0100 Subject: [PATCH] FIX: Check the left member of binary expression where we can --- src/Lib/Script/Ast/Parser/IrExpression.hh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Lib/Script/Ast/Parser/IrExpression.hh b/src/Lib/Script/Ast/Parser/IrExpression.hh index 0412c4b9..8713a302 100644 --- a/src/Lib/Script/Ast/Parser/IrExpression.hh +++ b/src/Lib/Script/Ast/Parser/IrExpression.hh @@ -30,28 +30,28 @@ auto updateExprWithInfixOperator = [throwUnexpectedToken, createOpElement]( }; auto updateLeft = []<typename IrT>(IrExpression *current, IrExpression *newLeft) -> void { - dynamic_cast<IrT *>(current)->left = newLeft; + IrT *typpedCurrent = dynamic_cast<IrT *>(current); + if (typpedCurrent->left != nullptr) + throw std::runtime_error("Unexpected token in expression parsing: " + tok.toString()); + typpedCurrent->left = newLeft; newLeft->setParent(current); }; auto updateExprWithConstExpr = [updateLeft](IrExpression *¤t, Token tok) -> void { IrExpression *newLeft = IrElement::create<IrEConstExpr>(current, tok); + /* (... `op` <- currentExpression) newLeft -> currentExpression */ - if (current != nullptr && current->left == nullptr) { + if (current != nullptr) { switch (current->type()) { - case Type::CompOp: updateLeft<IrECompOp>(current, newLeft); return; - case Type::ArithmeticOp: updateLeft<IrEArithmeticOp>(current, newLeft); return; - case Type::LogicOp: updateLeft<IrELogicOp>(current, newLeft); return; + case Type::CompOp: return <IrECompOp>(current, newLeft); + case Type::ArithmeticOp: return updateLeft<IrEArithmeticOp>(current, newLeft); + case Type::LogicOp: return updateLeft<IrELogicOp>(current, newLeft); case Type::ConstExpr: case Type::Call: case Type::VariableRef: throw std::runtime_error("Invalid expression"); } } - /* Can be the case for `1 + 2 2` which is invalid */ - else if (current != nullptr && current->left != nullptr) - throw std::runtime_error("Unexpected token in expression parsing: " + tok.toString()); - /* newLeft -> currentExpression */ else current = newLeft; -- GitLab