Added token class

Also added a couple of foward declarations for faster compiles
This commit is contained in:
Pasha Bibko
2025-04-17 11:40:00 +01:00
parent f28b81ab87
commit 84f2a4cc5e
3 changed files with 81 additions and 11 deletions

View File

@@ -2,8 +2,8 @@
namespace LX
{
std::vector<std::unique_ptr<GlobalDeclaration>> TurnTokensIntoAbstractSyntaxTree(std::vector<Token>& tokens, std::ofstream* log)
FileAST TurnTokensIntoAbstractSyntaxTree(std::vector<Token>& tokens, std::ofstream* log)
{
return std::vector<std::unique_ptr<GlobalDeclaration>>();
return FileAST();
}
}

View File

@@ -1,9 +1,24 @@
#pragma once
#include <fstream>
#include <vector>
#include <string>
// Foward declarations of STD classes to minimise includes //
namespace std
{
template<typename T1 = char>
struct char_traits;
template<typename T1, typename T2>
class basic_ifstream;
template<typename T1, typename T2>
class basic_ofstream;
using ifstream = basic_ifstream<char, char_traits<char>>;
using ofstream = basic_ofstream<char, char_traits<char>>;
}
// This file contains everything that is exported from Lexer.lib
// The rest of the items within the Lexer project are internal only

View File

@@ -1,21 +1,76 @@
#pragma once
// Lexer foward declares fstream components so we can use them here //
#include <Lexer.h>
#include <LLVM.h>
#include <fstream>
#include <vector>
#include <memory>
// 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<typename T1 = ConstantFolder, typename T2 = IRBuilderDefaultInserter>
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<std::unique_ptr<AST::Node>> body;
};
std::vector<std::unique_ptr<GlobalDeclaration>> TurnTokensIntoAbstractSyntaxTree(std::vector<Token>& tokens, std::ofstream* log);
struct FileAST
{
std::vector<FunctionDeclaration> functions;
};
FileAST TurnTokensIntoAbstractSyntaxTree(std::vector<Token>& tokens, std::ofstream* log);
}