Implements numeric for loops
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user