Commit 36d1f095 authored by Stefan Behnel's avatar Stefan Behnel

removed code redundancy: do not generate ParallelAssignmentNode in parser at...

removed code redundancy: do not generate ParallelAssignmentNode in parser at all, only in PostParse transform
parent 21205bb0
...@@ -242,18 +242,25 @@ class PostParse(CythonTransform): ...@@ -242,18 +242,25 @@ class PostParse(CythonTransform):
self.context.nonfatal_error(e) self.context.nonfatal_error(e)
return None return None
def visit_ParallelAssignmentNode(self, node): # support for parallel assignments (a,b = b,a) by splitting them
# into separate assignments that are executed rhs-first
def visit_SingleAssignmentNode(self, node):
self.visitchildren(node)
return self._visit_assignment_node(node, [node.lhs, node.rhs])
def visit_CascadedAssignmentNode(self, node):
self.visitchildren(node)
return self._visit_assignment_node(node, node.lhs_list + [node.rhs])
def _visit_assignment_node(self, node, expr_list):
"""Flatten parallel assignments into separate single """Flatten parallel assignments into separate single
assignments or cascaded assignments. assignments or cascaded assignments.
""" """
self.visitchildren(node) if sum([ 1 for expr in expr_list if expr.is_sequence_constructor ]) < 2:
expr_list = [] # no parallel assignments => nothing to do
for assign_node in node.stats: return node
if isinstance(assign_node, Nodes.CascadedAssignmentNode):
expr_list.extend(assign_node.lhs_list)
else:
expr_list.append(assign_node.lhs)
expr_list.append(assign_node.rhs)
expr_list_list = [] expr_list_list = []
flatten_parallel_assignments(expr_list, expr_list_list) flatten_parallel_assignments(expr_list, expr_list_list)
nodes = [] nodes = []
......
...@@ -920,18 +920,12 @@ def p_expression_or_assignment(s): ...@@ -920,18 +920,12 @@ def p_expression_or_assignment(s):
rhs = expr_list[-1] rhs = expr_list[-1]
if len(expr_list) == 2: if len(expr_list) == 2:
node = Nodes.SingleAssignmentNode(rhs.pos, return Nodes.SingleAssignmentNode(rhs.pos,
lhs = expr_list[0], rhs = rhs) lhs = expr_list[0], rhs = rhs)
else: else:
node = Nodes.CascadedAssignmentNode(rhs.pos, return Nodes.CascadedAssignmentNode(rhs.pos,
lhs_list = expr_list[:-1], rhs = rhs) lhs_list = expr_list[:-1], rhs = rhs)
if sum([ 1 for expr in expr_list if expr.is_sequence_constructor ]) > 1:
# at least one parallel assignment
return Nodes.ParallelAssignmentNode(node.pos, stats = [node])
else:
return node
def p_print_statement(s): def p_print_statement(s):
# s.sy == 'print' # s.sy == 'print'
pos = s.position() pos = s.position()
......
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