mirror of
https://github.com/PashaBibko/LX.git
synced 2026-04-03 17:39:02 +00:00
Added IfBranch class
This commit is contained in:
@@ -40,6 +40,7 @@ namespace LX::AST
|
|||||||
|
|
||||||
// Control flow Nodes //
|
// Control flow Nodes //
|
||||||
|
|
||||||
|
IF_STATEMENT,
|
||||||
RETURN_STATEMENT,
|
RETURN_STATEMENT,
|
||||||
|
|
||||||
// If an error happened somewhere //
|
// If an error happened somewhere //
|
||||||
|
|||||||
@@ -201,4 +201,31 @@ namespace LX::AST
|
|||||||
// Any arguments to pass into the function //
|
// Any arguments to pass into the function //
|
||||||
std::vector<std::unique_ptr<Node>> m_Args;
|
std::vector<std::unique_ptr<Node>> 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>& node) { m_Body.push_back(std::move(node)); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
// The condition of the branch //
|
||||||
|
std::unique_ptr<Node> m_Condition;
|
||||||
|
|
||||||
|
// The body of the if-statement //
|
||||||
|
std::vector<std::unique_ptr<Node>> m_Body;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
@@ -68,4 +68,9 @@ namespace LX::AST
|
|||||||
FunctionCall::FunctionCall(const std::string& name, std::vector<std::unique_ptr<AST::Node>>& args)
|
FunctionCall::FunctionCall(const std::string& name, std::vector<std::unique_ptr<AST::Node>>& args)
|
||||||
: Node(Node::FUNCTION_CALL), m_Name(name), m_Args(std::move(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{}
|
||||||
|
{}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -125,4 +125,9 @@ namespace LX::AST
|
|||||||
|
|
||||||
return LLVM.builder.CreateCall(LLVM.functions[m_Name], evaluatedArgs, "call_tmp");
|
return LLVM.builder.CreateCall(LLVM.functions[m_Name], evaluatedArgs, "call_tmp");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
llvm::Value* IfBranch::GenIR(InfoLLVM& LLVM, FunctionScope& scope)
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -111,4 +111,20 @@ namespace LX::AST
|
|||||||
{
|
{
|
||||||
return "Function call";
|
return "Function call";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IfBranch::Log(unsigned depth)
|
||||||
|
{
|
||||||
|
Log::out<Log::Priority::HIGH>(std::string(depth, '\t'), "If Statement");
|
||||||
|
Log::out<Log::Priority::HIGH>(std::string(depth + 1, '\t'), "Condition:");
|
||||||
|
|
||||||
|
m_Condition->Log(depth + 2);
|
||||||
|
|
||||||
|
Log::out<Log::Priority::HIGH>(std::string(depth + 1, '\t'), "Body:");
|
||||||
|
for (const auto& node : m_Body) { node->Log(depth + 1); }
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* IfBranch::TypeName()
|
||||||
|
{
|
||||||
|
return "If Statement";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,5 +7,10 @@ func main()
|
|||||||
{
|
{
|
||||||
int c = add(1, 2)
|
int c = add(1, 2)
|
||||||
|
|
||||||
return c
|
if (c == 3)
|
||||||
|
{
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return 3
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user