Files
MIRROR-LX-OriginalRepo/common/Parser.h
Pasha Bibko b3373a858c Removed LLVM
2025-04-19 11:41:52 +01:00

129 lines
2.6 KiB
C++

#pragma once
// Lexer foward declares fstream components so we can use them here //
#include <Lexer.h>
#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
struct Node
{
// 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,
NUMBER_LITERAL,
OPERATION,
// Control flow Nodes //
RETURN_STATEMENT,
// 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 C/C++ code (Currently not implemented) //
//virtual void GenC() = 0;
// The type of the node //
const NodeType m_Type;
};
class NumberLiteral : public Node
{
public:
// Constructor to set values and automatically set type
NumberLiteral(std::string num);
private:
// The number it stores
// Yes the number is stored as a string
// It's horrible I know
std::string m_Number;
};
//
class Operation : public Node
{
public:
// Constructor to set values and automatically set type
Operation(std::unique_ptr<Node> lhs, Token::TokenType op, std::unique_ptr<Node> rhs);
private:
// The sides of the operation
// Unary operations are handled by a different class
std::unique_ptr<Node> m_Lhs, m_Rhs;
// The operation to be applied to the two sides
Token::TokenType m_Operand;
};
//
class ReturnStatement : public Node
{
public:
// Constructor to set values and automatically set type
ReturnStatement(std::unique_ptr<Node> val);
private:
// What it is returning (can be null)
std::unique_ptr<Node> m_Val;
};
}
namespace LX
{
struct IRGenerationError {};
struct FunctionDefinition
{
FunctionDefinition()
: body{}
{}
std::vector<std::unique_ptr<AST::Node>> body;
};
struct FileAST
{
FileAST()
: functions{}
{}
std::vector<FunctionDefinition> functions;
};
FileAST TurnTokensIntoAbstractSyntaxTree(std::vector<Token>& tokens, std::ofstream* log);
}