Rework of variable scope to reduce load on evaluator
This commit is contained in:
@@ -4,60 +4,32 @@
|
||||
|
||||
EvaluationScope::EvaluationScope(unordered_map<size_t, shared_ptr<EvalValue>> *scriptVariables, int deepestScope) {
|
||||
_scriptScope = scriptVariables;
|
||||
_localScope = vector<shared_ptr<unordered_map<int, shared_ptr<EvalValue>>>>(deepestScope);
|
||||
for (int i = 0; i < deepestScope; i++){
|
||||
_localScope[i] = make_shared<unordered_map<int, shared_ptr<EvalValue>>>();
|
||||
}
|
||||
_currentScope = -1;
|
||||
_localScope = unordered_map<u_int64_t, shared_ptr<EvalValue>>(deepestScope);
|
||||
}
|
||||
|
||||
void EvaluationScope::CreateVariable(int scope, int id, const shared_ptr<EvalValue>& value) {
|
||||
if (scope == 0){
|
||||
_scriptScope->at(id) = value;
|
||||
void EvaluationScope::CreateVariable(BoundVariableKey* key, const shared_ptr<EvalValue> &value) {
|
||||
if (key->GetScopeId() == 0){
|
||||
_scriptScope -> at(key->GetIdentifier()) = value;
|
||||
} else{
|
||||
auto insert = _localScope[scope - 1]->insert({id, value});
|
||||
auto insert = _localScope.insert({key->GetHash(), value});
|
||||
if (!insert.second){
|
||||
_localScope[scope - 1]->at(id) = value;
|
||||
_localScope[key->GetHash()] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EvaluationScope::SetVariable(int scope, int id, const shared_ptr<EvalValue>& value) {
|
||||
if (scope == 0){
|
||||
_scriptScope->at(id) = value;
|
||||
void EvaluationScope::SetVariable(BoundVariableKey* key, const shared_ptr<EvalValue> &value) {
|
||||
if (key->GetScopeId() == 0){
|
||||
_scriptScope -> at(key->GetIdentifier()) = value;
|
||||
} else{
|
||||
_localScope[scope - 1]->at(id) = value;
|
||||
_localScope[key->GetHash()] = value;
|
||||
}
|
||||
}
|
||||
|
||||
shared_ptr<EvalValue> EvaluationScope::GetVariable(int scope, int id) {
|
||||
if (scope == 0){
|
||||
return _scriptScope->at(id);
|
||||
shared_ptr<EvalValue> EvaluationScope::GetVariable(BoundVariableKey* key) {
|
||||
if (key->GetScopeId() == 0){
|
||||
return _scriptScope -> at(key->GetIdentifier());
|
||||
} else{
|
||||
return _localScope[key->GetHash()];
|
||||
}
|
||||
return _localScope[scope - 1]->at(id);
|
||||
}
|
||||
|
||||
EvaluationScope* EvaluationScope::CreateBranchingScope(int scopeIndex){
|
||||
auto scope = new EvaluationScope(this -> _scriptScope, this -> _localScope.size());
|
||||
for (int i = 0; i <= scopeIndex; i++){
|
||||
scope->_localScope[i] = this->_localScope[i];
|
||||
}
|
||||
for (int i = scopeIndex + 1; i < _localScope.size(); i++){
|
||||
scope->_localScope[i] = make_shared<unordered_map<int, shared_ptr<EvalValue>>>();
|
||||
}
|
||||
scope->_currentScope = this -> _currentScope;
|
||||
return scope;
|
||||
}
|
||||
|
||||
void EvaluationScope::OuterScope() {
|
||||
_currentScope++;
|
||||
}
|
||||
|
||||
void EvaluationScope::InnerScope(bool clearScope) {
|
||||
if (clearScope){
|
||||
auto scope = this->_localScope[_currentScope];
|
||||
scope->clear();
|
||||
}
|
||||
_currentScope--;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user