syntax: refactor AClosureCallExpr
authorJean Privat <jean@pryen.org>
Tue, 16 Jun 2009 20:33:54 +0000 (16:33 -0400)
committerJean Privat <jean@pryen.org>
Wed, 24 Jun 2009 19:47:33 +0000 (15:47 -0400)
Add a alternative in the grammar to profit from generated walkers.
Makes AClosureCallExpr a subclass of AAbsAbsSendExpr instead of ACallFormExpr.

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

src/compiling/compiling_methods.nit
src/parser/nit.sablecc3xx
src/parser/parser_abs.nit
src/parser/parser_nodes.nit
src/parser/parser_prod.nit
src/syntax/typing.nit

index c8e56c0..f99df6d 100644 (file)
@@ -1692,7 +1692,7 @@ redef class AClosureDecl
 end
 
 redef class AClosureCallExpr
-       redef meth intern_compile_call(v)
+       meth intern_compile_call(v: CompilerVisitor): String
        do
                var cargs = new Array[String]
                compile_arguments_in(v, cargs)
@@ -1736,6 +1736,21 @@ redef class AClosureCallExpr
                end
                return va
        end
+
+       redef meth compile_expr(v)
+       do
+               var e = intern_compile_call(v)
+               assert e != null
+               return e
+       end
+
+       redef meth compile_stmt(v)
+       do
+               var e = intern_compile_call(v)
+               if e != null then
+                       v.add_instr(e + ";")
+               end
+       end
 end
 
 redef class AProxyExpr
index 1104c50..073d135 100644 (file)
@@ -629,6 +629,7 @@ expr        = {block} expr*
        | {bra} expr [args]:expr* [closure_defs]:closure_def* 
        | {bra_assign} expr [args]:expr* assign [value]:expr 
        | {bra_reassign} expr [args]:expr* assign_op [value]:expr 
+       | {closure_call} id [args]:expr* [closure_defs]:closure_def*
        | {var} id
        | {var_assign} id assign [value]:expr 
        | {var_reassign} id assign_op [value]:expr 
index 04a70b6..65fb483 100644 (file)
@@ -826,6 +826,12 @@ special PExpr
     readable writable attr _n_assign_op: PAssignOp = null
     readable writable attr _n_value: PExpr = null
 end
+class AClosureCallExpr
+special PExpr
+    readable writable attr _n_id: TId = null
+    readable writable attr _n_args: List[PExpr] = null
+    readable writable attr _n_closure_defs: List[PClosureDef] = null
+end
 class AVarExpr
 special PExpr
     readable writable attr _n_id: TId = null
index 49d2622..b135bfc 100644 (file)
@@ -779,6 +779,12 @@ class ABraReassignExpr
 special ABraFormExpr
 special AReassignFormExpr 
 end
+class AClosureCallExpr
+special PExpr
+    readable writable attr _n_id: TId = null
+    readable writable attr _n_args: List[PExpr] = null
+    readable writable attr _n_closure_defs: List[PClosureDef] = null
+end
 class AVarExpr
 special AVarFormExpr
 end
@@ -790,16 +796,6 @@ class AVarReassignExpr
 special AVarFormExpr
 special AReassignFormExpr 
 end
-class AClosureCallExpr
-special ACallFormExpr 
-       init(i: TId, a: List[PExpr], c: List[PClosureDef])
-       do
-               _n_id = i
-               _n_args = a
-               _n_closure_defs = c
-               _n_expr = null
-       end
-end
 class ARangeExpr
 special PExpr
     readable writable attr _n_expr: PExpr = null 
index 92f9525..24214af 100644 (file)
@@ -9269,6 +9269,115 @@ redef class ABraReassignExpr
         end
     end
 end
+redef class AClosureCallExpr
+    redef meth n_id=(n: TId)
+    do
+        _n_id = n
+        if n != null then
+           n.parent = self
+        end
+    end
+
+    private init empty_init do end
+
+    init init_aclosurecallexpr (
+            n_id: TId ,
+            n_args: Collection[Object] , # Should be Collection[PExpr]
+            n_closure_defs: Collection[Object]  # Should be Collection[PClosureDef]
+    )
+    do
+        empty_init
+        _n_id = n_id
+       if n_id != null then
+               n_id.parent = self
+       end
+        _n_args = new List[PExpr]
+       for n in n_args do
+               assert n isa PExpr
+               _n_args.add(n)
+               n.parent = self
+       end
+        _n_closure_defs = new List[PClosureDef]
+       for n in n_closure_defs do
+               assert n isa PClosureDef
+               _n_closure_defs.add(n)
+               n.parent = self
+       end
+    end
+
+    redef meth replace_child(old_child: PNode, new_child: PNode)
+    do
+        assert old_child != null
+        if _n_id == old_child then
+            if new_child != null then
+                new_child.parent = self
+               assert new_child isa TId
+                _n_id = new_child
+           else
+               _n_id = null
+            end
+            return
+       end
+        for i in [0.._n_args.length[ do
+            if _n_args[i] == old_child then
+                if new_child != null then
+                   assert new_child isa PExpr
+                    _n_args[i] = new_child
+                    new_child.parent = self
+                else
+                    _n_args.remove_at(i)
+                end
+                return
+            end
+        end
+        for i in [0.._n_closure_defs.length[ do
+            if _n_closure_defs[i] == old_child then
+                if new_child != null then
+                   assert new_child isa PClosureDef
+                    _n_closure_defs[i] = new_child
+                    new_child.parent = self
+                else
+                    _n_closure_defs.remove_at(i)
+                end
+                return
+            end
+        end
+    end
+
+    redef meth visit_all(v: Visitor)
+    do
+        if _n_id != null then
+            v.visit(_n_id)
+        end
+            for n in _n_args do
+                v.visit(n)
+           end
+            for n in _n_closure_defs do
+                v.visit(n)
+           end
+    end
+
+    redef meth visit_all_reverse(v: Visitor)
+    do
+        if _n_id != null then
+            v.visit(_n_id)
+        end
+       do
+           var i = _n_args.length
+            while i >= 0 do
+                v.visit(_n_args[i])
+               i = i - 1
+           end
+       end
+       do
+           var i = _n_closure_defs.length
+            while i >= 0 do
+                v.visit(_n_closure_defs[i])
+               i = i - 1
+           end
+       end
+    end
+end
 redef class AVarExpr
     redef meth n_id=(n: TId)
     do
index d3a196a..2bf36f4 100644 (file)
@@ -1273,7 +1273,7 @@ redef class ACallFormExpr
                        var variable = v.variable_ctx[name]
                        if variable != null then
                                if variable isa ClosureVariable then
-                                       var n = new AClosureCallExpr(n_id, n_args, n_closure_defs)
+                                       var n = new AClosureCallExpr.init_aclosurecallexpr(n_id, n_args, n_closure_defs)
                                        replace_with(n)
                                        n.variable = variable
                                        n.after_typing(v)
@@ -1368,16 +1368,16 @@ redef class AInitExpr
 end
 
 redef class AClosureCallExpr
+special AAbsAbsSendExpr
        redef meth after_typing(v)
        do
                var va = variable
                var sig = va.closure.signature
                var args = process_signature(v, sig, n_id.to_symbol, n_args.to_a)
-               if closure_defs != null then
-                       process_closures(v, sig, n_id.to_symbol, closure_defs)
+               if not n_closure_defs.is_empty then
+                       process_closures(v, sig, n_id.to_symbol, n_closure_defs.to_a)
                end
                if args == null then return
-               _prop = null
                _prop_signature = sig
                _arguments = args
                _stype = sig.return_type