#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); } void Evaluator::EvaluateStatement(BoundStatement *statement) { switch (statement->GetKind()){ 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; } } void Evaluator::EvaluateBlockStatement(BoundBlockStatement* statement) { for (auto s: statement->GetStatements()){ this -> EvaluateStatement(s); } } void Evaluator::EvaluateExpressionStatement(BoundExpressionStatement *statement) { // Delete previously saved value. delete this->_scriptData->_lastValue; // Save new value 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()){ case TypeClass ::Number: return this -> EvaluateIntegerExpression(expression); case TypeClass ::Bool: return this -> EvaluateBoolExpression(expression); case TypeClass ::String: return this -> EvaluateStringExpression(expression); default: throw; } } NumericEvalValue* Evaluator::EvaluateIntegerExpression(BoundExpression *expression) { switch (expression->GetKind()){ case BoundExpressionKind ::LiteralInteger: return new IntegerEvalValue(((BoundLiteralIntegerExpression*)expression)->GetValue()); case BoundExpressionKind ::LiteralFloat: return new FloatEvalValue(((BoundLiteralFloatExpression*)expression)->GetValue()); case BoundExpressionKind::Unary: return this -> EvaluateIntegerUnary((BoundUnaryExpression*)expression); case BoundExpressionKind ::Binary: return this -> EvaluateIntegerBinary((BoundBinaryExpression*)expression); case BoundExpressionKind ::LiteralString: case BoundExpressionKind ::LiteralBool: case BoundExpressionKind ::Bad: throw; } } BooleanEvalValue* Evaluator::EvaluateBoolExpression(BoundExpression *expression) { switch (expression->GetKind()) { case BoundExpressionKind::LiteralBool: return new BooleanEvalValue(((BoundLiteralBoolExpression*)expression)->GetValue()); case BoundExpressionKind::Unary: return this -> EvaluateBooleanUnary((BoundUnaryExpression*)expression); case BoundExpressionKind::Binary: return this -> EvaluateBooleanBinary((BoundBinaryExpression*)expression); case BoundExpressionKind::Bad: case BoundExpressionKind::LiteralInteger: case BoundExpressionKind::LiteralFloat: case BoundExpressionKind::LiteralString: throw; } } StringEvalValue* Evaluator::EvaluateStringExpression(BoundExpression *expression) { switch (expression->GetKind()) { case BoundExpressionKind::LiteralString: return new StringEvalValue(((BoundLiteralStringExpression*)expression)->GetValue()); case BoundExpressionKind::Binary: return this -> EvaluateStringBinary((BoundBinaryExpression*)expression); case BoundExpressionKind::Bad: case BoundExpressionKind::LiteralInteger: case BoundExpressionKind::LiteralFloat: case BoundExpressionKind::LiteralBool: case BoundExpressionKind::Unary: throw; }}