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

View File

@@ -2,8 +2,10 @@
#include "Evaluator.hpp"
#include "EvaluationException.hpp"
#include "../Script.hpp"
#include "EvaluationScope/EvaluationScope.hpp"
void Evaluator::Evaluate(BoundScriptStatement *statement) {
this->_evaluationScope = new EvaluationScope(this->_scriptData->_scriptVariables, statement->GetDeepestScope());
EvaluateBlockStatement(statement);
}
@@ -12,6 +14,10 @@ void Evaluator::EvaluateStatement(BoundStatement *statement) {
case BoundStatementKind ::Script: throw; // Should never happen
case BoundStatementKind ::Block: return this -> EvaluateBlockStatement((BoundBlockStatement*)statement);
case BoundStatementKind ::Expression: return this -> EvaluateExpressionStatement((BoundExpressionStatement*)statement);
case BoundStatementKind ::Assignment: return this -> EvaluateAssignmentStatement((BoundAssignmentStatement*)statement);
case BoundStatementKind::Bad:
throw;
}
}
@@ -28,6 +34,16 @@ void Evaluator::EvaluateExpressionStatement(BoundExpressionStatement *statement)
this->_scriptData->_lastValue = this -> EvaluateExpression(statement->GetExpression());
}
void Evaluator::EvaluateAssignmentStatement(BoundAssignmentStatement *statement) {
auto value = this -> EvaluateExpression(statement->GetExpression());
auto key = statement->GetKey();
if (key->IsCreation()){
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), value);
} else{
this->_evaluationScope->SetVariable(key->GetScopeId(), key->GetIdentifier(), value);
}
}
EvalValue *Evaluator::EvaluateExpression(BoundExpression *expression) {
auto type = expression -> GetType();
switch (type->GetClass()){

View File

@@ -9,7 +9,7 @@
#include "EvalValues/EvalValue.hpp"
#include "EvalValues/NumericEvalValue.hpp"
#include "EvalValues/StringEvalValue.hpp"
#include "EvaluationScope/EvaluationScope.hpp"
using namespace boost;
@@ -17,10 +17,12 @@ class Evaluator {
EvalValue* _result;
Script* _scriptData;
EvaluationScope* _evaluationScope;
void EvaluateStatement(BoundStatement* statement);
void EvaluateBlockStatement(BoundBlockStatement* statement);
void EvaluateExpressionStatement(BoundExpressionStatement* statement);
void EvaluateAssignmentStatement(BoundAssignmentStatement* statement);
EvalValue* EvaluateExpression(BoundExpression* expression);
NumericEvalValue* EvaluateIntegerExpression(BoundExpression* expression);
@@ -34,10 +36,17 @@ class Evaluator {
NumericEvalValue* EvaluateIntegerUnary(BoundUnaryExpression* expression);
BooleanEvalValue *EvaluateBooleanUnary(BoundUnaryExpression *expression);
public:
Evaluator(Script* script){
explicit Evaluator(Script* script){
_scriptData = script;
_result = nullptr;
_evaluationScope = nullptr;
}
~Evaluator(){
delete _result;
delete _evaluationScope;
}
void Evaluate(BoundScriptStatement* statement);
};

View File

@@ -36,4 +36,4 @@ BooleanEvalValue *Evaluator::EvaluateBooleanUnary(BoundUnaryExpression *expressi
case BoundUnaryOperation::Negation:
throw;
}
}
}