Added scope class

This commit is contained in:
Pasha Bibko
2025-04-27 21:18:09 +01:00
parent 321a7fea18
commit a64aa28432
10 changed files with 162 additions and 7 deletions

View File

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

View File

@@ -26,6 +26,9 @@
<ClCompile Include="src\AST-Loggers.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Scope.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="inc\AST.h">

View File

@@ -65,7 +65,7 @@ namespace LX::AST
class ReturnStatement : public Node
{
public:
// Constructor to set values and automatically set type
// Constructor to set values and automatically set type //
ReturnStatement(std::unique_ptr<Node> val);
// Function for generating LLVN IR (Intermediate representation) //
@@ -78,4 +78,24 @@ namespace LX::AST
// What it is returning (can be null) //
std::unique_ptr<Node> m_Val;
};
// Node to represent the declaration of a variable within the AST //
class VariableDeclaration : public Node
{
public:
// Constructor to set values and automatically set type //
VariableDeclaration(const std::string& name);
// Function for generating LLVN IR (Intermediate representation) //
llvm::Value* GenIR(InfoLLVM& LLVM) override;
// Function to log the node to a file //
void Log(std::ofstream* log, unsigned depth) override;
private:
// Name of the variable //
std::string m_Name;
// Doesnt need to store type as everything is currently int //
};
}

View File

@@ -66,4 +66,9 @@ namespace LX::AST
return out;
}
}
// Function for generating LLVN IR (Intermediate representation) //
llvm::Value* VariableDeclaration::GenIR(InfoLLVM& LLVM)
{
}
}

67
Parser/src/Scope.cpp Normal file
View File

@@ -0,0 +1,67 @@
#include <Parser.h>
#include <Util.h>
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)
{
// 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 //
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);
// 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 //
current = current->m_Child.get();
} while (current != nullptr);
// Else returns a nullptr and lets the caller handle the error checking //
return nullptr;
}
// Gets a variable from the scope by it's name //
Scope::Variable* Scope::GetVarOfName(const std::string& name)
{
// Gets the variable (if it exists) //
Variable* var = GetVarOfNameImpl(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];
}
}