Skip to content

Commit 5bff734

Browse files
author
Troy Melhase
committed
Changes and test case for issue natural#26.
1 parent f70b982 commit 5bff734

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

java2python/lib/walker.g

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -475,19 +475,35 @@ statement [block]
475475
case_group [block, switch_expr]
476476
: {
477477
other = block.newStatement("elif")
478-
right = block.missingValue
478+
right = None
479479
}
480480
#(CASE_GROUP
481481
(#("case"
482-
right = expression[other, False]) | "default")+
482+
newright = expression[other, False]
483+
{
484+
if not right:
485+
right = ("%s", newright)
486+
else:
487+
right = ("%s, %s", (right, newright))
488+
}
489+
) | "default" { right = block.emptyAssign } )+
483490
statement_list[other]
484491
)
485492
{
486-
if right is block.missingValue:
493+
if right is block.emptyAssign:
487494
other.setName("else")
488495
other.setExpression(None)
496+
elif right[0] == "%s":
497+
other.setExpression(("%s == %s", (switch_expr, right)))
489498
else:
490-
other.setExpression(("%s == %s", (switch_expr, ("%s", right))))
499+
other.setExpression(("%s in (%s)", (switch_expr, right)))
500+
501+
/* if only one break statement in the elif block, delete it, then
502+
a pass statement will be generated */
503+
if len(other.lines) == 1 and \
504+
hasattr(other.lines[0], "name") and \
505+
other.lines[0].name == "break":
506+
del other.lines[0]
491507
}
492508
;
493509

java2python/tests/Switches.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Switches {
2+
public static void main(String[] args) {
3+
System.out.println( switches() );
4+
}
5+
6+
public static int switches() {
7+
int i;
8+
i = 3;
9+
10+
switch (i) {
11+
case 1:
12+
case 2:
13+
break;
14+
case 3:
15+
i = 2;
16+
default:
17+
return -4;
18+
}
19+
20+
switch (i) {
21+
case 3:
22+
default:
23+
i = 2;
24+
}
25+
return -1;
26+
}
27+
28+
}
29+

0 commit comments

Comments
 (0)