From 69e7b0e6c1dbb2c2bc5296033bea3a9baa4215a9 Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Wed, 23 Jul 2025 14:50:38 +0100 Subject: [PATCH] Added AST constructors --- ast/CMakeLists.txt | 3 +-- ast/inc/NodeTypes.h | 36 +++++++++++++++++++++++++++++++++--- ast/src/NodeTypes.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 5 deletions(-) diff --git a/ast/CMakeLists.txt b/ast/CMakeLists.txt index 09a68b0..03cf063 100644 --- a/ast/CMakeLists.txt +++ b/ast/CMakeLists.txt @@ -1,6 +1,5 @@ # Fetches all files for the binary # -file (GLOB ASTSources src/*.cpp inc/*.h) -add_library(AST STATIC ${ASTSources}) +add_library(AST STATIC src/NodeTypes.cpp src/AST.cpp) # Adds the headers in the current directory # target_include_directories ( diff --git a/ast/inc/NodeTypes.h b/ast/inc/NodeTypes.h index 3875309..eaf06a2 100644 --- a/ast/inc/NodeTypes.h +++ b/ast/inc/NodeTypes.h @@ -1,23 +1,34 @@ #include #include +#include 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); + private: // The name of the function // Identifier m_FuncName; // The arguments of the function // - std::vector> m_Arguments; + std::vector> m_Arguments; }; class IntLiteral final : public NodeValue { + public: + IntLiteral(std::string& value); + private: // Yes numbers are stored as strings // std::string m_NumberValue; @@ -25,9 +36,12 @@ namespace LXC::AST class Operation final : public NodeValue { + public: + Operation(std::unique_ptr& left, Lexer::Token::TokenType operand, std::unique_ptr& right); + private: // The sides of the operation // - std::unique_ptr m_Lhs, m_Rhs; + std::unique_ptr m_Lhs, m_Rhs; // The operand of the operation // Lexer::Token::TokenType m_Operand; @@ -35,6 +49,10 @@ namespace LXC::AST class VarDeclaration final : public Node { + public: + VarDeclaration(Identifier& name); + VarDeclaration(Identifier& name, std::unique_ptr& value); + private: // The name of the variable // Identifier m_Name; @@ -45,6 +63,9 @@ namespace LXC::AST class VarAssignment final : public Node { + public: + VarAssignment(Identifier& name, std::unique_ptr& value); + private: // The name of the variable // Identifier m_Name; @@ -55,6 +76,9 @@ namespace LXC::AST class VarAccess final : public NodeValue { + public: + VarAccess(Identifier& name); + private: // The name of the variable // Identifier m_Name; @@ -62,9 +86,12 @@ namespace LXC::AST class IfBranch final : public Node { + public: + IfBranch(std::unique_ptr& condition, std::vector>& body); + private: // The condition of the branch // - std::unique_ptr m_Condition; + std::unique_ptr m_Condition; // The body of the if-statement // std::vector> m_Body; @@ -72,6 +99,9 @@ namespace LXC::AST class ReturnStatement final : public Node { + public: + ReturnStatement(std::unique_ptr& value); + private: // The value to return (nullable) // std::unique_ptr m_ReturnValue; diff --git a/ast/src/NodeTypes.cpp b/ast/src/NodeTypes.cpp index e69de29..4a7d1a6 100644 --- a/ast/src/NodeTypes.cpp +++ b/ast/src/NodeTypes.cpp @@ -0,0 +1,42 @@ +#include + +#include + +namespace LXC::AST +{ + FunctionCall::FunctionCall (Identifier& functionName, std::vector>& arguments) + : NodeValue(NodeType::FunctionCall), m_FuncName(std::move(functionName)), m_Arguments(std::move(arguments)) + {} + + IntLiteral::IntLiteral(std::string& value) + : NodeValue(NodeType::IntLiteral), m_NumberValue(std::move(value)) + {} + + Operation::Operation(std::unique_ptr& left, Lexer::Token::TokenType operand, std::unique_ptr& right) + : NodeValue(NodeType::Operation), m_Lhs(std::move(left)), m_Operand(operand), m_Rhs(std::move(right)) + {} + + VarDeclaration::VarDeclaration(Identifier& name) + : Node(NodeType::Var_Declare), m_Name(std::move(name)), m_Value(nullptr) + {} + + VarDeclaration::VarDeclaration(Identifier& name, std::unique_ptr& value) + : Node(NodeType::Var_Declare), m_Name(std::move(name)), m_Value(std::move(value)) + {} + + VarAssignment::VarAssignment(Identifier& name, std::unique_ptr& value) + : Node(NodeType::Var_Assign), m_Value(std::move(value)) + {} + + VarAccess::VarAccess(Identifier& name) + : NodeValue(NodeType::Var_Access), m_Name(std::move(m_Name)) + {} + + IfBranch::IfBranch(std::unique_ptr& condition, std::vector>& body) + : Node(NodeType::IfBranch), m_Condition(std::move(condition)), m_Body(std::move(body)) + {} + + ReturnStatement::ReturnStatement(std::unique_ptr& value) + : Node(NodeType::ReturnVal), m_ReturnValue(std::move(m_ReturnValue)) + {} +}