From 84f2a4cc5eb4746ede05b1101519f156331fc77b Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Thu, 17 Apr 2025 11:40:00 +0100 Subject: [PATCH] Added token class Also added a couple of foward declarations for faster compiles --- Parser/src/Parser.cpp | 4 +-- common/Lexer.h | 17 ++++++++++- common/Parser.h | 71 ++++++++++++++++++++++++++++++++++++++----- 3 files changed, 81 insertions(+), 11 deletions(-) diff --git a/Parser/src/Parser.cpp b/Parser/src/Parser.cpp index bfe8819..5903287 100644 --- a/Parser/src/Parser.cpp +++ b/Parser/src/Parser.cpp @@ -2,8 +2,8 @@ namespace LX { - std::vector> TurnTokensIntoAbstractSyntaxTree(std::vector& tokens, std::ofstream* log) + FileAST TurnTokensIntoAbstractSyntaxTree(std::vector& tokens, std::ofstream* log) { - return std::vector>(); + return FileAST(); } } diff --git a/common/Lexer.h b/common/Lexer.h index 2010860..76d32b1 100644 --- a/common/Lexer.h +++ b/common/Lexer.h @@ -1,9 +1,24 @@ #pragma once -#include #include #include +// Foward declarations of STD classes to minimise includes // +namespace std +{ + template + struct char_traits; + + template + class basic_ifstream; + + template + class basic_ofstream; + + using ifstream = basic_ifstream>; + using ofstream = basic_ofstream>; +} + // This file contains everything that is exported from Lexer.lib // The rest of the items within the Lexer project are internal only diff --git a/common/Parser.h b/common/Parser.h index 11c846f..3e04b9d 100644 --- a/common/Parser.h +++ b/common/Parser.h @@ -1,21 +1,76 @@ #pragma once +// Lexer foward declares fstream components so we can use them here // #include -#include -#include -#include #include +// Foward declares all items of the llvm lib that we need // +// Done to avoid including LLVM.h to shorten compile times // +namespace llvm +{ + class Value; + class LLVMContext; + class Module; + + class ConstantFolder; + class IRBuilderDefaultInserter; + + template + class IRBuilder; +} + +// The nodes of the abstract syntax tree constructed by the parser from the tokens // +namespace LX::AST +{ + // Base node that everything else inherits from + class Node + { + public: + // Enum for storing the type of node // + // Used so a pointer to Node can be used and then turned into it's true type // + enum NodeType + { + // General Nodes // + + IDENTIFIER, + + // Control flow Nodes // + + // If an error happened somewhere // + UNDEFINED = -1 + }; + + // Constructor to set the node type // + Node(NodeType type) + : m_Type(type) + {} + + // Virtual destructor because of polymorphism // + virtual ~Node() = default; + + // Function for generating LLVN IR (Intermediate representation) // + virtual llvm::Value* GenIR(llvm::LLVMContext& context, llvm::Module& module, llvm::IRBuilder<>& builder) = 0; + + // Function for generating C/C++ code (Currently not implemented) // + virtual void GenC() = 0; + + // The type of the node // + const NodeType m_Type; + }; +} + namespace LX { - struct GlobalDeclaration - { - }; - struct FunctionDeclaration { + std::vector> body; }; - std::vector> TurnTokensIntoAbstractSyntaxTree(std::vector& tokens, std::ofstream* log); + struct FileAST + { + std::vector functions; + }; + + FileAST TurnTokensIntoAbstractSyntaxTree(std::vector& tokens, std::ofstream* log); }