Skip to content

Commit 9590d48

Browse files
author
Troy Melhase
committed
Adds support for 'else if' without 'else'. Fixes natural#15.
1 parent 86fdb2a commit 9590d48

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

java2python/compiler/visitor.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,14 +482,19 @@ def acceptIf(self, node, memo):
482482
else:
483483
nextBlock = self.factory.methodContent(parent=self)
484484
nextBlock.walk(nextNode.children[1], memo)
485-
nextNode = nextNode.children[2]
486-
nextType = nextNode.type
485+
486+
try:
487+
nextNode = nextNode.children[2]
488+
except (IndexError, ):
489+
nextType = None
490+
else:
491+
nextType = nextNode.type
487492

488493
if nextType == tokens.EXPR:
489494
elseStat = self.factory.statement('else', fs=FS.lc, parent=self)
490495
elseBlock = self.factory.expr(parent=elseStat)
491496
elseBlock.walk(nextNode, memo)
492-
else: # nextType != tokens.BLOCK_SCOPE:
497+
elif nextType: # nextType != tokens.BLOCK_SCOPE:
493498
self.factory.statement('else', fs=FS.lc, parent=self)
494499
self.factory.methodContent(parent=self).walk(nextNode, memo)
495500

test/If6.java

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

0 commit comments

Comments
 (0)