1
0
mirror of synced 2024-11-28 09:30:51 +01:00

patterns: Allow usage of types within itself and used out-of-order

This commit is contained in:
WerWolv 2021-04-13 20:40:21 +02:00
parent 8a485575f5
commit 950598911c
5 changed files with 40 additions and 18 deletions

View File

@ -33,8 +33,6 @@ set(LIBIMHEX_SOURCES
source/lang/parser.cpp
source/lang/validator.cpp
source/lang/evaluator.cpp
source/lang/builtin_functions.cpp
source/providers/provider.cpp
source/views/view.cpp

View File

@ -149,7 +149,10 @@ namespace hex::lang {
ASTNodeTypeDecl(const ASTNodeTypeDecl& other) : ASTNode(other), Attributable(other) {
this->m_name = other.m_name;
this->m_type = other.m_type->clone();
if (other.m_type != nullptr)
this->m_type = other.m_type->clone();
else
this->m_type = nullptr;
this->m_endian = other.m_endian;
}

View File

@ -1,10 +0,0 @@
#include <hex.hpp>
#include <hex/api/content_registry.hpp>
namespace hex::lang {
}

View File

@ -517,10 +517,6 @@ namespace hex::lang {
for (auto &member : node->getMembers()) {
this->evaluateMember(member, memberPatterns, true);
structPattern->setMembers(memberPatterns);
for (auto &pmember : memberPatterns)
hex::print("{}\n", pmember->getVariableName());
hex::print("======\n");
}
structPattern->setSize(this->m_currOffset - startOffset);
@ -639,6 +635,9 @@ namespace hex::lang {
PatternData* Evaluator::evaluateType(ASTNodeTypeDecl *node) {
auto type = node->getType();
if (type == nullptr)
type = this->m_types[node->getName().data()];
this->m_endianStack.push_back(node->getEndian().value_or(this->m_defaultDataEndian));
PatternData *pattern;
@ -858,6 +857,18 @@ namespace hex::lang {
this->m_currOffset = 0;
try {
for (const auto& node : ast) {
if (auto typeDeclNode = dynamic_cast<ASTNodeTypeDecl*>(node); typeDeclNode != nullptr) {
if (this->m_types[typeDeclNode->getName().data()] == nullptr)
this->m_types[typeDeclNode->getName().data()] = typeDeclNode->getType();
}
}
for (const auto& [name, node] : this->m_types) {
if (auto typeDeclNode = static_cast<ASTNodeTypeDecl*>(node); typeDeclNode->getType() == nullptr)
this->getConsole().abortEvaluation(hex::format("unresolved type '{}'", name));
}
for (const auto& node : ast) {
this->m_currMembers.clear();
this->m_currMemberScope.clear();
@ -873,7 +884,7 @@ namespace hex::lang {
} else if (auto pointerDeclNode = dynamic_cast<ASTNodePointerVariableDecl*>(node); pointerDeclNode != nullptr) {
this->m_globalMembers.push_back(this->evaluatePointer(pointerDeclNode));
} else if (auto typeDeclNode = dynamic_cast<ASTNodeTypeDecl*>(node); typeDeclNode != nullptr) {
this->m_types[typeDeclNode->getName().data()] = typeDeclNode->getType();
// Handled above
} else if (auto functionCallNode = dynamic_cast<ASTNodeFunctionCall*>(node); functionCallNode != nullptr) {
auto result = this->evaluateFunctionCall(functionCallNode);
delete result;

View File

@ -498,6 +498,11 @@ namespace hex::lang {
const auto &typeName = getValue<std::string>(-2);
auto structGuard = SCOPE_GUARD { delete structNode; };
if (this->m_types.contains(typeName))
throwParseError(hex::format("redefinition of type '{}'", typeName));
this->m_types.insert({ typeName, new ASTNodeTypeDecl(typeName, nullptr) });
while (!MATCHES(sequence(SEPARATOR_CURLYBRACKETCLOSE))) {
structNode->addMember(parseMember());
}
@ -513,6 +518,11 @@ namespace hex::lang {
const auto &typeName = getValue<std::string>(-2);
auto unionGuard = SCOPE_GUARD { delete unionNode; };
if (this->m_types.contains(typeName))
throwParseError(hex::format("redefinition of type '{}'", typeName));
this->m_types.insert({ typeName, new ASTNodeTypeDecl(typeName, nullptr) });
while (!MATCHES(sequence(SEPARATOR_CURLYBRACKETCLOSE))) {
unionNode->addMember(parseMember());
}
@ -537,6 +547,11 @@ namespace hex::lang {
const auto enumNode = new ASTNodeEnum(underlyingType);
auto enumGuard = SCOPE_GUARD { delete enumNode; };
if (this->m_types.contains(typeName))
throwParseError(hex::format("redefinition of type '{}'", typeName));
this->m_types.insert({ typeName, new ASTNodeTypeDecl(typeName, nullptr) });
ASTNode *lastEntry = nullptr;
while (!MATCHES(sequence(SEPARATOR_CURLYBRACKETCLOSE))) {
if (MATCHES(sequence(IDENTIFIER, OPERATOR_ASSIGNMENT))) {
@ -581,6 +596,11 @@ namespace hex::lang {
const auto bitfieldNode = new ASTNodeBitfield();
auto enumGuard = SCOPE_GUARD { delete bitfieldNode; };
if (this->m_types.contains(typeName))
throwParseError(hex::format("redefinition of type '{}'", typeName));
this->m_types.insert({ typeName, new ASTNodeTypeDecl(typeName, nullptr) });
while (!MATCHES(sequence(SEPARATOR_CURLYBRACKETCLOSE))) {
if (MATCHES(sequence(IDENTIFIER, OPERATOR_INHERIT))) {
auto name = getValue<std::string>(-2);