From 5d1e53f469bcd8460b373402e9d22096d9a2d783 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 7 Jan 2021 17:34:50 +0100 Subject: [PATCH] Added boolean patterns --- include/lang/pattern_data.hpp | 28 +++++++++++++++++++++++++++- include/lang/token.hpp | 1 + source/lang/builtin_functions.cpp | 4 ++-- source/lang/evaluator.cpp | 4 ++++ source/lang/lexer.cpp | 6 ++++++ source/views/view_pattern.cpp | 4 ++-- 6 files changed, 42 insertions(+), 5 deletions(-) diff --git a/include/lang/pattern_data.hpp b/include/lang/pattern_data.hpp index fd6b3a88d..23277a620 100644 --- a/include/lang/pattern_data.hpp +++ b/include/lang/pattern_data.hpp @@ -359,6 +359,32 @@ namespace hex::lang { } }; + class PatternDataBoolean : public PatternData { + public: + explicit PatternDataBoolean(u64 offset, u32 color = 0) + : PatternData(offset, 1, color) { } + + PatternData* clone() override { + return new PatternDataBoolean(*this); + } + + void createEntry(prv::Provider* &provider) override { + u8 boolean; + provider->read(this->getOffset(), &boolean, 1); + + if (boolean == 0) + this->createDefaultEntry("false"); + else if (boolean == 1) + this->createDefaultEntry("true"); + else + this->createDefaultEntry("true*"); + } + + [[nodiscard]] std::string getFormattedName() const override { + return "bool"; + } + }; + class PatternDataCharacter : public PatternData { public: explicit PatternDataCharacter(u64 offset, u32 color = 0) @@ -376,7 +402,7 @@ namespace hex::lang { } [[nodiscard]] std::string getFormattedName() const override { - return "Character"; + return "char"; } }; diff --git a/include/lang/token.hpp b/include/lang/token.hpp index 174ea0ece..0f4a26f1d 100644 --- a/include/lang/token.hpp +++ b/include/lang/token.hpp @@ -71,6 +71,7 @@ namespace hex::lang { Unsigned128Bit = 0x100, Signed128Bit = 0x101, Character = 0x13, + Boolean = 0x14, Float = 0x42, Double = 0x82, CustomType = 0x00, diff --git a/source/lang/builtin_functions.cpp b/source/lang/builtin_functions.cpp index 04c4c2eac..b501cad61 100644 --- a/source/lang/builtin_functions.cpp +++ b/source/lang/builtin_functions.cpp @@ -56,7 +56,7 @@ namespace hex::lang { case 4: return new ASTNodeIntegerLiteral({ Token::ValueType::Unsigned32Bit, hex::changeEndianess(*reinterpret_cast(value), 4, this->getCurrentEndian()) }); case 8: return new ASTNodeIntegerLiteral({ Token::ValueType::Unsigned64Bit, hex::changeEndianess(*reinterpret_cast(value), 8, this->getCurrentEndian()) }); case 16: return new ASTNodeIntegerLiteral({ Token::ValueType::Unsigned128Bit, hex::changeEndianess(*reinterpret_cast(value), 16, this->getCurrentEndian()) }); - default: throwEvaluateError("invalid rvalue size", 1); + default: throwEvaluateError("invalid read size", 1); } }, address, size); } @@ -81,7 +81,7 @@ namespace hex::lang { case 4: return new ASTNodeIntegerLiteral({ Token::ValueType::Signed32Bit, hex::changeEndianess(*reinterpret_cast(value), 4, this->getCurrentEndian()) }); case 8: return new ASTNodeIntegerLiteral({ Token::ValueType::Signed64Bit, hex::changeEndianess(*reinterpret_cast(value), 8, this->getCurrentEndian()) }); case 16: return new ASTNodeIntegerLiteral({ Token::ValueType::Signed128Bit, hex::changeEndianess(*reinterpret_cast(value), 16, this->getCurrentEndian()) }); - default: throwEvaluateError("invalid rvalue size", 1); + default: throwEvaluateError("invalid read size", 1); } }, address, size); } diff --git a/source/lang/evaluator.cpp b/source/lang/evaluator.cpp index e3a886e5f..cfb6f5469 100644 --- a/source/lang/evaluator.cpp +++ b/source/lang/evaluator.cpp @@ -46,6 +46,8 @@ namespace hex::lang { } ASTNodeIntegerLiteral* Evaluator::evaluateRValue(ASTNodeRValue *node) { + if (this->m_currMembers.empty()) + throwEvaluateError("no variables available", node->getLineNumber()); const std::vector* currMembers = this->m_currMembers.back(); @@ -298,6 +300,8 @@ namespace hex::lang { if (type == Token::ValueType::Character) pattern = new PatternDataCharacter(this->m_currOffset); + else if (type == Token::ValueType::Boolean) + pattern = new PatternDataBoolean(this->m_currOffset); else if (Token::isUnsigned(type)) pattern = new PatternDataUnsigned(this->m_currOffset, typeSize); else if (Token::isSigned(type)) diff --git a/source/lang/lexer.cpp b/source/lang/lexer.cpp index 13a2136da..2f6ea3627 100644 --- a/source/lang/lexer.cpp +++ b/source/lang/lexer.cpp @@ -318,6 +318,10 @@ namespace hex::lang { tokens.emplace_back(TOKEN(Keyword, If)); else if (identifier == "else") tokens.emplace_back(TOKEN(Keyword, Else)); + else if (identifier == "false") + tokens.emplace_back(VALUE_TOKEN(Integer, Token::IntegerLiteral({ Token::ValueType::Unsigned8Bit, u8(0) }))); + else if (identifier == "true") + tokens.emplace_back(VALUE_TOKEN(Integer, Token::IntegerLiteral({ Token::ValueType::Unsigned8Bit, u8(1) }))); // Check for built-in types else if (identifier == "u8") @@ -346,6 +350,8 @@ namespace hex::lang { tokens.emplace_back(TOKEN(ValueType, Double)); else if (identifier == "char") tokens.emplace_back(TOKEN(ValueType, Character)); + else if (identifier == "bool") + tokens.emplace_back(TOKEN(ValueType, Boolean)); else if (identifier == "padding") tokens.emplace_back(TOKEN(ValueType, Padding)); diff --git a/source/views/view_pattern.cpp b/source/views/view_pattern.cpp index 74831a871..4524c8711 100644 --- a/source/views/view_pattern.cpp +++ b/source/views/view_pattern.cpp @@ -18,7 +18,7 @@ namespace hex { static TextEditor::LanguageDefinition langDef; if (!initialized) { static const char* const keywords[] = { - "using", "struct", "union", "enum", "bitfield", "be", "le", "if", "else" + "using", "struct", "union", "enum", "bitfield", "be", "le", "if", "else", "false", "true" }; for (auto& k : keywords) langDef.mKeywords.insert(k); @@ -26,7 +26,7 @@ namespace hex { static std::pair builtInTypes[] = { { "u8", 1 }, { "u16", 2 }, { "u32", 4 }, { "u64", 8 }, { "u128", 16 }, { "s8", 1 }, { "s16", 2 }, { "s32", 4 }, { "s64", 8 }, { "s128", 16 }, - { "float", 4 }, { "double", 8 }, { "char", 1 }, { "padding", 1 } + { "float", 4 }, { "double", 8 }, { "char", 1 }, { "bool", 1 }, { "padding", 1 } }; for (const auto &[name, size] : builtInTypes) { TextEditor::Identifier id;