Jump to specific function scope when calling function
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-06-08 16:44:47 +02:00
parent d385a9e3ee
commit 4d452b33e0
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
5 changed files with 28 additions and 6 deletions

View File

@ -80,6 +80,7 @@ BoundStatement *Binder::BindFunctionDeclarationStatement(ParsedStatement *statem
auto parameterTypes = vector<shared_ptr<ScriptType>>(parameters.size()); auto parameterTypes = vector<shared_ptr<ScriptType>>(parameters.size());
auto parameterKeys = vector<shared_ptr<BoundVariableKey>>(parameters.size()); auto parameterKeys = vector<shared_ptr<BoundVariableKey>>(parameters.size());
auto scopeIndex = this->_scope->GetCurrentScope();
this->_scope->GoInnerScope(); this->_scope->GoInnerScope();
for (int i = 0; i < parameters.size(); i++){ for (int i = 0; i < parameters.size(); i++){
auto var = parameters[i]; auto var = parameters[i];
@ -97,7 +98,7 @@ BoundStatement *Binder::BindFunctionDeclarationStatement(ParsedStatement *statem
auto identifier = functionStatement->GetIdentifier(); auto identifier = functionStatement->GetIdentifier();
auto returnType = make_shared<ScriptType>(TypeClass::Nil); auto returnType = make_shared<ScriptType>(TypeClass::Nil);
auto type = make_shared<FunctionScriptType>(returnType, parameterTypes, parameterKeys); auto type = make_shared<FunctionScriptType>(returnType, parameterTypes, parameterKeys, scopeIndex);
auto assignment = this->_scope->AssignVariable(identifier.GetHash(), type); auto assignment = this->_scope->AssignVariable(identifier.GetHash(), type);
if (assignment.GetResult() != VariableAssignmentResult::Ok){ if (assignment.GetResult() != VariableAssignmentResult::Ok){
this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::CantAssignVariable, statement->GetStartPosition(), statement->GetLength()); this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::CantAssignVariable, statement->GetStartPosition(), statement->GetLength());

View File

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

View File

@ -14,11 +14,17 @@ public:
explicit EvaluationScope(unordered_map<int, shared_ptr<EvalValue>>* scriptVariables, int deepestScope); explicit EvaluationScope(unordered_map<int, shared_ptr<EvalValue>>* scriptVariables, int deepestScope);
~EvaluationScope() = default; ~EvaluationScope() = default;
void CreateVariable(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, shared_ptr<EvalValue> value); void SetVariable(int scope, int id, const shared_ptr<EvalValue>& value);
void OuterScope(); void OuterScope();
void InnerScope(); void InnerScope();
shared_ptr<EvalValue> GetVariable(int scope, int id); 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 type = std::dynamic_pointer_cast<FunctionScriptType>(function->GetType());
auto parameterTypes = type->GetParameterTypes(); auto parameterTypes = type->GetParameterTypes();
auto parameterKeys = type->GetParameterKeys(); 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++){ for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++){
auto parameter = parameters[i]; auto parameter = parameters[i];
auto requiredType = parameterTypes.at(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->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone());
} }
this->EvaluateBlockStatement(function->GetInnerBlock().get()); this->EvaluateBlockStatement(function->GetInnerBlock().get());
this->_evaluationScope->JumpToScope(originalScope);
this->_hasReturned = false; this->_hasReturned = false;
auto r = this -> _returnValue; auto r = this -> _returnValue;
this -> _returnValue = nullptr; this -> _returnValue = nullptr;

View File

@ -82,13 +82,15 @@ class FunctionScriptType : public ScriptType{
shared_ptr<ScriptType> _returnType; shared_ptr<ScriptType> _returnType;
vector<shared_ptr<ScriptType>> _parameterTypes; vector<shared_ptr<ScriptType>> _parameterTypes;
vector<shared_ptr<BoundVariableKey>> _parameterKeys; vector<shared_ptr<BoundVariableKey>> _parameterKeys;
int _scopeIndex;
public: public:
FunctionScriptType(std::shared_ptr<ScriptType> returnType, vector<shared_ptr<ScriptType>> parameterTypes, FunctionScriptType(std::shared_ptr<ScriptType> returnType, vector<shared_ptr<ScriptType>> parameterTypes,
vector<shared_ptr<BoundVariableKey>> parameterKeys) vector<shared_ptr<BoundVariableKey>> parameterKeys, int scopeIndex)
: ScriptType(TypeClass::Function){ : ScriptType(TypeClass::Function){
_returnType = std::move(returnType); _returnType = std::move(returnType);
_parameterTypes = std::move(parameterTypes); _parameterTypes = std::move(parameterTypes);
_parameterKeys = std::move(parameterKeys); _parameterKeys = std::move(parameterKeys);
_scopeIndex = scopeIndex;
} }
shared_ptr<ScriptType> GetReturnType(){ shared_ptr<ScriptType> GetReturnType(){
return _returnType; return _returnType;
@ -101,6 +103,10 @@ public:
vector<shared_ptr<BoundVariableKey>> GetParameterKeys(){ vector<shared_ptr<BoundVariableKey>> GetParameterKeys(){
return _parameterKeys; return _parameterKeys;
} }
int GetScopeIndex(){
return _scopeIndex;
}
}; };
#endif //PORYGONLANG_SCRIPTTYPE_HPP #endif //PORYGONLANG_SCRIPTTYPE_HPP