Implements while loop
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2019-06-28 13:28:39 +02:00
parent f7a6ff7d87
commit df79489d4d
9 changed files with 142 additions and 29 deletions

View File

@@ -48,12 +48,16 @@ namespace Porygon::Evaluation {
return this->EvaluateNumericalForStatement((BoundNumericalForStatement*)statement);
case BoundStatementKind::GenericFor:
return this-> EvaluateGenericForStatement((BoundGenericForStatement*)statement);
case BoundStatementKind::While:
return this-> EvaluateWhileStatement((BoundWhileStatement*)statement);
case BoundStatementKind::Break:
this -> _hasBroken = true;
return;
case BoundStatementKind::Bad:
throw;
}
throw EvaluationException("Evaluating this statement is not supported");
}
void Evaluator::EvaluateBlockStatement(const BoundBlockStatement *statement) {
@@ -135,10 +139,11 @@ namespace Porygon::Evaluation {
auto identifier = statement -> GetIdentifier();
this -> _evaluationScope -> CreateVariable(identifier, nullptr);
auto block = (BoundBlockStatement*)statement -> GetBlock();
auto statements = *block -> GetStatements();
if (step >= 0){
for (long i = start; i <= end; i += step){
this -> _evaluationScope -> SetVariable(identifier, make_shared<IntegerEvalValue>(i));
for (auto s: *block->GetStatements()) {
for (auto s: statements) {
this->EvaluateStatement(s);
if (this->_hasReturned || this -> _hasBroken)
break;
@@ -149,7 +154,7 @@ namespace Porygon::Evaluation {
} else{
for (long i = start; i >= end; i += step){
this -> _evaluationScope -> SetVariable(identifier, make_shared<IntegerEvalValue>(i));
for (auto s: *block->GetStatements()) {
for (auto s: statements) {
this->EvaluateStatement(s);
if (this->_hasReturned || this -> _hasBroken)
break;
@@ -171,7 +176,7 @@ namespace Porygon::Evaluation {
if (valueVariable != nullptr)
this -> _evaluationScope -> CreateVariable(valueVariable, nullptr);
auto block = (BoundBlockStatement*)statement -> GetBlock();
auto statements = *block -> GetStatements();
while (iterator->MoveNext()){
auto currentKey = iterator->GetCurrent();
this -> _evaluationScope -> SetVariable(keyVariable, currentKey);
@@ -179,7 +184,7 @@ namespace Porygon::Evaluation {
auto currentValue = iteratorVal -> IndexValue(currentKey.get());
this -> _evaluationScope -> SetVariable(valueVariable, currentValue);
}
for (auto s: *block->GetStatements()) {
for (auto s: statements) {
this->EvaluateStatement(s);
if (this->_hasReturned || this -> _hasBroken)
break;
@@ -191,6 +196,22 @@ namespace Porygon::Evaluation {
delete iterator;
}
void Evaluator::EvaluateWhileStatement(const BoundWhileStatement *statement) {
auto condition = statement -> GetCondition();
auto block = (BoundBlockStatement*)statement -> GetBlock();
auto statements = *block -> GetStatements();
while (this->EvaluateBoolExpression(condition)->EvaluateBool()){
for (auto s: statements) {
this->EvaluateStatement(s);
if (this->_hasReturned || this -> _hasBroken)
break;
}
if (this->_hasReturned || this -> _hasBroken)
break;
}
this -> _hasBroken = false;
}
/////////////////
// Expressions //
/////////////////

View File

@@ -34,6 +34,7 @@ namespace Porygon::Evaluation{
void EvaluateConditionalStatement(const BoundConditionalStatement *statement);
void EvaluateNumericalForStatement(const BoundNumericalForStatement *statement);
void EvaluateGenericForStatement(const BoundGenericForStatement *statement);
void EvaluateWhileStatement(const BoundWhileStatement *statement);
const shared_ptr<EvalValue> EvaluateExpression(const BoundExpression *expression);
const shared_ptr<NumericEvalValue> EvaluateIntegerExpression(const BoundExpression *expression);