Added data contents of AST types

This commit is contained in:
Pasha Bibko
2025-07-23 14:10:20 +01:00
parent faf34661fb
commit 3550b015c9
8 changed files with 149 additions and 43 deletions

9
.gitignore vendored
View File

@@ -1,13 +1,8 @@
# CMake output directory #
out/
# Visual studio directory (excluding launch settings) #
.vs/LXC
.vs/CMake Overview
.vs/cmake.db
.vs/ProjectSettings.json
.vs/slnx.sqlite
.vs/VSWorkspaceState.json
# Visual studio directory #
.vs/
# Excludes binary outputs in root dir #
*.exe

View File

@@ -15,6 +15,7 @@
#include <unordered_map>
#include <cstdint>
#include <cstring>
#include <memory>
#include <vector>
// LXC util files //

View File

@@ -1,5 +1,6 @@
#include <LXC.h>
#include <NodeTypes.h>
#include <Lexer.h>
int main(int argc, char** argv)

View File

@@ -1,35 +0,0 @@
#include <LXC.h>
namespace LXC::AST
{
enum class NodeType
{
// General nodes //
Identifier,
Operation,
// Variable nodes //
Var_Declare,
Var_Assign,
Var_Access,
// Control flow nodes //
IfBranch,
ReturnVal
};
class Node
{
public:
Node(NodeType _type)
: m_Type(_type)
{}
protected:
const NodeType m_Type;
};
}

65
ast/inc/NodeBase.h Normal file
View File

@@ -0,0 +1,65 @@
#include <LXC.h>
namespace LXC::AST
{
// Enum to track which node it was created as //
enum class NodeType
{
// General nodes //
Identifier,
FunctionCall,
IntLiteral,
Operation,
// Variable nodes //
Var_Declare,
Var_Assign,
Var_Access,
// Control flow nodes //
IfBranch,
ReturnVal
};
// Base class of all AST nodes //
class Node
{
public:
// Virtual deconstructor for safe-polymorphism //
virtual ~Node() = default;
// Allows external access to find what the type is //
NodeType Type() const { return m_Type; }
protected:
// Stops the base type being created //
Node(NodeType _type)
: m_Type(_type)
{}
// Stores the type for polymorphism //
const NodeType m_Type;
};
// Nodes that can return values inherit from this instead //
class NodeValue : public Node
{
protected:
NodeValue(NodeType _type)
: Node(_type)
{}
};
// Returns a pointer to the actual type of the node //
template<typename T>
requires std::is_base_of_v<Node, T>
inline T* As(const Node& base)
{
return dynamic_cast<const T*>(base);
}
}

79
ast/inc/NodeTypes.h Normal file
View File

@@ -0,0 +1,79 @@
#include <LXC.h>
#include <NodeBase.h>
namespace LXC::AST
{
typedef std::string Identifier;
class FunctionCall final : public NodeValue
{
private:
// The name of the function //
Identifier m_FuncName;
// The arguments of the function //
std::vector<std::unique_ptr<Node>> m_Arguments;
};
class IntLiteral final : public NodeValue
{
private:
// Yes numbers are stored as strings //
std::string m_NumberValue;
};
class Operation final : public NodeValue
{
private:
// The sides of the operation //
std::unique_ptr<Node> m_Lhs, m_Rhs;
// The operand of the operation //
Lexer::Token::TokenType m_Operand;
};
class VarDeclaration final : public Node
{
private:
// The name of the variable //
Identifier m_Name;
// Default value of the variable (nullable) //
std::unique_ptr<VarAssignment> m_Value;
};
class VarAssignment final : public Node
{
private:
// The name of the variable //
Identifier m_Name;
// Value to assign to the variable //
std::unique_ptr<NodeValue> m_Value;
};
class VarAccess final : public NodeValue
{
private:
// The name of the variable //
Identifier m_Name;
};
class IfBranch final : public 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;
};
class ReturnStatement final : public Node
{
private:
// The value to return (nullable) //
std::unique_ptr<NodeValue> m_ReturnValue;
};
}

View File

@@ -1 +1 @@
#include <Node.h>
#include <NodeTypes.h>

0
ast/src/NodeTypes.cpp Normal file
View File