removed code redundancy: do not generate ParallelAssignmentNode in parser at all...
authorStefan Behnel <scoder@users.berlios.de>
Thu, 4 Mar 2010 21:56:20 +0000 (22:56 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Thu, 4 Mar 2010 21:56:20 +0000 (22:56 +0100)
Cython/Compiler/ParseTreeTransforms.py
Cython/Compiler/Parsing.py

index e58a1784867f4ef9401a2c7fcffb1001fcc88e57..0d6aa2b9773e03e43402728ffd25ac4761e6fc1b 100644 (file)
@@ -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 = []
index 2cc046f02e5041f706fe152c605c45d2e139606f..c97d700c8eb4ccf459910729dc4789b3c86063c3 100644 (file)
@@ -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()