Skip to content

Commit 4f6deb7

Browse files
committed
Fix #1: Execute any control flow body inside new sub environment (Scope)
1 parent a952d36 commit 4f6deb7

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

lilo/src/main/java/com/amrdeveloper/lilo/LiloInterpreter.kt

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ class LiloInterpreter : StatementVisitor<Unit>, ExpressionVisitor<Any> {
126126
throw LiloException(statement.keyword.position, "If condition must be boolean")
127127
}
128128

129+
// If condition is true execute the body in new sub scope
129130
if (condition == true) {
130-
statement.body.accept(this)
131+
executeBlockInScope(LiloScope(currentScope), statement.body)
131132
return
132133
}
133134

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

143+
// If one of alternative conditions is true execute the body in new sub scope
142144
if (alternativeCondition == true) {
143-
alternative.body.accept(this)
145+
executeBlockInScope(LiloScope(currentScope), alternative.body)
144146
return
145147
}
146148
}
@@ -151,7 +153,7 @@ class LiloInterpreter : StatementVisitor<Unit>, ExpressionVisitor<Any> {
151153
val condition = statement.condition.accept(this)
152154
if (condition is Boolean) {
153155
while (statement.condition.accept(this) == true) {
154-
statement.body.accept(this)
156+
executeBlockInScope(LiloScope(currentScope), statement.body)
155157
}
156158
return
157159
}
@@ -164,9 +166,7 @@ class LiloInterpreter : StatementVisitor<Unit>, ExpressionVisitor<Any> {
164166
Timber.tag(TAG).d("Evaluate RepeatStatement")
165167
val counter = statement.condition.accept(this)
166168
if (counter is Float) {
167-
repeat(counter.toInt()) {
168-
statement.body.accept(this)
169-
}
169+
repeat(counter.toInt()) { executeBlockInScope(LiloScope(currentScope), statement.body) }
170170
return
171171
}
172172

@@ -190,7 +190,6 @@ class LiloInterpreter : StatementVisitor<Unit>, ExpressionVisitor<Any> {
190190
Timber.tag(TAG).d("Evaluate CircleStatement")
191191
val radius = statement.radius.accept(this)
192192
if (radius is Float) {
193-
//canvas.drawCircle(currentXPosition, currentYPosition, radius.toFloat(), turtlePaint)
194193
val circleInst = CircleInst(currentXPosition, currentYPosition, radius.toFloat())
195194
onInstructionEmitterListener(circleInst)
196195
} else {
@@ -542,12 +541,8 @@ class LiloInterpreter : StatementVisitor<Unit>, ExpressionVisitor<Any> {
542541
if (statement is ReturnStatement) {
543542
returnValue = statement.value.accept(this)
544543
break
545-
} else if (statement is ExpressionStatement) {
546-
returnValue = statement.expression.accept(this)
547-
break
548-
} else {
549-
statement.accept(this)
550544
}
545+
statement.accept(this)
551546
}
552547
currentScope = previousScope
553548
return returnValue

0 commit comments

Comments
 (0)