Skip to content

Commit

Permalink
corrected loop controls
Browse files Browse the repository at this point in the history
  • Loading branch information
Calamity210 committed Sep 7, 2020
1 parent 05c9a36 commit da271c4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
26 changes: 23 additions & 3 deletions examples/for_loop.birb
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
for (int i = 0; i < 10; i++) {
screm(i);
}
// output 0
// output 1
// output 2
Expand All @@ -11,3 +8,26 @@ for (int i = 0; i < 10; i++) {
// output 7
// output 8
// output 9
for (int i = 0; i < 10; i++) {
screm(i);
}

// output 0
// output 1
for (int j = 0; j < 5; j++) {
if (j == 2) {
break;
}
screm(j);
}

// output 0
// output 1
// output 2
// output 4
for (int k = 0; k < 5; k++) {
if (k == 3) {
continue;
}
screm(k);
}
18 changes: 10 additions & 8 deletions lib/runtime/runtime.dart
Original file line number Diff line number Diff line change
Expand Up @@ -947,8 +947,8 @@ Future<ASTNode> visitCompound(Runtime runtime, ASTNode node) async {
return null;
}
} else if (visited.type == ASTType.AST_BREAK ||
await visited.type == ASTType.AST_CONTINUE) {
return await visited;
visited.type == ASTType.AST_CONTINUE) {
return visited;
}
}
}
Expand Down Expand Up @@ -1963,15 +1963,15 @@ Future<ASTNode> visitIf(Runtime runtime, ASTNode node) async {
if (node.ifExpression.type == ASTType.AST_UNARYOP) {
if (boolEval(await visit(runtime, node.ifExpression.unaryOpRight)) ==
false) {
await visit(runtime, node.ifBody);
return await visit(runtime, node.ifBody);
} else {
if (node.ifElse != null) return await visit(runtime, node.ifElse);

if (node.elseBody != null) return await visit(runtime, node.elseBody);
}
} else {
if (boolEval(await visit(runtime, node.ifExpression))) {
await visit(runtime, node.ifBody);
return await visit(runtime, node.ifBody);
} else {
if (node.ifElse != null) return await visit(runtime, node.ifElse);

Expand Down Expand Up @@ -2054,7 +2054,7 @@ Future<ASTNode> visitWhile(Runtime runtime, ASTNode node) async {
var visited = await visit(runtime, node.whileBody);

if (visited.type == ASTType.AST_BREAK) break;
if (visited.type == ASTType.AST_CONTINUE) continue;
else if (visited.type == ASTType.AST_CONTINUE) continue;
}

return node;
Expand All @@ -2067,9 +2067,11 @@ Future<ASTNode> visitFor(Runtime runtime, ASTNode node) async {
while (boolEval(await visit(runtime, node.forConditionStatement))) {
var visited = await visit(runtime, node.forBody);

if (visited.type == ASTType.AST_BREAK) break;
if (visited.type == ASTType.AST_CONTINUE) continue;

if (visited is BreakNode) break;
else if (visited is ContinueNode) {
await visit(runtime, node.forChangeStatement);
continue;
}
await visit(runtime, node.forChangeStatement);
}

Expand Down
11 changes: 0 additions & 11 deletions package-lock.json

This file was deleted.

0 comments on commit da271c4

Please sign in to comment.