nitg: handle direct compilation of comprehension arrays
authorJean Privat <jean@pryen.org>
Wed, 17 Dec 2014 16:48:32 +0000 (11:48 -0500)
committerJean Privat <jean@pryen.org>
Wed, 17 Dec 2014 20:29:28 +0000 (15:29 -0500)
Because of `transform`, it is only used if compiled
with `--disable-phase transform`

Signed-off-by: Jean Privat <jean@pryen.org>

src/compiler/abstract_compiler.nit

index 4c7987e..eeabf00 100644 (file)
@@ -1534,6 +1534,15 @@ abstract class AbstractCompilerVisitor
        fun stmt(nexpr: nullable AExpr)
        do
                if nexpr == null then return
+
+               var narray = nexpr.comprehension
+               if narray != null then
+                       var recv = frame.comprehension.as(not null)
+                       var val = expr(nexpr, narray.element_mtype)
+                       compile_callsite(narray.push_callsite.as(not null), [recv, val])
+                       return
+               end
+
                var old = self.current_node
                self.current_node = nexpr
                nexpr.stmt(self)
@@ -1683,6 +1692,9 @@ class Frame
        # Labels associated to a each escapemarks.
        # Because of inlinings, escape-marks must be associated to their context (the frame)
        private var escapemark_names = new HashMap[EscapeMark, String]
+
+       # The array comprehension currently filled, if any
+       private var comprehension: nullable RuntimeVariable = null
 end
 
 redef class MType
@@ -2652,11 +2664,16 @@ redef class AArrayExpr
        do
                var mtype = self.element_mtype.as(not null)
                var array = new Array[RuntimeVariable]
+               var res = v.array_instance(array, mtype)
+
+               var old_comprehension = v.frame.comprehension
+               v.frame.comprehension = res
                for nexpr in self.n_exprs do
-                       var i = v.expr(nexpr, mtype)
-                       array.add(i)
+                       v.stmt(nexpr)
                end
-               return v.array_instance(array, mtype)
+               v.frame.comprehension = old_comprehension
+
+               return res
        end
 end