From 57dcf6cc9339b5d227cf12865b0d11a55ac68c73 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 20 Nov 2020 21:59:27 +0100 Subject: [PATCH] Added padding type to pattern language --- include/lang/pattern_data.hpp | 13 ++++++++++++- include/lang/token.hpp | 3 ++- source/lang/evaluator.cpp | 18 ++++++++++-------- source/lang/lexer.cpp | 2 ++ source/lang/parser.cpp | 11 +++++++++++ source/views/view_pattern.cpp | 2 +- 6 files changed, 38 insertions(+), 11 deletions(-) diff --git a/include/lang/pattern_data.hpp b/include/lang/pattern_data.hpp index 293a40912..070aab8f8 100644 --- a/include/lang/pattern_data.hpp +++ b/include/lang/pattern_data.hpp @@ -31,7 +31,7 @@ namespace hex::lang { class PatternData { 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) : 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; }; + 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 { public: diff --git a/include/lang/token.hpp b/include/lang/token.hpp index bf08eff39..5a88aeb21 100644 --- a/include/lang/token.hpp +++ b/include/lang/token.hpp @@ -58,7 +58,8 @@ namespace hex::lang { Signed128Bit = 0x101, Float = 0x42, Double = 0x82, - CustomType = 0x00 + CustomType = 0x00, + Padding = 0x1F } type; } typeToken; }; diff --git a/source/lang/evaluator.cpp b/source/lang/evaluator.cpp index d7804b5f6..15982e622 100644 --- a/source/lang/evaluator.cpp +++ b/source/lang/evaluator.cpp @@ -173,32 +173,34 @@ namespace hex::lang { std::pair Evaluator::createArrayPattern(ASTNodeVariableDecl *varDeclNode, u64 offset) { std::vector entries; - size_t arraySize = 0; + size_t arrayOffset = 0; for (u32 i = 0; i < varDeclNode->getArraySize(); i++) { ASTNodeVariableDecl *nonArrayVarDeclNode = new ASTNodeVariableDecl(varDeclNode->getVariableType(), "[" + std::to_string(i) + "]", varDeclNode->getCustomVariableTypeName(), varDeclNode->getOffset(), 1); - if (varDeclNode->getVariableType() != Token::TypeToken::Type::CustomType) { - const auto &[pattern, size] = this->createBuiltInTypePattern(nonArrayVarDeclNode, offset + arraySize); + if (varDeclNode->getVariableType() == Token::TypeToken::Type::Padding) { + 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) return { nullptr, 0 }; entries.push_back(pattern); - arraySize += size; + arrayOffset += size; } else { - const auto &[pattern, size] = this->createCustomTypePattern(nonArrayVarDeclNode, offset + arraySize); + const auto &[pattern, size] = this->createCustomTypePattern(nonArrayVarDeclNode, offset + arrayOffset); if (pattern == nullptr) return { nullptr, 0 }; entries.push_back(pattern); - arraySize += size; + arrayOffset += size; } delete nonArrayVarDeclNode; } - return { new PatternDataArray(offset, arraySize, varDeclNode->getVariableName(), entries, 0x00FFFFFF), arraySize }; + return { new PatternDataArray(offset, arrayOffset, varDeclNode->getVariableName(), entries, 0x00FFFFFF), arrayOffset }; } std::pair Evaluator::createStringPattern(ASTNodeVariableDecl *varDeclNode, u64 offset) { @@ -242,7 +244,7 @@ namespace hex::lang { size_t typeSize = getTypeSize(type); size_t arraySize = varDeclNode->getArraySize(); - if (isSigned(type)) { + if (isSigned(type)) { if (typeSize == 1 && arraySize == 1) return { new PatternDataCharacter(offset, typeSize, varDeclNode->getVariableName()), 1 }; else if (arraySize > 1) diff --git a/source/lang/lexer.cpp b/source/lang/lexer.cpp index fe2913f24..5979f43aa 100644 --- a/source/lang/lexer.cpp +++ b/source/lang/lexer.cpp @@ -169,6 +169,8 @@ namespace hex::lang { tokens.push_back({ .type = Token::Type::Type, .typeToken = { .type = Token::TypeToken::Type::Float } }); else if (identifier == "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 diff --git a/source/lang/parser.cpp b/source/lang/parser.cpp index bbc66449e..98ab5ac10 100644 --- a/source/lang/parser.cpp +++ b/source/lang/parser.cpp @@ -39,6 +39,10 @@ namespace hex::lang { 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) { 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)); 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 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; } diff --git a/source/views/view_pattern.cpp b/source/views/view_pattern.cpp index ab84b6c49..ef2500d5b 100644 --- a/source/views/view_pattern.cpp +++ b/source/views/view_pattern.cpp @@ -22,7 +22,7 @@ namespace hex { static const char* const builtInTypes[] = { "u8", "u16", "u32", "u64", "u128", "s8", "s16", "s32", "s64", "s128", - "float", "double" + "float", "double", "padding" }; for (auto& k : builtInTypes) { TextEditor::Identifier id;