Skip to content

Commit

Permalink
Allow accessing of pointer values in mathematical expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Jan 8, 2021
1 parent 80e0782 commit 17ecdbf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions include/lang/pattern_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ namespace hex::lang {
return "Pointer";
}

[[nodiscard]] PatternData* getPointedAtPattern() {
return this->m_pointedAt;
}

private:
PatternData *m_pointedAt;
};
Expand Down
12 changes: 11 additions & 1 deletion source/lang/evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,18 @@ namespace hex::lang {
std::copy(this->m_globalMembers.begin(), this->m_globalMembers.end(), std::back_inserter(currMembers));

PatternData *currPattern = nullptr;
for (const auto &identifier : node->getPath()) {
for (u32 i = 0; i < node->getPath().size(); i++) {
const auto &identifier = node->getPath()[i];

if (auto structPattern = dynamic_cast<PatternDataStruct*>(currPattern); structPattern != nullptr)
currMembers = structPattern->getMembers();
else if (auto unionPattern = dynamic_cast<PatternDataUnion*>(currPattern); unionPattern != nullptr)
currMembers = unionPattern->getMembers();
else if (auto pointerPattern = dynamic_cast<PatternDataPointer*>(currPattern); pointerPattern != nullptr) {
currPattern = pointerPattern->getPointedAtPattern();
i--;
continue;
}
else if (currPattern != nullptr)
throwEvaluateError("tried to access member of a non-struct/union type", node->getLineNumber());

Expand All @@ -75,6 +82,9 @@ namespace hex::lang {
throwEvaluateError(hex::format("could not find identifier '%s'", identifier.c_str()), node->getLineNumber());
}

if (auto pointerPattern = dynamic_cast<PatternDataPointer*>(currPattern); pointerPattern != nullptr)
currPattern = pointerPattern->getPointedAtPattern();

if (auto unsignedPattern = dynamic_cast<PatternDataUnsigned*>(currPattern); unsignedPattern != nullptr) {
u8 value[unsignedPattern->getSize()];
this->m_provider->read(unsignedPattern->getOffset(), value, unsignedPattern->getSize());
Expand Down

0 comments on commit 17ecdbf

Please sign in to comment.