Added data contents of AST types
This commit is contained in:
65
ast/inc/NodeBase.h
Normal file
65
ast/inc/NodeBase.h
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user