Jump to specific function scope when calling function
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2019-06-08 16:44:47 +02:00
parent d385a9e3ee
commit 4d452b33e0
5 changed files with 28 additions and 6 deletions

View File

@@ -8,7 +8,7 @@ EvaluationScope::EvaluationScope(unordered_map<int, shared_ptr<EvalValue>> *scri
_currentScope = -1;
}
void EvaluationScope::CreateVariable(int scope, int id, shared_ptr<EvalValue> value) {
void EvaluationScope::CreateVariable(int scope, int id, const shared_ptr<EvalValue>& value) {
if (scope == 0){
_scriptScope->at(id) = value;
} else{
@@ -16,7 +16,7 @@ void EvaluationScope::CreateVariable(int scope, int id, shared_ptr<EvalValue> va
}
}
void EvaluationScope::SetVariable(int scope, int id, shared_ptr<EvalValue> value) {
void EvaluationScope::SetVariable(int scope, int id, const shared_ptr<EvalValue>& value) {
if (scope == 0){
_scriptScope->at(id) = value;
} else{
@@ -31,6 +31,10 @@ shared_ptr<EvalValue> EvaluationScope::GetVariable(int scope, int id) {
return _localScope[scope - 1][id];
}
void EvaluationScope::JumpToScope(int index){
_currentScope = index;
}
void EvaluationScope::OuterScope() {
_currentScope++;
}

View File

@@ -14,11 +14,17 @@ public:
explicit EvaluationScope(unordered_map<int, shared_ptr<EvalValue>>* scriptVariables, int deepestScope);
~EvaluationScope() = default;
void CreateVariable(int scope, int id, shared_ptr<EvalValue> value);
void SetVariable(int scope, int id, shared_ptr<EvalValue> value);
void CreateVariable(int scope, int id, const shared_ptr<EvalValue>& value);
void SetVariable(int scope, int id, const shared_ptr<EvalValue>& value);
void OuterScope();
void InnerScope();
shared_ptr<EvalValue> GetVariable(int scope, int id);
void JumpToScope(int index);
int GetCurrentScope(){
return _currentScope;
}
};

View File

@@ -188,6 +188,10 @@ 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->GetCurrentScope();
this->_evaluationScope->JumpToScope(scope);
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++){
auto parameter = parameters[i];
auto requiredType = parameterTypes.at(i);
@@ -198,6 +202,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(BoundExpression*
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone());
}
this->EvaluateBlockStatement(function->GetInnerBlock().get());
this->_evaluationScope->JumpToScope(originalScope);
this->_hasReturned = false;
auto r = this -> _returnValue;
this -> _returnValue = nullptr;