#include #include #include "Evaluator.hpp" #include "EvaluationException.hpp" #include "../Script.hpp" #include "EvaluationScope/EvaluationScope.hpp" #include "EvalValues/ScriptFunctionEvalValue.hpp" using namespace std; void Evaluator::Evaluate(BoundScriptStatement *statement) { this->_evaluationScope = new EvaluationScope(this->_scriptData->_scriptVariables, statement->GetDeepestScope()); EvaluateBlockStatement(statement); } void Evaluator::EvaluateStatement(BoundStatement *statement) { if (this->_hasReturned) return; 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 ::FunctionDeclaration: return this->EvaluateFunctionDeclarationStatement((BoundFunctionDeclarationStatement*)statement); case BoundStatementKind::Return: return this -> EvaluateReturnStatement((BoundReturnStatement*)statement); case BoundStatementKind::Conditional: return this -> EvaluateConditionalStatement((BoundConditionalStatement*)statement); case BoundStatementKind::Bad: throw; } } void Evaluator::EvaluateBlockStatement(BoundBlockStatement* statement) { this->_evaluationScope->OuterScope(); for (auto s: statement->GetStatements()){ this -> EvaluateStatement(s); if (this->_hasReturned) break; } this->_evaluationScope->InnerScope(); } void Evaluator::EvaluateExpressionStatement(BoundExpressionStatement *statement) { // Save new value this->_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); } } void Evaluator::EvaluateFunctionDeclarationStatement(BoundFunctionDeclarationStatement *statement) { auto type = statement->GetType(); auto key = statement->GetKey(); auto block = statement->GetBlock(); auto value = make_shared(block, type); if (key->IsCreation()){ this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), value); } else{ this->_evaluationScope->SetVariable(key->GetScopeId(), key->GetIdentifier(), value); } } void Evaluator::EvaluateReturnStatement(BoundReturnStatement* statement){ auto expression = statement->GetExpression(); this->_hasReturned = true; if (expression == nullptr){ return; } auto value = this -> EvaluateExpression(expression); this -> _returnValue = value; } void Evaluator::EvaluateConditionalStatement(BoundConditionalStatement *statement) { auto condition = statement->GetCondition(); if (EvaluateBoolExpression(condition) -> EvaluateBool()){ this -> EvaluateStatement(statement->GetBlock()); } else{ auto elseStatement = statement -> GetElseStatement(); if (elseStatement != nullptr){ this->EvaluateStatement(elseStatement); } } } shared_ptr 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); case TypeClass ::Function: return this->EvaluateFunctionExpression(expression); case TypeClass ::Nil: return this->EvaluateNilExpression(expression); default: throw; } } shared_ptr Evaluator::GetVariable(BoundVariableExpression* expression){ return this->_evaluationScope->GetVariable(expression->GetScope(), expression->GetId())->Clone(); } shared_ptr Evaluator::EvaluateIntegerExpression(BoundExpression *expression) { switch (expression->GetKind()){ case BoundExpressionKind ::LiteralInteger: return make_shared(((BoundLiteralIntegerExpression*)expression)->GetValue()); case BoundExpressionKind ::LiteralFloat: return make_shared(((BoundLiteralFloatExpression*)expression)->GetValue()); case BoundExpressionKind::Unary: return this -> EvaluateIntegerUnary((BoundUnaryExpression*)expression); case BoundExpressionKind ::Binary: return this -> EvaluateIntegerBinary((BoundBinaryExpression*)expression); case BoundExpressionKind::Variable: return dynamic_pointer_cast(this->GetVariable((BoundVariableExpression*)expression)); case BoundExpressionKind ::FunctionCall: return dynamic_pointer_cast(this->EvaluateFunctionCallExpression(expression)); case BoundExpressionKind ::Index: return dynamic_pointer_cast(this->EvaluateIndexExpression(expression)); case BoundExpressionKind ::LiteralString: case BoundExpressionKind ::LiteralBool: case BoundExpressionKind ::Bad: throw; } } shared_ptr Evaluator::EvaluateBoolExpression(BoundExpression *expression) { switch (expression->GetKind()) { case BoundExpressionKind ::LiteralBool: return make_shared(((BoundLiteralBoolExpression*)expression)->GetValue()); case BoundExpressionKind::Unary: return this -> EvaluateBooleanUnary((BoundUnaryExpression*)expression); case BoundExpressionKind::Binary: return this -> EvaluateBooleanBinary((BoundBinaryExpression*)expression); case BoundExpressionKind::Variable: return dynamic_pointer_cast(this->GetVariable((BoundVariableExpression*)expression)); case BoundExpressionKind ::FunctionCall: return dynamic_pointer_cast(this->EvaluateFunctionCallExpression(expression)); case BoundExpressionKind ::Index: return dynamic_pointer_cast(this->EvaluateIndexExpression(expression)); case BoundExpressionKind::Bad: case BoundExpressionKind::LiteralInteger: case BoundExpressionKind::LiteralFloat: case BoundExpressionKind::LiteralString: throw; } } shared_ptr Evaluator::EvaluateStringExpression(BoundExpression *expression) { switch (expression->GetKind()) { case BoundExpressionKind ::LiteralString: return make_shared(((BoundLiteralStringExpression*)expression)->GetValue()); case BoundExpressionKind::Binary: return this -> EvaluateStringBinary((BoundBinaryExpression*)expression); case BoundExpressionKind::Variable: return dynamic_pointer_cast(this->GetVariable((BoundVariableExpression*)expression)); case BoundExpressionKind ::FunctionCall: return dynamic_pointer_cast(this->EvaluateFunctionCallExpression(expression)); case BoundExpressionKind ::Index: return dynamic_pointer_cast(this->EvaluateIndexExpression(expression)); case BoundExpressionKind::Bad: case BoundExpressionKind::LiteralInteger: case BoundExpressionKind::LiteralFloat: case BoundExpressionKind::LiteralBool: case BoundExpressionKind::Unary: throw; } } shared_ptr Evaluator::EvaluateFunctionExpression(BoundExpression * expression){ switch (expression->GetKind()){ case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression); default: throw; } } shared_ptr Evaluator::EvaluateNilExpression(BoundExpression * expression){ switch (expression->GetKind()){ case BoundExpressionKind ::FunctionCall: return this->EvaluateFunctionCallExpression(expression); default: return nullptr; } } shared_ptr Evaluator::EvaluateFunctionCallExpression(BoundExpression* expression){ auto functionCall = (BoundFunctionCallExpression*)expression; auto function = dynamic_pointer_cast(this->EvaluateExpression(functionCall->GetFunctionExpression())); auto boundParameters = functionCall->GetParameters(); auto parameters = vector>(boundParameters.size()); for (int i = 0; i < boundParameters.size(); i++){ parameters[i] = this->EvaluateExpression(boundParameters[i]); } auto type = std::dynamic_pointer_cast(function->GetType()); auto parameterTypes = type->GetParameterTypes(); auto parameterKeys = type->GetParameterKeys(); for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++){ auto parameter = parameters[i]; auto requiredType = parameterTypes.at(i); if (*parameter->GetType() != requiredType.get()){ throw EvaluationException("Passed wrong type to function."); } auto key = parameterKeys.at(i); this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone()); } this->EvaluateBlockStatement(function->GetInnerBlock().get()); this->_hasReturned = false; auto r = this -> _returnValue; this -> _returnValue = nullptr; return r; } shared_ptr Evaluator::EvaluateFunction(ScriptFunctionEvalValue *function, vector parameters) { auto type = std::dynamic_pointer_cast(function->GetType()); auto parameterTypes = type->GetParameterTypes(); auto parameterKeys = type->GetParameterKeys(); for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++){ auto parameter = parameters[i]; auto requiredType = parameterTypes.at(i); if (*parameter->GetType() != requiredType.get()){ throw EvaluationException("Passed wrong type to function."); } auto key = parameterKeys.at(i); this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone()); } this->EvaluateBlockStatement(function->GetInnerBlock().get()); this->_hasReturned = false; auto r = this -> _returnValue; this -> _returnValue = nullptr; return r; } shared_ptr Evaluator::EvaluateIndexExpression(BoundExpression *expression) { auto indexExpression = (BoundIndexExpression*)expression; auto index = this -> EvaluateExpression(indexExpression->GetIndexExpression()); auto indexable = this -> EvaluateExpression(indexExpression->GetIndexableExpression()); return shared_ptr(indexable -> IndexValue(index.get())); }