1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Interpretation of a Nit program directly on the AST
18 module naive_interpreter
22 import auto_super_init
24 redef class ModelBuilder
25 # Execute the program from the entry point (Sys::main) of the `mainmodule'
26 # `arguments' are the command-line arguments in order
28 # 1. the AST is fully loaded.
29 # 2. the model is fully built.
30 # 3. the instructions are fully analysed.
31 fun run_naive_interpreter
(mainmodule
: MModule, arguments
: Array[String])
34 self.toolcontext
.info
("*** START INTERPRETING ***", 1)
36 var interpreter
= new NaiveInterpreter(self, mainmodule
, arguments
)
37 var sys_type
= mainmodule
.sys_type
38 if sys_type
== null then return # no class Sys
39 var mainobj
= new Instance(sys_type
)
40 interpreter
.mainobj
= mainobj
41 interpreter
.init_instance
(mainobj
)
42 var initprop
= mainmodule
.try_get_primitive_method
("init", sys_type
)
43 if initprop
!= null then
44 interpreter
.send
(initprop
, [mainobj
])
46 interpreter
.check_init_instance
(mainobj
)
47 var mainprop
= mainmodule
.try_get_primitive_method
("main", sys_type
)
48 if mainprop
!= null then
49 interpreter
.send
(mainprop
, [mainobj
])
53 self.toolcontext
.info
("*** END INTERPRETING: {time1-time0} ***", 2)
57 # The visitor that interprets the Nit Program by walking on the AST
58 private class NaiveInterpreter
59 # The modelbuilder that know the AST and its associations with the model
60 var modelbuilder
: ModelBuilder
62 # The main moduleof the program (used to lookup methoda
63 var mainmodule
: MModule
65 # The command line arguments of the interpreted program
66 # arguments.first is the program name
67 # arguments[1] is the first argument
68 var arguments
: Array[String]
70 var mainobj
: nullable Instance
72 init(modelbuilder
: ModelBuilder, mainmodule
: MModule, arguments
: Array[String])
74 self.modelbuilder
= modelbuilder
75 self.mainmodule
= mainmodule
76 self.arguments
= arguments
77 self.true_instance
= new PrimitiveInstance[Bool](mainmodule
.bool_type
, true)
78 self.false_instance
= new PrimitiveInstance[Bool](mainmodule
.bool_type
, false)
79 self.null_instance
= new Instance(mainmodule
.model
.null_type
)
82 # Subtype test in the context of the mainmodule
83 fun is_subtype
(sub
, sup
: MType): Bool
85 return sub
.is_subtype
(self.mainmodule
, self.frame
.arguments
.first
.mtype
.as(MClassType), sup
)
88 # Is a return executed?
89 # Set this mark to skip the evaluation until the end of the specified method frame
90 var returnmark
: nullable Frame = null
92 # Is a break executed?
93 # Set this mark to skip the evaluation until a labeled statement catch it with `is_break'
94 var breakmark
: nullable EscapeMark = null
96 # Is a continue executed?
97 # Set this mark to skip the evaluation until a labeled statement catch it with `is_continue'
98 var continuemark
: nullable EscapeMark = null
100 # Is a return or a break or a continue executed?
101 # Use this function to know if you must skip the evaluation of statements
102 fun is_escaping
: Bool do return returnmark
!= null or breakmark
!= null or continuemark
!= null
104 # The value associated with the current return/break/continue, if any.
105 # Set the value when you set a escapemark.
106 # Read the value when you catch a mark or reach the end of a method
107 var escapevalue
: nullable Instance = null
109 # If there is a break and is associated with `escapemark', then return true an clear the mark.
110 # If there is no break or if `escapemark' is null then return false.
111 # Use this function to catch a potential break.
112 fun is_break
(escapemark
: nullable EscapeMark): Bool
114 if escapemark
!= null and self.breakmark
== escapemark
then
115 self.breakmark
= null
122 # If there is a continue and is associated with `escapemark', then return true an clear the mark.
123 # If there is no continue or if `escapemark' is null then return false.
124 # Use this function to catch a potential continue.
125 fun is_continue
(escapemark
: nullable EscapeMark): Bool
127 if escapemark
!= null and self.continuemark
== escapemark
then
128 self.continuemark
= null
135 # Evaluate `n' as an expression in the current context.
136 # Return the value of the expression.
137 # If `n' cannot be evaluated, then aborts.
138 fun expr
(n
: AExpr): nullable Instance
140 var old
= self.frame
.current_node
141 self.frame
.current_node
= n
142 #n.debug("IN Execute expr")
144 if i
== null and not self.is_escaping
then
145 n
.debug
("inconsitance: no value and not escaping.")
147 #n.debug("OUT Execute expr: value is {i}")
148 #if not is_subtype(i.mtype, n.mtype.as(not null)) then n.debug("Expected {n.mtype.as(not null)} got {i}")
149 self.frame
.current_node
= old
153 # Evaluate `n' as a statement in the current context.
154 # Do nothing if `n' is sull.
155 # If `n' cannot be evaluated, then aborts.
156 fun stmt
(n
: nullable AExpr)
159 var old
= self.frame
.current_node
160 self.frame
.current_node
= n
161 #n.debug("Execute stmt")
163 self.frame
.current_node
= old
167 # Map used to store values of nodes that must be evaluated once in the system (AOnceExpr)
168 var onces
: Map[ANode, Instance] = new HashMap[ANode, Instance]
170 # Return the boolean instance associated with `val'.
171 fun bool_instance
(val
: Bool): Instance
173 if val
then return self.true_instance
else return self.false_instance
176 # Return the integer instance associated with `val'.
177 fun int_instance
(val
: Int): Instance
179 var ic
= self.mainmodule
.get_primitive_class
("Int")
180 return new PrimitiveInstance[Int](ic
.mclass_type
, val
)
183 # Return the char instance associated with `val'.
184 fun char_instance
(val
: Char): Instance
186 var ic
= self.mainmodule
.get_primitive_class
("Char")
187 return new PrimitiveInstance[Char](ic
.mclass_type
, val
)
190 # Return the float instance associated with `val'.
191 fun float_instance
(val
: Float): Instance
193 var ic
= self.mainmodule
.get_primitive_class
("Float")
194 return new PrimitiveInstance[Float](ic
.mclass_type
, val
)
197 # The unique intance of the `true' value.
198 var true_instance
: Instance
200 # The unique intance of the `false' value.
201 var false_instance
: Instance
203 # The unique intance of the `null' value.
204 var null_instance
: Instance
206 # Return a new array made of `values'.
207 # The dynamic type of the result is Array[elttype].
208 fun array_instance
(values
: Array[Instance], elttype
: MType): Instance
210 assert not elttype
.need_anchor
211 var nat
= new PrimitiveInstance[Array[Instance]](self.mainmodule
.get_primitive_class
("NativeArray").get_mtype
([elttype
]), values
)
212 var mtype
= self.mainmodule
.get_primitive_class
("Array").get_mtype
([elttype
])
213 var res
= new Instance(mtype
)
214 self.init_instance
(res
)
215 self.send
(self.mainmodule
.force_get_primitive_method
("with_native", mtype
), [res
, nat
, self.int_instance
(values
.length
)])
216 self.check_init_instance
(res
)
220 # Return a new native string initialized with `txt'
221 fun native_string_instance
(txt
: String): Instance
223 var val
= new Buffer.from
(txt
)
225 var ic
= self.mainmodule
.get_primitive_class
("NativeString")
226 return new PrimitiveInstance[Buffer](ic
.mclass_type
, val
)
229 # The current frame used to store local variables of the current method executed
230 fun frame
: Frame do return frames
.first
232 # The stack of all frames. The first one is the current one.
233 var frames
: List[Frame] = new List[Frame]
235 # Return a stack stace. One line per function
236 fun stack_trace
: String
239 b
.append
(",---- Stack trace -- - - -\n")
241 b
.append
("| {f.mpropdef} ({f.current_node.location})\n")
243 b
.append
("`------------------- - - -")
247 # Exit the program with a message
248 fun fatal
(message
: String)
250 if frames
.is_empty
then
253 self.frame
.current_node
.fatal
(self, message
)
258 # Execute `mpropdef' for a `args' (where args[0] is the receiver).
259 # Return a falue if `mpropdef' is a function, or null if it is a procedure.
260 # The call is direct/static. There is no message-seding/late-bindng.
261 fun call
(mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
263 var vararg_rank
= mpropdef
.msignature
.vararg_rank
264 if vararg_rank
>= 0 then
265 assert args
.length
>= mpropdef
.msignature
.arity
+ 1 # because of self
267 args
= new Array[Instance]
269 args
.add
(rawargs
.first
) # recv
271 for i
in [0..vararg_rank
[ do
272 args
.add
(rawargs
[i
+1])
275 var vararg_lastrank
= vararg_rank
+ rawargs
.length-1-mpropdef
.msignature
.arity
276 var vararg
= new Array[Instance]
277 for i
in [vararg_rank
..vararg_lastrank
] do
278 vararg
.add
(rawargs
[i
+1])
280 # FIXME: its it to late to determine the vararg type, this should have been done during a previous analysis
281 var elttype
= mpropdef
.msignature
.mparameters
[vararg_rank
].mtype
.anchor_to
(self.mainmodule
, args
.first
.mtype
.as(MClassType))
282 args
.add
(self.array_instance
(vararg
, elttype
))
284 for i
in [vararg_lastrank
+1..rawargs
.length-1
[ do
285 args
.add
(rawargs
[i
+1])
288 #assert args.length == mpropdef.msignature.arity + 1 # because of self
290 # Look for the AST node that implements the property
291 var mproperty
= mpropdef
.mproperty
292 if self.modelbuilder
.mpropdef2npropdef
.has_key
(mpropdef
) then
293 var npropdef
= self.modelbuilder
.mpropdef2npropdef
[mpropdef
]
294 return npropdef
.call
(self, mpropdef
, args
)
295 else if mproperty
.name
== "init" then
296 var nclassdef
= self.modelbuilder
.mclassdef2nclassdef
[mpropdef
.mclassdef
]
297 return nclassdef
.call
(self, mpropdef
, args
)
299 fatal
("Fatal Error: method {mpropdef} not found in the AST")
304 fun call_closure
(closure
: ClosureInstance, args
: Array[Instance]): nullable Instance
306 var nclosuredef
= closure
.nclosuredef
307 var f
= closure
.frame
308 for i
in [0..closure
.nclosuredef
.mclosure
.mtype
.as(MSignature).arity
[ do
309 var variable
= nclosuredef
.variables
[i
]
310 f
.map
[variable
] = args
[i
]
313 self.frames
.unshift
(f
)
315 self.stmt
(nclosuredef
.n_expr
)
319 if self.is_continue
(nclosuredef
.escapemark
) then
320 var res
= self.escapevalue
321 self.escapevalue
= null
327 # Execute `mproperty' for a `args' (where args[0] is the receiver).
328 # Return a falue if `mproperty' is a function, or null if it is a procedure.
329 # The call is polimotphic. There is a message-seding/late-bindng according to te receiver (args[0]).
330 fun send
(mproperty
: MMethod, args
: Array[Instance]): nullable Instance
332 var recv
= args
.first
333 var mtype
= recv
.mtype
334 if mtype
isa MNullType then
335 if mproperty
.name
== "==" then
336 return self.bool_instance
(args
[0] == args
[1])
337 else if mproperty
.name
== "!=" then
338 return self.bool_instance
(args
[0] != args
[1])
340 #fatal("Reciever is null. {mproperty}. {args.join(" ")} {self.frame.current_node.class_name}")
341 fatal
("Reciever is null")
344 var propdefs
= mproperty
.lookup_definitions
(self.mainmodule
, mtype
)
345 if propdefs
.length
> 1 then
346 fatal
("NOT YET IMPLEMETED ERROR: Property conflict: {propdefs.join(", ")}")
349 assert propdefs
.length
== 1 else
350 fatal
("Fatal Error: No property '{mproperty}' for '{recv}'")
353 var propdef
= propdefs
.first
354 return self.call
(propdef
, args
)
357 # Read the attribute `mproperty' of an instance `recv' and return its value.
358 # If the attribute in not yet initialized, then aborts with an error message.
359 fun read_attribute
(mproperty
: MAttribute, recv
: Instance): Instance
361 if not recv
.attributes
.has_key
(mproperty
) then
362 fatal
("Uninitialized attribute {mproperty.name}")
365 return recv
.attributes
[mproperty
]
368 # Fill the initial values of the newly created instance `recv'.
369 # `recv.mtype' is used to know what must be filled.
370 fun init_instance
(recv
: Instance)
372 for cd
in recv
.mtype
.collect_mclassdefs
(self.mainmodule
)
374 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
375 for npropdef
in n
.n_propdefs
do
376 if npropdef
isa AAttrPropdef then
377 npropdef
.init_expr
(self, recv
)
383 # Check that non nullable attributes of `recv' are correctly initialized.
384 # This function is used as the last instruction of a new
385 # FIXME: this will work better once there is nullable types
386 fun check_init_instance
(recv
: Instance)
388 for cd
in recv
.mtype
.collect_mclassdefs
(self.mainmodule
)
390 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
391 for npropdef
in n
.n_propdefs
do
392 if npropdef
isa AAttrPropdef and npropdef
.n_expr
== null then
393 # Force read to check the initialization
394 self.read_attribute
(npropdef
.mpropdef
.mproperty
, recv
)
400 # This function determine the correct type according the reciever of the current definition (self).
401 fun unanchor_type
(mtype
: MType): MType
403 return mtype
.anchor_to
(self.mainmodule
, self.frame
.arguments
.first
.mtype
.as(MClassType))
407 # An instance represents a value of the executed program.
409 # The dynamic type of the instance
410 # ASSERT: not self.mtype.is_anchored
413 # The values of the attributes
414 var attributes
: Map[MAttribute, Instance] = new HashMap[MAttribute, Instance]
416 # return true if the instance is the true value.
417 # return false if the instance is the true value.
419 fun is_true
: Bool do abort
421 # Return true if `self' IS `o' (using the Nit semantic of is)
422 fun eq_is
(o
: Instance): Bool do return self is o
424 # Human readable object identity "Type#number"
425 redef fun to_s
do return "{mtype}#{object_id}"
427 # Return the integer valur is the instance is an integer.
429 fun to_i
: Int do abort
431 # The real value encapsulated if the instance is primitive.
433 fun val
: Object do abort
436 # Special instance to handle primitives values (int, bool, etc.)
437 # The trick it just to encapsulate the <<real>> value
438 class PrimitiveInstance[E
: Object]
441 # The real value encapsulated
444 init(mtype
: MType, val
: E
)
452 if val
== true then return true
453 if val
== false then return false
459 if not o
isa PrimitiveInstance[Object] then return false
460 return self.val
== o
.val
465 if not o
isa PrimitiveInstance[Object] then return false
466 return self.val
is o
.val
469 redef fun to_s
do return "{mtype}#{val.object_id}({val})"
471 redef fun to_i
do return val
.as(Int)
474 private class ClosureInstance
479 var nclosuredef
: AClosureDef
481 init(mtype
: MType, frame
: Frame, nclosuredef
: AClosureDef)
485 self.nclosuredef
= nclosuredef
489 # Information about local variables in a running method
491 # The current visited node
492 # The node is stored by frame to keep a stack trace
493 var current_node
: ANode
494 # The executed property.
495 # A Method in case of a call, an attribute in case of a default initialization.
496 var mpropdef
: MPropDef
497 # Arguments of the method (the first is te receiver
498 var arguments
: Array[Instance]
499 # Mapping betwen a variable an the current value
500 var map
: Map[Variable, Instance] = new HashMap[Variable, Instance]
504 # Aborts the program with a message
505 # `v' is used to know if a colored message is displayed or not
506 private fun fatal
(v
: NaiveInterpreter, message
: String)
508 if v
.modelbuilder
.toolcontext
.opt_no_color
.value
== true then
509 print
("{message} ({location.file.filename}:{location.line_start})")
511 print
("{location}: {message}\n{location.colored_line("0;31")}")
519 # Execute a `mpropdef' associated with the current node.
520 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
522 fatal
(v
, "Unimplemented {mpropdef}")
527 redef class AConcreteMethPropdef
528 redef fun call
(v
, mpropdef
, args
)
530 var f
= new Frame(self, self.mpropdef
.as(not null), args
)
531 for i
in [0..mpropdef
.msignature
.arity
[ do
532 var variable
= self.n_signature
.n_params
[i
].variable
533 assert variable
!= null
534 f
.map
[variable
] = args
[i
+1]
536 for i
in [0..mpropdef
.msignature
.mclosures
.length
[ do
537 var c
= mpropdef
.msignature
.mclosures
[i
]
538 var variable
= self.n_signature
.n_closure_decls
[i
].variable
539 assert variable
!= null
540 f
.map
[variable
] = args
[i
+ 1 + mpropdef
.msignature
.arity
]
545 # Call the implicit super-init
546 var auto_super_inits
= self.auto_super_inits
547 if auto_super_inits
!= null then
548 var selfarg
= [args
.first
]
549 for auto_super_init
in auto_super_inits
do
550 if auto_super_init
.intro
.msignature
.arity
== 0 then
551 v
.send
(auto_super_init
, selfarg
)
553 v
.send
(auto_super_init
, args
)
560 if v
.returnmark
== f
then
562 var res
= v
.escapevalue
570 redef class AInternMethPropdef
571 redef fun call
(v
, mpropdef
, args
)
573 var pname
= mpropdef
.mproperty
.name
574 var cname
= mpropdef
.mclassdef
.mclass
.name
575 if pname
== "output" then
576 var recv
= args
.first
579 else if pname
== "object_id" then
580 var recv
= args
.first
581 if recv
isa PrimitiveInstance[Object] then
582 return v
.int_instance
(recv
.val
.object_id
)
584 return v
.int_instance
(recv
.object_id
)
586 else if pname
== "output_class_name" then
587 var recv
= args
.first
588 print recv
.mtype
.as(MClassType).mclass
590 else if pname
== "native_class_name" then
591 var recv
= args
.first
592 var txt
= recv
.mtype
.as(MClassType).mclass
.to_s
593 return v
.native_string_instance
(txt
)
594 else if pname
== "==" then
595 # == is correclt redefined for instances
596 return v
.bool_instance
(args
[0] == args
[1])
597 else if pname
== "!=" then
598 return v
.bool_instance
(args
[0] != args
[1])
599 else if pname
== "is_same_type" then
600 return v
.bool_instance
(args
[0].mtype
== args
[1].mtype
)
601 else if pname
== "exit" then
604 else if pname
== "sys" then
606 else if cname
== "Int" then
607 if pname
== "unary -" then
608 return v
.int_instance
(-args
[0].to_i
)
609 else if pname
== "succ" then
610 return v
.int_instance
(args
[0].to_i
+ 1)
611 else if pname
== "prec" then
612 return v
.int_instance
(args
[0].to_i
- 1)
613 else if pname
== "+" then
614 return v
.int_instance
(args
[0].to_i
+ args
[1].to_i
)
615 else if pname
== "-" then
616 return v
.int_instance
(args
[0].to_i
- args
[1].to_i
)
617 else if pname
== "*" then
618 return v
.int_instance
(args
[0].to_i
* args
[1].to_i
)
619 else if pname
== "%" then
620 return v
.int_instance
(args
[0].to_i
% args
[1].to_i
)
621 else if pname
== "/" then
622 return v
.int_instance
(args
[0].to_i
/ args
[1].to_i
)
623 else if pname
== "<" then
624 return v
.bool_instance
(args
[0].to_i
< args
[1].to_i
)
625 else if pname
== ">" then
626 return v
.bool_instance
(args
[0].to_i
> args
[1].to_i
)
627 else if pname
== "<=" then
628 return v
.bool_instance
(args
[0].to_i
<= args
[1].to_i
)
629 else if pname
== ">=" then
630 return v
.bool_instance
(args
[0].to_i
>= args
[1].to_i
)
631 else if pname
== "<=>" then
632 return v
.int_instance
(args
[0].to_i
<=> args
[1].to_i
)
633 else if pname
== "ascii" then
634 return v
.char_instance
(args
[0].to_i
.ascii
)
635 else if pname
== "to_f" then
636 return v
.float_instance
(args
[0].to_i
.to_f
)
637 else if pname
== "lshift" then
638 return v
.int_instance
(args
[0].to_i
.lshift
(args
[1].to_i
))
639 else if pname
== "rshift" then
640 return v
.int_instance
(args
[0].to_i
.rshift
(args
[1].to_i
))
642 else if cname
== "Char" then
643 var recv
= args
[0].val
.as(Char)
644 if pname
== "ascii" then
645 return v
.int_instance
(recv
.ascii
)
646 else if pname
== "succ" then
647 return v
.char_instance
(recv
.succ
)
648 else if pname
== "prec" then
649 return v
.char_instance
(recv
.prec
)
650 else if pname
== "<" then
651 return v
.bool_instance
(recv
< args
[1].val
.as(Char))
652 else if pname
== ">" then
653 return v
.bool_instance
(recv
> args
[1].val
.as(Char))
654 else if pname
== "<=" then
655 return v
.bool_instance
(recv
<= args
[1].val
.as(Char))
656 else if pname
== ">=" then
657 return v
.bool_instance
(recv
>= args
[1].val
.as(Char))
658 else if pname
== "<=>" then
659 return v
.int_instance
(recv
<=> args
[1].val
.as(Char))
661 else if cname
== "Float" then
663 return v
.float_instance
(args
[0].val
.as(Float) + args
[1].val
.as(Float))
664 else if pname
== "-" then
665 return v
.float_instance
(args
[0].val
.as(Float) - args
[1].val
.as(Float))
666 else if pname
== "*" then
667 return v
.float_instance
(args
[0].val
.as(Float) * args
[1].val
.as(Float))
668 else if pname
== "/" then
669 return v
.float_instance
(args
[0].val
.as(Float) / args
[1].val
.as(Float))
670 else if pname
== "to_i" then
671 return v
.int_instance
(args
[0].val
.as(Float).to_i
)
673 else if cname
== "NativeString" then
674 var recvval
= args
.first
.val
.as(Buffer)
675 if pname
== "[]" then
676 var arg1
= args
[1].to_i
677 if arg1
>= recvval
.length
or arg1
< 0 then
678 debug
("Illegal access on {recvval} for element {arg1}/{recvval.length}")
680 return v
.char_instance
(recvval
[arg1
])
681 else if pname
== "[]=" then
682 var arg1
= args
[1].to_i
683 if arg1
>= recvval
.length
or arg1
< 0 then
684 debug
("Illegal access on {recvval} for element {arg1}/{recvval.length}")
686 recvval
[arg1
] = args
[2].val
.as(Char)
688 else if pname
== "copy_to" then
689 # sig= copy_to(dest: NativeString, length: Int, from: Int, to: Int)
690 var destval
= args
[1].val
.as(Buffer)
691 var lenval
= args
[2].to_i
692 var fromval
= args
[3].to_i
693 var toval
= args
[4].to_i
695 debug
("Illegal access on {recvval} for element {fromval}/{recvval.length}")
697 if fromval
+ lenval
>= recvval
.length
then
698 debug
("Illegal access on {recvval} for element {fromval}+{lenval}/{recvval.length}")
701 debug
("Illegal access on {destval} for element {toval}/{destval.length}")
703 if toval
+ lenval
>= destval
.length
then
704 debug
("Illegal access on {destval} for element {toval}+{lenval}/{destval.length}")
706 recvval
.copy
(fromval
, lenval
, destval
, toval
)
708 else if pname
== "atoi" then
709 return v
.int_instance
(recvval
.to_i
)
711 else if pname
== "calloc_string" then
712 return v
.native_string_instance
("!" * args
[1].to_i
)
713 else if cname
== "NativeArray" then
714 var recvval
= args
.first
.val
.as(Array[Instance])
715 if pname
== "[]" then
716 if args
[1].to_i
>= recvval
.length
or args
[1].to_i
< 0 then
717 debug
("Illegal access on {recvval} for element {args[1].to_i}/{recvval.length}")
719 return recvval
[args
[1].to_i
]
720 else if pname
== "[]=" then
721 recvval
[args
[1].to_i
] = args
[2]
723 else if pname
== "copy_to" then
724 recvval
.copy
(0, args
[2].to_i
, args
[1].val
.as(Array[Instance]), 0)
727 else if pname
== "calloc_array" then
728 var recvtype
= args
.first
.mtype
.as(MClassType)
729 var mtype
: MType = recvtype
.supertype_to
(v
.mainmodule
, recvtype
, v
.mainmodule
.get_primitive_class
("ArrayCapable"))
730 mtype
= mtype
.as(MGenericType).arguments
.first
731 var val
= new Array[Instance].filled_with
(v
.null_instance
, args
[1].to_i
)
732 return new PrimitiveInstance[Array[Instance]](v
.mainmodule
.get_primitive_class
("NativeArray").get_mtype
([mtype
]), val
)
734 fatal
(v
, "Unimplemented intern {mpropdef}")
739 redef class AbstractArray[E
]
740 fun copy
(start
: Int, len
: Int, dest
: AbstractArray[E
], new_start
: Int)
742 self.copy_to
(start
, len
, dest
, new_start
)
746 redef class AExternInitPropdef
747 redef fun call
(v
, mpropdef
, args
)
749 var pname
= mpropdef
.mproperty
.name
750 var cname
= mpropdef
.mclassdef
.mclass
.name
751 if pname
== "native_stdout" then
752 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, stdout
)
753 else if pname
== "native_stdin" then
754 return new PrimitiveInstance[IStream](mpropdef
.mclassdef
.mclass
.mclass_type
, stdin
)
755 else if pname
== "native_stderr" then
756 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, stderr
)
757 else if pname
== "io_open_read" then
758 var a1
= args
[1].val
.as(Buffer)
759 return new PrimitiveInstance[IStream](mpropdef
.mclassdef
.mclass
.mclass_type
, new IFStream.open
(a1
.to_s
))
760 else if pname
== "io_open_write" then
761 var a1
= args
[1].val
.as(Buffer)
762 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, new OFStream.open
(a1
.to_s
))
764 fatal
(v
, "Unimplemented extern init {mpropdef}")
769 redef class AExternMethPropdef
771 redef fun call
(v
, mpropdef
, args
)
773 var pname
= mpropdef
.mproperty
.name
774 var cname
= mpropdef
.mclassdef
.mclass
.name
775 if cname
== "Int" then
776 var recvval
= args
.first
.val
.as(Int)
777 if pname
== "rand" then
778 var res
= recvval
.rand
779 return v
.int_instance
(res
)
781 else if cname
== "NativeFile" then
782 var recvval
= args
.first
.val
783 if pname
== "io_write" then
784 var a1
= args
[1].val
.as(Buffer)
785 recvval
.as(OStream).write
(a1
.substring
(0, args
[2].to_i
))
787 else if pname
== "io_read" then
788 var str
= recvval
.as(IStream).read
(args
[2].to_i
)
789 var a1
= args
[1].val
.as(Buffer)
790 new Buffer.from
(str
).copy
(0, str
.length
, a1
, 0)
791 return v
.int_instance
(str
.length
)
792 else if pname
== "io_close" then
793 recvval
.as(IOS).close
794 return v
.int_instance
(0)
796 else if cname
== "NativeString" then
797 var recvval
= args
.first
.val
.as(Buffer)
798 if pname
== "file_exists" then
799 return v
.bool_instance
(recvval
.to_s
.file_exists
)
800 else if pname
== "file_mkdir" then
803 else if pname
== "get_environ" then
804 var txt
= recvval
.to_s
.environ
805 return v
.native_string_instance
(txt
)
806 else if pname
== "system" then
807 var res
= sys
.system
(recvval
.to_s
)
808 return v
.int_instance
(res
)
810 else if pname
== "native_argc" then
811 return v
.int_instance
(v
.arguments
.length
)
812 else if pname
== "native_argv" then
813 var txt
= v
.arguments
[args
[1].to_i
]
814 return v
.native_string_instance
(txt
)
815 else if pname
== "get_time" then
816 return v
.int_instance
(get_time
)
817 else if pname
== "lexer_goto" then
818 return v
.int_instance
(lexer_goto
(args
[1].to_i
, args
[2].to_i
))
819 else if pname
== "lexer_accept" then
820 return v
.int_instance
(lexer_accept
(args
[1].to_i
))
821 else if pname
== "parser_goto" then
822 return v
.int_instance
(parser_goto
(args
[1].to_i
, args
[2].to_i
))
823 else if pname
== "parser_action" then
824 return v
.int_instance
(parser_action
(args
[1].to_i
, args
[2].to_i
))
826 fatal
(v
, "Unimplemented extern {mpropdef}")
831 redef class AAttrPropdef
832 redef fun call
(v
, mpropdef
, args
)
834 var attr
= self.mpropdef
.mproperty
835 if args
.length
== 1 then
836 return v
.read_attribute
(attr
, args
.first
)
838 assert args
.length
== 2
839 args
.first
.attributes
[attr
] = args
[1]
844 # Evaluate and set the default value of the attribute in `recv'
845 private fun init_expr
(v
: NaiveInterpreter, recv
: Instance)
847 var nexpr
= self.n_expr
848 if nexpr
!= null then
849 var f
= new Frame(self, self.mpropdef
.as(not null), [recv
])
851 var val
= v
.expr
(nexpr
)
854 assert not v
.is_escaping
855 recv
.attributes
[self.mpropdef
.mproperty
] = val
858 var mtype
= self.mpropdef
.static_mtype
.as(not null)
859 # TODO The needinit info is statically computed, move it to modelbuilder or whatever
860 mtype
= mtype
.resolve_for
(self.mpropdef
.mclassdef
.bound_mtype
, self.mpropdef
.mclassdef
.bound_mtype
, self.mpropdef
.mclassdef
.mmodule
, true)
861 if mtype
isa MNullableType then
862 recv
.attributes
[self.mpropdef
.mproperty
] = v
.null_instance
867 redef class AClassdef
868 # Execute an implicit `mpropdef' associated with the current node.
869 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
871 var super_inits
= self.super_inits
872 if super_inits
!= null then
873 assert args
.length
== 1
874 for su
in super_inits
do
879 var recv
= args
.first
881 # Collect undefined attributes
882 for npropdef
in self.n_propdefs
do
883 if npropdef
isa AAttrPropdef and npropdef
.n_expr
== null then
884 recv
.attributes
[npropdef
.mpropdef
.mproperty
] = args
[i
]
893 # Evaluate the node as a possible expression.
894 # Return a possible value
895 # NOTE: Do not call this method directly, but use `v.expr'
896 # This method is here to be implemented by subclasses.
897 private fun expr
(v
: NaiveInterpreter): nullable Instance
899 fatal
(v
, "Unimplemented expr {class_name}")
903 # Evaluate the node as a statement.
904 # NOTE: Do not call this method directly, but use `v.stmt'
905 # This method is here to be implemented by subclasses (no need to return something).
906 private fun stmt
(v
: NaiveInterpreter)
913 redef class ABlockExpr
916 for e
in self.n_expr
do
918 if v
.is_escaping
then return
923 redef class AVardeclExpr
929 if i
== null then return
930 v
.frame
.map
[self.variable
.as(not null)] = i
938 return v
.frame
.map
[self.variable
.as(not null)]
942 redef class AVarAssignExpr
945 var i
= v
.expr
(self.n_value
)
946 if i
== null then return
947 v
.frame
.map
[self.variable
.as(not null)] = i
951 redef class AVarReassignExpr
954 var vari
= v
.frame
.map
[self.variable
.as(not null)]
955 var value
= v
.expr
(self.n_value
)
956 if value
== null then return
957 var res
= v
.send
(reassign_property
.mproperty
, [vari
, value
])
959 v
.frame
.map
[self.variable
.as(not null)] = res
963 redef class ASelfExpr
966 return v
.frame
.arguments
.first
970 redef class AContinueExpr
976 if i
== null then return
979 v
.continuemark
= self.escapemark
983 redef class ABreakExpr
989 if i
== null then return
992 v
.breakmark
= self.escapemark
996 redef class AReturnExpr
1002 if i
== null then return
1005 v
.returnmark
= v
.frame
1009 redef class AAbortExpr
1020 var cond
= v
.expr
(self.n_expr
)
1021 if cond
== null then return
1022 if cond
.is_true
then
1030 redef class AIfexprExpr
1033 var cond
= v
.expr
(self.n_expr
)
1034 if cond
== null then return null
1035 if cond
.is_true
then
1036 return v
.expr
(self.n_then
)
1038 return v
.expr
(self.n_else
)
1046 v
.stmt
(self.n_block
)
1047 v
.is_break
(self.escapemark
) # Clear the break (if any)
1051 redef class AWhileExpr
1055 var cond
= v
.expr
(self.n_expr
)
1056 if cond
== null then return
1057 if not cond
.is_true
then return
1058 v
.stmt
(self.n_block
)
1059 if v
.is_break
(self.escapemark
) then return
1060 v
.is_continue
(self.escapemark
) # Clear the break
1061 if v
.is_escaping
then return
1066 redef class ALoopExpr
1070 v
.stmt
(self.n_block
)
1071 if v
.is_break
(self.escapemark
) then return
1072 v
.is_continue
(self.escapemark
) # Clear the break
1073 if v
.is_escaping
then return
1078 redef class AForExpr
1081 var col
= v
.expr
(self.n_expr
)
1082 if col
== null then return
1083 #self.debug("col {col}")
1084 var iter
= v
.send
(v
.mainmodule
.force_get_primitive_method
("iterator", col
.mtype
), [col
]).as(not null)
1085 #self.debug("iter {iter}")
1087 var isok
= v
.send
(v
.mainmodule
.force_get_primitive_method
("is_ok", iter
.mtype
), [iter
]).as(not null)
1088 if not isok
.is_true
then return
1089 if self.variables
.length
== 1 then
1090 var item
= v
.send
(v
.mainmodule
.force_get_primitive_method
("item", iter
.mtype
), [iter
]).as(not null)
1091 #self.debug("item {item}")
1092 v
.frame
.map
[self.variables
.first
] = item
1093 else if self.variables
.length
== 2 then
1094 var key
= v
.send
(v
.mainmodule
.force_get_primitive_method
("key", iter
.mtype
), [iter
]).as(not null)
1095 v
.frame
.map
[self.variables
[0]] = key
1096 var item
= v
.send
(v
.mainmodule
.force_get_primitive_method
("item", iter
.mtype
), [iter
]).as(not null)
1097 v
.frame
.map
[self.variables
[1]] = item
1101 v
.stmt
(self.n_block
)
1102 if v
.is_break
(self.escapemark
) then return
1103 v
.is_continue
(self.escapemark
) # Clear the break
1104 if v
.is_escaping
then return
1105 v
.send
(v
.mainmodule
.force_get_primitive_method
("next", iter
.mtype
), [iter
])
1110 redef class AAssertExpr
1113 var cond
= v
.expr
(self.n_expr
)
1114 if cond
== null then return
1115 if not cond
.is_true
then
1117 if v
.is_escaping
then return
1120 fatal
(v
, "Assert '{nid.text}' failed")
1122 fatal
(v
, "Assert failed")
1132 var cond
= v
.expr
(self.n_expr
)
1133 if cond
== null then return null
1134 if cond
.is_true
then return cond
1135 return v
.expr
(self.n_expr2
)
1139 redef class AAndExpr
1142 var cond
= v
.expr
(self.n_expr
)
1143 if cond
== null then return null
1144 if not cond
.is_true
then return cond
1145 return v
.expr
(self.n_expr2
)
1149 redef class ANotExpr
1152 var cond
= v
.expr
(self.n_expr
)
1153 if cond
== null then return null
1154 return v
.bool_instance
(not cond
.is_true
)
1158 redef class AOrElseExpr
1161 var i
= v
.expr
(self.n_expr
)
1162 if i
== null then return null
1163 if i
!= v
.null_instance
then return i
1164 return v
.expr
(self.n_expr2
)
1171 var i
= v
.expr
(self.n_expr
)
1172 if i
== null then return null
1173 var i2
= v
.expr
(self.n_expr2
)
1174 if i2
== null then return null
1175 return v
.bool_instance
(i
.eq_is
(i2
))
1179 redef class AIntExpr
1182 return v
.int_instance
(self.value
.as(not null))
1186 redef class AFloatExpr
1189 return v
.float_instance
(self.value
.as(not null))
1193 redef class ACharExpr
1196 return v
.char_instance
(self.value
.as(not null))
1200 redef class AArrayExpr
1203 var val
= new Array[Instance]
1204 for nexpr
in self.n_exprs
.n_exprs
do
1205 var i
= v
.expr
(nexpr
)
1206 if i
== null then return null
1209 var mtype
= v
.unanchor_type
(self.mtype
.as(not null)).as(MGenericType)
1210 var elttype
= mtype
.arguments
.first
1211 return v
.array_instance
(val
, elttype
)
1215 redef class AStringFormExpr
1218 var txt
= self.value
.as(not null)
1219 var nat
= v
.native_string_instance
(txt
)
1220 var res
= new Instance(v
.mainmodule
.get_primitive_class
("String").mclass_type
)
1221 v
.init_instance
(res
)
1222 v
.send
(v
.mainmodule
.force_get_primitive_method
("from_cstring", res
.mtype
), [res
, nat
])
1223 v
.check_init_instance
(res
)
1228 redef class ASuperstringExpr
1231 var array
= new Array[Instance]
1232 for nexpr
in n_exprs
do
1233 var i
= v
.expr
(nexpr
)
1234 if i
== null then return null
1237 var i
= v
.array_instance
(array
, v
.mainmodule
.get_primitive_class
("Object").mclass_type
)
1238 var res
= v
.send
(v
.mainmodule
.force_get_primitive_method
("to_s", i
.mtype
), [i
])
1244 redef class ACrangeExpr
1247 var e1
= v
.expr
(self.n_expr
)
1248 if e1
== null then return null
1249 var e2
= v
.expr
(self.n_expr2
)
1250 if e2
== null then return null
1251 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1252 var res
= new Instance(mtype
)
1253 v
.init_instance
(res
)
1254 v
.send
(v
.mainmodule
.force_get_primitive_method
("init", mtype
), [res
, e1
, e2
])
1255 v
.check_init_instance
(res
)
1260 redef class AOrangeExpr
1263 var e1
= v
.expr
(self.n_expr
)
1264 if e1
== null then return null
1265 var e2
= v
.expr
(self.n_expr2
)
1266 if e2
== null then return null
1267 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1268 var res
= new Instance(mtype
)
1269 v
.init_instance
(res
)
1270 v
.send
(v
.mainmodule
.force_get_primitive_method
("without_last", mtype
), [res
, e1
, e2
])
1271 v
.check_init_instance
(res
)
1276 redef class ATrueExpr
1279 return v
.bool_instance
(true)
1283 redef class AFalseExpr
1286 return v
.bool_instance
(false)
1290 redef class ANullExpr
1293 return v
.null_instance
1297 redef class AIsaExpr
1300 var i
= v
.expr
(self.n_expr
)
1301 if i
== null then return null
1302 var mtype
= v
.unanchor_type
(self.cast_type
.as(not null))
1303 return v
.bool_instance
(v
.is_subtype
(i
.mtype
, mtype
))
1307 redef class AAsCastExpr
1310 var i
= v
.expr
(self.n_expr
)
1311 if i
== null then return null
1312 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1313 if not v
.is_subtype
(i
.mtype
, mtype
) then
1314 #fatal(v, "Cast failed expected {mtype}, got {i}")
1315 fatal
(v
, "Cast failed")
1321 redef class AAsNotnullExpr
1324 var i
= v
.expr
(self.n_expr
)
1325 if i
== null then return null
1326 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1327 if i
.mtype
isa MNullType then
1328 fatal
(v
, "Cast failed")
1334 redef class AParExpr
1337 return v
.expr
(self.n_expr
)
1341 redef class AOnceExpr
1344 if v
.onces
.has_key
(self) then
1345 return v
.onces
[self]
1347 var res
= v
.expr
(self.n_expr
)
1348 if res
== null then return null
1355 redef class ASendExpr
1358 var recv
= v
.expr
(self.n_expr
)
1359 if recv
== null then return null
1361 for a
in compute_raw_arguments
do
1363 if i
== null then return null
1366 for c
in self.n_closure_defs
do
1367 var mtype
= c
.mclosure
.mtype
1368 var instance
= new ClosureInstance(mtype
, v
.frame
, c
)
1371 var mproperty
= self.mproperty
.as(not null)
1373 var res
= v
.send
(mproperty
, args
)
1374 if v
.is_break
(self.escapemark
) then
1376 v
.escapevalue
= null
1382 redef class ASendReassignFormExpr
1385 var recv
= v
.expr
(self.n_expr
)
1386 if recv
== null then return
1388 for a
in compute_raw_arguments
do
1390 if i
== null then return
1393 var value
= v
.expr
(self.n_value
)
1394 if value
== null then return
1396 var mproperty
= self.mproperty
.as(not null)
1397 var read
= v
.send
(mproperty
, args
)
1400 var write
= v
.send
(self.reassign_property
.mproperty
, [read
, value
])
1401 assert write
!= null
1405 v
.send
(self.write_mproperty
.as(not null), args
)
1409 redef class ASuperExpr
1412 var recv
= v
.frame
.arguments
.first
1414 for a
in self.n_args
.n_exprs
do
1416 if i
== null then return null
1419 if args
.length
== 1 then
1420 args
= v
.frame
.arguments
1423 var mproperty
= self.mproperty
1424 if mproperty
!= null then
1425 if mproperty
.intro
.msignature
.arity
== 0 then
1429 var res
= v
.send
(mproperty
, args
)
1433 # stantard call-next-method
1434 var mpropdef
= v
.frame
.mpropdef
1435 # FIXME: we do not want an ugly static call!
1436 var mpropdefs
= mpropdef
.mproperty
.lookup_super_definitions
(mpropdef
.mclassdef
.mmodule
, mpropdef
.mclassdef
.bound_mtype
)
1437 if mpropdefs
.length
!= 1 then
1438 debug
("MPRODFEFS for super {mpropdef} for {recv}: {mpropdefs.join(", ")}")
1440 mpropdef
= mpropdefs
.first
1441 assert mpropdef
isa MMethodDef
1442 var res
= v
.call
(mpropdef
, args
)
1447 redef class ANewExpr
1450 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1451 var recv
= new Instance(mtype
)
1452 v
.init_instance
(recv
)
1454 for a
in self.n_args
.n_exprs
do
1456 if i
== null then return null
1459 var mproperty
= self.mproperty
.as(not null)
1460 var res2
= v
.send
(mproperty
, args
)
1461 if res2
!= null then
1462 #self.debug("got {res2} from {mproperty}. drop {recv}")
1465 v
.check_init_instance
(recv
)
1470 redef class AAttrExpr
1473 var recv
= v
.expr
(self.n_expr
)
1474 if recv
== null then return null
1475 var mproperty
= self.mproperty
.as(not null)
1476 return v
.read_attribute
(mproperty
, recv
)
1480 redef class AAttrAssignExpr
1483 var recv
= v
.expr
(self.n_expr
)
1484 if recv
== null then return
1485 var i
= v
.expr
(self.n_value
)
1486 if i
== null then return
1487 var mproperty
= self.mproperty
.as(not null)
1488 recv
.attributes
[mproperty
] = i
1492 redef class AAttrReassignExpr
1495 var recv
= v
.expr
(self.n_expr
)
1496 if recv
== null then return
1497 var value
= v
.expr
(self.n_value
)
1498 if value
== null then return
1499 var mproperty
= self.mproperty
.as(not null)
1500 var attr
= v
.read_attribute
(mproperty
, recv
)
1501 var res
= v
.send
(reassign_property
.mproperty
, [attr
, value
])
1503 recv
.attributes
[mproperty
] = res
1507 redef class AIssetAttrExpr
1510 var recv
= v
.expr
(self.n_expr
)
1511 if recv
== null then return null
1512 var mproperty
= self.mproperty
.as(not null)
1513 return v
.bool_instance
(recv
.attributes
.has_key
(mproperty
))
1517 redef class AClosureCallExpr
1520 var args
= new Array[Instance]
1521 for a
in self.n_args
.n_exprs
do
1523 if i
== null then return null
1526 var i
= v
.frame
.map
[self.variable
.as(not null)]
1527 assert i
isa ClosureInstance
1528 var res
= v
.call_closure
(i
, args
)
1533 redef class ADebugTypeExpr