Rework evaluation to use shared pointers, fix bugs

This commit is contained in:
2019-06-01 21:38:39 +02:00
parent 4408cf00cd
commit 6206fef4c5
17 changed files with 122 additions and 133 deletions

View File

@@ -1,25 +1,22 @@
#include "EvaluationScope.hpp"
#include <memory>
EvaluationScope::EvaluationScope(unordered_map<int, EvalValue *> *scriptVariables, int deepestScope) {
EvaluationScope::EvaluationScope(unordered_map<int, shared_ptr<EvalValue>> *scriptVariables, int deepestScope) {
_scriptScope = scriptVariables;
_localScope = vector<unordered_map<int, EvalValue*>>(deepestScope);
_localScope = vector<unordered_map<int, shared_ptr<EvalValue>>>(deepestScope);
_currentScope = -1;
}
EvaluationScope::~EvaluationScope() {
_localScope.clear();
}
void EvaluationScope::CreateVariable(int scope, int id, EvalValue *value) {
void EvaluationScope::CreateVariable(int scope, int id, shared_ptr<EvalValue> value) {
if (scope == 0){
_scriptScope->insert_or_assign(id, value);
} else{
_localScope[scope - 1].insert({id, value});
_localScope[scope - 1].insert_or_assign(id, value);
}
}
void EvaluationScope::SetVariable(int scope, int id, EvalValue *value) {
void EvaluationScope::SetVariable(int scope, int id, shared_ptr<EvalValue> value) {
if (scope == 0){
_scriptScope->insert_or_assign(id, value);
} else{
@@ -27,7 +24,7 @@ void EvaluationScope::SetVariable(int scope, int id, EvalValue *value) {
}
}
EvalValue *EvaluationScope::GetVariable(int scope, int id) {
shared_ptr<EvalValue> EvaluationScope::GetVariable(int scope, int id) {
if (scope == 0){
return _scriptScope->at(id);
}
@@ -40,9 +37,7 @@ void EvaluationScope::OuterScope() {
void EvaluationScope::InnerScope() {
auto scope = this->_localScope[_currentScope];
for (auto v: scope){
delete v.second;
}
scope.clear();
_currentScope--;
}

View File

@@ -7,18 +7,18 @@
#include "../EvalValues/EvalValue.hpp"
class EvaluationScope {
unordered_map<int, EvalValue*>* _scriptScope;
vector<unordered_map<int, EvalValue*>> _localScope;
unordered_map<int, shared_ptr<EvalValue>>* _scriptScope;
vector<unordered_map<int, shared_ptr<EvalValue>>> _localScope;
int _currentScope;
public:
explicit EvaluationScope(unordered_map<int, EvalValue*>* scriptVariables, int deepestScope);
~EvaluationScope();
explicit EvaluationScope(unordered_map<int, shared_ptr<EvalValue>>* scriptVariables, int deepestScope);
~EvaluationScope() = default;
void CreateVariable(int scope, int id, EvalValue* value);
void SetVariable(int scope, int id, EvalValue* value);
void CreateVariable(int scope, int id, shared_ptr<EvalValue> value);
void SetVariable(int scope, int id, shared_ptr<EvalValue> value);
void OuterScope();
void InnerScope();
EvalValue* GetVariable(int scope, int id);
shared_ptr<EvalValue> GetVariable(int scope, int id);
};