mirror of
https://github.com/PashaBibko/LX.git
synced 2026-04-04 01:49:05 +00:00
Removed how variables were handled
This commit is contained in:
@@ -63,28 +63,6 @@ namespace LX::AST
|
|||||||
|
|
||||||
namespace LX
|
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 //
|
// Holds all needed info about a function //
|
||||||
// Currently only holds the body but in the future will hold: params, namespace/class-member //
|
// Currently only holds the body but in the future will hold: params, namespace/class-member //
|
||||||
struct FunctionDefinition
|
struct FunctionDefinition
|
||||||
@@ -98,9 +76,6 @@ namespace LX
|
|||||||
// The parameters of the function //
|
// The parameters of the function //
|
||||||
std::vector<std::string> params;
|
std::vector<std::string> params;
|
||||||
|
|
||||||
// The scope off the function //
|
|
||||||
Scope scope;
|
|
||||||
|
|
||||||
// The instructions of the body of the function //
|
// The instructions of the body of the function //
|
||||||
std::vector<std::unique_ptr<AST::Node>> body;
|
std::vector<std::unique_ptr<AST::Node>> body;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -153,7 +153,6 @@
|
|||||||
<ClCompile Include="src\GenIR.cpp" />
|
<ClCompile Include="src\GenIR.cpp" />
|
||||||
<ClCompile Include="src\Parser.cpp" />
|
<ClCompile Include="src\Parser.cpp" />
|
||||||
<ClCompile Include="src\ParserErrors.cpp" />
|
<ClCompile Include="src\ParserErrors.cpp" />
|
||||||
<ClCompile Include="src\Scope.cpp" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="inc\AST.h" />
|
<ClInclude Include="inc\AST.h" />
|
||||||
|
|||||||
@@ -20,9 +20,6 @@
|
|||||||
<ClCompile Include="src\GenIR.cpp">
|
<ClCompile Include="src\GenIR.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="src\Scope.cpp">
|
|
||||||
<Filter>Source Files</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="src\ParserErrors.cpp">
|
<ClCompile Include="src\ParserErrors.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
|||||||
@@ -13,9 +13,6 @@ namespace LX
|
|||||||
llvm::LLVMContext context;
|
llvm::LLVMContext context;
|
||||||
llvm::Module module;
|
llvm::Module module;
|
||||||
llvm::IRBuilder<> builder;
|
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 //
|
// Default constructor that just initalises LLVM variables that it holds //
|
||||||
InfoLLVM::InfoLLVM(std::string name)
|
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) //
|
// Reserves space for nodes (stops excess allocations) //
|
||||||
|
|||||||
@@ -101,27 +101,16 @@ namespace LX::AST
|
|||||||
// Function for generating LLVM IR (Intermediate representation) //
|
// Function for generating LLVM IR (Intermediate representation) //
|
||||||
llvm::Value* VariableDeclaration::GenIR(InfoLLVM& LLVM)
|
llvm::Value* VariableDeclaration::GenIR(InfoLLVM& LLVM)
|
||||||
{
|
{
|
||||||
// Creates the variable within the scope //
|
return nullptr;
|
||||||
return LLVM.scope->CreateVar(m_Name, LLVM);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
llvm::Value* VariableAssignment::GenIR(InfoLLVM& LLVM)
|
llvm::Value* VariableAssignment::GenIR(InfoLLVM& LLVM)
|
||||||
{
|
{
|
||||||
// Gets the variable from the current scope //
|
return nullptr;
|
||||||
llvm::AllocaInst* asignee = LLVM.scope->GetVar(m_Name);
|
|
||||||
ThrowIf<VariableDoesntExist>(asignee == nullptr);
|
|
||||||
|
|
||||||
// Creates the assignment //
|
|
||||||
return LLVM.builder.CreateStore(m_Value->GenIR(LLVM), asignee);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
llvm::Value* VariableAccess::GenIR(InfoLLVM& LLVM)
|
llvm::Value* VariableAccess::GenIR(InfoLLVM& LLVM)
|
||||||
{
|
{
|
||||||
// Loads the variable from the current scope //
|
return nullptr;
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ namespace LX
|
|||||||
// Adds the function's parameters to the scope //
|
// Adds the function's parameters to the scope //
|
||||||
for (std::string& param : funcAST.params)
|
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 //
|
// 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 //
|
// Loops over the functions to generate their LLVM IR //
|
||||||
for (auto& func : ast.functions)
|
for (auto& func : ast.functions)
|
||||||
{
|
{
|
||||||
LLVM.scope = &func.scope; // Sets the current scope for the builder
|
|
||||||
GenerateFunctionIR(func, LLVM);
|
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