engines: the entry point is `sys.run` or else `sys.main`
[nit.git] / src / naive_interpreter.nit
index ba2fb39..7525a00 100644 (file)
@@ -22,6 +22,7 @@ import typing
 import auto_super_init
 import frontend
 import common_ffi
+private import parser::tables
 
 redef class ToolContext
        # --discover-call-trace
@@ -63,7 +64,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
@@ -441,6 +443,13 @@ private class NaiveInterpreter
                recv.attributes[mproperty] = value
        end
 
+       # Is the attribute `mproperty` initialized the instance `recv`?
+       fun isset_attribute(mproperty: MAttribute, recv: Instance): Bool
+       do
+               assert recv isa MutableInstance
+               return recv.attributes.has_key(mproperty)
+       end
+
        # Collect attributes of a type in the order of their init
        fun collect_attr_propdef(mtype: MType): Array[AAttrPropdef]
        do
@@ -982,28 +991,26 @@ redef class AAttrPropdef
                var recv = args.first
                assert recv isa MutableInstance
                var attr = self.mpropdef.mproperty
-               if args.length == 1 then
-                       return v.read_attribute(attr, recv)
-               else
+               if mpropdef == mreadpropdef then
+                       assert args.length == 1
+                       if not is_lazy or v.isset_attribute(attr, recv) then return v.read_attribute(attr, recv)
+                       return evaluate_expr(v, recv)
+               else if mpropdef == mwritepropdef then
                        assert args.length == 2
                        v.write_attribute(attr, recv, args[1])
                        return null
+               else
+                       abort
                end
        end
 
        # Evaluate and set the default value of the attribute in `recv`
        private fun init_expr(v: NaiveInterpreter, recv: Instance)
        do
-               assert recv isa MutableInstance
+               if is_lazy then return
                var nexpr = self.n_expr
                if nexpr != null then
-                       var f = new Frame(self, self.mpropdef.as(not null), [recv])
-                       v.frames.unshift(f)
-                       var val = v.expr(nexpr)
-                       assert val != null
-                       v.frames.shift
-                       assert not v.is_escaping
-                       v.write_attribute(self.mpropdef.mproperty, recv, val)
+                       evaluate_expr(v, recv)
                        return
                end
                var mtype = self.mpropdef.static_mtype.as(not null)
@@ -1012,6 +1019,21 @@ redef class AAttrPropdef
                        v.write_attribute(self.mpropdef.mproperty, recv, v.null_instance)
                end
        end
+
+       private fun evaluate_expr(v: NaiveInterpreter, recv: Instance): Instance
+       do
+               assert recv isa MutableInstance
+               var nexpr = self.n_expr
+               assert nexpr != null
+               var f = new Frame(self, self.mpropdef.as(not null), [recv])
+               v.frames.unshift(f)
+               var val = v.expr(nexpr)
+               assert val != null
+               v.frames.shift
+               assert not v.is_escaping
+               v.write_attribute(self.mpropdef.mproperty, recv, val)
+               return val
+       end
 end
 
 redef class AClassdef
@@ -1669,8 +1691,7 @@ redef class AIssetAttrExpr
                if recv == null then return null
                if recv.mtype isa MNullType then fatal(v, "Receiver is null")
                var mproperty = self.mproperty.as(not null)
-               assert recv isa MutableInstance
-               return v.bool_instance(recv.attributes.has_key(mproperty))
+               return v.bool_instance(v.isset_attribute(mproperty, recv))
        end
 end