Rework function evaluation scope to handle tables
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2019-06-12 17:56:47 +02:00
parent c022c91777
commit 3477ddd18c
7 changed files with 57 additions and 30 deletions

View File

@@ -12,7 +12,7 @@
using namespace std;
void Evaluator::Evaluate(BoundScriptStatement *statement) {
this->_evaluationScope = new EvaluationScope(this->_scriptData->_scriptVariables, statement->GetDeepestScope());
this->_evaluationScope = make_shared<EvaluationScope>(this->_scriptData->_scriptVariables, statement->GetDeepestScope());
EvaluateBlockStatement(statement, false);
}
@@ -62,7 +62,8 @@ void Evaluator::EvaluateFunctionDeclarationStatement(BoundFunctionDeclarationSta
auto type = statement->GetType();
auto key = statement->GetKey();
auto block = statement->GetBlock();
auto value = make_shared<ScriptFunctionEvalValue>(block, type);
auto evaluator = shared_ptr<EvaluationScope>(this->_evaluationScope->CreateBranchingScope(this->_evaluationScope->GetCurrentScope() + 1));
auto value = make_shared<ScriptFunctionEvalValue>(block, evaluator, type);
if (key->IsCreation()){
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), value);
} else{
@@ -174,6 +175,7 @@ shared_ptr<StringEvalValue> Evaluator::EvaluateStringExpression(BoundExpression
shared_ptr<EvalValue> Evaluator::EvaluateFunctionExpression(BoundExpression * expression){
switch (expression->GetKind()){
case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression);
case BoundExpressionKind ::Index: return this->EvaluateIndexExpression(expression);
default: throw;
}
}
@@ -216,8 +218,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(BoundExpression*
auto parameterKeys = type->GetParameterKeys();
auto scope = type -> GetScopeIndex();
auto originalScope = this->_evaluationScope;
auto functionScope = this->_evaluationScope->CreateBranchingScope(scope);
this->_evaluationScope = functionScope;
this->_evaluationScope = shared_ptr<EvaluationScope>(function -> GetScope() -> CreateBranchingScope(scope));
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++){
auto parameter = parameters[i];
@@ -230,7 +231,6 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(BoundExpression*
}
this->EvaluateBlockStatement(function->GetInnerBlock().get(), true);
delete this->_evaluationScope;
this->_evaluationScope = originalScope;
this->_hasReturned = false;
auto r = this -> _returnValue;
@@ -245,8 +245,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunction(ScriptFunctionEvalValue *funct
auto scope = type -> GetScopeIndex();
auto originalScope = this->_evaluationScope;
auto functionScope = this->_evaluationScope->CreateBranchingScope(scope);
this->_evaluationScope = functionScope;
this->_evaluationScope = shared_ptr<EvaluationScope>(function -> GetScope() -> CreateBranchingScope(scope));
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++){
auto parameter = parameters[i];
@@ -258,7 +257,6 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunction(ScriptFunctionEvalValue *funct
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone());
}
this->EvaluateBlockStatement(function->GetInnerBlock().get(), true);
delete this->_evaluationScope;
this->_evaluationScope = originalScope;
this->_hasReturned = false;
auto r = this -> _returnValue;
@@ -293,7 +291,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(BoundExpression
for (auto i : *declaredVars){
variables->insert({i.first, nullptr});
}
auto evaluator = new EvaluationScope(variables.get(), type -> GetDeepestScope());
auto evaluator = make_shared<EvaluationScope>(variables.get(), type -> GetDeepestScope());
auto currentEvaluator = this -> _evaluationScope;
this -> _evaluationScope = evaluator;
this -> EvaluateBlockStatement(tableExpression->GetBlock(), false);