2019-06-13 14:26:10 +00:00
|
|
|
|
2019-06-01 19:38:39 +00:00
|
|
|
#include <memory>
|
2019-05-23 16:50:09 +00:00
|
|
|
#include "Evaluator.hpp"
|
|
|
|
#include "EvaluationException.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"
|
2019-06-29 17:59:42 +00:00
|
|
|
#include "../Binder/BoundExpressions/BoundFunctionCallExpression.hpp"
|
2019-08-10 14:45:15 +00:00
|
|
|
#include "../Binder/BoundExpressions/BoundRequireExpression.hpp"
|
2019-07-28 10:58:38 +00:00
|
|
|
#include "../ScriptTypes/TableScriptType.hpp"
|
2019-06-21 15:03:13 +00:00
|
|
|
#include "../UserData/UserDataFunction.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 {
|
2019-07-27 15:59:42 +00:00
|
|
|
const EvalValue* Evaluator::Evaluate(const BoundScriptStatement *statement) {
|
2019-07-25 15:23:54 +00:00
|
|
|
this->_evaluationScope = make_shared<EvaluationScope>(this->_scriptVariables);
|
2019-07-27 08:51:24 +00:00
|
|
|
auto statements = statement->GetStatements();
|
|
|
|
if (statements -> size() == 1 && statements->at(0)->GetKind() == BoundStatementKind::Expression){
|
|
|
|
auto expStatement = (BoundExpressionStatement*) statements -> at(0);
|
2019-07-27 15:59:42 +00:00
|
|
|
return this -> EvaluateExpression(expStatement -> GetExpression()).Clone();
|
2019-07-27 08:51:24 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
EvaluateBlockStatement(statement);
|
2019-07-27 15:59:42 +00:00
|
|
|
if (this->_returnValue.Get() == nullptr){
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return this->_returnValue.Clone();
|
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:
|
2019-07-04 16:24:49 +00:00
|
|
|
return this->EvaluateBlockStatement((BoundBlockStatement*)statement);
|
2019-06-17 16:35:12 +00:00
|
|
|
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-28 11:28:39 +00:00
|
|
|
case BoundStatementKind::While:
|
|
|
|
return this-> EvaluateWhileStatement((BoundWhileStatement*)statement);
|
|
|
|
|
2019-06-27 13:55:46 +00:00
|
|
|
case BoundStatementKind::Break:
|
|
|
|
this -> _hasBroken = true;
|
|
|
|
return;
|
2019-06-17 16:35:12 +00:00
|
|
|
case BoundStatementKind::Bad:
|
|
|
|
throw;
|
|
|
|
}
|
2019-06-28 11:28:39 +00:00
|
|
|
throw EvaluationException("Evaluating this statement is not supported");
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2019-07-27 08:51:24 +00:00
|
|
|
this->EvaluateExpression(statement->GetExpression());
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Evaluator::EvaluateAssignmentStatement(const BoundAssignmentStatement *statement) {
|
|
|
|
auto value = this->EvaluateExpression(statement->GetExpression());
|
|
|
|
auto key = statement->GetKey();
|
|
|
|
if (key->IsCreation()) {
|
2019-07-27 15:59:42 +00:00
|
|
|
this->_evaluationScope->CreateVariable(key, value.Clone());
|
2019-06-17 16:35:12 +00:00
|
|
|
} else {
|
2019-07-27 15:59:42 +00:00
|
|
|
this->_evaluationScope->SetVariable(key, value.Clone());
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
2019-07-27 15:59:42 +00:00
|
|
|
table->SetIndexValue(key.Get(), value.Take());
|
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();
|
2019-06-29 17:59:42 +00:00
|
|
|
auto option = new Evaluation::EvaluationScriptFunctionOption(block, this->_evaluationScope);
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
if (key->IsCreation()) {
|
2019-07-27 15:59:42 +00:00
|
|
|
auto p = new GenericFunctionEvalValue(type, Utilities::Random::Get());
|
|
|
|
p->RegisterOption(option);
|
|
|
|
auto value = EvalValuePointer(p);
|
2019-06-17 16:35:12 +00:00
|
|
|
this->_evaluationScope->CreateVariable(key, value);
|
|
|
|
} else {
|
2019-07-27 16:30:05 +00:00
|
|
|
auto var = (GenericFunctionEvalValue*)this -> _evaluationScope ->GetVariable(key).Take();
|
2019-06-29 17:59:42 +00:00
|
|
|
var->RegisterOption(option);
|
2019-07-27 16:30:05 +00:00
|
|
|
delete var;
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
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-07-27 15:59:42 +00:00
|
|
|
this -> _returnValue.ClearAssign(nullptr);
|
2019-06-17 16:35:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-07-27 15:59:42 +00:00
|
|
|
auto value = this->EvaluateExpression(expression).Take();
|
2019-06-12 16:45:47 +00:00
|
|
|
this->_hasReturned = true;
|
2019-07-27 15:59:42 +00:00
|
|
|
this->_returnValue.ClearAssign(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();
|
2019-07-27 15:59:42 +00:00
|
|
|
if (EvaluateExpression(condition)->EvaluateBool()) {
|
2019-06-17 16:35:12 +00:00
|
|
|
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) {
|
2019-07-27 15:59:42 +00:00
|
|
|
long start = this->EvaluateExpression(statement -> GetStart()) -> EvaluateInteger();
|
|
|
|
long end = this->EvaluateExpression(statement -> GetEnd()) -> EvaluateInteger();
|
2019-06-22 15:35:33 +00:00
|
|
|
long step = 1;
|
|
|
|
auto stepExp = statement -> GetStep();
|
|
|
|
if (stepExp != nullptr){
|
2019-07-27 15:59:42 +00:00
|
|
|
step = this -> EvaluateExpression(stepExp) -> EvaluateInteger();
|
2019-06-22 15:35:33 +00:00
|
|
|
}
|
|
|
|
auto identifier = statement -> GetIdentifier();
|
|
|
|
this -> _evaluationScope -> CreateVariable(identifier, nullptr);
|
|
|
|
auto block = (BoundBlockStatement*)statement -> GetBlock();
|
2019-06-28 11:28:39 +00:00
|
|
|
auto statements = *block -> GetStatements();
|
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){
|
2019-07-27 15:59:42 +00:00
|
|
|
this -> _evaluationScope -> SetVariable(identifier, new IntegerEvalValue(i));
|
2019-06-28 11:28:39 +00:00
|
|
|
for (auto s: statements) {
|
2019-06-22 15:35:33 +00:00
|
|
|
this->EvaluateStatement(s);
|
2019-06-27 13:55:46 +00:00
|
|
|
if (this->_hasReturned || this -> _hasBroken)
|
2019-06-22 15:35:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-06-27 13:55:46 +00:00
|
|
|
if (this->_hasReturned || this -> _hasBroken)
|
|
|
|
break;
|
2019-06-22 15:35:33 +00:00
|
|
|
}
|
|
|
|
} else{
|
|
|
|
for (long i = start; i >= end; i += step){
|
2019-07-27 15:59:42 +00:00
|
|
|
this -> _evaluationScope -> SetVariable(identifier, new IntegerEvalValue(i));
|
2019-06-28 11:28:39 +00:00
|
|
|
for (auto s: statements) {
|
2019-06-22 15:35:33 +00:00
|
|
|
this->EvaluateStatement(s);
|
2019-06-27 13:55:46 +00:00
|
|
|
if (this->_hasReturned || this -> _hasBroken)
|
2019-06-22 15:35:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-06-27 13:55:46 +00:00
|
|
|
if (this->_hasReturned || this -> _hasBroken)
|
|
|
|
break;
|
2019-06-22 15:35:33 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-27 13:55:46 +00:00
|
|
|
this -> _hasBroken = false;
|
2019-06-22 15:35:33 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2019-06-28 11:28:39 +00:00
|
|
|
auto statements = *block -> GetStatements();
|
2019-06-26 14:19:34 +00:00
|
|
|
while (iterator->MoveNext()){
|
2019-07-27 15:59:42 +00:00
|
|
|
auto currentKey = EvalValuePointer(iterator->GetCurrent());
|
2019-06-26 14:19:34 +00:00
|
|
|
this -> _evaluationScope -> SetVariable(keyVariable, currentKey);
|
|
|
|
if (valueVariable != nullptr){
|
2019-07-27 15:59:42 +00:00
|
|
|
auto currentValue = EvalValuePointer(iteratorVal -> IndexValue(currentKey.Get()));
|
2019-06-26 14:19:34 +00:00
|
|
|
this -> _evaluationScope -> SetVariable(valueVariable, currentValue);
|
|
|
|
}
|
2019-06-28 11:28:39 +00:00
|
|
|
for (auto s: statements) {
|
2019-06-27 13:55:46 +00:00
|
|
|
this->EvaluateStatement(s);
|
|
|
|
if (this->_hasReturned || this -> _hasBroken)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (this->_hasReturned || this -> _hasBroken)
|
|
|
|
break;
|
2019-06-26 14:19:34 +00:00
|
|
|
}
|
2019-06-27 13:55:46 +00:00
|
|
|
this -> _hasBroken = false;
|
2019-06-27 12:27:31 +00:00
|
|
|
delete iterator;
|
2019-06-26 14:19:34 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 11:28:39 +00:00
|
|
|
void Evaluator::EvaluateWhileStatement(const BoundWhileStatement *statement) {
|
|
|
|
auto condition = statement -> GetCondition();
|
|
|
|
auto block = (BoundBlockStatement*)statement -> GetBlock();
|
|
|
|
auto statements = *block -> GetStatements();
|
2019-07-27 15:59:42 +00:00
|
|
|
while (this->EvaluateExpression(condition)->EvaluateBool()){
|
2019-06-28 11:28:39 +00:00
|
|
|
for (auto s: statements) {
|
|
|
|
this->EvaluateStatement(s);
|
|
|
|
if (this->_hasReturned || this -> _hasBroken)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (this->_hasReturned || this -> _hasBroken)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this -> _hasBroken = false;
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:19:34 +00:00
|
|
|
/////////////////
|
|
|
|
// Expressions //
|
|
|
|
/////////////////
|
|
|
|
|
2019-07-27 15:59:42 +00:00
|
|
|
EvalValuePointer Evaluator::EvaluateExpression(const BoundExpression *expression) {
|
|
|
|
switch (expression->GetKind()){
|
2019-06-17 16:35:12 +00:00
|
|
|
|
2019-07-27 15:59:42 +00:00
|
|
|
case BoundExpressionKind::Bad: throw;
|
2019-06-17 16:35:12 +00:00
|
|
|
case BoundExpressionKind::LiteralInteger:
|
2019-07-27 15:59:42 +00:00
|
|
|
return new IntegerEvalValue(((BoundLiteralIntegerExpression *) expression)->GetValue());
|
2019-06-17 16:35:12 +00:00
|
|
|
case BoundExpressionKind::LiteralFloat:
|
2019-07-27 15:59:42 +00:00
|
|
|
return new FloatEvalValue(((BoundLiteralFloatExpression *) expression)->GetValue());
|
|
|
|
case BoundExpressionKind::LiteralString:
|
|
|
|
return new StringEvalValue(*((BoundLiteralStringExpression *) expression)->GetValue());
|
2019-06-17 16:35:12 +00:00
|
|
|
case BoundExpressionKind::LiteralBool:
|
2019-07-27 15:59:42 +00:00
|
|
|
return new BooleanEvalValue(((BoundLiteralBoolExpression *) expression)->GetValue());
|
2019-06-17 16:35:12 +00:00
|
|
|
case BoundExpressionKind::Variable:
|
2019-07-27 15:59:42 +00:00
|
|
|
return this -> GetVariable((BoundVariableExpression*)expression);
|
|
|
|
case BoundExpressionKind::Unary:
|
|
|
|
return this->EvaluateUnary((BoundUnaryExpression *) expression);
|
2019-06-17 16:35:12 +00:00
|
|
|
case BoundExpressionKind::Binary:
|
2019-07-27 15:59:42 +00:00
|
|
|
return this->EvaluateBinary((BoundBinaryExpression *) expression);
|
2019-06-17 16:35:12 +00:00
|
|
|
case BoundExpressionKind::FunctionCall:
|
2019-07-27 15:59:42 +00:00
|
|
|
return this->EvaluateFunctionCallExpression(expression);
|
2019-06-17 16:35:12 +00:00
|
|
|
case BoundExpressionKind::Index:
|
2019-07-27 15:59:42 +00:00
|
|
|
return this->EvaluateIndexExpression(expression).Take();
|
2019-06-17 16:35:12 +00:00
|
|
|
case BoundExpressionKind::PeriodIndex:
|
2019-07-27 15:59:42 +00:00
|
|
|
return this->EvaluatePeriodIndexExpression(expression);
|
|
|
|
case BoundExpressionKind::NumericalTable:
|
|
|
|
return this->EvaluateNumericTableExpression(expression);
|
|
|
|
case BoundExpressionKind::Table:
|
|
|
|
return this->EvaluateComplexTableExpression(expression);
|
2019-08-10 14:45:15 +00:00
|
|
|
case BoundExpressionKind::Require:
|
|
|
|
return this -> EvaluateRequireExpression(expression);
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
2019-06-01 17:20:31 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
2019-07-27 15:59:42 +00:00
|
|
|
EvalValuePointer Evaluator::EvaluateBinary(const BoundBinaryExpression *expression){
|
|
|
|
auto leftValue = this -> EvaluateExpression(expression->GetLeft());
|
|
|
|
auto rightValue = this -> EvaluateExpression(expression->GetRight());
|
|
|
|
auto operation = expression->GetOperation();
|
|
|
|
if (operation == BoundBinaryOperation::Equality){
|
|
|
|
return new BooleanEvalValue(leftValue == rightValue);
|
|
|
|
} else if (operation == BoundBinaryOperation::Inequality){
|
|
|
|
return new BooleanEvalValue(leftValue != rightValue);
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
2019-07-27 15:59:42 +00:00
|
|
|
return leftValue->BinaryOperation(operation, rightValue.Get());
|
2019-06-01 17:20:31 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
2019-07-27 15:59:42 +00:00
|
|
|
EvalValuePointer Evaluator::EvaluateUnary(const BoundUnaryExpression *expression) {
|
|
|
|
auto exp = expression->GetOperand();
|
|
|
|
auto val = EvaluateExpression(exp);
|
|
|
|
return val->UnaryOperation(expression->GetOperation());
|
2019-06-01 17:20:31 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
2019-07-27 15:59:42 +00:00
|
|
|
EvalValuePointer Evaluator::GetVariable(const BoundVariableExpression *expression) {
|
|
|
|
auto variable = this->_evaluationScope->GetVariable(expression->GetKey());
|
|
|
|
if (variable.Get() == nullptr) {
|
|
|
|
throw EvaluationException("Variable not found");
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
2019-07-27 15:59:42 +00:00
|
|
|
return variable;
|
2019-06-09 18:15:09 +00:00
|
|
|
}
|
|
|
|
|
2019-07-27 15:59:42 +00:00
|
|
|
EvalValuePointer Evaluator::EvaluateFunctionCallExpression(const BoundExpression *expression) {
|
2019-06-17 16:35:12 +00:00
|
|
|
auto functionCall = (BoundFunctionCallExpression *) expression;
|
2019-07-27 15:59:42 +00:00
|
|
|
auto function = (GenericFunctionEvalValue*)this->EvaluateExpression(functionCall->GetFunctionExpression()).Clone();
|
2019-06-01 17:20:31 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
auto boundParameters = functionCall->GetParameters();
|
2019-07-27 15:59:42 +00:00
|
|
|
auto parameters = vector<EvalValuePointer>(boundParameters->size());
|
2019-07-25 15:23:54 +00:00
|
|
|
for (size_t i = 0; i < boundParameters->size(); i++) {
|
2019-06-17 16:35:12 +00:00
|
|
|
parameters[i] = this->EvaluateExpression(boundParameters->at(i));
|
|
|
|
}
|
2019-06-01 17:20:31 +00:00
|
|
|
|
2019-07-25 15:23:54 +00:00
|
|
|
auto type = std::dynamic_pointer_cast<const GenericFunctionScriptType>(function->GetType());
|
2019-07-27 15:59:42 +00:00
|
|
|
auto func = function;
|
2019-06-29 17:59:42 +00:00
|
|
|
auto option = functionCall ->GetFunctionOption();
|
|
|
|
auto opt = func->GetOption(option->GetOptionId());
|
|
|
|
if (option -> IsScriptFunction()){
|
|
|
|
auto parameterTypes = option->GetParameterTypes();
|
2019-07-04 16:24:49 +00:00
|
|
|
auto scriptFunctionType = dynamic_cast<const ScriptFunctionOption*>(option);
|
2019-06-21 15:03:13 +00:00
|
|
|
auto parameterKeys = scriptFunctionType->GetParameterKeys();
|
|
|
|
auto originalScope = this->_evaluationScope;
|
2019-07-25 15:23:54 +00:00
|
|
|
auto scriptOption = dynamic_pointer_cast<const EvaluationScriptFunctionOption>(opt);
|
2019-06-29 17:59:42 +00:00
|
|
|
this->_evaluationScope = scriptOption->GetScope();
|
2019-06-21 15:03:13 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++) {
|
|
|
|
auto parameter = parameters[i];
|
|
|
|
auto key = parameterKeys.at(i);
|
2019-07-27 15:59:42 +00:00
|
|
|
this->_evaluationScope->CreateVariable(key.get(), parameter.Clone());
|
2019-06-21 15:03:13 +00:00
|
|
|
}
|
2019-06-29 17:59:42 +00:00
|
|
|
this->EvaluateBlockStatement(scriptOption->GetInnerBlock().get());
|
2019-06-21 15:03:13 +00:00
|
|
|
this->_evaluationScope = originalScope;
|
|
|
|
|
|
|
|
this->_hasReturned = false;
|
|
|
|
auto r = this->_returnValue;
|
2019-07-27 15:59:42 +00:00
|
|
|
this->_returnValue.ClearAssign(nullptr);
|
|
|
|
delete function;
|
2019-06-21 15:03:13 +00:00
|
|
|
return r;
|
|
|
|
} else{
|
2019-07-25 15:23:54 +00:00
|
|
|
auto scriptOption = dynamic_pointer_cast<const UserData::UserDataFunction>(opt);
|
|
|
|
const EvalValue* arr[parameters.size()];
|
|
|
|
for (size_t i = 0; i < parameters.size(); i++){
|
2019-07-27 15:59:42 +00:00
|
|
|
arr[i] = parameters[i].Get();
|
2019-06-21 15:03:13 +00:00
|
|
|
}
|
2019-07-27 15:59:42 +00:00
|
|
|
delete function;
|
2019-08-10 09:55:45 +00:00
|
|
|
return scriptOption -> Call(this -> _scriptOptions, arr, parameters.size());
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
2019-06-01 17:20:31 +00:00
|
|
|
}
|
|
|
|
|
2019-07-27 15:59:42 +00:00
|
|
|
const EvalValue* Evaluator::EvaluateFunction(const GenericFunctionEvalValue *function,
|
2019-06-17 16:35:12 +00:00
|
|
|
const vector<EvalValue *> ¶meters) {
|
2019-07-27 15:59:42 +00:00
|
|
|
auto type = function->GetType();
|
2019-07-04 16:24:49 +00:00
|
|
|
auto option = dynamic_cast<const ScriptFunctionOption*>(type->GetFirstOption());
|
2019-06-29 17:59:42 +00:00
|
|
|
|
|
|
|
auto parameterTypes = option->GetParameterTypes();
|
|
|
|
auto parameterKeys = option->GetParameterKeys();
|
2019-06-08 14:44:47 +00:00
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
auto originalScope = this->_evaluationScope;
|
2019-07-25 15:23:54 +00:00
|
|
|
auto scriptOption = dynamic_pointer_cast<const EvaluationScriptFunctionOption>(function->GetOption(0));
|
2019-06-29 17:59:42 +00:00
|
|
|
this->_evaluationScope = scriptOption->GetScope();
|
2019-06-17 16:35:12 +00:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
2019-06-29 17:59:42 +00:00
|
|
|
this->EvaluateBlockStatement(scriptOption->GetInnerBlock().get());
|
2019-06-17 16:35:12 +00:00
|
|
|
this->_evaluationScope = originalScope;
|
|
|
|
this->_hasReturned = false;
|
2019-07-27 15:59:42 +00:00
|
|
|
auto r = this->_returnValue.Take();
|
|
|
|
delete function;
|
2019-06-17 16:35:12 +00:00
|
|
|
return r;
|
2019-06-01 17:20:31 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
2019-07-27 15:59:42 +00:00
|
|
|
EvalValuePointer Evaluator::EvaluateIndexExpression(const BoundExpression *expression) {
|
2019-06-17 16:35:12 +00:00
|
|
|
auto indexExpression = (BoundIndexExpression *) expression;
|
|
|
|
auto index = this->EvaluateExpression(indexExpression->GetIndexExpression());
|
|
|
|
auto indexable = this->EvaluateExpression(indexExpression->GetIndexableExpression());
|
2019-07-27 15:59:42 +00:00
|
|
|
return indexable->IndexValue(index.Get());
|
2019-06-05 16:44:23 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
2019-07-27 15:59:42 +00:00
|
|
|
EvalValuePointer Evaluator::EvaluatePeriodIndexExpression(const BoundExpression *expression) {
|
2019-06-17 16:35:12 +00:00
|
|
|
auto indexExpression = (BoundPeriodIndexExpression *) expression;
|
2019-07-25 15:23:54 +00:00
|
|
|
auto index = indexExpression->GetIndex()->GetHash();
|
2019-06-17 16:35:12 +00:00
|
|
|
auto indexable = this->EvaluateExpression(indexExpression->GetIndexableExpression());
|
2019-07-27 15:59:42 +00:00
|
|
|
return indexable->IndexValue(index);
|
2019-06-09 18:15:09 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
2019-07-27 15:59:42 +00:00
|
|
|
EvalValuePointer Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) {
|
2019-06-17 16:35:12 +00:00
|
|
|
auto tableExpression = (BoundNumericalTableExpression *) expression;
|
|
|
|
auto valueExpressions = tableExpression->GetExpressions();
|
2019-07-27 15:59:42 +00:00
|
|
|
auto values = new vector<EvalValuePointer>(valueExpressions->size());
|
2019-07-25 15:23:54 +00:00
|
|
|
for (size_t i = 0; i < valueExpressions->size(); i++) {
|
2019-06-17 16:35:12 +00:00
|
|
|
auto val = this->EvaluateExpression(valueExpressions->at(i));
|
2019-07-27 15:59:42 +00:00
|
|
|
values->at(i) = val.Take();
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
2019-07-27 15:59:42 +00:00
|
|
|
auto valuesPointer = shared_ptr<vector<EvalValuePointer>>(values);
|
|
|
|
return new NumericalTableEvalValue(valuesPointer);
|
2019-06-12 13:19:28 +00:00
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
|
2019-07-27 15:59:42 +00:00
|
|
|
EvalValuePointer Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) {
|
2019-06-17 16:35:12 +00:00
|
|
|
auto tableExpression = (BoundTableExpression *) expression;
|
2019-07-27 15:59:42 +00:00
|
|
|
const auto& baseType = tableExpression -> GetType();
|
|
|
|
auto type = dynamic_pointer_cast<const TableScriptType>(baseType);
|
2019-06-17 16:35:12 +00:00
|
|
|
auto declaredVars = type->GetValues();
|
2019-07-27 15:59:42 +00:00
|
|
|
auto variables = make_shared<map<Utilities::HashedString, EvalValuePointer>>();
|
2019-06-27 13:55:46 +00:00
|
|
|
for (const auto& i : *declaredVars) {
|
2019-06-17 16:35:12 +00:00
|
|
|
variables->insert({i.first, nullptr});
|
|
|
|
}
|
2019-07-25 15:23:54 +00:00
|
|
|
auto evaluator = make_shared<EvaluationScope>(variables.get());
|
2019-06-17 16:35:12 +00:00
|
|
|
auto currentEvaluator = this->_evaluationScope;
|
|
|
|
this->_evaluationScope = evaluator;
|
|
|
|
this->EvaluateBlockStatement(tableExpression->GetBlock());
|
|
|
|
this->_evaluationScope = currentEvaluator;
|
2019-07-27 15:59:42 +00:00
|
|
|
return new TableEvalValue(variables);
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|
2019-08-10 14:45:15 +00:00
|
|
|
|
|
|
|
EvalValuePointer Evaluator::EvaluateRequireExpression(const BoundExpression* expression) {
|
|
|
|
auto module = dynamic_cast<const BoundRequireExpression*>(expression)->GetModule();
|
|
|
|
if (module ->GetReturnType() == nullptr){
|
|
|
|
for (const auto& v: *module->GetScriptVariables()){
|
|
|
|
this->_scriptVariables->insert({v.first, v.second.Clone()});
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
} else{
|
|
|
|
return module -> Evaluate().Take();
|
|
|
|
}
|
|
|
|
}
|
2019-06-17 16:35:12 +00:00
|
|
|
}
|