Added AST constructors
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
# Fetches all files for the binary #
|
# Fetches all files for the binary #
|
||||||
file (GLOB ASTSources src/*.cpp inc/*.h)
|
add_library(AST STATIC src/NodeTypes.cpp src/AST.cpp)
|
||||||
add_library(AST STATIC ${ASTSources})
|
|
||||||
|
|
||||||
# Adds the headers in the current directory #
|
# Adds the headers in the current directory #
|
||||||
target_include_directories (
|
target_include_directories (
|
||||||
|
|||||||
@@ -1,23 +1,34 @@
|
|||||||
#include <LXC.h>
|
#include <LXC.h>
|
||||||
|
|
||||||
#include <NodeBase.h>
|
#include <NodeBase.h>
|
||||||
|
#include <Token.h>
|
||||||
|
|
||||||
namespace LXC::AST
|
namespace LXC::AST
|
||||||
{
|
{
|
||||||
|
// Will be replaced later to account for things like namespaces //
|
||||||
typedef std::string Identifier;
|
typedef std::string Identifier;
|
||||||
|
|
||||||
|
// Foward declares so it can be used in VarDecl //
|
||||||
|
class VarAssignment;
|
||||||
|
|
||||||
class FunctionCall final : public NodeValue
|
class FunctionCall final : public NodeValue
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
FunctionCall(Identifier& functionName, std::vector<std::unique_ptr<NodeValue>>& arguments);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// The name of the function //
|
// The name of the function //
|
||||||
Identifier m_FuncName;
|
Identifier m_FuncName;
|
||||||
|
|
||||||
// The arguments of the function //
|
// The arguments of the function //
|
||||||
std::vector<std::unique_ptr<Node>> m_Arguments;
|
std::vector<std::unique_ptr<NodeValue>> m_Arguments;
|
||||||
};
|
};
|
||||||
|
|
||||||
class IntLiteral final : public NodeValue
|
class IntLiteral final : public NodeValue
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
IntLiteral(std::string& value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Yes numbers are stored as strings //
|
// Yes numbers are stored as strings //
|
||||||
std::string m_NumberValue;
|
std::string m_NumberValue;
|
||||||
@@ -25,9 +36,12 @@ namespace LXC::AST
|
|||||||
|
|
||||||
class Operation final : public NodeValue
|
class Operation final : public NodeValue
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
Operation(std::unique_ptr<NodeValue>& left, Lexer::Token::TokenType operand, std::unique_ptr<NodeValue>& right);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// The sides of the operation //
|
// The sides of the operation //
|
||||||
std::unique_ptr<Node> m_Lhs, m_Rhs;
|
std::unique_ptr<NodeValue> m_Lhs, m_Rhs;
|
||||||
|
|
||||||
// The operand of the operation //
|
// The operand of the operation //
|
||||||
Lexer::Token::TokenType m_Operand;
|
Lexer::Token::TokenType m_Operand;
|
||||||
@@ -35,6 +49,10 @@ namespace LXC::AST
|
|||||||
|
|
||||||
class VarDeclaration final : public Node
|
class VarDeclaration final : public Node
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
VarDeclaration(Identifier& name);
|
||||||
|
VarDeclaration(Identifier& name, std::unique_ptr<VarAssignment>& value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// The name of the variable //
|
// The name of the variable //
|
||||||
Identifier m_Name;
|
Identifier m_Name;
|
||||||
@@ -45,6 +63,9 @@ namespace LXC::AST
|
|||||||
|
|
||||||
class VarAssignment final : public Node
|
class VarAssignment final : public Node
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
VarAssignment(Identifier& name, std::unique_ptr<NodeValue>& value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// The name of the variable //
|
// The name of the variable //
|
||||||
Identifier m_Name;
|
Identifier m_Name;
|
||||||
@@ -55,6 +76,9 @@ namespace LXC::AST
|
|||||||
|
|
||||||
class VarAccess final : public NodeValue
|
class VarAccess final : public NodeValue
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
VarAccess(Identifier& name);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// The name of the variable //
|
// The name of the variable //
|
||||||
Identifier m_Name;
|
Identifier m_Name;
|
||||||
@@ -62,9 +86,12 @@ namespace LXC::AST
|
|||||||
|
|
||||||
class IfBranch final : public Node
|
class IfBranch final : public Node
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
IfBranch(std::unique_ptr<NodeValue>& condition, std::vector<std::unique_ptr<Node>>& body);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// The condition of the branch //
|
// The condition of the branch //
|
||||||
std::unique_ptr<Node> m_Condition;
|
std::unique_ptr<NodeValue> m_Condition;
|
||||||
|
|
||||||
// The body of the if-statement //
|
// The body of the if-statement //
|
||||||
std::vector<std::unique_ptr<Node>> m_Body;
|
std::vector<std::unique_ptr<Node>> m_Body;
|
||||||
@@ -72,6 +99,9 @@ namespace LXC::AST
|
|||||||
|
|
||||||
class ReturnStatement final : public Node
|
class ReturnStatement final : public Node
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
ReturnStatement(std::unique_ptr<NodeValue>& value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// The value to return (nullable) //
|
// The value to return (nullable) //
|
||||||
std::unique_ptr<NodeValue> m_ReturnValue;
|
std::unique_ptr<NodeValue> m_ReturnValue;
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
#include <LXC.h>
|
||||||
|
|
||||||
|
#include <NodeTypes.h>
|
||||||
|
|
||||||
|
namespace LXC::AST
|
||||||
|
{
|
||||||
|
FunctionCall::FunctionCall (Identifier& functionName, std::vector<std::unique_ptr<NodeValue>>& 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<NodeValue>& left, Lexer::Token::TokenType operand, std::unique_ptr<NodeValue>& 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<VarAssignment>& value)
|
||||||
|
: Node(NodeType::Var_Declare), m_Name(std::move(name)), m_Value(std::move(value))
|
||||||
|
{}
|
||||||
|
|
||||||
|
VarAssignment::VarAssignment(Identifier& name, std::unique_ptr<NodeValue>& 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<NodeValue>& condition, std::vector<std::unique_ptr<Node>>& body)
|
||||||
|
: Node(NodeType::IfBranch), m_Condition(std::move(condition)), m_Body(std::move(body))
|
||||||
|
{}
|
||||||
|
|
||||||
|
ReturnStatement::ReturnStatement(std::unique_ptr<NodeValue>& value)
|
||||||
|
: Node(NodeType::ReturnVal), m_ReturnValue(std::move(m_ReturnValue))
|
||||||
|
{}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user