src: cleanup importations
[nit.git] / src / naive_interpreter.nit
index 138c7e2..291f222 100644 (file)
 module naive_interpreter
 
 import literal
-import typing
-import auto_super_init
-import frontend
 import common_ffi
+import semantize
+private import parser::tables
 
 redef class ToolContext
        # --discover-call-trace
@@ -63,7 +62,8 @@ redef class ModelBuilder
                if initprop != null then
                        interpreter.send(initprop, [mainobj])
                end
-               var mainprop = mainmodule.try_get_primitive_method("main", sys_type.mclass)
+               var mainprop = mainmodule.try_get_primitive_method("run", sys_type.mclass) or else
+                       mainmodule.try_get_primitive_method("main", sys_type.mclass)
                if mainprop != null then
                        interpreter.send(mainprop, [mainobj])
                end
@@ -406,6 +406,22 @@ private class NaiveInterpreter
        # Use this method, instead of `send` to execute and control the aditionnal behavior of the call-sites
        fun callsite(callsite: nullable CallSite, arguments: Array[Instance]): nullable Instance
        do
+               var initializers = callsite.mpropdef.initializers
+               if not initializers.is_empty then
+                       assert initializers.length == arguments.length - 1 else debug("expected {initializers.length} got {arguments.length - 1}")
+                       var recv = arguments.first
+                       var i = 1
+                       for p in initializers do
+                               if p isa MMethod then
+                                       self.send(p, [recv, arguments[i]])
+                               else if p isa MAttribute then
+                                       assert recv isa MutableInstance
+                                       recv.attributes[p] = arguments[i]
+                               else abort
+                               i += 1
+                       end
+                       return send(callsite.mproperty, [recv])
+               end
                return send(callsite.mproperty, arguments)
        end
 
@@ -646,9 +662,15 @@ redef class AMethPropdef
                                for i in [0..auto_super_init.msignature.arity+1[ do
                                        args.add(arguments[i])
                                end
+                               assert auto_super_init.mproperty != mpropdef.mproperty
                                v.callsite(auto_super_init, args)
                        end
                end
+               if auto_super_call then
+                       # standard call-next-method
+                       var superpd = mpropdef.lookup_next_definition(v.mainmodule, arguments.first.mtype)
+                       v.call_without_varargs(superpd, arguments)
+               end
 
                if n_block != null then
                        v.stmt(self.n_block)
@@ -1038,6 +1060,17 @@ redef class AClassdef
        # Execute an implicit `mpropdef` associated with the current node.
        private fun call(v: NaiveInterpreter, mpropdef: MMethodDef, args: Array[Instance]): nullable Instance
        do
+               if mpropdef.mproperty.is_root_init then
+                       assert self.super_inits == null
+                       assert args.length == 1
+                       if not mpropdef.is_intro then
+                               # standard call-next-method
+                               var superpd = mpropdef.lookup_next_definition(v.mainmodule, args.first.mtype)
+                               v.call_without_varargs(superpd, args)
+                       end
+                       return null
+               end
+
                var super_inits = self.super_inits
                if super_inits != null then
                        var args_of_super = args