Removed how variables were handled

This commit is contained in:
Pasha Bibko
2025-05-10 13:39:25 +01:00
parent 050eeb5a53
commit 9e9606681f
8 changed files with 5 additions and 88 deletions

View File

@@ -63,28 +63,6 @@ namespace LX::AST
namespace LX
{
class Scope
{
public:
// Default constructor //
Scope()
: m_LocalVariables{}, m_Child(nullptr)
{}
// Gets a variable from the scope by it's name //
llvm::AllocaInst* GetVar(const std::string& name);
// Creates a variable of the given name //
llvm::AllocaInst* CreateVar(const std::string& name, InfoLLVM& LLVM);
private:
// Holds all the variables in the scope (excluding ones owned by the children //
std::unordered_map<std::string, llvm::AllocaInst*> m_LocalVariables;
// Holds a section of the scope, for example the variables created in a while loop //
std::unique_ptr<Scope> m_Child;
};
// Holds all needed info about a function //
// Currently only holds the body but in the future will hold: params, namespace/class-member //
struct FunctionDefinition
@@ -97,9 +75,6 @@ namespace LX
// The parameters of the function //
std::vector<std::string> params;
// The scope off the function //
Scope scope;
// The instructions of the body of the function //
std::vector<std::unique_ptr<AST::Node>> body;

View File

@@ -153,7 +153,6 @@
<ClCompile Include="src\GenIR.cpp" />
<ClCompile Include="src\Parser.cpp" />
<ClCompile Include="src\ParserErrors.cpp" />
<ClCompile Include="src\Scope.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="inc\AST.h" />

View File

@@ -20,9 +20,6 @@
<ClCompile Include="src\GenIR.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Scope.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\ParserErrors.cpp">
<Filter>Source Files</Filter>
</ClCompile>

View File

@@ -13,9 +13,6 @@ 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;
};
}

View File

@@ -8,7 +8,7 @@ namespace LX
{
// Default constructor that just initalises LLVM variables that it holds //
InfoLLVM::InfoLLVM(std::string name)
: context{}, builder(context), module(name, context), scope(nullptr)
: context{}, builder(context), module(name, context)
{}
// Reserves space for nodes (stops excess allocations) //

View File

@@ -101,27 +101,16 @@ namespace LX::AST
// Function for generating LLVM IR (Intermediate representation) //
llvm::Value* VariableDeclaration::GenIR(InfoLLVM& LLVM)
{
// Creates the variable within the scope //
return LLVM.scope->CreateVar(m_Name, LLVM);
return nullptr;
}
llvm::Value* VariableAssignment::GenIR(InfoLLVM& LLVM)
{
// Gets the variable from the current scope //
llvm::AllocaInst* asignee = LLVM.scope->GetVar(m_Name);
ThrowIf<VariableDoesntExist>(asignee == nullptr);
// Creates the assignment //
return LLVM.builder.CreateStore(m_Value->GenIR(LLVM), asignee);
return nullptr;
}
llvm::Value* VariableAccess::GenIR(InfoLLVM& LLVM)
{
// Loads the variable from the current scope //
llvm::AllocaInst* var = LLVM.scope->GetVar(m_Name);
ThrowIf<VariableDoesntExist>(var == nullptr);
// Creates the load within the AST //
return LLVM.builder.CreateLoad(llvm::Type::getInt32Ty(LLVM.context), var, m_Name + "_val");
return nullptr;
}
}

View File

@@ -40,7 +40,7 @@ namespace LX
// Adds the function's parameters to the scope //
for (std::string& param : funcAST.params)
{
LLVM.scope->CreateVar(param, LLVM);
//LLVM.scope->CreateVar(param, LLVM);
}
// Generates the IR within the function by looping over the nodes //
@@ -73,7 +73,6 @@ 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);
}

View File

@@ -1,39 +0,0 @@
#include <LX-Common.h>
#include <Parser.h>
#include <ParserErrors.h>
#include <AST.h>
namespace LX
{
llvm::AllocaInst* Scope::GetVar(const std::string& name)
{
// Stores a pointer to the current scope //
Scope* current = this;
do
{
// Checks if the variable exists in the current scope //
bool exists = current->m_LocalVariables.contains(name);
if (exists) { return m_LocalVariables[name]; }
// Travels to the next scope //
current = current->m_Child.get();
} while (current != nullptr);
// If it gets here it means it couldnt find the variable so it doesnt exist in the current context //
return nullptr;
}
llvm::AllocaInst* Scope::CreateVar(const std::string& name, InfoLLVM& LLVM)
{
// Checks variable of the same name doesn't exist //
ThrowIf<VariableAlreadyExists>(GetVar(name) != nullptr);
// Else inserts it into the local set //
m_LocalVariables[name] = LLVM.builder.CreateAlloca(LLVM.builder.getInt32Ty(), nullptr, name);
return m_LocalVariables[name];
}
}