From af42d2ff66c6ba8c185766265d48c3f05f1ba9c9 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 4 Jan 2021 14:10:59 +0100 Subject: [PATCH] Properly convert rvalue to mathematical expression --- source/lang/evaluator.cpp | 8 +++++--- source/lang/parser.cpp | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/source/lang/evaluator.cpp b/source/lang/evaluator.cpp index 6f31ddfd7..d9cda1fe1 100644 --- a/source/lang/evaluator.cpp +++ b/source/lang/evaluator.cpp @@ -317,11 +317,13 @@ namespace hex::lang { auto startOffset = this->m_currOffset; - auto sizeNode = dynamic_cast(node->getSize()); - if (sizeNode == nullptr) + ASTNodeIntegerLiteral *valueNode; + + if (auto sizeNumericExpression = dynamic_cast(node->getSize()); sizeNumericExpression != nullptr) + valueNode = evaluateMathematicalExpression(sizeNumericExpression); + else throwEvaluateError("array size not a numeric expression", node->getLineNumber()); - auto valueNode = evaluateMathematicalExpression(sizeNode); SCOPE_EXIT( delete valueNode; ); auto arraySize = std::get(valueNode->getValue()); diff --git a/source/lang/parser.cpp b/source/lang/parser.cpp index 22309628d..2f31a65b6 100644 --- a/source/lang/parser.cpp +++ b/source/lang/parser.cpp @@ -5,6 +5,8 @@ #define MATCHES(x) (begin() && x) +#define TO_NUMERIC_EXPRESSION(node) new ASTNodeNumericExpression((node), new ASTNodeIntegerLiteral(0, Token::ValueType::Signed128Bit), Token::Operator::Plus) + // Definition syntax: // [A] : Either A or no token // [A|B] : Either A, B or no token @@ -27,13 +29,13 @@ namespace hex::lang { else throwParseError("expected member name", -1); } else - return new ASTNodeRValue(path); + return TO_NUMERIC_EXPRESSION(new ASTNodeRValue(path)); } // ASTNode* Parser::parseFactor() { if (MATCHES(sequence(INTEGER))) - return new ASTNodeNumericExpression(new ASTNodeIntegerLiteral(getValue(-1), Token::ValueType::Signed128Bit), new ASTNodeIntegerLiteral(0, Token::ValueType::Signed128Bit), Token::Operator::Plus); + return TO_NUMERIC_EXPRESSION(new ASTNodeIntegerLiteral(getValue(-1), Token::ValueType::Signed128Bit)); else if (MATCHES(sequence(SEPARATOR_ROUNDBRACKETOPEN))) { auto node = this->parseMathematicalExpression(); if (!MATCHES(sequence(SEPARATOR_ROUNDBRACKETCLOSE)))