diff --git a/include/hex.hpp b/include/hex.hpp index 66fe4963c..217dce9f0 100644 --- a/include/hex.hpp +++ b/include/hex.hpp @@ -8,10 +8,10 @@ using u32 = std::uint32_t; using u64 = std::uint64_t; using u128 = __uint128_t; -using s8 = std::int8_t; -using s16 = std::int16_t; -using s32 = std::int32_t; -using s64 = std::int64_t; +using s8 = std::int8_t; +using s16 = std::int16_t; +using s32 = std::int32_t; +using s64 = std::int64_t; using s128 = __int128_t; #include "parser/result.hpp" diff --git a/include/parser/ast_node.hpp b/include/parser/ast_node.hpp index 09fba5ce6..bd29348ee 100644 --- a/include/parser/ast_node.hpp +++ b/include/parser/ast_node.hpp @@ -27,16 +27,19 @@ namespace hex::lang { class ASTNodeVariableDecl : public ASTNode { public: - explicit ASTNodeVariableDecl(const Token::TypeToken::Type &type, const std::string &name, const std::string& customTypeName = "") - : ASTNode(Type::VariableDecl), m_type(type), m_name(name), m_customTypeName(customTypeName) { } + explicit ASTNodeVariableDecl(const Token::TypeToken::Type &type, const std::string &name, const std::string& customTypeName = "", std::optional offset = { }) + : ASTNode(Type::VariableDecl), m_type(type), m_name(name), m_customTypeName(customTypeName), m_offset(offset) { } 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 getOffset() { return this->m_offset; }; private: Token::TypeToken::Type m_type; std::string m_name, m_customTypeName; + std::optional m_offset; + }; class ASTNodeScope : public ASTNode { @@ -50,16 +53,14 @@ namespace hex::lang { class ASTNodeStruct : public ASTNode { public: - explicit ASTNodeStruct(std::string name, std::vector nodes, std::optional offset = { }) - : ASTNode(Type::Struct), m_name(name), m_nodes(nodes), m_offset(offset) { } + explicit ASTNodeStruct(std::string name, std::vector nodes) + : ASTNode(Type::Struct), m_name(name), m_nodes(nodes) { } const std::string& getName() const { return this->m_name; } std::vector &getNodes() { return this->m_nodes; } - std::optional getOffset() { return this->m_offset; }; private: std::string m_name; std::vector m_nodes; - std::optional m_offset; }; class ASTNodeTypeDecl : public ASTNode { diff --git a/include/views/view_pattern.hpp b/include/views/view_pattern.hpp index 7435586e7..9b9ff309a 100644 --- a/include/views/view_pattern.hpp +++ b/include/views/view_pattern.hpp @@ -49,14 +49,25 @@ namespace hex { return 0; } - - for (auto &structNode : _this->findNodes(lang::ASTNode::Type::Struct, ast)) { - if (!structNode->getOffset().has_value()) + for (auto &varNode : _this->findNodes(lang::ASTNode::Type::VariableDecl, ast)) { + if (!varNode->getOffset().has_value()) continue; - u64 offset = structNode->getOffset().value(); - if (_this->highlightStruct(ast, structNode, offset) == -1) - _this->m_hexEditor->clearHighlights(); + u64 offset = varNode->getOffset().value(); + if (varNode->getVariableType() != lang::Token::TypeToken::Type::CustomType) { + _this->m_hexEditor->setHighlight(offset, static_cast(varNode->getVariableType()) >> 4); + } else { + for (auto &structNode : _this->findNodes(lang::ASTNode::Type::Struct, ast)) + if (varNode->getCustomVariableTypeName() == structNode->getName()) + if (_this->highlightStruct(ast, structNode, offset) == -1) + _this->m_hexEditor->clearHighlights(); + + for (auto &usingNode : _this->findNodes(lang::ASTNode::Type::TypeDecl, ast)) + if (varNode->getCustomVariableTypeName() == usingNode->getTypeName()) + if (_this->highlightUsingDecls(ast, usingNode, offset) == -1) + _this->m_hexEditor->clearHighlights(); + } + } for(auto &node : ast) delete node; @@ -145,7 +156,7 @@ namespace hex { if (size == -1) return -1; - offset += size; + offset = size; foundType = true; break; } diff --git a/source/parser/lexer.cpp b/source/parser/lexer.cpp index 24504bb0e..9da01ef2f 100644 --- a/source/parser/lexer.cpp +++ b/source/parser/lexer.cpp @@ -56,9 +56,9 @@ namespace hex::lang { if (isdigit(c)) integer += (c - '0'); else if (c >= 'A' && c <= 'F') - integer += (c - 'A'); + integer += 10 + (c - 'A'); else if (c >= 'a' && c <= 'f') - integer += (c - 'a'); + integer += 10 + (c - 'a'); else return { }; } @@ -138,7 +138,7 @@ namespace hex::lang { char *end = nullptr; std::strtoull(&code[offset], &end, 0); - auto integer = parseInt(std::string_view(&code[offset], end));// matchTillInvalid(&code[offset], [](char c) -> bool { return std::isdigit(c); }); + auto integer = parseInt(std::string_view(&code[offset], end)); if (!integer.has_value()) return { ResultLexicalError, {}}; diff --git a/source/parser/parser.cpp b/source/parser/parser.cpp index 0957d41df..b30c5dbb7 100644 --- a/source/parser/parser.cpp +++ b/source/parser/parser.cpp @@ -35,10 +35,17 @@ namespace hex::lang { return new ASTNodeVariableDecl(Token::TypeToken::Type::CustomType, curr[-2].identifierToken.identifier, curr[-3].identifierToken.identifier); } + ASTNode* parseFreeBuiltinVariableDecl(TokenIter &curr) { + return new ASTNodeVariableDecl(curr[-5].typeToken.type, curr[-4].identifierToken.identifier, "", curr[-2].integerToken.integer); + } + + ASTNode* parseFreeCustomTypeVariableDecl(TokenIter &curr) { + return new ASTNodeVariableDecl(Token::TypeToken::Type::CustomType, curr[-4].identifierToken.identifier, curr[-5].identifierToken.identifier, curr[-2].integerToken.integer); + } + std::optional parseStruct(TokenIter &curr) { const std::string &structName = curr[-2].identifierToken.identifier; std::vector nodes; - std::optional offset = { }; while (!tryConsume(curr, {Token::Type::ScopeClose})) { if (tryConsume(curr, {Token::Type::Type, Token::Type::Identifier, Token::Type::EndOfExpression})) @@ -48,26 +55,12 @@ namespace hex::lang { else break; } - if (tryConsume(curr, {Token::Type::Operator})) { - if (curr[-1].operatorToken.op == Token::OperatorToken::Operator::AtDeclaration) { - if (tryConsume(curr, {Token::Type::Integer})) { - offset = curr[-1].integerToken.integer; - } else { - for(auto &node : nodes) delete node; - return { }; - } - } else { - for(auto &node : nodes) delete node; - return { }; - } - } - if (!tryConsume(curr, {Token::Type::EndOfExpression})) { for(auto &node : nodes) delete node; return { }; } - return new ASTNodeStruct(structName, nodes, offset); + return new ASTNodeStruct(structName, nodes); } ASTNode *parseScope(TokenIter &curr) { @@ -101,6 +94,7 @@ namespace hex::lang { std::optional> parseStatement(TokenIter &curr) { std::vector program; + // Struct if (tryConsume(curr, { Token::Type::Keyword, Token::Type::Identifier, Token::Type::ScopeOpen })) { if (curr[-3].keywordToken.keyword == Token::KeywordToken::Keyword::Struct) { auto structAst = parseStruct(curr); @@ -114,10 +108,14 @@ namespace hex::lang { } return program; + + // Scope } else if (tryConsume(curr, { Token::Type::ScopeOpen })) { program.push_back(parseScope(curr)); return program; + + // Using declaration with built-in type } else if (tryConsume(curr, { Token::Type::Keyword, Token::Type::Identifier, Token::Type::Operator, Token::Type::Type, Token::Type::EndOfExpression})) { auto usingDecl = parseUsingDeclaration(curr); @@ -129,6 +127,8 @@ namespace hex::lang { program.push_back(usingDecl.value()); return program; + + // Using declaration with custom type } else if (tryConsume(curr, { Token::Type::Keyword, Token::Type::Identifier, Token::Type::Operator, Token::Type::Identifier, Token::Type::EndOfExpression})) { auto usingDecl = parseUsingDeclaration(curr); @@ -139,6 +139,21 @@ namespace hex::lang { program.push_back(usingDecl.value()); + return program; + // Variable declaration with built-in type + } else if (tryConsume(curr, { Token::Type::Type, Token::Type::Identifier, Token::Type::Operator, Token::Type::Integer, Token::Type::EndOfExpression})) { + auto variableDecl = parseFreeBuiltinVariableDecl(curr); + + program.push_back(variableDecl); + + return program; + + // Variable declaration with custom type + } else if (tryConsume(curr, { Token::Type::Identifier, Token::Type::Identifier, Token::Type::Operator, Token::Type::Integer, Token::Type::EndOfExpression})) { + auto variableDecl = parseFreeCustomTypeVariableDecl(curr); + + program.push_back(variableDecl); + return program; } else {