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

Skip to content

Commit 873f67a

Browse files
author
Troy Melhase
committed
Adds support for if-expr and else-expr forms. See issue natural#4.
1 parent 1278f29 commit 873f67a

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

java2python/compiler/visitor.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,24 +455,35 @@ def acceptIf(self, node, memo):
455455
children = node.children
456456
ifStat = self.factory.statement('if', fs=FS.lsrc, parent=self)
457457
ifStat.expr.walk(children[0], memo)
458-
ifBlock = self.factory.methodContent(parent=self)
459-
ifBlock.walk(node.children[1], memo)
458+
if node.children[1].type == tokens.BLOCK_SCOPE:
459+
ifBlock = self.factory.methodContent(parent=self)
460+
ifBlock.walk(node.children[1], memo)
461+
elif node.children[1].type == tokens.EXPR:
462+
ifBlock = self.factory.expr(parent=ifStat)
463+
ifBlock.walk(node.children[1], memo)
460464
if len(children) == 3:
461465
nextNode = children[2]
462466
nextType = nextNode.type
467+
463468
while nextType == tokens.IF:
464469
nextStat = self.factory.statement('elif', fs=FS.lsrc, parent=self)
465470
nextStat.expr.walk(nextNode.children[0], memo)
466-
nextBlock = self.factory.methodContent(parent=self)
471+
if nextNode.children[1].type == tokens.BLOCK_SCOPE:
472+
nextBlock = self.factory.methodContent(parent=self)
473+
else:
474+
nextBlock = self.factory.expr(parent=nextStat)
467475
nextBlock.walk(nextNode.children[1], memo)
468-
try:
469-
nextNode = nextNode.children[2]
470-
nextType = nextNode.type
471-
except (IndexError, ):
472-
nextType = None
476+
nextNode = nextNode.children[2]
477+
nextType = nextNode.type
478+
473479
if nextType == tokens.BLOCK_SCOPE:
474480
self.factory.statement('else', fs=FS.lc, parent=self)
475481
self.factory.methodContent(parent=self).walk(nextNode, memo)
482+
elif nextType == tokens.EXPR:
483+
elseStat = self.factory.statement('else', fs=FS.lc, parent=self)
484+
elseBlock = self.factory.expr(parent=elseStat)
485+
elseBlock.walk(nextNode, memo)
486+
476487

477488
def acceptSwitch(self, node, memo):
478489
""" Accept and process a switch block. """

test/If2.java

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

test/If3.java

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

test/If4.java

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

0 commit comments

Comments
 (0)