mirror of
https://github.com/PashaBibko/LX.git
synced 2026-04-03 17:39:02 +00:00
Changed how scopes work
Also added logging and GenIR functions for VariableDeclaration
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
// Lexer foward declares fstream components so we can use them here //
|
||||
#include <Lexer.h>
|
||||
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <memory>
|
||||
|
||||
// Foward declares STD stuff that is passed around //
|
||||
@@ -30,6 +30,7 @@ namespace LX::AST
|
||||
|
||||
NUMBER_LITERAL,
|
||||
OPERATION,
|
||||
VARIABLE_DECLARATION,
|
||||
|
||||
// Control flow Nodes //
|
||||
|
||||
@@ -81,18 +82,6 @@ namespace LX
|
||||
class Scope
|
||||
{
|
||||
public:
|
||||
// Struct to store all the info of a variable (excluding name as that is stored in the map) //
|
||||
struct __declspec(novtable) Variable final
|
||||
{
|
||||
// Default constructor //
|
||||
Variable() {}
|
||||
|
||||
// Will hold the type as part of the type here //
|
||||
|
||||
// The value of the variable //
|
||||
llvm::Value* val = nullptr;
|
||||
};
|
||||
|
||||
// Error thrown if the user tried to create a variable that already existed //
|
||||
struct __declspec(novtable) VariableAlreadyExists final {};
|
||||
|
||||
@@ -105,17 +94,14 @@ namespace LX
|
||||
{}
|
||||
|
||||
// Gets a variable from the scope by it's name //
|
||||
Variable* GetVarOfName(const std::string& name);
|
||||
bool DoesVarExist(const std::string& name);
|
||||
|
||||
// Creates a variable of the given name, returns nullptr if a variable with that name already exists //
|
||||
Variable* CreateVar(const std::string& name);
|
||||
// Creates a variable of the given name //
|
||||
void CreateVar(const std::string& name);
|
||||
|
||||
private:
|
||||
// Base logic for getting a variable by it's name without any error checking //
|
||||
Variable* GetVarOfNameImpl(const std::string& name);
|
||||
|
||||
// Holds all the variables in the scope (excluding ones owned by the children //
|
||||
std::unordered_map<std::string, Variable> m_LocalVariables;
|
||||
std::unordered_set<std::string> m_LocalVariables;
|
||||
|
||||
// Holds a section of the scope, for example the variables created in a while loop //
|
||||
std::unique_ptr<Scope> m_Child;
|
||||
|
||||
@@ -15,6 +15,9 @@ namespace LX
|
||||
llvm::LLVMContext context;
|
||||
llvm::Module module;
|
||||
llvm::IRBuilder<> builder;
|
||||
|
||||
// Not LLVM I just cba to add this parameter to the functions that needed it //
|
||||
Scope* scope;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace LX
|
||||
{
|
||||
// Default constructor that just initalises LLVM variables that it holds //
|
||||
InfoLLVM::InfoLLVM(std::string name)
|
||||
: context{}, builder(context), module(name, context)
|
||||
: context{}, builder(context), module(name, context), scope(nullptr)
|
||||
{}
|
||||
|
||||
// Reserves space for nodes (stops excess allocations) //
|
||||
@@ -41,4 +41,9 @@ namespace LX::AST
|
||||
ReturnStatement::ReturnStatement(std::unique_ptr<Node> val)
|
||||
: Node(Node::RETURN_STATEMENT), m_Val(std::move(val))
|
||||
{}
|
||||
|
||||
// Passes constructor args to values and sets type //
|
||||
VariableDeclaration::VariableDeclaration(const std::string& name)
|
||||
: Node(Node::VARIABLE_DECLARATION), m_Name(name)
|
||||
{}
|
||||
}
|
||||
|
||||
@@ -70,5 +70,10 @@ namespace LX::AST
|
||||
// Function for generating LLVN IR (Intermediate representation) //
|
||||
llvm::Value* VariableDeclaration::GenIR(InfoLLVM& LLVM)
|
||||
{
|
||||
// Creates the variable within the scope //
|
||||
LLVM.scope->CreateVar(m_Name);
|
||||
|
||||
// Creates the declaration within the IR //
|
||||
return LLVM.builder.CreateAlloca(LLVM.builder.getInt32Ty(), nullptr, m_Name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,4 +40,10 @@ namespace LX::AST
|
||||
m_Val->Log(log, depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
void VariableDeclaration::Log(std::ofstream* log, unsigned depth)
|
||||
{
|
||||
(*log) << std::string(depth, '\t');
|
||||
(*log) << "Variable declaration: " << m_Name << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,6 +55,7 @@ namespace LX
|
||||
// Loops over the functions to generate their LLVM IR //
|
||||
for (auto& func : ast.functions)
|
||||
{
|
||||
LLVM.scope = &func.scope; // Sets the current scope for the builder
|
||||
GenerateFunctionIR(func, LLVM);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,64 +4,32 @@
|
||||
|
||||
namespace LX
|
||||
{
|
||||
// Util function for getting a pointer to an item from a map //
|
||||
static inline Scope::Variable* GetFromMap(const std::string& name, std::unordered_map<std::string, Scope::Variable>& map)
|
||||
bool Scope::DoesVarExist(const std::string& name)
|
||||
{
|
||||
// Checks if it is in a map and if so returns it //
|
||||
if (auto it = map.find(name); it != map.end()) { return &it->second; }
|
||||
|
||||
// Else returns null //
|
||||
else { return nullptr; }
|
||||
}
|
||||
|
||||
// Base logic for getting a variable by it's name without any error checking //
|
||||
Scope::Variable* Scope::GetVarOfNameImpl(const std::string& name)
|
||||
{
|
||||
// Stores the current scope that is being checked for the variable //
|
||||
// Stores a pointer to the current scope //
|
||||
Scope* current = this;
|
||||
|
||||
// Loops over the scope and it's child to find the variable //
|
||||
do
|
||||
{
|
||||
// Gets the variable (if it exists) //
|
||||
Variable* var = GetFromMap(name, current->m_LocalVariables);
|
||||
// Checks if the variable exists in the current scope //
|
||||
bool exists = current->m_LocalVariables.contains(name);
|
||||
if (exists) { return true; }
|
||||
|
||||
// Returns the variable if it exists //
|
||||
if (var != nullptr) { return var; }
|
||||
|
||||
// Assigns current to the child to recursively check //
|
||||
// Doing it like this avoids recursive functions //
|
||||
// Travels to the next scope //
|
||||
current = current->m_Child.get();
|
||||
|
||||
} while (current != nullptr);
|
||||
|
||||
// Else returns a nullptr and lets the caller handle the error checking //
|
||||
return nullptr;
|
||||
// If it gets here it means it couldnt find the variable so it doesnt exist in the current context //
|
||||
return false;
|
||||
}
|
||||
|
||||
// Gets a variable from the scope by it's name //
|
||||
Scope::Variable* Scope::GetVarOfName(const std::string& name)
|
||||
void Scope::CreateVar(const std::string& name)
|
||||
{
|
||||
// Gets the variable (if it exists) //
|
||||
Variable* var = GetVarOfNameImpl(name);
|
||||
// Checks variable of the same name doesn't exist //
|
||||
ThrowIf<Scope::VariableAlreadyExists>(DoesVarExist(name));
|
||||
|
||||
// Throws an error if the variable doesn't exist //
|
||||
ThrowIf<VariableDoesntExist>(var == nullptr);
|
||||
|
||||
// Else it can return the variable //
|
||||
return var;
|
||||
}
|
||||
|
||||
// Creates a variable of the given name, returns nullptr if a variable with that name already exists //
|
||||
Scope::Variable* Scope::CreateVar(const std::string& name)
|
||||
{
|
||||
// Checks if a variable with the same name already exists //
|
||||
Variable* alreadyExist = GetVarOfNameImpl(name);
|
||||
|
||||
// Throws an error if the variable already exists //
|
||||
ThrowIf<VariableAlreadyExists>(alreadyExist != nullptr);
|
||||
|
||||
// Else creates the variable and returns it //
|
||||
return &m_LocalVariables[name];
|
||||
// Else inserts it into the local set //
|
||||
m_LocalVariables.insert(name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
func main()
|
||||
{
|
||||
int a = 5
|
||||
int a
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user