Implements numeric for loops
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2019-06-22 17:35:33 +02:00
parent 694b0ac0c0
commit e472dcec1c
16 changed files with 311 additions and 23 deletions

View File

@@ -42,6 +42,8 @@ namespace Porygon::Evaluation {
return this->EvaluateReturnStatement((BoundReturnStatement *) statement);
case BoundStatementKind::Conditional:
return this->EvaluateConditionalStatement((BoundConditionalStatement *) statement);
case BoundStatementKind::NumericalFor:
return this->EvaluateNumericalForStatement((BoundNumericalForStatement*)statement);
case BoundStatementKind::Bad:
throw;
@@ -116,6 +118,38 @@ namespace Porygon::Evaluation {
}
}
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();
if (step > 0){
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;
}
}
}
}
const shared_ptr<EvalValue> Evaluator::EvaluateExpression(const BoundExpression *expression) {
auto type = expression->GetType();
switch (type->GetClass()) {
@@ -389,4 +423,5 @@ namespace Porygon::Evaluation {
throw;
}
}
}

View File

@@ -31,6 +31,7 @@ namespace Porygon::Evaluation{
void EvaluateFunctionDeclarationStatement(const BoundFunctionDeclarationStatement *statement);
void EvaluateReturnStatement(const BoundReturnStatement *statement);
void EvaluateConditionalStatement(const BoundConditionalStatement *statement);
void EvaluateNumericalForStatement(const BoundNumericalForStatement *statement);
const shared_ptr<EvalValue> EvaluateExpression(const BoundExpression *expression);
const shared_ptr<NumericEvalValue> EvaluateIntegerExpression(const BoundExpression *expression);