#include #include "Evaluator.hpp" #include "EvaluationException.hpp" #include "EvaluationScope/EvaluationScope.hpp" #include "EvalValues/ScriptFunctionEvalValue.hpp" #include "EvalValues/TableEvalValue.hpp" #include "../Binder/BoundExpressions/BoundTableExpression.hpp" #include "../Binder/BoundExpressions/BoundFunctionCallExpression.hpp" #include "../TableScriptType.hpp" #include "../UserData/UserDataFunction.hpp" #include "EvalValues/NumericalTableEvalValue.hpp" #include "../Utilities/Random.hpp" using namespace std; using namespace Porygon::Binder; namespace Porygon::Evaluation { const EvalValue *Evaluator::Evaluate(const BoundScriptStatement *statement) { this->_evaluationScope = make_shared(this->_scriptVariables); EvaluateBlockStatement(statement); return this->_returnValue.get(); } void Evaluator::EvaluateStatement(const 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::IndexAssignment: return this->EvaluateIndexAssignmentStatement((BoundIndexAssignmentStatement *) 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::NumericalFor: return this->EvaluateNumericalForStatement((BoundNumericalForStatement*)statement); case BoundStatementKind::GenericFor: return this-> EvaluateGenericForStatement((BoundGenericForStatement*)statement); case BoundStatementKind::While: return this-> EvaluateWhileStatement((BoundWhileStatement*)statement); case BoundStatementKind::Break: this -> _hasBroken = true; return; case BoundStatementKind::Bad: throw; } throw EvaluationException("Evaluating this statement is not supported"); } void Evaluator::EvaluateBlockStatement(const BoundBlockStatement *statement) { for (auto s: *statement->GetStatements()) { this->EvaluateStatement(s); if (this->_hasReturned) break; } } void Evaluator::EvaluateExpressionStatement(const BoundExpressionStatement *statement) { // Save new value this->_lastValue = this->EvaluateExpression(statement->GetExpression()); } void Evaluator::EvaluateAssignmentStatement(const BoundAssignmentStatement *statement) { auto value = this->EvaluateExpression(statement->GetExpression()); auto key = statement->GetKey(); if (key->IsCreation()) { this->_evaluationScope->CreateVariable(key, value); } else { this->_evaluationScope->SetVariable(key, value); } } void Evaluator::EvaluateIndexAssignmentStatement(const BoundIndexAssignmentStatement *statement) { auto indexExpression = statement->GetIndexExpression(); auto value = this->EvaluateExpression(statement->GetValueExpression()); auto index = ((BoundIndexExpression *) indexExpression); auto table = this->EvaluateExpression(index->GetIndexableExpression()); auto key = this->EvaluateExpression(index->GetIndexExpression()); table->SetIndexValue(key.get(), value); } void Evaluator::EvaluateFunctionDeclarationStatement(const BoundFunctionDeclarationStatement *statement) { auto type = statement->GetType(); auto key = statement->GetKey(); auto block = statement->GetBlock(); auto option = new Evaluation::EvaluationScriptFunctionOption(block, this->_evaluationScope); if (key->IsCreation()) { auto value = make_shared(type, Utilities::Random::Get()); value->RegisterOption(option); this->_evaluationScope->CreateVariable(key, value); } else { auto var = dynamic_pointer_cast(this -> _evaluationScope ->GetVariable(key)); var->RegisterOption(option); this->_evaluationScope->SetVariable(key, var); } } void Evaluator::EvaluateReturnStatement(const BoundReturnStatement *statement) { auto expression = statement->GetExpression(); if (expression == nullptr) { this->_hasReturned = true; this -> _returnValue = nullptr; return; } auto value = this->EvaluateExpression(expression); this->_hasReturned = true; this->_returnValue = value; } void Evaluator::EvaluateConditionalStatement(const 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); } } } void Evaluator::EvaluateNumericalForStatement(const BoundNumericalForStatement *statement) { long start = this->EvaluateIntegerExpression(statement -> GetStart()) -> EvaluateInteger(); long end = this->EvaluateIntegerExpression(statement -> GetEnd()) -> EvaluateInteger(); long step = 1; auto stepExp = statement -> GetStep(); if (stepExp != nullptr){ step = this -> EvaluateIntegerExpression(stepExp) -> EvaluateInteger(); } auto identifier = statement -> GetIdentifier(); this -> _evaluationScope -> CreateVariable(identifier, nullptr); auto block = (BoundBlockStatement*)statement -> GetBlock(); auto statements = *block -> GetStatements(); if (step >= 0){ for (long i = start; i <= end; i += step){ this -> _evaluationScope -> SetVariable(identifier, make_shared(i)); for (auto s: statements) { this->EvaluateStatement(s); if (this->_hasReturned || this -> _hasBroken) break; } if (this->_hasReturned || this -> _hasBroken) break; } } else{ for (long i = start; i >= end; i += step){ this -> _evaluationScope -> SetVariable(identifier, make_shared(i)); for (auto s: statements) { this->EvaluateStatement(s); if (this->_hasReturned || this -> _hasBroken) break; } if (this->_hasReturned || this -> _hasBroken) break; } } this -> _hasBroken = false; } void Evaluator::EvaluateGenericForStatement(const BoundGenericForStatement *statement) { auto iteratorVal = EvaluateExpression(statement -> GetIterator()); auto iterator = iteratorVal -> GetKeyIterator(); auto keyVariable = statement ->GetKeyIdentifier(); auto valueVariable = statement ->GetValueIdentifier(); this -> _evaluationScope -> CreateVariable(keyVariable, nullptr); if (valueVariable != nullptr) this -> _evaluationScope -> CreateVariable(valueVariable, nullptr); auto block = (BoundBlockStatement*)statement -> GetBlock(); auto statements = *block -> GetStatements(); while (iterator->MoveNext()){ auto currentKey = iterator->GetCurrent(); this -> _evaluationScope -> SetVariable(keyVariable, currentKey); if (valueVariable != nullptr){ auto currentValue = iteratorVal -> IndexValue(currentKey.get()); this -> _evaluationScope -> SetVariable(valueVariable, currentValue); } for (auto s: statements) { this->EvaluateStatement(s); if (this->_hasReturned || this -> _hasBroken) break; } if (this->_hasReturned || this -> _hasBroken) break; } this -> _hasBroken = false; delete iterator; } void Evaluator::EvaluateWhileStatement(const BoundWhileStatement *statement) { auto condition = statement -> GetCondition(); auto block = (BoundBlockStatement*)statement -> GetBlock(); auto statements = *block -> GetStatements(); while (this->EvaluateBoolExpression(condition)->EvaluateBool()){ for (auto s: statements) { this->EvaluateStatement(s); if (this->_hasReturned || this -> _hasBroken) break; } if (this->_hasReturned || this -> _hasBroken) break; } this -> _hasBroken = false; } ///////////////// // Expressions // ///////////////// shared_ptr Evaluator::EvaluateExpression(const 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); case TypeClass::Table: return this->EvaluateTableExpression(expression); case TypeClass::UserData: return this->EvaluateUserDataExpression(expression); default: throw; } } shared_ptr Evaluator::GetVariable(const BoundVariableExpression *expression) { auto variable = this->_evaluationScope->GetVariable(expression->GetKey()); if (variable == nullptr) { throw EvaluationException("Variable not found"); } return variable->Clone(); } shared_ptr Evaluator::EvaluateIntegerExpression(const 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::PeriodIndex: return dynamic_pointer_cast(this->EvaluatePeriodIndexExpression(expression)); default: throw; } } shared_ptr Evaluator::EvaluateBoolExpression(const 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::PeriodIndex: return dynamic_pointer_cast(this->EvaluatePeriodIndexExpression(expression)); default: throw; } } shared_ptr Evaluator::EvaluateStringExpression(const 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::PeriodIndex: return dynamic_pointer_cast(this->EvaluatePeriodIndexExpression(expression)); default: throw; } } shared_ptr Evaluator::EvaluateFunctionExpression(const BoundExpression *expression) { switch (expression->GetKind()) { case BoundExpressionKind::Variable: return this->GetVariable((BoundVariableExpression *) expression); case BoundExpressionKind::Index: return this->EvaluateIndexExpression(expression); case BoundExpressionKind::PeriodIndex: return this->EvaluatePeriodIndexExpression(expression); default: throw; } } shared_ptr Evaluator::EvaluateNilExpression(const BoundExpression *expression) { switch (expression->GetKind()) { case BoundExpressionKind::FunctionCall: return this->EvaluateFunctionCallExpression(expression); default: return nullptr; } } shared_ptr Evaluator::EvaluateTableExpression(const BoundExpression *expression) { switch (expression->GetKind()) { case BoundExpressionKind::FunctionCall: return this->EvaluateFunctionCallExpression(expression); case BoundExpressionKind::Variable: return this->GetVariable((BoundVariableExpression *) expression); case BoundExpressionKind::Index: return this->EvaluateIndexExpression(expression); case BoundExpressionKind::NumericalTable: return this->EvaluateNumericTableExpression(expression); case BoundExpressionKind::Table: return this->EvaluateComplexTableExpression(expression); case BoundExpressionKind::PeriodIndex: return this->EvaluatePeriodIndexExpression(expression); default: throw; } } shared_ptr Evaluator::EvaluateFunctionCallExpression(const 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 (size_t i = 0; i < boundParameters->size(); i++) { parameters[i] = this->EvaluateExpression(boundParameters->at(i)); } auto type = std::dynamic_pointer_cast(function->GetType()); auto func = dynamic_pointer_cast(function); auto option = functionCall ->GetFunctionOption(); auto opt = func->GetOption(option->GetOptionId()); if (option -> IsScriptFunction()){ auto parameterTypes = option->GetParameterTypes(); auto scriptFunctionType = dynamic_cast(option); auto parameterKeys = scriptFunctionType->GetParameterKeys(); auto originalScope = this->_evaluationScope; auto scriptOption = dynamic_pointer_cast(opt); this->_evaluationScope = scriptOption->GetScope(); for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++) { auto parameter = parameters[i]; auto key = parameterKeys.at(i); this->_evaluationScope->CreateVariable(key.get(), parameter->Clone()); } this->EvaluateBlockStatement(scriptOption->GetInnerBlock().get()); this->_evaluationScope = originalScope; this->_hasReturned = false; auto r = this->_returnValue; this->_returnValue = nullptr; return r; } else{ auto scriptOption = dynamic_pointer_cast(opt); const EvalValue* arr[parameters.size()]; for (size_t i = 0; i < parameters.size(); i++){ arr[i] = parameters[i].get(); } return shared_ptr(scriptOption -> Call(arr, parameters.size())); } } shared_ptr Evaluator::EvaluateFunction(const GenericFunctionEvalValue *function, const vector ¶meters) { auto type = std::dynamic_pointer_cast(function->GetType()); auto option = dynamic_cast(type->GetFirstOption()); auto parameterTypes = option->GetParameterTypes(); auto parameterKeys = option->GetParameterKeys(); auto originalScope = this->_evaluationScope; auto scriptOption = dynamic_pointer_cast(function->GetOption(0)); this->_evaluationScope = scriptOption->GetScope(); for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++) { auto parameter = parameters[i]; auto key = parameterKeys.at(i); this->_evaluationScope->CreateVariable(key.get(), parameter->Clone()); } this->EvaluateBlockStatement(scriptOption->GetInnerBlock().get()); this->_evaluationScope = originalScope; this->_hasReturned = false; auto r = this->_returnValue; this->_returnValue = nullptr; return r; } shared_ptr Evaluator::EvaluateIndexExpression(const BoundExpression *expression) { auto indexExpression = (BoundIndexExpression *) expression; auto index = this->EvaluateExpression(indexExpression->GetIndexExpression()); auto indexable = this->EvaluateExpression(indexExpression->GetIndexableExpression()); return indexable->IndexValue(index.get()); } shared_ptr Evaluator::EvaluatePeriodIndexExpression(const BoundExpression *expression) { auto indexExpression = (BoundPeriodIndexExpression *) expression; auto index = indexExpression->GetIndex()->GetHash(); auto indexable = this->EvaluateExpression(indexExpression->GetIndexableExpression()); return indexable->IndexValue(index)->Clone(); } shared_ptr Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) { auto tableExpression = (BoundNumericalTableExpression *) expression; auto valueExpressions = tableExpression->GetExpressions(); auto values = new vector>(valueExpressions->size()); for (size_t i = 0; i < valueExpressions->size(); i++) { auto val = this->EvaluateExpression(valueExpressions->at(i)); values->at(i) = val; } auto valuesPointer = shared_ptr>>(values); return make_shared(valuesPointer); } shared_ptr Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) { auto tableExpression = (BoundTableExpression *) expression; auto type = dynamic_pointer_cast(tableExpression->GetType()); auto declaredVars = type->GetValues(); auto variables = make_shared>>(); for (const auto& i : *declaredVars) { variables->insert({i.first, nullptr}); } auto evaluator = make_shared(variables.get()); auto currentEvaluator = this->_evaluationScope; this->_evaluationScope = evaluator; this->EvaluateBlockStatement(tableExpression->GetBlock()); this->_evaluationScope = currentEvaluator; return make_shared(variables); } shared_ptr Evaluator::EvaluateUserDataExpression(const BoundExpression *expression) { switch (expression->GetKind()) { case BoundExpressionKind::Variable: return this->GetVariable((BoundVariableExpression *) expression); case BoundExpressionKind::Index: return this->EvaluateIndexExpression(expression); default: throw; } } }