From 86a178b7d52b19e6e680dda8995f276600dca115 Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Wed, 23 Jul 2025 19:33:33 +0100 Subject: [PATCH] Added parser project --- CMakeLists.txt | 1 + Common/modules/Result.h | 2 +- LXC/CMakeLists.txt | 1 + LXC/LXC.cpp | 14 ++++++++++---- ast/inc/NodeBase.h | 11 +++++++++++ ast/inc/NodeTypes.h | 34 +++++++++++++++------------------- ast/src/NodeTypes.cpp | 12 ++++++------ parser/CMakeLists.txt | 18 ++++++++++++++++++ parser/inc/Parser.h | 19 +++++++++++++++++++ parser/src/Parser.cpp | 11 +++++++++++ 10 files changed, 93 insertions(+), 30 deletions(-) create mode 100644 parser/CMakeLists.txt create mode 100644 parser/inc/Parser.h create mode 100644 parser/src/Parser.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 72a2273..850b010 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ add_subdirectory(external/googletest) # The order is equivalent to which order they are compiled # add_subdirectory(lexer) add_subdirectory(ast) +add_subdirectory(parser) # Executable directories # add_subdirectory(LXC) diff --git a/Common/modules/Result.h b/Common/modules/Result.h index 756ca00..7b88780 100644 --- a/Common/modules/Result.h +++ b/Common/modules/Result.h @@ -74,7 +74,7 @@ namespace LXC::Util {} // Destructor // - ~ReturnVal() {} + ~ReturnVal() {}; // Different getters of the class // diff --git a/LXC/CMakeLists.txt b/LXC/CMakeLists.txt index 2ea22fc..57671df 100644 --- a/LXC/CMakeLists.txt +++ b/LXC/CMakeLists.txt @@ -4,6 +4,7 @@ add_executable(LXC LXC.cpp) # Links the other binaries # target_link_libraries(LXC PRIVATE Lexer) target_link_libraries(LXC PRIVATE AST) +target_link_libraries(LXC PRIVATE Parser) # Creates the precompiled header for the binary # target_include_directories(LXC PRIVATE ${CMAKE_SOURCE_DIR}/common) diff --git a/LXC/LXC.cpp b/LXC/LXC.cpp index a0f9285..1fa61f7 100644 --- a/LXC/LXC.cpp +++ b/LXC/LXC.cpp @@ -1,6 +1,7 @@ #include #include +#include #include int main(int argc, char** argv) @@ -57,12 +58,17 @@ int main(int argc, char** argv) Util::Log("Error occured in Lexer: ", Lexer::LexerError::ReasonStr(err.reason)); Util::Stop(); } - - // Prints all of the tokens to the log // - for (const auto& token : tokens.Result()) + else { - Util::Log(token); + // Prints all of the tokensto the log // + for (const auto& token : tokens.Result()) + { + Util::Log(token); + } } + // Turns the tokens into into an abstract syntax tree // + Util::ReturnVal functionsAST = Parser::TurnTokensIntoAST(tokens); + return 0; } diff --git a/ast/inc/NodeBase.h b/ast/inc/NodeBase.h index e9d2698..df7df61 100644 --- a/ast/inc/NodeBase.h +++ b/ast/inc/NodeBase.h @@ -1,3 +1,5 @@ +#pragma once + #include namespace LXC::AST @@ -55,6 +57,15 @@ namespace LXC::AST {} }; + // Typedefs for easier use with the AST // + typedef std::unique_ptr NodePtr; + typedef std::unique_ptr NodeValuePtr; + typedef std::vector SyntaxBranch; + typedef std::vector ValueList; + + // Will be replaced later to account for things like namespaces // + typedef std::string Identifier; + // Returns a pointer to the actual type of the node // template requires std::is_base_of_v diff --git a/ast/inc/NodeTypes.h b/ast/inc/NodeTypes.h index eaf06a2..bc9f032 100644 --- a/ast/inc/NodeTypes.h +++ b/ast/inc/NodeTypes.h @@ -1,3 +1,5 @@ +#pragma once + #include #include @@ -5,23 +7,17 @@ namespace LXC::AST { - // Will be replaced later to account for things like namespaces // - typedef std::string Identifier; - - // Foward declares so it can be used in VarDecl // - class VarAssignment; - class FunctionCall final : public NodeValue { public: - FunctionCall(Identifier& functionName, std::vector>& arguments); + FunctionCall(Identifier& functionName, ValueList& arguments); private: // The name of the function // Identifier m_FuncName; // The arguments of the function // - std::vector> m_Arguments; + ValueList m_Arguments; }; class IntLiteral final : public NodeValue @@ -37,11 +33,11 @@ namespace LXC::AST class Operation final : public NodeValue { public: - Operation(std::unique_ptr& left, Lexer::Token::TokenType operand, std::unique_ptr& right); + Operation(NodeValuePtr& left, Lexer::Token::TokenType operand, NodeValuePtr& right); private: // The sides of the operation // - std::unique_ptr m_Lhs, m_Rhs; + NodeValuePtr m_Lhs, m_Rhs; // The operand of the operation // Lexer::Token::TokenType m_Operand; @@ -51,27 +47,27 @@ namespace LXC::AST { public: VarDeclaration(Identifier& name); - VarDeclaration(Identifier& name, std::unique_ptr& value); + VarDeclaration(Identifier& name, NodeValuePtr& value); private: // The name of the variable // Identifier m_Name; // Default value of the variable (nullable) // - std::unique_ptr m_Value; + NodeValuePtr m_Value; }; class VarAssignment final : public Node { public: - VarAssignment(Identifier& name, std::unique_ptr& value); + VarAssignment(Identifier& name, NodeValuePtr& value); private: // The name of the variable // Identifier m_Name; // Value to assign to the variable // - std::unique_ptr m_Value; + NodeValuePtr m_Value; }; class VarAccess final : public NodeValue @@ -87,23 +83,23 @@ namespace LXC::AST class IfBranch final : public Node { public: - IfBranch(std::unique_ptr& condition, std::vector>& body); + IfBranch(NodeValuePtr& condition, SyntaxBranch& body); private: // The condition of the branch // - std::unique_ptr m_Condition; + NodeValuePtr m_Condition; // The body of the if-statement // - std::vector> m_Body; + SyntaxBranch m_Body; }; class ReturnStatement final : public Node { public: - ReturnStatement(std::unique_ptr& value); + ReturnStatement(NodeValuePtr& value); private: // The value to return (nullable) // - std::unique_ptr m_ReturnValue; + NodeValuePtr m_ReturnValue; }; } diff --git a/ast/src/NodeTypes.cpp b/ast/src/NodeTypes.cpp index 4a7d1a6..a501c02 100644 --- a/ast/src/NodeTypes.cpp +++ b/ast/src/NodeTypes.cpp @@ -4,7 +4,7 @@ namespace LXC::AST { - FunctionCall::FunctionCall (Identifier& functionName, std::vector>& arguments) + FunctionCall::FunctionCall (Identifier& functionName, ValueList& arguments) : NodeValue(NodeType::FunctionCall), m_FuncName(std::move(functionName)), m_Arguments(std::move(arguments)) {} @@ -12,7 +12,7 @@ namespace LXC::AST : NodeValue(NodeType::IntLiteral), m_NumberValue(std::move(value)) {} - Operation::Operation(std::unique_ptr& left, Lexer::Token::TokenType operand, std::unique_ptr& right) + Operation::Operation(NodeValuePtr& left, Lexer::Token::TokenType operand, NodeValuePtr& right) : NodeValue(NodeType::Operation), m_Lhs(std::move(left)), m_Operand(operand), m_Rhs(std::move(right)) {} @@ -20,11 +20,11 @@ namespace LXC::AST : Node(NodeType::Var_Declare), m_Name(std::move(name)), m_Value(nullptr) {} - VarDeclaration::VarDeclaration(Identifier& name, std::unique_ptr& value) + VarDeclaration::VarDeclaration(Identifier& name, NodeValuePtr& value) : Node(NodeType::Var_Declare), m_Name(std::move(name)), m_Value(std::move(value)) {} - VarAssignment::VarAssignment(Identifier& name, std::unique_ptr& value) + VarAssignment::VarAssignment(Identifier& name, NodeValuePtr& value) : Node(NodeType::Var_Assign), m_Value(std::move(value)) {} @@ -32,11 +32,11 @@ namespace LXC::AST : NodeValue(NodeType::Var_Access), m_Name(std::move(m_Name)) {} - IfBranch::IfBranch(std::unique_ptr& condition, std::vector>& body) + IfBranch::IfBranch(NodeValuePtr& condition, SyntaxBranch& body) : Node(NodeType::IfBranch), m_Condition(std::move(condition)), m_Body(std::move(body)) {} - ReturnStatement::ReturnStatement(std::unique_ptr& value) + ReturnStatement::ReturnStatement(NodeValuePtr& value) : Node(NodeType::ReturnVal), m_ReturnValue(std::move(m_ReturnValue)) {} } diff --git a/parser/CMakeLists.txt b/parser/CMakeLists.txt new file mode 100644 index 0000000..f024733 --- /dev/null +++ b/parser/CMakeLists.txt @@ -0,0 +1,18 @@ +# Fetches all .cpp files for the binary # +add_library(Parser STATIC + src/Parser.cpp + "inc/Parser.h") + +# Adds the headers in the current directory # +target_include_directories ( + Parser PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/inc +) + +# Links to the Lexer and AST so it can use their data types # +target_link_libraries(Parser PUBLIC Lexer) +target_link_libraries(Parser PUBLIC AST) + +# Creates the precompiled header of the binary # +target_include_directories(Parser PRIVATE ${CMAKE_SOURCE_DIR}/common) +target_precompile_headers(Parser PRIVATE ${CMAKE_SOURCE_DIR}/common/LXC.h) diff --git a/parser/inc/Parser.h b/parser/inc/Parser.h new file mode 100644 index 0000000..7569001 --- /dev/null +++ b/parser/inc/Parser.h @@ -0,0 +1,19 @@ +#pragma once + +#include + +#include +#include + +namespace LXC::Parser +{ + struct ParserError {}; + + struct FunctionAST + { + std::string name; + AST::SyntaxBranch contents; + }; + + Util::ReturnVal, ParserError> TurnTokensIntoAST(const Lexer::LexerOutput& input); +} diff --git a/parser/src/Parser.cpp b/parser/src/Parser.cpp new file mode 100644 index 0000000..5d1c493 --- /dev/null +++ b/parser/src/Parser.cpp @@ -0,0 +1,11 @@ +#include + +#include + +namespace LXC::Parser +{ + Util::ReturnVal, ParserError> TurnTokensIntoAST(const Lexer::LexerOutput& input) + { + return Util::FunctionFail(); + } +}