Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c6ee8e

Browse files
committed
fix continue in for loops
1 parent 3463036 commit 0c6ee8e

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

java2python/compiler/visitor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,10 @@ def acceptCatch(self, node, memo):
407407

408408
def acceptContinue(self, node, memo):
409409
""" Accept and process a continue statement. """
410+
parent = node.parents(lambda x: x.type in {tokens.FOR, tokens.FOR_EACH, tokens.DO, tokens.WHILE}).next()
411+
if parent.type == tokens.FOR:
412+
updateStat = self.factory.expr(parent=self)
413+
updateStat.walk(parent.firstChildOfType(tokens.FOR_UPDATE), memo)
410414
contStat = self.factory.statement('continue', fs=FS.lsr, parent=self)
411415
if len(node.children):
412416
warn('Detected unhandled continue statement with label; generated code incorrect.')

test/Continue0.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ public static void main(String[] args) {
33
int x = 0;
44
while (x < 10) {
55
System.out.println(x);
6-
if (x==6) {
7-
break ;
6+
if (x == 6) {
7+
break;
88
} else {
9-
x+=2;
10-
continue ;
9+
x += 2;
10+
continue;
1111
}
12-
}
12+
}
1313
}
1414
}

test/Continue1.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Continue1 {
2+
public static void main(String[] args) {
3+
int[] ints = {1, 2, 3, 4, 5, 6, 7};
4+
for (int x : ints) {
5+
if (x == 6) {
6+
break;
7+
} else if (x == 3) {
8+
continue;
9+
}
10+
System.out.println(x);
11+
}
12+
}
13+
}

test/Continue2.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Continue2 {
2+
public static void main(String[] args) {
3+
int x = 0;
4+
do {
5+
System.out.println(x);
6+
if (x == 6) {
7+
break;
8+
} else {
9+
x += 2;
10+
continue;
11+
}
12+
} while (x < 10);
13+
}
14+
}

test/ForLoop2.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class ForLoop2 {
2+
public static void main(String[] args) {
3+
for (int i = 0; i < 3; i++) {
4+
if (i == 1)
5+
continue;
6+
System.out.println(i);
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)