Added parser project

This commit is contained in:
Pasha Bibko
2025-07-23 19:33:33 +01:00
parent 6a534ba635
commit 86a178b7d5
10 changed files with 93 additions and 30 deletions

View File

@@ -1,3 +1,5 @@
#pragma once
#include <LXC.h>
namespace LXC::AST
@@ -55,6 +57,15 @@ namespace LXC::AST
{}
};
// Typedefs for easier use with the AST //
typedef std::unique_ptr<Node> NodePtr;
typedef std::unique_ptr<NodeValue> NodeValuePtr;
typedef std::vector<NodePtr> SyntaxBranch;
typedef std::vector<NodeValuePtr> 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<typename T>
requires std::is_base_of_v<Node, T>

View File

@@ -1,3 +1,5 @@
#pragma once
#include <LXC.h>
#include <NodeBase.h>
@@ -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<std::unique_ptr<NodeValue>>& arguments);
FunctionCall(Identifier& functionName, ValueList& arguments);
private:
// The name of the function //
Identifier m_FuncName;
// The arguments of the function //
std::vector<std::unique_ptr<NodeValue>> 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<NodeValue>& left, Lexer::Token::TokenType operand, std::unique_ptr<NodeValue>& right);
Operation(NodeValuePtr& left, Lexer::Token::TokenType operand, NodeValuePtr& right);
private:
// The sides of the operation //
std::unique_ptr<NodeValue> 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<VarAssignment>& value);
VarDeclaration(Identifier& name, NodeValuePtr& value);
private:
// The name of the variable //
Identifier m_Name;
// Default value of the variable (nullable) //
std::unique_ptr<VarAssignment> m_Value;
NodeValuePtr m_Value;
};
class VarAssignment final : public Node
{
public:
VarAssignment(Identifier& name, std::unique_ptr<NodeValue>& value);
VarAssignment(Identifier& name, NodeValuePtr& value);
private:
// The name of the variable //
Identifier m_Name;
// Value to assign to the variable //
std::unique_ptr<NodeValue> 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<NodeValue>& condition, std::vector<std::unique_ptr<Node>>& body);
IfBranch(NodeValuePtr& condition, SyntaxBranch& body);
private:
// The condition of the branch //
std::unique_ptr<NodeValue> m_Condition;
NodeValuePtr m_Condition;
// The body of the if-statement //
std::vector<std::unique_ptr<Node>> m_Body;
SyntaxBranch m_Body;
};
class ReturnStatement final : public Node
{
public:
ReturnStatement(std::unique_ptr<NodeValue>& value);
ReturnStatement(NodeValuePtr& value);
private:
// The value to return (nullable) //
std::unique_ptr<NodeValue> m_ReturnValue;
NodeValuePtr m_ReturnValue;
};
}

View File

@@ -4,7 +4,7 @@
namespace LXC::AST
{
FunctionCall::FunctionCall (Identifier& functionName, std::vector<std::unique_ptr<NodeValue>>& 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<NodeValue>& left, Lexer::Token::TokenType operand, std::unique_ptr<NodeValue>& 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<VarAssignment>& 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<NodeValue>& 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<NodeValue>& condition, std::vector<std::unique_ptr<Node>>& 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<NodeValue>& value)
ReturnStatement::ReturnStatement(NodeValuePtr& value)
: Node(NodeType::ReturnVal), m_ReturnValue(std::move(m_ReturnValue))
{}
}