Implements complex tables
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2019-06-12 15:19:28 +02:00
parent ba4fe888fa
commit c022c91777
21 changed files with 272 additions and 50 deletions

View File

@@ -6,12 +6,14 @@
#include "EvaluationScope/EvaluationScope.hpp"
#include "EvalValues/ScriptFunctionEvalValue.hpp"
#include "EvalValues/TableEvalValue.hpp"
#include "../Binder/BoundExpressions/BoundTableExpression.hpp"
#include "../TableScriptType.hpp"
using namespace std;
void Evaluator::Evaluate(BoundScriptStatement *statement) {
this->_evaluationScope = new EvaluationScope(this->_scriptData->_scriptVariables, statement->GetDeepestScope());
EvaluateBlockStatement(statement);
EvaluateBlockStatement(statement, false);
}
void Evaluator::EvaluateStatement(BoundStatement *statement) {
@@ -19,7 +21,7 @@ void Evaluator::EvaluateStatement(BoundStatement *statement) {
return;
switch (statement->GetKind()){
case BoundStatementKind ::Script: throw; // Should never happen
case BoundStatementKind ::Block: return this -> EvaluateBlockStatement((BoundBlockStatement*)statement);
case BoundStatementKind ::Block: return this -> EvaluateBlockStatement((BoundBlockStatement*)statement, true);
case BoundStatementKind ::Expression: return this -> EvaluateExpressionStatement((BoundExpressionStatement*)statement);
case BoundStatementKind ::Assignment: return this -> EvaluateAssignmentStatement((BoundAssignmentStatement*)statement);
case BoundStatementKind ::FunctionDeclaration: return this->EvaluateFunctionDeclarationStatement((BoundFunctionDeclarationStatement*)statement);
@@ -31,14 +33,14 @@ void Evaluator::EvaluateStatement(BoundStatement *statement) {
}
}
void Evaluator::EvaluateBlockStatement(BoundBlockStatement* statement) {
void Evaluator::EvaluateBlockStatement(BoundBlockStatement* statement, bool clearScope) {
this->_evaluationScope->OuterScope();
for (auto s: statement->GetStatements()){
this -> EvaluateStatement(s);
if (this->_hasReturned)
break;
}
this->_evaluationScope->InnerScope();
this->_evaluationScope->InnerScope(clearScope);
}
void Evaluator::EvaluateExpressionStatement(BoundExpressionStatement *statement) {
@@ -122,6 +124,7 @@ shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(BoundExpressio
case BoundExpressionKind ::LiteralBool:
case BoundExpressionKind ::Bad:
case BoundExpressionKind::NumericalTable:
case BoundExpressionKind::Table:
throw;
}
}
@@ -140,6 +143,7 @@ shared_ptr<BooleanEvalValue> Evaluator::EvaluateBoolExpression(BoundExpression *
case BoundExpressionKind::LiteralFloat:
case BoundExpressionKind::LiteralString:
case BoundExpressionKind::NumericalTable:
case BoundExpressionKind::Table:
throw;
}
@@ -161,6 +165,7 @@ shared_ptr<StringEvalValue> Evaluator::EvaluateStringExpression(BoundExpression
case BoundExpressionKind::LiteralBool:
case BoundExpressionKind::Unary:
case BoundExpressionKind::NumericalTable:
case BoundExpressionKind::Table:
throw;
}
@@ -187,8 +192,9 @@ shared_ptr<EvalValue> Evaluator::EvaluateTableExpression(BoundExpression * expre
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);
default:
return nullptr;
throw;
}
}
@@ -222,8 +228,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(BoundExpression*
auto key = parameterKeys.at(i);
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone());
}
this->_evaluationScope->OuterScope();
this->EvaluateBlockStatement(function->GetInnerBlock().get());
this->EvaluateBlockStatement(function->GetInnerBlock().get(), true);
delete this->_evaluationScope;
this->_evaluationScope = originalScope;
@@ -237,6 +242,12 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunction(ScriptFunctionEvalValue *funct
auto type = std::dynamic_pointer_cast<FunctionScriptType>(function->GetType());
auto parameterTypes = type->GetParameterTypes();
auto parameterKeys = type->GetParameterKeys();
auto scope = type -> GetScopeIndex();
auto originalScope = this->_evaluationScope;
auto functionScope = this->_evaluationScope->CreateBranchingScope(scope);
this->_evaluationScope = functionScope;
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++){
auto parameter = parameters[i];
auto requiredType = parameterTypes.at(i);
@@ -246,7 +257,9 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunction(ScriptFunctionEvalValue *funct
auto key = parameterKeys.at(i);
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone());
}
this->EvaluateBlockStatement(function->GetInnerBlock().get());
this->EvaluateBlockStatement(function->GetInnerBlock().get(), true);
delete this->_evaluationScope;
this->_evaluationScope = originalScope;
this->_hasReturned = false;
auto r = this -> _returnValue;
this -> _returnValue = nullptr;
@@ -272,3 +285,18 @@ shared_ptr<EvalValue> Evaluator::EvaluateNumericTableExpression(BoundExpression
return make_shared<TableEvalValue>(valuesPointer, tableExpression->GetType());
}
shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(BoundExpression *expression) {
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});
}
auto evaluator = new EvaluationScope(variables.get(), type -> GetDeepestScope());
auto currentEvaluator = this -> _evaluationScope;
this -> _evaluationScope = evaluator;
this -> EvaluateBlockStatement(tableExpression->GetBlock(), false);
this -> _evaluationScope = currentEvaluator;
return shared_ptr<TableEvalValue>(new TableEvalValue(variables, tableExpression->GetType()));
}