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-05-23 16:50:09 +00:00
|
|
|
|
2019-06-01 19:38:39 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
EvalValue* Evaluator::Evaluate(const BoundScriptStatement *statement) {
|
2019-06-13 14:26:10 +00:00
|
|
|
this->_evaluationScope = make_shared<EvaluationScope>(this->_scriptData->_scriptVariables, statement->GetLocalVariableCount());
|
2019-06-13 15:12:46 +00:00
|
|
|
EvaluateBlockStatement(statement);
|
2019-06-12 16:45:47 +00:00
|
|
|
return this -> _returnValue.get();
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
void Evaluator::EvaluateStatement(const BoundStatement *statement) {
|
2019-06-07 13:23:13 +00:00
|
|
|
if (this->_hasReturned)
|
|
|
|
return;
|
2019-05-23 16:50:09 +00:00
|
|
|
switch (statement->GetKind()){
|
|
|
|
case BoundStatementKind ::Script: throw; // Should never happen
|
2019-06-13 15:12:46 +00:00
|
|
|
case BoundStatementKind ::Block: return this->EvaluateBlockStatement((BoundBlockStatement *) statement);
|
2019-05-23 16:50:09 +00:00
|
|
|
case BoundStatementKind ::Expression: return this -> EvaluateExpressionStatement((BoundExpressionStatement*)statement);
|
2019-05-29 12:55:03 +00:00
|
|
|
case BoundStatementKind ::Assignment: return this -> EvaluateAssignmentStatement((BoundAssignmentStatement*)statement);
|
2019-06-14 15:12:27 +00:00
|
|
|
case BoundStatementKind::IndexAssignment:
|
|
|
|
return this -> EvaluateIndexAssignmentStatement((BoundIndexAssignmentStatement*)statement);
|
2019-06-01 10:33:52 +00:00
|
|
|
case BoundStatementKind ::FunctionDeclaration: return this->EvaluateFunctionDeclarationStatement((BoundFunctionDeclarationStatement*)statement);
|
2019-06-07 13:23:13 +00:00
|
|
|
case BoundStatementKind::Return: return this -> EvaluateReturnStatement((BoundReturnStatement*)statement);
|
2019-06-08 12:25:15 +00:00
|
|
|
case BoundStatementKind::Conditional: return this -> EvaluateConditionalStatement((BoundConditionalStatement*)statement);
|
2019-05-29 12:55:03 +00:00
|
|
|
|
|
|
|
case BoundStatementKind::Bad:
|
|
|
|
throw;
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
void Evaluator::EvaluateBlockStatement(const BoundBlockStatement *statement) {
|
|
|
|
for (auto s: *statement->GetStatements()){
|
2019-05-23 16:50:09 +00:00
|
|
|
this -> EvaluateStatement(s);
|
2019-06-07 13:23:13 +00:00
|
|
|
if (this->_hasReturned)
|
|
|
|
break;
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
void Evaluator::EvaluateExpressionStatement(const BoundExpressionStatement *statement) {
|
2019-05-25 10:26:11 +00:00
|
|
|
// Save new value
|
2019-06-05 15:46:46 +00:00
|
|
|
this->_lastValue = this -> EvaluateExpression(statement->GetExpression());
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
void Evaluator::EvaluateAssignmentStatement(const BoundAssignmentStatement *statement) {
|
2019-05-29 12:55:03 +00:00
|
|
|
auto value = this -> EvaluateExpression(statement->GetExpression());
|
|
|
|
auto key = statement->GetKey();
|
|
|
|
if (key->IsCreation()){
|
2019-06-13 13:16:41 +00:00
|
|
|
this->_evaluationScope->CreateVariable(key, value);
|
2019-05-29 12:55:03 +00:00
|
|
|
} else{
|
2019-06-13 13:16:41 +00:00
|
|
|
this->_evaluationScope->SetVariable(key, value);
|
2019-05-29 12:55:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 15:12:27 +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());
|
|
|
|
table -> SetIndexValue(key.get(), value);
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
void Evaluator::EvaluateFunctionDeclarationStatement(const BoundFunctionDeclarationStatement *statement) {
|
2019-06-01 10:33:52 +00:00
|
|
|
auto type = statement->GetType();
|
|
|
|
auto key = statement->GetKey();
|
|
|
|
auto block = statement->GetBlock();
|
2019-06-13 13:16:41 +00:00
|
|
|
auto value = make_shared<ScriptFunctionEvalValue>(block, this->_evaluationScope, type);
|
2019-06-01 10:33:52 +00:00
|
|
|
if (key->IsCreation()){
|
2019-06-13 13:16:41 +00:00
|
|
|
this->_evaluationScope->CreateVariable(key, value);
|
2019-06-01 10:33:52 +00:00
|
|
|
} else{
|
2019-06-13 13:16:41 +00:00
|
|
|
this->_evaluationScope->SetVariable(key, value);
|
2019-06-01 10:33:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
void Evaluator::EvaluateReturnStatement(const BoundReturnStatement* statement){
|
2019-06-07 13:23:13 +00:00
|
|
|
auto expression = statement->GetExpression();
|
|
|
|
if (expression == nullptr){
|
2019-06-12 16:45:47 +00:00
|
|
|
this->_hasReturned = true;
|
2019-06-07 13:23:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto value = this -> EvaluateExpression(expression);
|
2019-06-12 16:45:47 +00:00
|
|
|
this->_hasReturned = true;
|
2019-06-07 13:23:13 +00:00
|
|
|
this -> _returnValue = value;
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
void Evaluator::EvaluateConditionalStatement(const BoundConditionalStatement *statement) {
|
2019-06-08 12:25:15 +00:00
|
|
|
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-13 15:12:46 +00:00
|
|
|
shared_ptr<EvalValue> Evaluator::EvaluateExpression(const BoundExpression *expression) {
|
2019-05-23 16:50:09 +00:00
|
|
|
auto type = expression -> GetType();
|
|
|
|
switch (type->GetClass()){
|
2019-05-24 17:14:30 +00:00
|
|
|
case TypeClass ::Number: return this -> EvaluateIntegerExpression(expression);
|
2019-05-25 11:30:20 +00:00
|
|
|
case TypeClass ::Bool: return this -> EvaluateBoolExpression(expression);
|
2019-05-25 14:15:20 +00:00
|
|
|
case TypeClass ::String: return this -> EvaluateStringExpression(expression);
|
2019-06-01 17:20:31 +00:00
|
|
|
case TypeClass ::Function: return this->EvaluateFunctionExpression(expression);
|
|
|
|
case TypeClass ::Nil: return this->EvaluateNilExpression(expression);
|
2019-06-09 18:15:09 +00:00
|
|
|
case TypeClass ::Table: return this-> EvaluateTableExpression(expression);
|
2019-06-14 12:59:38 +00:00
|
|
|
case TypeClass ::UserData: return this -> EvaluateUserDataExpression(expression);
|
2019-05-24 17:14:30 +00:00
|
|
|
default: throw;
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
shared_ptr<EvalValue> Evaluator::GetVariable(const BoundVariableExpression* expression){
|
2019-06-13 13:16:41 +00:00
|
|
|
auto variable = this->_evaluationScope->GetVariable(expression->GetKey());
|
|
|
|
if (variable == nullptr){
|
|
|
|
throw EvaluationException("Variable not found");
|
|
|
|
}
|
2019-06-08 16:33:56 +00:00
|
|
|
return variable->Clone();
|
2019-05-30 13:23:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(const BoundExpression *expression) {
|
2019-05-23 16:50:09 +00:00
|
|
|
switch (expression->GetKind()){
|
2019-06-01 19:38:39 +00:00
|
|
|
case BoundExpressionKind ::LiteralInteger: return make_shared<IntegerEvalValue>(((BoundLiteralIntegerExpression*)expression)->GetValue());
|
|
|
|
case BoundExpressionKind ::LiteralFloat: return make_shared<FloatEvalValue>(((BoundLiteralFloatExpression*)expression)->GetValue());
|
2019-05-25 12:59:12 +00:00
|
|
|
case BoundExpressionKind::Unary: return this -> EvaluateIntegerUnary((BoundUnaryExpression*)expression);
|
2019-05-23 16:50:09 +00:00
|
|
|
case BoundExpressionKind ::Binary: return this -> EvaluateIntegerBinary((BoundBinaryExpression*)expression);
|
2019-06-01 19:38:39 +00:00
|
|
|
case BoundExpressionKind::Variable: return dynamic_pointer_cast<NumericEvalValue>(this->GetVariable((BoundVariableExpression*)expression));
|
|
|
|
case BoundExpressionKind ::FunctionCall: return dynamic_pointer_cast<NumericEvalValue>(this->EvaluateFunctionCallExpression(expression));
|
2019-06-06 15:35:51 +00:00
|
|
|
case BoundExpressionKind ::Index: return dynamic_pointer_cast<NumericEvalValue>(this->EvaluateIndexExpression(expression));
|
2019-05-23 16:50:09 +00:00
|
|
|
|
2019-05-24 13:31:11 +00:00
|
|
|
case BoundExpressionKind ::LiteralString:
|
|
|
|
case BoundExpressionKind ::LiteralBool:
|
|
|
|
case BoundExpressionKind ::Bad:
|
2019-06-09 18:15:09 +00:00
|
|
|
case BoundExpressionKind::NumericalTable:
|
2019-06-12 13:19:28 +00:00
|
|
|
case BoundExpressionKind::Table:
|
2019-05-24 13:31:11 +00:00
|
|
|
throw;
|
|
|
|
}
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
shared_ptr<BooleanEvalValue> Evaluator::EvaluateBoolExpression(const BoundExpression *expression) {
|
2019-05-25 11:30:20 +00:00
|
|
|
switch (expression->GetKind()) {
|
2019-06-01 19:38:39 +00:00
|
|
|
case BoundExpressionKind ::LiteralBool: return make_shared<BooleanEvalValue>(((BoundLiteralBoolExpression*)expression)->GetValue());
|
2019-05-25 12:59:12 +00:00
|
|
|
case BoundExpressionKind::Unary: return this -> EvaluateBooleanUnary((BoundUnaryExpression*)expression);
|
2019-05-25 11:30:20 +00:00
|
|
|
case BoundExpressionKind::Binary: return this -> EvaluateBooleanBinary((BoundBinaryExpression*)expression);
|
2019-06-01 19:38:39 +00:00
|
|
|
case BoundExpressionKind::Variable: return dynamic_pointer_cast<BooleanEvalValue>(this->GetVariable((BoundVariableExpression*)expression));
|
|
|
|
case BoundExpressionKind ::FunctionCall: return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluateFunctionCallExpression(expression));
|
2019-06-06 15:35:51 +00:00
|
|
|
case BoundExpressionKind ::Index: return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluateIndexExpression(expression));
|
2019-05-25 14:15:20 +00:00
|
|
|
|
2019-05-25 11:30:20 +00:00
|
|
|
case BoundExpressionKind::Bad:
|
|
|
|
case BoundExpressionKind::LiteralInteger:
|
|
|
|
case BoundExpressionKind::LiteralFloat:
|
|
|
|
case BoundExpressionKind::LiteralString:
|
2019-06-09 18:15:09 +00:00
|
|
|
case BoundExpressionKind::NumericalTable:
|
2019-06-12 13:19:28 +00:00
|
|
|
case BoundExpressionKind::Table:
|
2019-05-25 11:30:20 +00:00
|
|
|
throw;
|
|
|
|
|
|
|
|
}
|
2019-05-23 16:50:09 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
shared_ptr<StringEvalValue> Evaluator::EvaluateStringExpression(const BoundExpression *expression) {
|
2019-05-25 14:15:20 +00:00
|
|
|
switch (expression->GetKind()) {
|
2019-06-01 19:38:39 +00:00
|
|
|
case BoundExpressionKind ::LiteralString:
|
|
|
|
return make_shared<StringEvalValue>(((BoundLiteralStringExpression*)expression)->GetValue());
|
2019-05-25 14:15:20 +00:00
|
|
|
case BoundExpressionKind::Binary:
|
|
|
|
return this -> EvaluateStringBinary((BoundBinaryExpression*)expression);
|
2019-06-01 19:38:39 +00:00
|
|
|
case BoundExpressionKind::Variable: return dynamic_pointer_cast<StringEvalValue>(this->GetVariable((BoundVariableExpression*)expression));
|
|
|
|
case BoundExpressionKind ::FunctionCall: return dynamic_pointer_cast<StringEvalValue>(this->EvaluateFunctionCallExpression(expression));
|
2019-06-06 15:35:51 +00:00
|
|
|
case BoundExpressionKind ::Index: return dynamic_pointer_cast<StringEvalValue>(this->EvaluateIndexExpression(expression));
|
2019-05-25 14:15:20 +00:00
|
|
|
|
|
|
|
case BoundExpressionKind::Bad:
|
|
|
|
case BoundExpressionKind::LiteralInteger:
|
|
|
|
case BoundExpressionKind::LiteralFloat:
|
|
|
|
case BoundExpressionKind::LiteralBool:
|
|
|
|
case BoundExpressionKind::Unary:
|
2019-06-09 18:15:09 +00:00
|
|
|
case BoundExpressionKind::NumericalTable:
|
2019-06-12 13:19:28 +00:00
|
|
|
case BoundExpressionKind::Table:
|
2019-05-25 14:15:20 +00:00
|
|
|
throw;
|
|
|
|
|
2019-06-01 17:20:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
shared_ptr<EvalValue> Evaluator::EvaluateFunctionExpression(const BoundExpression * expression){
|
2019-06-01 17:20:31 +00:00
|
|
|
switch (expression->GetKind()){
|
|
|
|
case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression);
|
2019-06-12 15:56:47 +00:00
|
|
|
case BoundExpressionKind ::Index: return this->EvaluateIndexExpression(expression);
|
2019-06-01 17:20:31 +00:00
|
|
|
default: throw;
|
|
|
|
}
|
|
|
|
}
|
2019-06-13 15:12:46 +00:00
|
|
|
shared_ptr<EvalValue> Evaluator::EvaluateNilExpression(const BoundExpression * expression){
|
2019-06-01 17:20:31 +00:00
|
|
|
switch (expression->GetKind()){
|
|
|
|
case BoundExpressionKind ::FunctionCall:
|
|
|
|
return this->EvaluateFunctionCallExpression(expression);
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2019-06-13 15:12:46 +00:00
|
|
|
shared_ptr<EvalValue> Evaluator::EvaluateTableExpression(const BoundExpression * expression){
|
2019-06-09 18:15:09 +00:00
|
|
|
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);
|
2019-06-12 13:19:28 +00:00
|
|
|
case BoundExpressionKind ::Table: return this -> EvaluateComplexTableExpression(expression);
|
2019-06-09 18:15:09 +00:00
|
|
|
default:
|
2019-06-12 13:19:28 +00:00
|
|
|
throw;
|
2019-06-09 18:15:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-01 17:20:31 +00:00
|
|
|
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(const BoundExpression* expression){
|
2019-06-01 17:20:31 +00:00
|
|
|
auto functionCall = (BoundFunctionCallExpression*)expression;
|
2019-06-01 19:38:39 +00:00
|
|
|
auto function = dynamic_pointer_cast<ScriptFunctionEvalValue>(this->EvaluateExpression(functionCall->GetFunctionExpression()));
|
2019-06-08 16:33:56 +00:00
|
|
|
|
2019-06-01 17:20:31 +00:00
|
|
|
auto boundParameters = functionCall->GetParameters();
|
2019-06-13 15:12:46 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
auto type = std::dynamic_pointer_cast<FunctionScriptType>(function->GetType());
|
|
|
|
auto parameterTypes = type->GetParameterTypes();
|
|
|
|
auto parameterKeys = type->GetParameterKeys();
|
2019-06-08 16:33:56 +00:00
|
|
|
auto originalScope = this->_evaluationScope;
|
2019-06-13 13:16:41 +00:00
|
|
|
this->_evaluationScope = function->GetScope();
|
2019-06-08 14:44:47 +00:00
|
|
|
|
2019-06-08 14:02:21 +00:00
|
|
|
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++){
|
2019-06-01 17:20:31 +00:00
|
|
|
auto parameter = parameters[i];
|
2019-06-08 14:02:21 +00:00
|
|
|
auto key = parameterKeys.at(i);
|
2019-06-13 13:16:41 +00:00
|
|
|
this->_evaluationScope->CreateVariable(key.get(), parameter->Clone());
|
2019-06-01 17:20:31 +00:00
|
|
|
}
|
2019-06-13 15:12:46 +00:00
|
|
|
this->EvaluateBlockStatement(function->GetInnerBlock().get());
|
2019-06-08 16:33:56 +00:00
|
|
|
this->_evaluationScope = originalScope;
|
2019-06-13 13:16:41 +00:00
|
|
|
|
2019-06-07 13:23:13 +00:00
|
|
|
this->_hasReturned = false;
|
|
|
|
auto r = this -> _returnValue;
|
|
|
|
this -> _returnValue = nullptr;
|
|
|
|
return r;
|
2019-06-05 15:46:46 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
shared_ptr<EvalValue> Evaluator::EvaluateFunction(const ScriptFunctionEvalValue *function, const vector<EvalValue *>& parameters) {
|
2019-06-05 16:44:23 +00:00
|
|
|
auto type = std::dynamic_pointer_cast<FunctionScriptType>(function->GetType());
|
|
|
|
auto parameterTypes = type->GetParameterTypes();
|
|
|
|
auto parameterKeys = type->GetParameterKeys();
|
2019-06-12 13:19:28 +00:00
|
|
|
|
|
|
|
auto originalScope = this->_evaluationScope;
|
2019-06-13 13:16:41 +00:00
|
|
|
this->_evaluationScope = function->GetScope();
|
2019-06-12 13:19:28 +00:00
|
|
|
|
2019-06-08 14:02:21 +00:00
|
|
|
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++){
|
2019-06-05 16:44:23 +00:00
|
|
|
auto parameter = parameters[i];
|
2019-06-08 14:02:21 +00:00
|
|
|
auto key = parameterKeys.at(i);
|
2019-06-13 13:16:41 +00:00
|
|
|
this->_evaluationScope->CreateVariable(key.get(), parameter->Clone());
|
2019-06-05 16:44:23 +00:00
|
|
|
}
|
2019-06-13 15:12:46 +00:00
|
|
|
this->EvaluateBlockStatement(function->GetInnerBlock().get());
|
2019-06-12 13:19:28 +00:00
|
|
|
this->_evaluationScope = originalScope;
|
2019-06-07 13:23:13 +00:00
|
|
|
this->_hasReturned = false;
|
|
|
|
auto r = this -> _returnValue;
|
|
|
|
this -> _returnValue = nullptr;
|
|
|
|
return r;
|
2019-06-06 15:35:51 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
shared_ptr<EvalValue> Evaluator::EvaluateIndexExpression(const BoundExpression *expression) {
|
2019-06-06 15:35:51 +00:00
|
|
|
auto indexExpression = (BoundIndexExpression*)expression;
|
|
|
|
auto index = this -> EvaluateExpression(indexExpression->GetIndexExpression());
|
|
|
|
auto indexable = this -> EvaluateExpression(indexExpression->GetIndexableExpression());
|
2019-06-09 18:15:09 +00:00
|
|
|
return indexable -> IndexValue(index.get()) -> Clone();
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
shared_ptr<EvalValue> Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) {
|
2019-06-09 18:15:09 +00:00
|
|
|
auto tableExpression = (BoundNumericalTableExpression*)expression;
|
|
|
|
auto valueExpressions = tableExpression->GetExpressions();
|
2019-06-13 15:12:46 +00:00
|
|
|
auto values = new unordered_map<size_t, shared_ptr<EvalValue>>(valueExpressions->size());
|
|
|
|
for (int i = 0; i < valueExpressions->size(); i++){
|
|
|
|
auto val = this -> EvaluateExpression(valueExpressions -> at(i));
|
2019-06-09 18:15:09 +00:00
|
|
|
values -> insert({i + 1, val});
|
|
|
|
}
|
|
|
|
auto valuesPointer = shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>>(values);
|
2019-06-13 14:26:10 +00:00
|
|
|
return make_shared<TableEvalValue>(valuesPointer);
|
2019-06-08 12:25:15 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:46 +00:00
|
|
|
shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(const BoundExpression *expression) {
|
2019-06-12 13:19:28 +00:00
|
|
|
auto tableExpression = (BoundTableExpression*)expression;
|
|
|
|
auto type = dynamic_pointer_cast<TableScriptType>(tableExpression->GetType());
|
|
|
|
auto declaredVars = type -> GetValues();
|
|
|
|
auto variables = make_shared<unordered_map<size_t, shared_ptr<EvalValue>>>(declaredVars->size());
|
|
|
|
for (auto i : *declaredVars){
|
|
|
|
variables->insert({i.first, nullptr});
|
|
|
|
}
|
2019-06-13 14:26:10 +00:00
|
|
|
auto evaluator = make_shared<EvaluationScope>(variables.get(), type -> GetLocalVariableCount());
|
2019-06-12 13:19:28 +00:00
|
|
|
auto currentEvaluator = this -> _evaluationScope;
|
|
|
|
this -> _evaluationScope = evaluator;
|
2019-06-13 15:12:46 +00:00
|
|
|
this->EvaluateBlockStatement(tableExpression->GetBlock());
|
2019-06-12 13:19:28 +00:00
|
|
|
this -> _evaluationScope = currentEvaluator;
|
2019-06-13 14:26:10 +00:00
|
|
|
return make_shared<TableEvalValue>(variables);
|
2019-06-12 13:19:28 +00:00
|
|
|
}
|
2019-06-14 12:59:38 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2019-06-14 15:12:27 +00:00
|
|
|
|