mirror of https://github.com/WerWolv/ImHex.git
Added padding type to pattern language
This commit is contained in:
parent
48296775ae
commit
57dcf6cc93
|
@ -31,7 +31,7 @@ namespace hex::lang {
|
||||||
|
|
||||||
class PatternData {
|
class PatternData {
|
||||||
public:
|
public:
|
||||||
enum class Type { Unsigned, Signed, Float, Character, String, Struct, Union, Array, Enum };
|
enum class Type { Padding, Unsigned, Signed, Float, Character, String, Struct, Union, Array, Enum };
|
||||||
|
|
||||||
PatternData(Type type, u64 offset, size_t size, const std::string &name, u32 color = 0)
|
PatternData(Type type, u64 offset, size_t size, const std::string &name, u32 color = 0)
|
||||||
: m_type(type), m_offset(offset), m_size(size), m_color(color), m_name(name) {
|
: m_type(type), m_offset(offset), m_size(size), m_color(color), m_name(name) {
|
||||||
|
@ -144,6 +144,17 @@ namespace hex::lang {
|
||||||
static inline u8 s_paletteOffset = 0;
|
static inline u8 s_paletteOffset = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class PatternDataPadding : public PatternData {
|
||||||
|
public:
|
||||||
|
PatternDataPadding(u64 offset, size_t size) : PatternData(Type::Padding, offset, size, "", 0x00FFFFFF) { }
|
||||||
|
|
||||||
|
void createEntry(prv::Provider* &provider) override {
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string getTypeName() override {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class PatternDataUnsigned : public PatternData {
|
class PatternDataUnsigned : public PatternData {
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -58,7 +58,8 @@ namespace hex::lang {
|
||||||
Signed128Bit = 0x101,
|
Signed128Bit = 0x101,
|
||||||
Float = 0x42,
|
Float = 0x42,
|
||||||
Double = 0x82,
|
Double = 0x82,
|
||||||
CustomType = 0x00
|
CustomType = 0x00,
|
||||||
|
Padding = 0x1F
|
||||||
} type;
|
} type;
|
||||||
} typeToken;
|
} typeToken;
|
||||||
};
|
};
|
||||||
|
|
|
@ -173,32 +173,34 @@ namespace hex::lang {
|
||||||
std::pair<PatternData*, size_t> Evaluator::createArrayPattern(ASTNodeVariableDecl *varDeclNode, u64 offset) {
|
std::pair<PatternData*, size_t> Evaluator::createArrayPattern(ASTNodeVariableDecl *varDeclNode, u64 offset) {
|
||||||
std::vector<PatternData*> entries;
|
std::vector<PatternData*> entries;
|
||||||
|
|
||||||
size_t arraySize = 0;
|
size_t arrayOffset = 0;
|
||||||
for (u32 i = 0; i < varDeclNode->getArraySize(); i++) {
|
for (u32 i = 0; i < varDeclNode->getArraySize(); i++) {
|
||||||
ASTNodeVariableDecl *nonArrayVarDeclNode = new ASTNodeVariableDecl(varDeclNode->getVariableType(), "[" + std::to_string(i) + "]", varDeclNode->getCustomVariableTypeName(), varDeclNode->getOffset(), 1);
|
ASTNodeVariableDecl *nonArrayVarDeclNode = new ASTNodeVariableDecl(varDeclNode->getVariableType(), "[" + std::to_string(i) + "]", varDeclNode->getCustomVariableTypeName(), varDeclNode->getOffset(), 1);
|
||||||
|
|
||||||
if (varDeclNode->getVariableType() != Token::TypeToken::Type::CustomType) {
|
if (varDeclNode->getVariableType() == Token::TypeToken::Type::Padding) {
|
||||||
const auto &[pattern, size] = this->createBuiltInTypePattern(nonArrayVarDeclNode, offset + arraySize);
|
return { new PatternDataPadding(offset, varDeclNode->getArraySize()), varDeclNode->getArraySize() };
|
||||||
|
} else if (varDeclNode->getVariableType() != Token::TypeToken::Type::CustomType) {
|
||||||
|
const auto &[pattern, size] = this->createBuiltInTypePattern(nonArrayVarDeclNode, offset + arrayOffset);
|
||||||
|
|
||||||
if (pattern == nullptr)
|
if (pattern == nullptr)
|
||||||
return { nullptr, 0 };
|
return { nullptr, 0 };
|
||||||
|
|
||||||
entries.push_back(pattern);
|
entries.push_back(pattern);
|
||||||
arraySize += size;
|
arrayOffset += size;
|
||||||
} else {
|
} else {
|
||||||
const auto &[pattern, size] = this->createCustomTypePattern(nonArrayVarDeclNode, offset + arraySize);
|
const auto &[pattern, size] = this->createCustomTypePattern(nonArrayVarDeclNode, offset + arrayOffset);
|
||||||
|
|
||||||
if (pattern == nullptr)
|
if (pattern == nullptr)
|
||||||
return { nullptr, 0 };
|
return { nullptr, 0 };
|
||||||
|
|
||||||
entries.push_back(pattern);
|
entries.push_back(pattern);
|
||||||
arraySize += size;
|
arrayOffset += size;
|
||||||
}
|
}
|
||||||
|
|
||||||
delete nonArrayVarDeclNode;
|
delete nonArrayVarDeclNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
return { new PatternDataArray(offset, arraySize, varDeclNode->getVariableName(), entries, 0x00FFFFFF), arraySize };
|
return { new PatternDataArray(offset, arrayOffset, varDeclNode->getVariableName(), entries, 0x00FFFFFF), arrayOffset };
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<PatternData*, size_t> Evaluator::createStringPattern(ASTNodeVariableDecl *varDeclNode, u64 offset) {
|
std::pair<PatternData*, size_t> Evaluator::createStringPattern(ASTNodeVariableDecl *varDeclNode, u64 offset) {
|
||||||
|
@ -242,7 +244,7 @@ namespace hex::lang {
|
||||||
size_t typeSize = getTypeSize(type);
|
size_t typeSize = getTypeSize(type);
|
||||||
size_t arraySize = varDeclNode->getArraySize();
|
size_t arraySize = varDeclNode->getArraySize();
|
||||||
|
|
||||||
if (isSigned(type)) {
|
if (isSigned(type)) {
|
||||||
if (typeSize == 1 && arraySize == 1)
|
if (typeSize == 1 && arraySize == 1)
|
||||||
return { new PatternDataCharacter(offset, typeSize, varDeclNode->getVariableName()), 1 };
|
return { new PatternDataCharacter(offset, typeSize, varDeclNode->getVariableName()), 1 };
|
||||||
else if (arraySize > 1)
|
else if (arraySize > 1)
|
||||||
|
|
|
@ -169,6 +169,8 @@ namespace hex::lang {
|
||||||
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Float } });
|
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Float } });
|
||||||
else if (identifier == "double")
|
else if (identifier == "double")
|
||||||
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Double } });
|
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Double } });
|
||||||
|
else if (identifier == "padding")
|
||||||
|
tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Padding } });
|
||||||
|
|
||||||
// If it's not a keyword and a builtin type, it has to be an identifier
|
// If it's not a keyword and a builtin type, it has to be an identifier
|
||||||
|
|
||||||
|
|
|
@ -39,6 +39,10 @@ namespace hex::lang {
|
||||||
return new ASTNodeVariableDecl(curr[-6].typeToken.type, curr[-5].identifierToken.identifier, "", { }, curr[-3].integerToken.integer);
|
return new ASTNodeVariableDecl(curr[-6].typeToken.type, curr[-5].identifierToken.identifier, "", { }, curr[-3].integerToken.integer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ASTNode* parsePaddingDecl(TokenIter &curr) {
|
||||||
|
return new ASTNodeVariableDecl(curr[-5].typeToken.type, "", "", { }, curr[-3].integerToken.integer);
|
||||||
|
}
|
||||||
|
|
||||||
ASTNode* parseCustomTypeArrayDecl(TokenIter &curr) {
|
ASTNode* parseCustomTypeArrayDecl(TokenIter &curr) {
|
||||||
return new ASTNodeVariableDecl(Token::TypeToken::Type::CustomType, curr[-5].identifierToken.identifier, curr[-6].identifierToken.identifier, { }, curr[-3].integerToken.integer);
|
return new ASTNodeVariableDecl(Token::TypeToken::Type::CustomType, curr[-5].identifierToken.identifier, curr[-6].identifierToken.identifier, { }, curr[-3].integerToken.integer);
|
||||||
}
|
}
|
||||||
|
@ -64,6 +68,13 @@ namespace hex::lang {
|
||||||
nodes.push_back(parseBuiltinArrayDecl(curr));
|
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}))
|
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));
|
nodes.push_back(parseCustomTypeArrayDecl(curr));
|
||||||
|
else if (tryConsume(curr, {Token::Type::Type, Token::Type::ArrayOpen, Token::Type::Integer, Token::Type::ArrayClose, Token::Type::EndOfExpression})) {
|
||||||
|
if (curr[-5].typeToken.type != Token::TypeToken::Type::Padding) {
|
||||||
|
for(auto &node : nodes) delete node;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
nodes.push_back(parsePaddingDecl(curr));
|
||||||
|
}
|
||||||
else break;
|
else break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ namespace hex {
|
||||||
static const char* const builtInTypes[] = {
|
static const char* const builtInTypes[] = {
|
||||||
"u8", "u16", "u32", "u64", "u128",
|
"u8", "u16", "u32", "u64", "u128",
|
||||||
"s8", "s16", "s32", "s64", "s128",
|
"s8", "s16", "s32", "s64", "s128",
|
||||||
"float", "double"
|
"float", "double", "padding"
|
||||||
};
|
};
|
||||||
for (auto& k : builtInTypes) {
|
for (auto& k : builtInTypes) {
|
||||||
TextEditor::Identifier id;
|
TextEditor::Identifier id;
|
||||||
|
|
Loading…
Reference in New Issue