Rework of variable scope to reduce load on evaluator

This commit is contained in:
2019-06-13 15:16:41 +02:00
parent 0339c44a89
commit 813ab1e90b
9 changed files with 65 additions and 91 deletions

View File

@@ -35,13 +35,11 @@ void Evaluator::EvaluateStatement(BoundStatement *statement) {
}
void Evaluator::EvaluateBlockStatement(BoundBlockStatement* statement, bool clearScope) {
this->_evaluationScope->OuterScope();
for (auto s: statement->GetStatements()){
this -> EvaluateStatement(s);
if (this->_hasReturned)
break;
}
this->_evaluationScope->InnerScope(clearScope);
}
void Evaluator::EvaluateExpressionStatement(BoundExpressionStatement *statement) {
@@ -53,9 +51,9 @@ void Evaluator::EvaluateAssignmentStatement(BoundAssignmentStatement *statement)
auto value = this -> EvaluateExpression(statement->GetExpression());
auto key = statement->GetKey();
if (key->IsCreation()){
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), value);
this->_evaluationScope->CreateVariable(key, value);
} else{
this->_evaluationScope->SetVariable(key->GetScopeId(), key->GetIdentifier(), value);
this->_evaluationScope->SetVariable(key, value);
}
}
@@ -63,12 +61,11 @@ void Evaluator::EvaluateFunctionDeclarationStatement(BoundFunctionDeclarationSta
auto type = statement->GetType();
auto key = statement->GetKey();
auto block = statement->GetBlock();
auto evaluator = shared_ptr<EvaluationScope>(this->_evaluationScope->CreateBranchingScope(this->_evaluationScope->GetCurrentScope() + 1));
auto value = make_shared<ScriptFunctionEvalValue>(block, evaluator, type);
auto value = make_shared<ScriptFunctionEvalValue>(block, this->_evaluationScope, type);
if (key->IsCreation()){
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), value);
this->_evaluationScope->CreateVariable(key, value);
} else{
this->_evaluationScope->SetVariable(key->GetScopeId(), key->GetIdentifier(), value);
this->_evaluationScope->SetVariable(key, value);
}
}
@@ -109,7 +106,10 @@ shared_ptr<EvalValue> Evaluator::EvaluateExpression(BoundExpression *expression)
}
shared_ptr<EvalValue> Evaluator::GetVariable(BoundVariableExpression* expression){
auto variable = this->_evaluationScope->GetVariable(expression->GetScope(), expression->GetId());
auto variable = this->_evaluationScope->GetVariable(expression->GetKey());
if (variable == nullptr){
throw EvaluationException("Variable not found");
}
return variable->Clone();
}
@@ -218,9 +218,8 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(BoundExpression*
auto type = std::dynamic_pointer_cast<FunctionScriptType>(function->GetType());
auto parameterTypes = type->GetParameterTypes();
auto parameterKeys = type->GetParameterKeys();
auto scope = type -> GetScopeIndex();
auto originalScope = this->_evaluationScope;
this->_evaluationScope = shared_ptr<EvaluationScope>(function -> GetScope() -> CreateBranchingScope(scope));
this->_evaluationScope = function->GetScope();
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++){
auto parameter = parameters[i];
@@ -229,11 +228,11 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(BoundExpression*
throw EvaluationException("Passed wrong type to function.");
}
auto key = parameterKeys.at(i);
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone());
this->_evaluationScope->CreateVariable(key.get(), parameter->Clone());
}
this->EvaluateBlockStatement(function->GetInnerBlock().get(), true);
this->_evaluationScope = originalScope;
this->_hasReturned = false;
auto r = this -> _returnValue;
this -> _returnValue = nullptr;
@@ -245,9 +244,8 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunction(ScriptFunctionEvalValue *funct
auto parameterTypes = type->GetParameterTypes();
auto parameterKeys = type->GetParameterKeys();
auto scope = type -> GetScopeIndex();
auto originalScope = this->_evaluationScope;
this->_evaluationScope = shared_ptr<EvaluationScope>(function -> GetScope() -> CreateBranchingScope(scope));
this->_evaluationScope = function->GetScope();
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++){
auto parameter = parameters[i];
@@ -256,7 +254,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunction(ScriptFunctionEvalValue *funct
throw EvaluationException("Passed wrong type to function.");
}
auto key = parameterKeys.at(i);
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone());
this->_evaluationScope->CreateVariable(key.get(), parameter->Clone());
}
this->EvaluateBlockStatement(function->GetInnerBlock().get(), true);
this->_evaluationScope = originalScope;