Files
LXC/ast/inc/NodeBase.h
2025-07-23 19:33:33 +01:00

77 lines
1.7 KiB
C++

#pragma once
#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)
{}
};
// 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>
inline T* As(const Node& base)
{
return dynamic_cast<const T*>(base);
}
}