From: Stefan Behnel Date: Thu, 4 Mar 2010 21:56:20 +0000 (+0100) Subject: removed code redundancy: do not generate ParallelAssignmentNode in parser at all... X-Git-Tag: 0.13.beta0~2^2~102^2~15 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=36d1f095a6a29dd11d5e2d9e189e5f60cf82d19e;p=cython.git removed code redundancy: do not generate ParallelAssignmentNode in parser at all, only in PostParse transform --- diff --git a/Cython/Compiler/ParseTreeTransforms.py b/Cython/Compiler/ParseTreeTransforms.py index e58a1784..0d6aa2b9 100644 --- a/Cython/Compiler/ParseTreeTransforms.py +++ b/Cython/Compiler/ParseTreeTransforms.py @@ -242,18 +242,25 @@ class PostParse(CythonTransform): self.context.nonfatal_error(e) 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 assignments or cascaded assignments. """ - self.visitchildren(node) - expr_list = [] - for assign_node in node.stats: - 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) + if sum([ 1 for expr in expr_list if expr.is_sequence_constructor ]) < 2: + # no parallel assignments => nothing to do + return node + expr_list_list = [] flatten_parallel_assignments(expr_list, expr_list_list) nodes = [] diff --git a/Cython/Compiler/Parsing.py b/Cython/Compiler/Parsing.py index 2cc046f0..c97d700c 100644 --- a/Cython/Compiler/Parsing.py +++ b/Cython/Compiler/Parsing.py @@ -920,18 +920,12 @@ def p_expression_or_assignment(s): rhs = expr_list[-1] if len(expr_list) == 2: - node = Nodes.SingleAssignmentNode(rhs.pos, + return Nodes.SingleAssignmentNode(rhs.pos, lhs = expr_list[0], rhs = rhs) else: - node = Nodes.CascadedAssignmentNode(rhs.pos, + return Nodes.CascadedAssignmentNode(rhs.pos, 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): # s.sy == 'print' pos = s.position()