Commit 7789c221 authored by Stefan Behnel's avatar Stefan Behnel

Apply the switch-transformation also recursively to if-clauses and else-body...

Apply the switch-transformation also recursively to if-clauses and else-body of if-statements that were converted.
parent 9470a79b
...@@ -2,6 +2,16 @@ ...@@ -2,6 +2,16 @@
Cython Changelog Cython Changelog
================ ================
0.28.2 (2018-??-??)
===================
Bugs fixed
----------
* The ``switch`` statement generation failed to apply to the body of converted
if-statements
0.28.1 (2018-03-18) 0.28.1 (2018-03-18)
=================== ===================
......
...@@ -1205,6 +1205,11 @@ class SwitchTransform(Visitor.EnvTransform): ...@@ -1205,6 +1205,11 @@ class SwitchTransform(Visitor.EnvTransform):
self.visitchildren(node) self.visitchildren(node)
return node return node
# Recurse into body subtrees that we left untouched so far.
self.visitchildren(node, 'else_clause')
for case in cases:
self.visitchildren(case, 'body')
common_var = unwrap_node(common_var) common_var = unwrap_node(common_var)
switch_node = Nodes.SwitchStatNode(pos=node.pos, switch_node = Nodes.SwitchStatNode(pos=node.pos,
test=common_var, test=common_var,
......
...@@ -143,6 +143,75 @@ def switch_c(int x): ...@@ -143,6 +143,75 @@ def switch_c(int x):
return -1 return -1
@cython.test_assert_path_exists(
'//SwitchStatNode',
'//SwitchStatNode//SwitchStatNode',
)
@cython.test_fail_if_path_exists('//BoolBinopNode', '//PrimaryCmpNode')
def switch_in_switch(int x, int y):
"""
>>> switch_in_switch(1, 1)
(1, 1)
>>> switch_in_switch(1, 2)
(1, 2)
>>> switch_in_switch(1, 4)
(1, 3)
>>> switch_in_switch(2, 1)
(2, 1)
>>> switch_in_switch(2, 2)
(2, 2)
>>> switch_in_switch(2, 3)
(2, 3)
>>> switch_in_switch(2, 4)
(2, 4)
>>> switch_in_switch(2, 20)
(2, 4)
>>> switch_in_switch(3, 0)
False
>>> switch_in_switch(3, 1)
True
>>> switch_in_switch(3, 2)
True
>>> switch_in_switch(3, 3)
True
>>> switch_in_switch(3, 4)
False
>>> switch_in_switch(20, 0)
True
>>> switch_in_switch(20, 1)
False
>>> switch_in_switch(20, 3)
False
>>> switch_in_switch(20, 4)
True
"""
if x == 1:
if y == 1:
return 1,1
elif y == 2:
return 1,2
else:
return 1,3
elif x == 2:
if y == 1:
return 2,1
elif y == 2:
return 2,2
elif y == 3:
return 2,3
else:
return 2,4
elif x == 3:
return y in (1,2,3)
else:
return y not in (1,2,3)
return 'FAILED'
@cython.test_assert_path_exists('//SwitchStatNode') @cython.test_assert_path_exists('//SwitchStatNode')
@cython.test_fail_if_path_exists('//IfStatNode') @cython.test_fail_if_path_exists('//IfStatNode')
def switch_or(int x): def switch_or(int x):
......
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