Skip to content

Commit

Permalink
Improved SymbolTable class implementation source file.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Dec 21, 2024
1 parent ad060b0 commit c0ca6b8
Showing 1 changed file with 11 additions and 59 deletions.
70 changes: 11 additions & 59 deletions src/n8/core/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@
#include <n8/core/Runtime.hpp>
#include <n8/core/SymbolTable.hpp>

void SymbolTable::reset() {
this->id.clear();
this->table.clear();

if(this->parent != nullptr) {
this->parent->reset();
this->parent.reset();
}
}

SymbolTable& SymbolTable::operator=(SymbolTable&& other) noexcept {
if(this != &other) {
parent = std::move(other.parent);
Expand All @@ -53,19 +43,6 @@ SymbolTable& SymbolTable::operator=(const SymbolTable& other) {
return *this;
}

SymbolTable::~SymbolTable() {
this->waitForTasks();
this->reset();
}

std::string SymbolTable::getTableId() const {
return this->id;
}

void SymbolTable::reassignUuid() {
this->id = N8Util::generateUuid();
}

DynamicObject SymbolTable::getSymbol(
std::shared_ptr<Token> reference,
const std::string& name
Expand All @@ -87,40 +64,19 @@ DynamicObject SymbolTable::getSymbol(

void SymbolTable::setSymbol(
std::shared_ptr<Token> reference,
DynamicObject value,
bool isDeclaration
DynamicObject value
) {
std::string name = reference->getImage();
if(isDeclaration) {
if(!this->hasSymbol(name))
this->table[name] = std::move(value);
else throw ASTNodeException(
std::move(reference),
"Symbol already declared: " + name
);

return;
}

if(!this->hasSymbol(name))
throw ASTNodeException(
std::move(reference),
"Cannot set symbol: " + name
);

if(this->table.find(name) != this->table.end()) {
DynamicObject& current = this->table[name];
if(current.hasLock())
return;

current = std::move(value);
}
else if(this->parent)
this->parent->setSymbol(
std::move(reference),
std::move(value),
isDeclaration
);
else if(this->parent && this->parent->hasSymbol(name))
this->parent->setSymbol(std::move(reference), std::move(value));
else this->table[name] = std::move(value);
}

void SymbolTable::removeSymbol(const std::string& name) {
Expand All @@ -134,8 +90,8 @@ void SymbolTable::removeSymbol(const std::string& name) {
}

bool SymbolTable::hasSymbol(const std::string& name) {
return this->table.find(name) != this->table.end() ||
(this->parent && this->parent->hasSymbol(name));
return (this->parent && this->parent->hasSymbol(name)) ||
this->table.find(name) != this->table.end();
}

void SymbolTable::addParallelism(std::future<void> par) {
Expand All @@ -160,10 +116,8 @@ void SymbolTable::lock(std::string name, SymbolTable& requestOrigin) {
return;

if(this->table.find(name) != this->table.end()) {
DynamicObject& current = this->table[name];

current.own(requestOrigin.id);
current.lock();
this->table[name].own(requestOrigin.id);
this->table[name].lock();
}
else if(this->parent)
this->parent->lock(name, requestOrigin);
Expand All @@ -173,11 +127,9 @@ void SymbolTable::unlock(std::string name, SymbolTable& requestOrigin) {
if(!this->hasSymbol(name))
return;

if(this->table.find(name) != this->table.end()) {
DynamicObject& current = this->table[name];
if(current.ownerId() == requestOrigin.id)
current.unlock();
}
if(this->table.find(name) != this->table.end() &&
this->table[name].ownerId() == requestOrigin.id)
this->table[name].unlock();
else if(this->parent)
this->parent->unlock(name, requestOrigin);
}

0 comments on commit c0ca6b8

Please sign in to comment.