Antonio Cuni avatar Antonio Cuni committed 97e1b1a

in-progress: make list comprehensions in their own function; I just check this is to push it on my notebook :-)

Comments (0)

Files changed (4)

pypy/interpreter/astcompiler/codegen.py

         codegen.emit_op(ops.POP_TOP)
 
 
+class __extend__(ast.ListComp):
+
+    def build_container(self, codegen):
+        codegen.emit_op_arg(ops.BUILD_LIST, 0)
+
+    def get_generators(self):
+        return self.generators
+
+    def accept_comp_iteration(self, codegen, index):
+        self.elt.walkabout(codegen)
+        codegen.emit_op_arg(ops.LIST_APPEND, index + 1)
+
+
 class __extend__(ast.SetComp):
 
     def build_container(self, codegen):
         self.use_next_block(anchor)
 
     def visit_ListComp(self, lc):
-        self.update_position(lc.lineno)
-        if len(lc.generators) != 1 or lc.generators[0].ifs:
-            single = False
-            self.emit_op_arg(ops.BUILD_LIST, 0)
-        else:
-            single = True
-        self._listcomp_generator(lc.generators, 0, lc.elt, single=single)
+        self._compile_comprehension(lc, "<listcomp>",
+                                    ComprehensionCodeGenerator)
+
+    ## def visit_ListComp(self, lc):
+    ##     self.update_position(lc.lineno)
+    ##     if len(lc.generators) != 1 or lc.generators[0].ifs:
+    ##         single = False
+    ##         self.emit_op_arg(ops.BUILD_LIST, 0)
+    ##     else:
+    ##         single = True
+    ##     self._listcomp_generator(lc.generators, 0, lc.elt, single=single)
 
     def _comp_generator(self, node, generators, gen_index):
         start = self.new_block()

pypy/interpreter/astcompiler/symtable.py

             item.walkabout(self)
         self.pop_scope()
 
+    def visit_ListComp(self, listcomp):
+        self._visit_comprehension(listcomp, listcomp.generators, listcomp.elt)
+
     def visit_GeneratorExp(self, genexp):
         self._visit_comprehension(genexp, genexp.generators, genexp.elt)
 

pypy/interpreter/astcompiler/test/test_compiler.py

 class TestOptimizations:
     def count_instructions(self, source):
         code, blocks = generate_function_code(source, self.space)
+        import pdb;pdb.set_trace()
         instrs = []
         for block in blocks:
             instrs.extend(block.instructions)

pypy/interpreter/astcompiler/test/test_symtable.py

     def test_genexp(self):
         self.check_comprehension("(%s)")
 
+    def test_listcomp(self):
+        self.check_comprehension("[%s]")
+
     def test_setcomp(self):
         self.check_comprehension("{%s}")
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.