Skip to content

Commit f4e1db3

Browse files
committed
Add support for for loops without braces
1 parent b803756 commit f4e1db3

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

java2python/compiler/visitor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ def acceptDo(self, node, memo):
434434
tokens.FOR_EACH,
435435
tokens.METHOD_CALL,
436436
tokens.ARGUMENT_LIST,
437+
tokens.FOR,
437438
)
438439

439440
def acceptExpr(self, node, memo):
@@ -452,7 +453,9 @@ def acceptFor(self, node, memo):
452453
else:
453454
whileStat.expr.walk(cond, memo)
454455
whileBlock = self.factory.methodContent(parent=self)
455-
if not node.firstChildOfType(tokens.BLOCK_SCOPE).children:
456+
if not node.firstChildOfType(tokens.BLOCK_SCOPE): # A for loop without braces
457+
whileBlock.walk(node.children[3], memo)
458+
elif not node.firstChildOfType(tokens.BLOCK_SCOPE).children:
456459
self.factory.expr(left='pass', parent=whileBlock)
457460
else:
458461
whileBlock.walk(node.firstChildOfType(tokens.BLOCK_SCOPE), memo)

test/ForLoop3.java

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

test/ForLoop4.java

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

0 commit comments

Comments
 (0)