From 3550b015c99fb806a8289efce0afae8d6c5494f7 Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Wed, 23 Jul 2025 14:10:20 +0100 Subject: [PATCH] Added data contents of AST types --- .gitignore | 9 ++--- Common/LXC.h | 1 + LXC/LXC.cpp | 1 + ast/inc/Node.h | 35 ------------------- ast/inc/NodeBase.h | 65 +++++++++++++++++++++++++++++++++++ ast/inc/NodeTypes.h | 79 +++++++++++++++++++++++++++++++++++++++++++ ast/src/AST.cpp | 2 +- ast/src/NodeTypes.cpp | 0 8 files changed, 149 insertions(+), 43 deletions(-) delete mode 100644 ast/inc/Node.h create mode 100644 ast/inc/NodeBase.h create mode 100644 ast/inc/NodeTypes.h create mode 100644 ast/src/NodeTypes.cpp diff --git a/.gitignore b/.gitignore index 3d67197..8eefa16 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Common/LXC.h b/Common/LXC.h index 9404982..78e78b1 100644 --- a/Common/LXC.h +++ b/Common/LXC.h @@ -15,6 +15,7 @@ #include #include #include +#include #include // LXC util files // diff --git a/LXC/LXC.cpp b/LXC/LXC.cpp index a7de45c..a0f9285 100644 --- a/LXC/LXC.cpp +++ b/LXC/LXC.cpp @@ -1,5 +1,6 @@ #include +#include #include int main(int argc, char** argv) diff --git a/ast/inc/Node.h b/ast/inc/Node.h deleted file mode 100644 index 1ce5e04..0000000 --- a/ast/inc/Node.h +++ /dev/null @@ -1,35 +0,0 @@ -#include - -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; - }; -} diff --git a/ast/inc/NodeBase.h b/ast/inc/NodeBase.h new file mode 100644 index 0000000..e9d2698 --- /dev/null +++ b/ast/inc/NodeBase.h @@ -0,0 +1,65 @@ +#include + +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 + requires std::is_base_of_v + inline T* As(const Node& base) + { + return dynamic_cast(base); + } +} diff --git a/ast/inc/NodeTypes.h b/ast/inc/NodeTypes.h new file mode 100644 index 0000000..3875309 --- /dev/null +++ b/ast/inc/NodeTypes.h @@ -0,0 +1,79 @@ +#include + +#include + +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> 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 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 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 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 m_Condition; + + // The body of the if-statement // + std::vector> m_Body; + }; + + class ReturnStatement final : public Node + { + private: + // The value to return (nullable) // + std::unique_ptr m_ReturnValue; + }; +} diff --git a/ast/src/AST.cpp b/ast/src/AST.cpp index 3c9f4b2..3474563 100644 --- a/ast/src/AST.cpp +++ b/ast/src/AST.cpp @@ -1 +1 @@ -#include +#include diff --git a/ast/src/NodeTypes.cpp b/ast/src/NodeTypes.cpp new file mode 100644 index 0000000..e69de29