Implements variable assignment evaluation

This commit is contained in:
2019-05-29 14:55:03 +02:00
parent 6185f755a4
commit f6cf4d96dd
11 changed files with 104 additions and 8 deletions

View File

@@ -0,0 +1,2 @@
#include "EvaluationScope.hpp"

View File

@@ -0,0 +1,44 @@
#ifndef PORYGONLANG_EVALUATIONSCOPE_HPP
#define PORYGONLANG_EVALUATIONSCOPE_HPP
#include <unordered_map>
#include <vector>
#include "../EvalValues/EvalValue.hpp"
class EvaluationScope {
unordered_map<int, EvalValue*>* _scriptScope;
vector<unordered_map<int, EvalValue*>> _localScope;
public:
explicit EvaluationScope(unordered_map<int, EvalValue*>* scriptVariables, int deepestScope){
_scriptScope = scriptVariables;
_localScope = vector<unordered_map<int, EvalValue*>>(deepestScope);
}
~EvaluationScope(){
_localScope.clear();
}
void CreateVariable(int scope, int id, EvalValue* value){
if (scope == 0){
_scriptScope->insert_or_assign(id, value);
} else{
_localScope[scope - 1].insert({id, value});
}
}
void SetVariable(int scope, int id, EvalValue* value){
if (scope == 0){
_scriptScope->insert_or_assign(id, value);
} else{
_localScope[scope - 1][id] = value;
}
}
EvalValue* GetVariable(int scope, int id){
return _localScope[scope - 1][id];
}
};
#endif //PORYGONLANG_EVALUATIONSCOPE_HPP