Skip to content

Commit e58fb46

Browse files
committed
Make generated code for switch/case more clean
1 parent c2d3021 commit e58fb46

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

java2python/lib/sourcetypes.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@ def declFormat(self):
307307

308308
@property
309309
def className(self):
310+
""" Return className of the source in.
311+
312+
"""
310313
if isinstance(self, Class):
311314
return self.name
312315
elif self.parent is not None:
@@ -380,10 +383,7 @@ def newSwitch(self):
380383
while_stat = Statement(self, 'while')
381384
while_stat.setExpression('True')
382385
self.addSource(while_stat)
383-
s = Statement(while_stat, 'if')
384-
s.expr = 'False'
385-
while_stat.addSource(s)
386-
return while_stat, s
386+
return while_stat
387387

388388
def newStatement(self, name):
389389
""" creates a new Statement as a child of this block
@@ -407,7 +407,21 @@ def newStatementBefore(self, name, stat=None):
407407
self.addSourceBefore(s, stat)
408408
return s
409409

410+
def removeStatement(self, stat):
411+
""" remove a statement from source.
412+
413+
@param stat Statement to be removed.
414+
@return None
415+
"""
416+
idx = self.lines.index(stat)
417+
del self.lines[idx]
418+
410419
def newVariable(self, name=None):
420+
""" creates a new Variable for the block
421+
422+
@param name name of the variable
423+
@return Variable instance
424+
"""
411425
var = Variable(self)
412426
self.addVariable(var, True)
413427
return var
@@ -459,23 +473,6 @@ def setName(self, name):
459473
"""
460474
self.name = name
461475

462-
def fixSwitch(self, while_block, block):
463-
""" fixes the first clause in an generated switch statement
464-
465-
@param block Statement instance and child of this block
466-
@return None
467-
"""
468-
lines = while_block.lines
469-
if (not block in lines) or (block.name != 'if'):
470-
return
471-
i = lines.index(block)
472-
if (len(lines) > i):
473-
next = lines[i+1]
474-
if next.name == 'elif':
475-
lines.remove(next)
476-
block.expr = next.expr
477-
block.lines = next.lines
478-
479476
def trimLines(self):
480477
""" removes empty lines from the end of this block
481478

java2python/lib/walker.g

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -471,14 +471,13 @@ statement [block]
471471

472472

473473
| {
474-
while_block, switch_block = block.newSwitch()
474+
while_block = block.newSwitch()
475475
}
476476
#("switch" switch_expr = expression[block, False]
477477
(c:case_group[while_block, switch_expr])*
478478
)
479479
{
480480
while_block.newStatement("break")
481-
block.fixSwitch(while_block, switch_block)
482481
}
483482

484483
| {
@@ -505,17 +504,24 @@ case_group [block, switch_expr]
505504
(#("case"
506505
newright = expression[other, False]
507506
{
508-
if not right:
509-
right = ("%s", newright)
510-
else:
511-
right = ("%s, %s", (right, newright))
507+
if not right:
508+
right = ("%s", newright)
509+
else:
510+
right = ("%s, %s", (right, newright))
511+
}
512+
)
513+
| "default"
514+
{
515+
right = block.emptyAssign
516+
block.removeStatement(other)
517+
other = block
512518
}
513-
) | "default" { right = block.emptyAssign } )+
514-
statement_list[other]
519+
)+
520+
statement_list[other]
515521
)
516522
{
517523
if right is block.emptyAssign:
518-
other.setExpression("True")
524+
pass
519525
elif right[0] == "%s":
520526
other.setExpression(("%s == %s", (switch_expr, right)))
521527
else:

java2python/tests/Switches.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public int switch4(int i) {
5757
switch (i) {
5858
case 1:
5959
if (i == 1) break;
60+
case 5:
6061
default:
6162
i = 2;
6263
}

0 commit comments

Comments
 (0)