Implements variable assignment evaluation
This commit is contained in:
2
src/Evaluator/EvaluationScope/EvaluationScope.cpp
Normal file
2
src/Evaluator/EvaluationScope/EvaluationScope.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
#include "EvaluationScope.hpp"
|
||||
44
src/Evaluator/EvaluationScope/EvaluationScope.hpp
Normal file
44
src/Evaluator/EvaluationScope/EvaluationScope.hpp
Normal 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
|
||||
Reference in New Issue
Block a user