Commit d1dd80fd authored by Stefan Behnel's avatar Stefan Behnel

enable the switch transform also for long 'or' expressions in a single 'if' statement

parent 117049de
...@@ -56,9 +56,8 @@ class SwitchTransform(Visitor.VisitorTransform): ...@@ -56,9 +56,8 @@ class SwitchTransform(Visitor.VisitorTransform):
def visit_IfStatNode(self, node): def visit_IfStatNode(self, node):
self.visitchildren(node) self.visitchildren(node)
if len(node.if_clauses) < 3:
return node
common_var = None common_var = None
case_count = 0
cases = [] cases = []
for if_clause in node.if_clauses: for if_clause in node.if_clauses:
var, conditions = self.extract_conditions(if_clause.condition) var, conditions = self.extract_conditions(if_clause.condition)
...@@ -70,9 +69,12 @@ class SwitchTransform(Visitor.VisitorTransform): ...@@ -70,9 +69,12 @@ class SwitchTransform(Visitor.VisitorTransform):
return node return node
else: else:
common_var = var common_var = var
case_count += len(conditions)
cases.append(Nodes.SwitchCaseNode(pos = if_clause.pos, cases.append(Nodes.SwitchCaseNode(pos = if_clause.pos,
conditions = conditions, conditions = conditions,
body = if_clause.body)) body = if_clause.body))
if case_count < 2:
return node
common_var = unwrap_node(common_var) common_var = unwrap_node(common_var)
return Nodes.SwitchStatNode(pos = node.pos, return Nodes.SwitchStatNode(pos = node.pos,
......
...@@ -62,6 +62,33 @@ __doc__ = u""" ...@@ -62,6 +62,33 @@ __doc__ = u"""
12 12
>>> switch_c(13) >>> switch_c(13)
0 0
>>> switch_or(0)
0
>>> switch_or(1)
1
>>> switch_or(2)
1
>>> switch_or(3)
1
>>> switch_or(4)
0
>>> switch_short(0)
0
>>> switch_short(1)
1
>>> switch_short(2)
2
>>> switch_short(3)
0
>>> switch_off(0)
0
>>> switch_off(1)
1
>>> switch_off(2)
0
""" """
def switch_simple_py(x): def switch_simple_py(x):
...@@ -123,3 +150,26 @@ def switch_c(int x): ...@@ -123,3 +150,26 @@ def switch_c(int x):
else: else:
return 0 return 0
return -1 return -1
def switch_or(int x):
if x == 1 or x == 2 or x == 3:
return 1
else:
return 0
return -1
def switch_short(int x):
if x == 1:
return 1
elif 2 == x:
return 2
else:
return 0
return -1
def switch_off(int x):
if x == 1:
return 1
else:
return 0
return -1
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment