Implemented generic for loops
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2019-06-26 16:19:34 +02:00
parent cfd558b718
commit d86e9ba8ae
18 changed files with 325 additions and 44 deletions

View File

@@ -45,6 +45,8 @@ namespace Porygon::Evaluation {
return this->EvaluateConditionalStatement((BoundConditionalStatement *) statement);
case BoundStatementKind::NumericalFor:
return this->EvaluateNumericalForStatement((BoundNumericalForStatement*)statement);
case BoundStatementKind::GenericFor:
return this-> EvaluateGenericForStatement((BoundGenericForStatement*)statement);
case BoundStatementKind::Bad:
throw;
@@ -151,6 +153,32 @@ namespace Porygon::Evaluation {
}
}
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 //
/////////////////
const shared_ptr<EvalValue> Evaluator::EvaluateExpression(const BoundExpression *expression) {
auto type = expression->GetType();
switch (type->GetClass()) {
@@ -425,5 +453,4 @@ namespace Porygon::Evaluation {
throw;
}
}
}