Skip to content

Commit

Permalink
Fix #1: Execute any control flow body inside new sub environment (Scope)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Sep 21, 2022
1 parent a952d36 commit 4f6deb7
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions lilo/src/main/java/com/amrdeveloper/lilo/LiloInterpreter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ class LiloInterpreter : StatementVisitor<Unit>, ExpressionVisitor<Any> {
throw LiloException(statement.keyword.position, "If condition must be boolean")
}

// If condition is true execute the body in new sub scope
if (condition == true) {
statement.body.accept(this)
executeBlockInScope(LiloScope(currentScope), statement.body)
return
}

Expand All @@ -139,8 +140,9 @@ class LiloInterpreter : StatementVisitor<Unit>, ExpressionVisitor<Any> {
throw LiloException(alternative.keyword.position, "condition must be boolean")
}

// If one of alternative conditions is true execute the body in new sub scope
if (alternativeCondition == true) {
alternative.body.accept(this)
executeBlockInScope(LiloScope(currentScope), alternative.body)
return
}
}
Expand All @@ -151,7 +153,7 @@ class LiloInterpreter : StatementVisitor<Unit>, ExpressionVisitor<Any> {
val condition = statement.condition.accept(this)
if (condition is Boolean) {
while (statement.condition.accept(this) == true) {
statement.body.accept(this)
executeBlockInScope(LiloScope(currentScope), statement.body)
}
return
}
Expand All @@ -164,9 +166,7 @@ class LiloInterpreter : StatementVisitor<Unit>, ExpressionVisitor<Any> {
Timber.tag(TAG).d("Evaluate RepeatStatement")
val counter = statement.condition.accept(this)
if (counter is Float) {
repeat(counter.toInt()) {
statement.body.accept(this)
}
repeat(counter.toInt()) { executeBlockInScope(LiloScope(currentScope), statement.body) }
return
}

Expand All @@ -190,7 +190,6 @@ class LiloInterpreter : StatementVisitor<Unit>, ExpressionVisitor<Any> {
Timber.tag(TAG).d("Evaluate CircleStatement")
val radius = statement.radius.accept(this)
if (radius is Float) {
//canvas.drawCircle(currentXPosition, currentYPosition, radius.toFloat(), turtlePaint)
val circleInst = CircleInst(currentXPosition, currentYPosition, radius.toFloat())
onInstructionEmitterListener(circleInst)
} else {
Expand Down Expand Up @@ -542,12 +541,8 @@ class LiloInterpreter : StatementVisitor<Unit>, ExpressionVisitor<Any> {
if (statement is ReturnStatement) {
returnValue = statement.value.accept(this)
break
} else if (statement is ExpressionStatement) {
returnValue = statement.expression.accept(this)
break
} else {
statement.accept(this)
}
statement.accept(this)
}
currentScope = previousScope
return returnValue
Expand Down

0 comments on commit 4f6deb7

Please sign in to comment.