mirror of
https://github.com/PashaBibko/LX.git
synced 2026-04-03 17:39:02 +00:00
Removed how variables were handled
This commit is contained in:
@@ -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" />
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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) //
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user