2019-06-13 14:26:10 +00:00
|
|
|
|
2019-06-05 15:46:46 +00:00
|
|
|
#include <utility>
|
2019-06-01 19:38:39 +00:00
|
|
|
#include <memory>
|
2019-05-23 16:50:09 +00:00
|
|
|
#include "Evaluator.hpp"
|
|
|
|
#include "EvaluationException.hpp"
|
|
|
|
#include "../Script.hpp"
|
2019-05-29 12:55:03 +00:00
|
|
|
#include "EvaluationScope/EvaluationScope.hpp"
|
2019-06-01 10:33:52 +00:00
|
|
|
#include "EvalValues/ScriptFunctionEvalValue.hpp"
|
2019-06-09 18:15:09 +00:00
|
|
|
#include "EvalValues/TableEvalValue.hpp"
|
2019-06-12 13:19:28 +00:00
|
|
|
#include "../Binder/BoundExpressions/BoundTableExpression.hpp"
|
|
|
|
#include "../TableScriptType.hpp"
|
2019-06-21 15:03:13 +00:00
|
|
|
#include "../UserData/UserDataFunction.hpp"
|
2019-06-23 13:58:14 +00:00
|
|
|
#include "../Utilities/StringUtils.hpp"
|
2019-06-27 12:02:31 +00:00
|
|
|
#include "EvalValues/NumericalTableEvalValue.hpp"
|
2019-05-23 16:50:09 +00:00
|
|
|
|
2019-06-01 19:38:39 +00:00
|
|
|
using namespace std;
|
2019-06-17 16:35:12 +00:00
|
|
|
using namespace Porygon::Binder;
|
|
|
|
|
|
|
|
namespace Porygon::Evaluation {
|
|
|
|
EvalValue *Evaluator::Evaluate(const BoundScriptStatement *statement) {
|
|
|
|
this->_evaluationScope = make_shared<EvaluationScope>(this->_scriptVariables,
|
|
|
|
statement->GetLocalVariableCount());
|
|
|
|
EvaluateBlockStatement(statement);
|
|
|
|
return this->_returnValue.get();
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
void Evaluator::EvaluateStatement(const BoundStatement *statement) {
|
2019-06-07 13:23:13 +00:00
|
|
|
if (this->_hasReturned)
|
2019-06-17 16:35:12 +00:00
|
|
|
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);
|
2019-06-22 15:35:33 +00:00
|
|
|
case BoundStatementKind::NumericalFor:
|
|
|
|
return this->EvaluateNumericalForStatement((BoundNumericalForStatement*)statement);
|
2019-06-26 14:19:34 +00:00
|
|
|
case BoundStatementKind::GenericFor:
|
|
|
|
return this-> EvaluateGenericForStatement((BoundGenericForStatement*)statement);
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
case BoundStatementKind::Bad:
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Evaluator::EvaluateBlockStatement(const BoundBlockStatement *statement) {
|
|
|
|
for (auto s: *statement->GetStatements()) {
|
|
|
|
this->EvaluateStatement(s);
|
|
|
|
if (this->_hasReturned)
|
|
|
|
break;
|
|
|
|
}
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
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);
|
2019-05-29 12:55:03 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
void Evaluator::EvaluateFunctionDeclarationStatement(const BoundFunctionDeclarationStatement *statement) {
|
|
|
|
auto type = statement->GetType();
|
|
|
|
auto key = statement->GetKey();
|
|
|
|
auto block = statement->GetBlock();
|
|
|
|
auto value = make_shared<ScriptFunctionEvalValue>(block, this->_evaluationScope, type);
|
|
|
|
if (key->IsCreation()) {
|
|
|
|
this->_evaluationScope->CreateVariable(key, value);
|
|
|
|
} else {
|
|
|
|
this->_evaluationScope->SetVariable(key, value);
|
|
|
|
}
|
2019-06-01 10:33:52 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
void Evaluator::EvaluateReturnStatement(const BoundReturnStatement *statement) {
|
|
|
|
auto expression = statement->GetExpression();
|
|
|
|
if (expression == nullptr) {
|
|
|
|
this->_hasReturned = true;
|
2019-06-19 14:21:21 +00:00
|
|
|
this -> _returnValue = nullptr;
|
2019-06-17 16:35:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto value = this->EvaluateExpression(expression);
|
2019-06-12 16:45:47 +00:00
|
|
|
this->_hasReturned = true;
|
2019-06-17 16:35:12 +00:00
|
|
|
this->_returnValue = value;
|
2019-06-07 13:23:13 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2019-06-08 12:25:15 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
2019-06-22 15:35:33 +00:00
|
|
|
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();
|
2019-06-23 10:58:01 +00:00
|
|
|
if (step >= 0){
|
2019-06-22 15:35:33 +00:00
|
|
|
for (long i = start; i <= end; i += step){
|
|
|
|
this -> _evaluationScope -> SetVariable(identifier, make_shared<IntegerEvalValue>(i));
|
|
|
|
for (auto s: *block->GetStatements()) {
|
|
|
|
this->EvaluateStatement(s);
|
|
|
|
if (this->_hasReturned)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else{
|
|
|
|
for (long i = start; i >= end; i += step){
|
|
|
|
this -> _evaluationScope -> SetVariable(identifier, make_shared<IntegerEvalValue>(i));
|
|
|
|
for (auto s: *block->GetStatements()) {
|
|
|
|
this->EvaluateStatement(s);
|
|
|
|
if (this->_hasReturned)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:19:34 +00:00
|
|
|
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();
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
this -> EvaluateBlockStatement(block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////
|
|
|
|
// Expressions //
|
|
|
|
/////////////////
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
const shared_ptr<EvalValue> 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;
|
|
|
|
}
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
const shared_ptr<EvalValue> Evaluator::GetVariable(const BoundVariableExpression *expression) {
|
|
|
|
auto variable = this->_evaluationScope->GetVariable(expression->GetKey());
|
|
|
|
if (variable == nullptr) {
|
|
|
|
throw EvaluationException("Variable not found");
|
|
|
|
}
|
|
|
|
return variable->Clone();
|
2019-06-13 13:16:41 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
const shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(const BoundExpression *expression) {
|
|
|
|
switch (expression->GetKind()) {
|
|
|
|
case BoundExpressionKind::LiteralInteger:
|
|
|
|
return make_shared<IntegerEvalValue>(((BoundLiteralIntegerExpression *) expression)->GetValue());
|
|
|
|
case BoundExpressionKind::LiteralFloat:
|
|
|
|
return make_shared<FloatEvalValue>(((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<NumericEvalValue>(
|
|
|
|
this->GetVariable((BoundVariableExpression *) expression));
|
|
|
|
case BoundExpressionKind::FunctionCall:
|
|
|
|
return dynamic_pointer_cast<NumericEvalValue>(this->EvaluateFunctionCallExpression(expression));
|
|
|
|
case BoundExpressionKind::Index:
|
|
|
|
return dynamic_pointer_cast<NumericEvalValue>(this->EvaluateIndexExpression(expression));
|
|
|
|
case BoundExpressionKind::PeriodIndex:
|
|
|
|
return dynamic_pointer_cast<NumericEvalValue>(this->EvaluatePeriodIndexExpression(expression));
|
|
|
|
|
|
|
|
case BoundExpressionKind::LiteralString:
|
|
|
|
case BoundExpressionKind::LiteralBool:
|
|
|
|
case BoundExpressionKind::Bad:
|
|
|
|
case BoundExpressionKind::NumericalTable:
|
|
|
|
case BoundExpressionKind::Table:
|
|
|
|
throw;
|
|
|
|
}
|
2019-05-24 13:31:11 +00:00
|
|
|
}
|
2019-05-25 11:30:20 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
const shared_ptr<BooleanEvalValue> Evaluator::EvaluateBoolExpression(const BoundExpression *expression) {
|
|
|
|
switch (expression->GetKind()) {
|
|
|
|
case BoundExpressionKind::LiteralBool:
|
|
|
|
return make_shared<BooleanEvalValue>(((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<BooleanEvalValue>(
|
|
|
|
this->GetVariable((BoundVariableExpression *) expression));
|
|
|
|
case BoundExpressionKind::FunctionCall:
|
|
|
|
return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluateFunctionCallExpression(expression));
|
|
|
|
case BoundExpressionKind::Index:
|
|
|
|
return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluateIndexExpression(expression));
|
|
|
|
case BoundExpressionKind::PeriodIndex:
|
|
|
|
return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluatePeriodIndexExpression(expression));
|
|
|
|
|
|
|
|
case BoundExpressionKind::Bad:
|
|
|
|
case BoundExpressionKind::LiteralInteger:
|
|
|
|
case BoundExpressionKind::LiteralFloat:
|
|
|
|
case BoundExpressionKind::LiteralString:
|
|
|
|
case BoundExpressionKind::NumericalTable:
|
|
|
|
case BoundExpressionKind::Table:
|
|
|
|
throw;
|
|
|
|
|
|
|
|
}
|
2019-05-25 11:30:20 +00:00
|
|
|
}
|
2019-05-25 14:15:20 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
const shared_ptr<StringEvalValue> Evaluator::EvaluateStringExpression(const BoundExpression *expression) {
|
|
|
|
switch (expression->GetKind()) {
|
|
|
|
case BoundExpressionKind::LiteralString:
|
|
|
|
return make_shared<StringEvalValue>(((BoundLiteralStringExpression *) expression)->GetValue());
|
|
|
|
case BoundExpressionKind::Binary:
|
|
|
|
return this->EvaluateStringBinary((BoundBinaryExpression *) expression);
|
|
|
|
case BoundExpressionKind::Variable:
|
|
|
|
return dynamic_pointer_cast<StringEvalValue>(this->GetVariable((BoundVariableExpression *) expression));
|
|
|
|
case BoundExpressionKind::FunctionCall:
|
|
|
|
return dynamic_pointer_cast<StringEvalValue>(this->EvaluateFunctionCallExpression(expression));
|
|
|
|
case BoundExpressionKind::Index:
|
|
|
|
return dynamic_pointer_cast<StringEvalValue>(this->EvaluateIndexExpression(expression));
|
|
|
|
case BoundExpressionKind::PeriodIndex:
|
|
|
|
return dynamic_pointer_cast<StringEvalValue>(this->EvaluatePeriodIndexExpression(expression));
|
|
|
|
|
|
|
|
case BoundExpressionKind::Bad:
|
|
|
|
case BoundExpressionKind::LiteralInteger:
|
|
|
|
case BoundExpressionKind::LiteralFloat:
|
|
|
|
case BoundExpressionKind::LiteralBool:
|
|
|
|
case BoundExpressionKind::Unary:
|
|
|
|
case BoundExpressionKind::NumericalTable:
|
|
|
|
case BoundExpressionKind::Table:
|
|
|
|
throw;
|
|
|
|
|
|
|
|
}
|
2019-06-01 17:20:31 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
const shared_ptr<EvalValue> 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;
|
|
|
|
}
|
2019-06-01 17:20:31 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
const shared_ptr<EvalValue> Evaluator::EvaluateNilExpression(const BoundExpression *expression) {
|
|
|
|
switch (expression->GetKind()) {
|
|
|
|
case BoundExpressionKind::FunctionCall:
|
|
|
|
return this->EvaluateFunctionCallExpression(expression);
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-06-01 17:20:31 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
const shared_ptr<EvalValue> 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;
|
|
|
|
}
|
2019-06-09 18:15:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
const shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(const BoundExpression *expression) {
|
|
|
|
auto functionCall = (BoundFunctionCallExpression *) expression;
|
2019-06-21 15:03:13 +00:00
|
|
|
auto function = dynamic_pointer_cast<GenericFunctionEvalValue>(
|
2019-06-17 16:35:12 +00:00
|
|
|
this->EvaluateExpression(functionCall->GetFunctionExpression()));
|
2019-06-01 17:20:31 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
auto boundParameters = functionCall->GetParameters();
|
|
|
|
auto parameters = vector<shared_ptr<EvalValue>>(boundParameters->size());
|
|
|
|
for (int i = 0; i < boundParameters->size(); i++) {
|
|
|
|
parameters[i] = this->EvaluateExpression(boundParameters->at(i));
|
|
|
|
}
|
2019-06-01 17:20:31 +00:00
|
|
|
|
2019-06-21 15:03:13 +00:00
|
|
|
auto type = std::dynamic_pointer_cast<GenericFunctionScriptType>(function->GetType());
|
|
|
|
if (type -> IsScriptFunction()){
|
2019-06-21 21:07:17 +00:00
|
|
|
auto parameterTypes = type->GetParameterTypes();
|
2019-06-21 15:03:13 +00:00
|
|
|
auto scriptFunctionType = std::dynamic_pointer_cast<FunctionScriptType>(type);
|
|
|
|
auto parameterKeys = scriptFunctionType->GetParameterKeys();
|
|
|
|
auto originalScope = this->_evaluationScope;
|
|
|
|
auto scriptFunction = dynamic_pointer_cast<ScriptFunctionEvalValue>(function);
|
|
|
|
this->_evaluationScope = scriptFunction->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(scriptFunction->GetInnerBlock().get());
|
|
|
|
this->_evaluationScope = originalScope;
|
|
|
|
|
|
|
|
this->_hasReturned = false;
|
|
|
|
auto r = this->_returnValue;
|
|
|
|
this->_returnValue = nullptr;
|
|
|
|
return r;
|
|
|
|
} else{
|
|
|
|
auto userDataFunction = dynamic_pointer_cast<UserData::UserDataFunction>(function);
|
|
|
|
EvalValue* arr[parameters.size()];
|
|
|
|
for (int i = 0; i < parameters.size(); i++){
|
|
|
|
arr[i] = parameters[i].get();
|
|
|
|
}
|
|
|
|
return shared_ptr<EvalValue>(userDataFunction -> Call(arr, parameters.size()));
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
2019-06-01 17:20:31 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
const shared_ptr<EvalValue> Evaluator::EvaluateFunction(const ScriptFunctionEvalValue *function,
|
|
|
|
const vector<EvalValue *> ¶meters) {
|
|
|
|
auto type = std::dynamic_pointer_cast<FunctionScriptType>(function->GetType());
|
|
|
|
auto parameterTypes = type->GetParameterTypes();
|
|
|
|
auto parameterKeys = type->GetParameterKeys();
|
2019-06-08 14:44:47 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
auto originalScope = this->_evaluationScope;
|
|
|
|
this->_evaluationScope = function->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(function->GetInnerBlock().get());
|
|
|
|
this->_evaluationScope = originalScope;
|
|
|
|
this->_hasReturned = false;
|
|
|
|
auto r = this->_returnValue;
|
|
|
|
this->_returnValue = nullptr;
|
|
|
|
return r;
|
2019-06-01 17:20:31 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
const shared_ptr<EvalValue> 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())->Clone();
|
2019-06-05 16:44:23 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
const shared_ptr<EvalValue> 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();
|
2019-06-09 18:15:09 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
const shared_ptr<EvalValue> Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) {
|
|
|
|
auto tableExpression = (BoundNumericalTableExpression *) expression;
|
|
|
|
auto valueExpressions = tableExpression->GetExpressions();
|
2019-06-27 12:02:31 +00:00
|
|
|
auto values = new vector<shared_ptr<EvalValue>>(valueExpressions->size());
|
2019-06-17 16:35:12 +00:00
|
|
|
for (int i = 0; i < valueExpressions->size(); i++) {
|
|
|
|
auto val = this->EvaluateExpression(valueExpressions->at(i));
|
2019-06-27 12:02:31 +00:00
|
|
|
values->at(i) = val;
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
2019-06-27 12:02:31 +00:00
|
|
|
auto valuesPointer = shared_ptr<vector<shared_ptr<EvalValue>>>(values);
|
|
|
|
return make_shared<NumericalTableEvalValue>(valuesPointer);
|
2019-06-12 13:19:28 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
const shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) {
|
|
|
|
auto tableExpression = (BoundTableExpression *) expression;
|
|
|
|
auto type = dynamic_pointer_cast<TableScriptType>(tableExpression->GetType());
|
|
|
|
auto declaredVars = type->GetValues();
|
2019-06-23 13:58:14 +00:00
|
|
|
auto variables = make_shared<map<Utilities::HashedString, shared_ptr<EvalValue>>>();
|
2019-06-17 16:35:12 +00:00
|
|
|
for (auto i : *declaredVars) {
|
|
|
|
variables->insert({i.first, nullptr});
|
|
|
|
}
|
|
|
|
auto evaluator = make_shared<EvaluationScope>(variables.get(), type->GetLocalVariableCount());
|
|
|
|
auto currentEvaluator = this->_evaluationScope;
|
|
|
|
this->_evaluationScope = evaluator;
|
|
|
|
this->EvaluateBlockStatement(tableExpression->GetBlock());
|
|
|
|
this->_evaluationScope = currentEvaluator;
|
|
|
|
return make_shared<TableEvalValue>(variables);
|
2019-06-14 12:59:38 +00:00
|
|
|
}
|
2019-06-14 15:12:27 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
const shared_ptr<EvalValue> 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|