From a1de06adbd76a08e097e5ecefe5c6ea205fd5c20 Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Mon, 16 Jun 2025 17:06:06 +0100 Subject: [PATCH] Added IfBranch class --- IR-Generator/inc/Parser.h | 1 + Parser/inc/AST.h | 27 +++++++++++++++++++++++++++ Parser/src/AST/AST-Constructors.cpp | 5 +++++ Parser/src/AST/AST-LLVM.cpp | 5 +++++ Parser/src/AST/AST-Loggers.cpp | 16 ++++++++++++++++ example/main.lx | 7 ++++++- 6 files changed, 60 insertions(+), 1 deletion(-) diff --git a/IR-Generator/inc/Parser.h b/IR-Generator/inc/Parser.h index ac5fdb0..a05fa1f 100644 --- a/IR-Generator/inc/Parser.h +++ b/IR-Generator/inc/Parser.h @@ -40,6 +40,7 @@ namespace LX::AST // Control flow Nodes // + IF_STATEMENT, RETURN_STATEMENT, // If an error happened somewhere // diff --git a/Parser/inc/AST.h b/Parser/inc/AST.h index 4a1fe04..2c35e02 100644 --- a/Parser/inc/AST.h +++ b/Parser/inc/AST.h @@ -201,4 +201,31 @@ namespace LX::AST // Any arguments to pass into the function // std::vector> m_Args; }; + + // Node to represent a if-statement within the AST // + class IfBranch : public Node + { + public: + // Constructor to set the name of the function and any args it may have // + IfBranch(); + + // Function for generating LLVM IR (Intermediate representation) // + llvm::Value* GenIR(InfoLLVM& LLVM, FunctionScope& func) override; + + // Function to log the node to a file // + void Log(unsigned depth) override; + + // Function to get the node's type name // + const char* TypeName() override; + + // Function to add a node to the branch // + inline void Push(std::unique_ptr& node) { m_Body.push_back(std::move(node)); } + + private: + // The condition of the branch // + std::unique_ptr m_Condition; + + // The body of the if-statement // + std::vector> m_Body; + }; } \ No newline at end of file diff --git a/Parser/src/AST/AST-Constructors.cpp b/Parser/src/AST/AST-Constructors.cpp index 6830189..8073527 100644 --- a/Parser/src/AST/AST-Constructors.cpp +++ b/Parser/src/AST/AST-Constructors.cpp @@ -68,4 +68,9 @@ namespace LX::AST FunctionCall::FunctionCall(const std::string& name, std::vector>& args) : Node(Node::FUNCTION_CALL), m_Name(name), m_Args(std::move(args)) {} + + // Passes constructor args to values and sets type // + IfBranch::IfBranch() + : Node(Node::IF_STATEMENT), m_Body{} + {} } diff --git a/Parser/src/AST/AST-LLVM.cpp b/Parser/src/AST/AST-LLVM.cpp index 1334ef3..8e403cd 100644 --- a/Parser/src/AST/AST-LLVM.cpp +++ b/Parser/src/AST/AST-LLVM.cpp @@ -125,4 +125,9 @@ namespace LX::AST return LLVM.builder.CreateCall(LLVM.functions[m_Name], evaluatedArgs, "call_tmp"); } + + llvm::Value* IfBranch::GenIR(InfoLLVM& LLVM, FunctionScope& scope) + { + return nullptr; + } } diff --git a/Parser/src/AST/AST-Loggers.cpp b/Parser/src/AST/AST-Loggers.cpp index b68ea72..676fd04 100644 --- a/Parser/src/AST/AST-Loggers.cpp +++ b/Parser/src/AST/AST-Loggers.cpp @@ -111,4 +111,20 @@ namespace LX::AST { return "Function call"; } + + void IfBranch::Log(unsigned depth) + { + Log::out(std::string(depth, '\t'), "If Statement"); + Log::out(std::string(depth + 1, '\t'), "Condition:"); + + m_Condition->Log(depth + 2); + + Log::out(std::string(depth + 1, '\t'), "Body:"); + for (const auto& node : m_Body) { node->Log(depth + 1); } + } + + const char* IfBranch::TypeName() + { + return "If Statement"; + } } diff --git a/example/main.lx b/example/main.lx index d71ea6a..43332a3 100644 --- a/example/main.lx +++ b/example/main.lx @@ -7,5 +7,10 @@ func main() { int c = add(1, 2) - return c + if (c == 3) + { + return 1 + } + + return 3 }