More fixes for boundscope

This commit is contained in:
2019-05-28 18:50:23 +02:00
parent 2c84c1e229
commit 6185f755a4
6 changed files with 58 additions and 20 deletions

View File

@@ -4,11 +4,34 @@
BoundScope::BoundScope(unordered_map<int, BoundVariable *> *scriptScope) {
_scriptScope = scriptScope;
_currentScope = 1;
_localScope = vector<unordered_map<int, BoundVariable*>>(1);
_deepestScope = 1;
auto localUpmostScope = new unordered_map<int, BoundVariable*>();
_localScope.push_back(localUpmostScope);
}
BoundScope::~BoundScope() {
_localScope.clear();
for (auto scope : _localScope){
for (auto v : *scope){
delete v.second;
}
delete scope;
}
}
void BoundScope::GoInnerScope() {
_currentScope++;
if (_localScope.size() < _currentScope){
auto innerScope = new unordered_map<int, BoundVariable*>();
_localScope.push_back(innerScope);
}
if (_deepestScope < _currentScope){
_deepestScope = _currentScope;
}
}
void BoundScope::GoOuterScope() {
_localScope[_currentScope - 1]->clear();
_currentScope--;
}
int BoundScope::Exists(int key) {
@@ -18,8 +41,8 @@ int BoundScope::Exists(int key) {
}
for (int i = _currentScope - 1; i >= 0; i--){
auto scope = _localScope.at(i);
found = scope.find(key);
if (found != scope.end()){
found = scope -> find(key);
if (found != scope -> end()){
return i + 1;
}
}
@@ -35,24 +58,24 @@ BoundVariable *BoundScope::GetVariable(int scope, int identifier) {
return nullptr;
} else{
auto s = this->_localScope.at(scope);
auto find = s.find(identifier);
if (find != s.end()){
auto find = s -> find(identifier);
if (find != s -> end()){
return find -> second;
}
return nullptr;
}
}
VariableAssignment BoundScope::CreateExplicitLocal(int identifier, ScriptType *type) {
VariableAssignment BoundScope::CreateExplicitLocal(int identifier, const ScriptType& type) {
auto scope = this->_localScope.at(this->_currentScope);
if (scope.find(identifier) != scope.end()){
if (scope -> find(identifier) != scope -> end()){
return VariableAssignment(VariableAssignmentResult::ExplicitLocalVariableExists, nullptr);
}
scope.insert({identifier, new BoundVariable(type)});
scope -> insert({identifier, new BoundVariable(type)});
return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, this->_currentScope, true));
}
VariableAssignment BoundScope::AssignVariable(int identifier, ScriptType *type) {
VariableAssignment BoundScope::AssignVariable(int identifier, const ScriptType& type) {
int exists = this->Exists(identifier);
if (exists == -1){
// Creation