VarDec and VarAssign now work

This commit is contained in:
Pasha Bibko
2025-04-28 20:43:41 +01:00
parent 3ec6cc0c6e
commit cbc179411a
4 changed files with 26 additions and 17 deletions

View File

@@ -77,15 +77,17 @@ namespace LX::AST
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);
return LLVM.scope->CreateVar(m_Name, LLVM);
}
llvm::Value* VariableAssignment::GenIR(InfoLLVM& LLVM)
{
return nullptr;
// Gets the variable from the current scope //
llvm::AllocaInst* asignee = LLVM.scope->GetVar(m_Name);
ThrowIf<Scope::VariableDoesntExist>(asignee == nullptr);
// Creates the assignment //
return LLVM.builder.CreateStore(m_Value->GenIR(LLVM), asignee);
}
llvm::Value* VariableAccess::GenIR(InfoLLVM& LLVM)

View File

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