niti: Introduce a method to create and initialize a Frame
[nit.git] / src / interpreter / naive_interpreter.nit
index bb0b3b3..c92ba90 100644 (file)
@@ -115,7 +115,7 @@ class NaiveInterpreter
 
        # Is a return executed?
        # Set this mark to skip the evaluation until the end of the specified method frame
-       var returnmark: nullable Frame = null
+       var returnmark: nullable FRAME = null
 
        # Is a break or a continue executed?
        # Set this mark to skip the evaluation until a labeled statement catch it with `is_escape`
@@ -283,11 +283,14 @@ class NaiveInterpreter
                return res
        end
 
+       # The virtual type of the frames used in the execution engine
+       type FRAME: Frame
+
        # The current frame used to store local variables of the current method executed
-       fun frame: Frame do return frames.first
+       fun frame: FRAME do return frames.first
 
        # The stack of all frames. The first one is the current one.
-       var frames = new List[Frame]
+       var frames = new List[FRAME]
 
        # Return a stack trace. One line per function
        fun stack_trace: String
@@ -314,6 +317,15 @@ class NaiveInterpreter
                return frames.first.arguments.first.mtype.as(MClassType)
        end
 
+       # Initialize the environment for a call and return a new Frame
+       # *`node` The AST node
+       # *`mpropdef` The corresponding mpropdef
+       # *`args` Arguments of the call
+       fun new_frame(node: ANode, mpropdef: MPropDef, args: Array[Instance]): FRAME
+       do
+               return new Frame(node, mpropdef, args)
+       end
+
        # Exit the program with a message
        fun fatal(message: String)
        do
@@ -413,6 +425,14 @@ class NaiveInterpreter
                var val = mpropdef.constant_value
 
                var node = modelbuilder.mpropdef2node(mpropdef)
+               if mpropdef.is_abstract then
+                       if node != null then
+                               self.frames.unshift new_frame(node, mpropdef, args)
+                       end
+                       fatal("Abstract method `{mpropdef.mproperty.name}` called on `{args.first.mtype}`")
+                       abort
+               end
+
                if node isa APropdef then
                        self.parameter_check(node, mpropdef, args)
                        return node.call(self, mpropdef, args)
@@ -703,7 +723,7 @@ redef class AMethPropdef
 
        redef fun call(v, mpropdef, args)
        do
-               var f = new Frame(self, self.mpropdef.as(not null), args)
+               var f = v.new_frame(self, mpropdef, args)
                var res = call_commons(v, mpropdef, args, f)
                v.frames.shift
                if v.returnmark == f then
@@ -725,11 +745,6 @@ redef class AMethPropdef
                        v.write_variable(variable, arguments[i+1])
                end
 
-               if mpropdef.is_abstract then
-                       v.fatal("Abstract method `{mpropdef.mproperty.name}` called on `{arguments.first.mtype}`")
-                       abort
-               end
-
                # Call the implicit super-init
                var auto_super_inits = self.auto_super_inits
                if auto_super_inits != null then
@@ -1145,7 +1160,8 @@ redef class AAttrPropdef
                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)
+                       var f = v.new_frame(self, mpropdef, args)
+                       return evaluate_expr(v, recv, f)
                else if mpropdef == mwritepropdef then
                        assert args.length == 2
                        v.write_attribute(attr, recv, args[1])
@@ -1160,20 +1176,22 @@ redef class AAttrPropdef
        do
                if is_lazy then return
                if has_value then
-                       evaluate_expr(v, recv)
+                       var f = v.new_frame(self, mpropdef.as(not null), [recv])
+                       evaluate_expr(v, recv, f)
                        return
                end
-               var mtype = self.mpropdef.static_mtype.as(not null)
+               var mpropdef = self.mpropdef
+               if mpropdef == null then return
+               var mtype = mpropdef.static_mtype.as(not null)
                mtype = mtype.anchor_to(v.mainmodule, recv.mtype.as(MClassType))
                if mtype isa MNullableType then
                        v.write_attribute(self.mpropdef.mproperty, recv, v.null_instance)
                end
        end
 
-       private fun evaluate_expr(v: NaiveInterpreter, recv: Instance): Instance
+       private fun evaluate_expr(v: NaiveInterpreter, recv: Instance, f: Frame): Instance
        do
                assert recv isa MutableInstance
-               var f = new Frame(self, self.mpropdef.as(not null), [recv])
                v.frames.unshift(f)
 
                var val