1
0
mirror of synced 2024-11-12 02:00:52 +01:00

Added arrays

This commit is contained in:
WerWolv 2020-11-12 23:57:43 +01:00
parent f0fe3a85d2
commit 83bb358427
6 changed files with 50 additions and 19 deletions

View File

@ -27,19 +27,20 @@ namespace hex::lang {
class ASTNodeVariableDecl : public ASTNode {
public:
explicit ASTNodeVariableDecl(const Token::TypeToken::Type &type, const std::string &name, const std::string& customTypeName = "", std::optional<u64> offset = { })
: ASTNode(Type::VariableDecl), m_type(type), m_name(name), m_customTypeName(customTypeName), m_offset(offset) { }
explicit ASTNodeVariableDecl(const Token::TypeToken::Type &type, const std::string &name, const std::string& customTypeName = "", std::optional<u64> offset = { }, size_t arraySize = 1)
: ASTNode(Type::VariableDecl), m_type(type), m_name(name), m_customTypeName(customTypeName), m_offset(offset), m_arraySize(arraySize) { }
const Token::TypeToken::Type& getVariableType() const { return this->m_type; }
const std::string& getCustomVariableTypeName() const { return this->m_customTypeName; }
const std::string& getVariableName() const { return this->m_name; };
std::optional<u64> getOffset() { return this->m_offset; };
std::optional<u64> getOffset() const { return this->m_offset; }
size_t getArraySize() const { return this->m_arraySize; }
private:
Token::TypeToken::Type m_type;
std::string m_name, m_customTypeName;
std::optional<u64> m_offset;
size_t m_arraySize;
};
class ASTNodeScope : public ASTNode {

View File

@ -16,6 +16,8 @@ namespace hex::lang {
EndOfExpression,
ScopeOpen,
ScopeClose,
ArrayOpen,
ArrayClose,
Separator,
EndOfProgram
} type;

View File

@ -83,6 +83,12 @@ namespace hex::lang {
} else if (c == '}') {
tokens.push_back({.type = Token::Type::ScopeClose});
offset += 1;
} else if (c == '[') {
tokens.push_back({.type = Token::Type::ArrayOpen});
offset += 1;
} else if (c == ']') {
tokens.push_back({.type = Token::Type::ArrayClose});
offset += 1;
} else if (c == ',') {
tokens.push_back({.type = Token::Type::Separator});
offset += 1;

View File

@ -35,6 +35,14 @@ namespace hex::lang {
return new ASTNodeVariableDecl(Token::TypeToken::Type::CustomType, curr[-2].identifierToken.identifier, curr[-3].identifierToken.identifier);
}
ASTNode* parseBuiltinArrayDecl(TokenIter &curr) {
return new ASTNodeVariableDecl(curr[-6].typeToken.type, curr[-5].identifierToken.identifier, "", { }, curr[-3].integerToken.integer);
}
ASTNode* parseCustomTypeArrayDecl(TokenIter &curr) {
return new ASTNodeVariableDecl(Token::TypeToken::Type::CustomType, curr[-5].identifierToken.identifier, curr[-6].identifierToken.identifier, { }, curr[-3].integerToken.integer);
}
ASTNode* parseFreeBuiltinVariableDecl(TokenIter &curr) {
return new ASTNodeVariableDecl(curr[-5].typeToken.type, curr[-4].identifierToken.identifier, "", curr[-2].integerToken.integer);
}
@ -52,6 +60,10 @@ namespace hex::lang {
nodes.push_back(parseBuiltinVariableDecl(curr));
else if (tryConsume(curr, {Token::Type::Identifier, Token::Type::Identifier, Token::Type::EndOfExpression}))
nodes.push_back(parseCustomTypeVariableDecl(curr));
else if (tryConsume(curr, {Token::Type::Type, Token::Type::Identifier, Token::Type::ArrayOpen, Token::Type::Integer, Token::Type::ArrayClose, Token::Type::EndOfExpression}))
nodes.push_back(parseBuiltinArrayDecl(curr));
else if (tryConsume(curr, {Token::Type::Identifier, Token::Type::Identifier, Token::Type::ArrayOpen, Token::Type::Integer, Token::Type::ArrayClose, Token::Type::EndOfExpression}))
nodes.push_back(parseCustomTypeArrayDecl(curr));
else break;
}

View File

@ -119,7 +119,7 @@ namespace hex {
u64 offset = varNode->getOffset().value();
if (varNode->getVariableType() != lang::Token::TypeToken::Type::CustomType) {
this->setHighlight(offset, static_cast<u32>(varNode->getVariableType()) >> 4, varNode->getVariableName());
this->setHighlight(offset, (static_cast<u32>(varNode->getVariableType()) >> 4) * varNode->getArraySize(), varNode->getVariableName());
} else {
for (auto &structNode : findNodes<lang::ASTNodeStruct>(lang::ASTNode::Type::Struct, ast))
if (varNode->getCustomVariableTypeName() == structNode->getName())
@ -139,7 +139,7 @@ namespace hex {
s32 ViewPattern::highlightUsingDecls(std::vector<lang::ASTNode*> &ast, lang::ASTNodeTypeDecl* currTypeDeclNode, lang::ASTNodeVariableDecl* currVarDecl, u64 offset) {
if (currTypeDeclNode->getAssignedType() != lang::Token::TypeToken::Type::CustomType) {
size_t size = static_cast<u32>(currTypeDeclNode->getAssignedType()) >> 4;
size_t size = (static_cast<u32>(currTypeDeclNode->getAssignedType()) >> 4) * currVarDecl->getArraySize();
this->setHighlight(offset, size, currVarDecl->getVariableName());
offset += size;
@ -173,7 +173,7 @@ namespace hex {
auto var = static_cast<lang::ASTNodeVariableDecl*>(node);
if (var->getVariableType() != lang::Token::TypeToken::Type::CustomType) {
size_t size = static_cast<u32>(var->getVariableType()) >> 4;
size_t size = (static_cast<u32>(var->getVariableType()) >> 4) * var->getArraySize();
this->setHighlight(offset, size, var->getVariableName());
offset += size;
@ -181,10 +181,15 @@ namespace hex {
bool foundType = false;
for (auto &structNode : findNodes<lang::ASTNodeStruct>(lang::ASTNode::Type::Struct, ast))
if (structNode->getName() == var->getCustomVariableTypeName()) {
auto size = this->highlightStruct(ast, structNode, offset);
size_t size = 0;
for (size_t i = 0; i < var->getArraySize(); i++) {
size = this->highlightStruct(ast, structNode, offset);
if (size == -1)
return -1;
if (size == -1)
return -1;
offset += size;
}
offset += size;
foundType = true;

View File

@ -34,19 +34,24 @@ namespace hex {
if (ImGui::Begin("Pattern Data", &this->m_windowOpen)) {
ImGui::BeginChild("##scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNav);
for (auto& [offset, size, color, name] : this->m_highlights) {
std::vector<u8> buffer(size + 1, 0x00);
if (this->m_dataProvider != nullptr && this->m_dataProvider->isReadable()) {
this->m_dataProvider->read(offset, buffer.data(), size);
for (auto&[offset, size, color, name] : this->m_highlights) {
std::vector<u8> buffer(size + 1, 0x00);
if (size <= 8) {
u64 data = 0;
std::memcpy(&data, buffer.data(), size);
this->m_dataProvider->read(offset, buffer.data(), size);
ImGui::LabelText(name.c_str(), "[0x%08lx:0x%08lx] %lu (0x%08lx) \"%s\"", offset, offset + size, data, data, makeDisplayable(buffer.data(), buffer.size()).c_str());
if (size <= 8) {
u64 data = 0;
std::memcpy(&data, buffer.data(), size);
ImGui::LabelText(name.c_str(), "[0x%08lx:0x%08lx] %lu (0x%08lx) \"%s\"", offset,
offset + size, data, data,
makeDisplayable(buffer.data(), buffer.size()).c_str());
} else
ImGui::LabelText(name.c_str(), "[0x%08lx:0x%08lx] [ ARRAY ] \"%s\"", offset, offset + size,
makeDisplayable(buffer.data(), buffer.size()).c_str());
}
else
ImGui::LabelText(name.c_str(), "[0x%08lx:0x%08lx] [ ARRAY ] \"%s\"", offset, offset + size, makeDisplayable(buffer.data(), buffer.size()).c_str());
}
ImGui::EndChild();