Allow enum entries to be accessed via the scope resolution operator

This commit is contained in:
WerWolv
2021-01-06 16:28:41 +01:00
parent be515d4c40
commit 7fd0d87d56
4 changed files with 49 additions and 21 deletions

View File

@@ -4,7 +4,6 @@
#include <bit>
#include <algorithm>
#include <ranges>
#include <unistd.h>
@@ -14,6 +13,25 @@ namespace hex::lang {
: m_provider(provider), m_defaultDataEndian(defaultDataEndian) {
}
ASTNodeIntegerLiteral* Evaluator::evaluateScopeResolution(ASTNodeScopeResolution *node) {
ASTNode *currScope = nullptr;
for (const auto &identifier : node->getPath()) {
if (currScope == nullptr) {
if (!this->m_types.contains(identifier))
break;
currScope = this->m_types[identifier.data()];
} else if (auto enumNode = dynamic_cast<ASTNodeEnum*>(currScope); enumNode != nullptr) {
if (!enumNode->getEntries().contains(identifier))
break;
else
return evaluateMathematicalExpression(static_cast<ASTNodeNumericExpression*>(enumNode->getEntries().at(identifier)));
}
}
throwEvaluateError("failed to find identifier", node->getLineNumber());
}
ASTNodeIntegerLiteral* Evaluator::evaluateRValue(ASTNodeRValue *node) {
const std::vector<PatternData*>* currMembers = this->m_currMembers.back();
@@ -159,6 +177,8 @@ namespace hex::lang {
leftInteger = evaluateMathematicalExpression(leftExprExpression);
else if (auto leftExprRvalue = dynamic_cast<ASTNodeRValue*>(node->getLeftOperand()); leftExprRvalue != nullptr)
leftInteger = evaluateRValue(leftExprRvalue);
else if (auto leftExprScopeResolution = dynamic_cast<ASTNodeScopeResolution*>(node->getLeftOperand()); leftExprScopeResolution != nullptr)
leftInteger = evaluateScopeResolution(leftExprScopeResolution);
else
throwEvaluateError("invalid expression. Expected integer literal", node->getLineNumber());
@@ -168,6 +188,8 @@ namespace hex::lang {
rightInteger = evaluateMathematicalExpression(rightExprExpression);
else if (auto rightExprRvalue = dynamic_cast<ASTNodeRValue*>(node->getRightOperand()); rightExprRvalue != nullptr)
rightInteger = evaluateRValue(rightExprRvalue);
else if (auto rightExprScopeResolution = dynamic_cast<ASTNodeScopeResolution*>(node->getRightOperand()); rightExprScopeResolution != nullptr)
rightInteger = evaluateScopeResolution(rightExprScopeResolution);
else
throwEvaluateError("invalid expression. Expected integer literal", node->getLineNumber());
@@ -486,6 +508,8 @@ namespace hex::lang {
patterns.push_back(this->evaluateArray(arrayDeclNode));
} else if (auto pointerDeclNode = dynamic_cast<ASTNodePointerVariableDecl*>(node); pointerDeclNode != nullptr) {
patterns.push_back(this->evaluatePointer(pointerDeclNode));
} else if (auto typeDeclNode = dynamic_cast<ASTNodeTypeDecl*>(node); typeDeclNode != nullptr) {
this->m_types[typeDeclNode->getName().data()] = typeDeclNode->getType();
}
}