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 private import parser
::tables
24 redef class ToolContext
25 # --discover-call-trace
26 var opt_discover_call_trace
: OptionBool = new OptionBool("Trace calls of the first invocation of a method", "--discover-call-trace")
31 self.option_context
.add_option
(self.opt_discover_call_trace
)
35 redef class ModelBuilder
36 # Execute the program from the entry point (`Sys::main`) of the `mainmodule`
37 # `arguments` are the command-line arguments in order
39 # 1. the AST is fully loaded.
40 # 2. the model is fully built.
41 # 3. the instructions are fully analysed.
42 fun run_naive_interpreter
(mainmodule
: MModule, arguments
: Array[String])
45 self.toolcontext
.info
("*** START INTERPRETING ***", 1)
47 var interpreter
= new NaiveInterpreter(self, mainmodule
, arguments
)
48 init_naive_interpreter
(interpreter
, mainmodule
)
51 self.toolcontext
.info
("*** END INTERPRETING: {time1-time0} ***", 2)
54 private fun init_naive_interpreter
(interpreter
: NaiveInterpreter, mainmodule
: MModule) do
55 var sys_type
= mainmodule
.sys_type
56 if sys_type
== null then return # no class Sys
57 var mainobj
= new MutableInstance(sys_type
)
58 interpreter
.mainobj
= mainobj
59 interpreter
.init_instance
(mainobj
)
60 var initprop
= mainmodule
.try_get_primitive_method
("init", sys_type
.mclass
)
61 if initprop
!= null then
62 interpreter
.send
(initprop
, [mainobj
])
64 var mainprop
= mainmodule
.try_get_primitive_method
("run", sys_type
.mclass
) or else
65 mainmodule
.try_get_primitive_method
("main", sys_type
.mclass
)
66 if mainprop
!= null then
67 interpreter
.send
(mainprop
, [mainobj
])
72 # The visitor that interprets the Nit Program by walking on the AST
73 private class NaiveInterpreter
74 # The modelbuilder that know the AST and its associations with the model
75 var modelbuilder
: ModelBuilder
77 # The main moduleof the program (used to lookup methoda
78 var mainmodule
: MModule
80 # The command line arguments of the interpreted program
81 # arguments.first is the program name
82 # arguments[1] is the first argument
83 var arguments
: Array[String]
85 var mainobj
: nullable Instance
87 init(modelbuilder
: ModelBuilder, mainmodule
: MModule, arguments
: Array[String])
89 self.modelbuilder
= modelbuilder
90 self.mainmodule
= mainmodule
91 self.arguments
= arguments
92 self.true_instance
= new PrimitiveInstance[Bool](mainmodule
.bool_type
, true)
93 self.false_instance
= new PrimitiveInstance[Bool](mainmodule
.bool_type
, false)
94 self.null_instance
= new MutableInstance(mainmodule
.model
.null_type
)
97 # Subtype test in the context of the mainmodule
98 fun is_subtype
(sub
, sup
: MType): Bool
100 return sub
.is_subtype
(self.mainmodule
, self.frame
.arguments
.first
.mtype
.as(MClassType), sup
)
103 fun force_get_primitive_method
(name
: String, recv
: MType): MMethod
105 assert recv
isa MClassType
106 return self.modelbuilder
.force_get_primitive_method
(self.frame
.current_node
, name
, recv
.mclass
, self.mainmodule
)
109 # Is a return executed?
110 # Set this mark to skip the evaluation until the end of the specified method frame
111 var returnmark
: nullable Frame = null
113 # Is a break executed?
114 # Set this mark to skip the evaluation until a labeled statement catch it with `is_break`
115 var breakmark
: nullable EscapeMark = null
117 # Is a continue executed?
118 # Set this mark to skip the evaluation until a labeled statement catch it with `is_continue`
119 var continuemark
: nullable EscapeMark = null
121 # Is a return or a break or a continue executed?
122 # Use this function to know if you must skip the evaluation of statements
123 fun is_escaping
: Bool do return returnmark
!= null or breakmark
!= null or continuemark
!= null
125 # The value associated with the current return/break/continue, if any.
126 # Set the value when you set a escapemark.
127 # Read the value when you catch a mark or reach the end of a method
128 var escapevalue
: nullable Instance = null
130 # If there is a break and is associated with `escapemark`, then return true an clear the mark.
131 # If there is no break or if `escapemark` is null then return false.
132 # Use this function to catch a potential break.
133 fun is_break
(escapemark
: nullable EscapeMark): Bool
135 if escapemark
!= null and self.breakmark
== escapemark
then
136 self.breakmark
= null
143 # If there is a continue and is associated with `escapemark`, then return true an clear the mark.
144 # If there is no continue or if `escapemark` is null then return false.
145 # Use this function to catch a potential continue.
146 fun is_continue
(escapemark
: nullable EscapeMark): Bool
148 if escapemark
!= null and self.continuemark
== escapemark
then
149 self.continuemark
= null
156 # Evaluate `n` as an expression in the current context.
157 # Return the value of the expression.
158 # If `n` cannot be evaluated, then aborts.
159 fun expr
(n
: AExpr): nullable Instance
161 var frame
= self.frame
162 var old
= frame
.current_node
163 frame
.current_node
= n
164 #n.debug("IN Execute expr")
166 if i
== null and not self.is_escaping
then
167 n
.debug
("inconsitance: no value and not escaping.")
169 var implicit_cast_to
= n
.implicit_cast_to
170 if implicit_cast_to
!= null then
171 var mtype
= self.unanchor_type
(implicit_cast_to
)
172 if not self.is_subtype
(i
.mtype
, mtype
) then n
.fatal
(self, "Cast failed. Expected `{implicit_cast_to}`, got `{i.mtype}`")
175 #n.debug("OUT Execute expr: value is {i}")
176 #if not is_subtype(i.mtype, n.mtype.as(not null)) then n.debug("Expected {n.mtype.as(not null)} got {i}")
177 frame
.current_node
= old
181 # Evaluate `n` as a statement in the current context.
182 # Do nothing if `n` is null.
183 # If `n` cannot be evaluated, then aborts.
184 fun stmt
(n
: nullable AExpr)
187 var frame
= self.frame
188 var old
= frame
.current_node
189 frame
.current_node
= n
190 #n.debug("Execute stmt")
192 frame
.current_node
= old
196 # Map used to store values of nodes that must be evaluated once in the system (`AOnceExpr`)
197 var onces
: Map[ANode, Instance] = new HashMap[ANode, Instance]
199 # Return the boolean instance associated with `val`.
200 fun bool_instance
(val
: Bool): Instance
202 if val
then return self.true_instance
else return self.false_instance
205 # Return the integer instance associated with `val`.
206 fun int_instance
(val
: Int): Instance
208 var ic
= self.mainmodule
.get_primitive_class
("Int")
209 return new PrimitiveInstance[Int](ic
.mclass_type
, val
)
212 # Return the char instance associated with `val`.
213 fun char_instance
(val
: Char): Instance
215 var ic
= self.mainmodule
.get_primitive_class
("Char")
216 return new PrimitiveInstance[Char](ic
.mclass_type
, val
)
219 # Return the float instance associated with `val`.
220 fun float_instance
(val
: Float): Instance
222 var ic
= self.mainmodule
.get_primitive_class
("Float")
223 return new PrimitiveInstance[Float](ic
.mclass_type
, val
)
226 # The unique intance of the `true` value.
227 var true_instance
: Instance
229 # The unique intance of the `false` value.
230 var false_instance
: Instance
232 # The unique intance of the `null` value.
233 var null_instance
: Instance
235 # Return a new array made of `values`.
236 # The dynamic type of the result is Array[elttype].
237 fun array_instance
(values
: Array[Instance], elttype
: MType): Instance
239 assert not elttype
.need_anchor
240 var nat
= new PrimitiveInstance[Array[Instance]](self.mainmodule
.get_primitive_class
("NativeArray").get_mtype
([elttype
]), values
)
241 var mtype
= self.mainmodule
.get_primitive_class
("Array").get_mtype
([elttype
])
242 var res
= new MutableInstance(mtype
)
243 self.init_instance
(res
)
244 self.send
(self.force_get_primitive_method
("with_native", mtype
), [res
, nat
, self.int_instance
(values
.length
)])
248 # Return a new native string initialized with `txt`
249 fun native_string_instance
(txt
: String): Instance
251 var val
= new FlatBuffer.from
(txt
)
253 var ic
= self.mainmodule
.get_primitive_class
("NativeString")
254 return new PrimitiveInstance[Buffer](ic
.mclass_type
, val
)
257 # The current frame used to store local variables of the current method executed
258 fun frame
: Frame do return frames
.first
260 # The stack of all frames. The first one is the current one.
261 var frames
: List[Frame] = new List[Frame]
263 # Return a stack stace. One line per function
264 fun stack_trace
: String
266 var b
= new FlatBuffer
267 b
.append
(",---- Stack trace -- - - -\n")
269 b
.append
("| {f.mpropdef} ({f.current_node.location})\n")
271 b
.append
("`------------------- - - -")
275 # Exit the program with a message
276 fun fatal
(message
: String)
278 if frames
.is_empty
then
281 self.frame
.current_node
.fatal
(self, message
)
286 # Debug on the current node
287 fun debug
(message
: String)
289 if frames
.is_empty
then
292 self.frame
.current_node
.debug
(message
)
296 # Store known method, used to trace methods as thez are reached
297 var discover_call_trace
: Set[MMethodDef] = new HashSet[MMethodDef]
299 # Common code for calls to injected methods and normal methods
300 fun call_commons
(mpropdef
: MMethodDef, args
: Array[Instance]): Array[Instance]
302 var vararg_rank
= mpropdef
.msignature
.vararg_rank
303 if vararg_rank
>= 0 then
304 assert args
.length
>= mpropdef
.msignature
.arity
+ 1 # because of self
306 args
= new Array[Instance]
308 args
.add
(rawargs
.first
) # recv
310 for i
in [0..vararg_rank
[ do
311 args
.add
(rawargs
[i
+1])
314 var vararg_lastrank
= vararg_rank
+ rawargs
.length-1-mpropdef
.msignature
.arity
315 var vararg
= new Array[Instance]
316 for i
in [vararg_rank
..vararg_lastrank
] do
317 vararg
.add
(rawargs
[i
+1])
319 # FIXME: its it to late to determine the vararg type, this should have been done during a previous analysis
320 var elttype
= mpropdef
.msignature
.mparameters
[vararg_rank
].mtype
.anchor_to
(self.mainmodule
, args
.first
.mtype
.as(MClassType))
321 args
.add
(self.array_instance
(vararg
, elttype
))
323 for i
in [vararg_lastrank
+1..rawargs
.length-1
[ do
324 args
.add
(rawargs
[i
+1])
330 # Execute `mpropdef` for a `args` (where `args[0]` is the receiver).
331 # Return a falue if `mpropdef` is a function, or null if it is a procedure.
332 # The call is direct/static. There is no message-seding/late-binding.
333 fun call
(mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
335 args
= call_commons
(mpropdef
, args
)
336 return call_without_varargs
(mpropdef
, args
)
339 # Common code to call and this function
341 # Call only executes the variadic part, this avoids
342 # double encapsulation of variadic parameters into an Array
343 fun call_without_varargs
(mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
345 if self.modelbuilder
.toolcontext
.opt_discover_call_trace
.value
and not self.discover_call_trace
.has
(mpropdef
) then
346 self.discover_call_trace
.add mpropdef
347 self.debug
("Discovered {mpropdef}")
349 assert args
.length
== mpropdef
.msignature
.arity
+ 1 else debug
("Invalid arity for {mpropdef}. {args.length} arguments given.")
351 # Look for the AST node that implements the property
352 var mproperty
= mpropdef
.mproperty
353 if self.modelbuilder
.mpropdef2npropdef
.has_key
(mpropdef
) then
354 var npropdef
= self.modelbuilder
.mpropdef2npropdef
[mpropdef
]
355 self.parameter_check
(npropdef
, mpropdef
, args
)
356 return npropdef
.call
(self, mpropdef
, args
)
357 else if mproperty
.name
== "init" then
358 var nclassdef
= self.modelbuilder
.mclassdef2nclassdef
[mpropdef
.mclassdef
]
359 self.parameter_check
(nclassdef
, mpropdef
, args
)
360 return nclassdef
.call
(self, mpropdef
, args
)
362 fatal
("Fatal Error: method {mpropdef} not found in the AST")
367 # Generate type checks in the C code to check covariant parameters
368 fun parameter_check
(node
: ANode, mpropdef
: MMethodDef, args
: Array[Instance])
370 var msignature
= mpropdef
.msignature
371 for i
in [0..msignature
.arity
[ do
372 # skip test for vararg since the array is instantiated with the correct polymorphic type
373 if msignature
.vararg_rank
== i
then continue
375 # skip if the cast is not required
376 var origmtype
= mpropdef
.mproperty
.intro
.msignature
.mparameters
[i
].mtype
377 if not origmtype
.need_anchor
then continue
379 # get the parameter type
380 var mtype
= msignature
.mparameters
[i
].mtype
381 var anchor
= args
.first
.mtype
.as(MClassType)
382 var amtype
= mtype
.anchor_to
(self.mainmodule
, anchor
)
383 if not args
[i
+1].mtype
.is_subtype
(self.mainmodule
, anchor
, amtype
) then
384 node
.fatal
(self, "Cast failed. Expected `{mtype}`, got `{args[i+1].mtype}`")
389 # Common code for runtime injected calls and normal calls
390 fun send_commons
(mproperty
: MMethod, args
: Array[Instance], mtype
: MType): nullable Instance
392 if mtype
isa MNullType then
393 if mproperty
.name
== "==" then
394 return self.bool_instance
(args
[0] == args
[1])
395 else if mproperty
.name
== "!=" then
396 return self.bool_instance
(args
[0] != args
[1])
398 #fatal("Receiver is null. {mproperty}. {args.join(" ")} {self.frame.current_node.class_name}")
399 fatal
("Receiver is null")
404 # Execute a full `callsite` for given `args`
405 # Use this method, instead of `send` to execute and control the aditionnal behavior of the call-sites
406 fun callsite
(callsite
: nullable CallSite, arguments
: Array[Instance]): nullable Instance
408 var initializers
= callsite
.mpropdef
.initializers
409 if not initializers
.is_empty
then
410 assert initializers
.length
== arguments
.length
- 1 else debug
("expected {initializers.length} got {arguments.length - 1}")
411 var recv
= arguments
.first
413 for p
in initializers
do
414 if p
isa MMethod then
415 self.send
(p
, [recv
, arguments
[i
]])
416 else if p
isa MAttribute then
417 assert recv
isa MutableInstance
418 recv
.attributes
[p
] = arguments
[i
]
422 return send
(callsite
.mproperty
, [recv
])
424 return send
(callsite
.mproperty
, arguments
)
427 # Execute `mproperty` for a `args` (where `args[0]` is the receiver).
428 # Return a falue if `mproperty` is a function, or null if it is a procedure.
429 # The call is polimotphic. There is a message-seding/late-bindng according to te receiver (args[0]).
430 fun send
(mproperty
: MMethod, args
: Array[Instance]): nullable Instance
432 var recv
= args
.first
433 var mtype
= recv
.mtype
434 var ret
= send_commons
(mproperty
, args
, mtype
)
435 if ret
!= null then return ret
436 var propdef
= mproperty
.lookup_first_definition
(self.mainmodule
, mtype
)
437 return self.call
(propdef
, args
)
440 # Read the attribute `mproperty` of an instance `recv` and return its value.
441 # If the attribute in not yet initialized, then aborts with an error message.
442 fun read_attribute
(mproperty
: MAttribute, recv
: Instance): Instance
444 assert recv
isa MutableInstance
445 if not recv
.attributes
.has_key
(mproperty
) then
446 fatal
("Uninitialized attribute {mproperty.name}")
449 return recv
.attributes
[mproperty
]
452 # Replace in `recv` the value of the attribute `mproperty` by `value`
453 fun write_attribute
(mproperty
: MAttribute, recv
: Instance, value
: Instance)
455 assert recv
isa MutableInstance
456 recv
.attributes
[mproperty
] = value
459 # Is the attribute `mproperty` initialized the instance `recv`?
460 fun isset_attribute
(mproperty
: MAttribute, recv
: Instance): Bool
462 assert recv
isa MutableInstance
463 return recv
.attributes
.has_key
(mproperty
)
466 # Collect attributes of a type in the order of their init
467 fun collect_attr_propdef
(mtype
: MType): Array[AAttrPropdef]
469 var cache
= self.collect_attr_propdef_cache
470 if cache
.has_key
(mtype
) then return cache
[mtype
]
472 var res
= new Array[AAttrPropdef]
473 var cds
= mtype
.collect_mclassdefs
(self.mainmodule
).to_a
474 self.mainmodule
.linearize_mclassdefs
(cds
)
476 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
477 for npropdef
in n
.n_propdefs
do
478 if npropdef
isa AAttrPropdef then
488 var collect_attr_propdef_cache
= new HashMap[MType, Array[AAttrPropdef]]
490 # Fill the initial values of the newly created instance `recv`.
491 # `recv.mtype` is used to know what must be filled.
492 fun init_instance
(recv
: Instance)
494 for npropdef
in collect_attr_propdef
(recv
.mtype
) do
495 npropdef
.init_expr
(self, recv
)
499 # This function determine the correct type according the reciever of the current definition (self).
500 fun unanchor_type
(mtype
: MType): MType
502 return mtype
.anchor_to
(self.mainmodule
, self.frame
.arguments
.first
.mtype
.as(MClassType))
506 # An instance represents a value of the executed program.
507 abstract class Instance
508 # The dynamic type of the instance
509 # ASSERT: not self.mtype.is_anchored
512 # return true if the instance is the true value.
513 # return false if the instance is the true value.
515 fun is_true
: Bool do abort
517 # Return true if `self` IS `o` (using the Nit semantic of is)
518 fun eq_is
(o
: Instance): Bool do return self.is_same_instance
(o
)
520 # Human readable object identity "Type#number"
521 redef fun to_s
do return "{mtype}"
523 # Return the integer value if the instance is an integer.
525 fun to_i
: Int do abort
527 # Return the integer value if the instance is a float.
529 fun to_f
: Float do abort
531 # The real value encapsulated if the instance is primitive.
533 fun val
: Object do abort
536 # A instance with attribute (standards objects)
537 class MutableInstance
540 # The values of the attributes
541 var attributes
: Map[MAttribute, Instance] = new HashMap[MAttribute, Instance]
544 # Special instance to handle primitives values (int, bool, etc.)
545 # The trick it just to encapsulate the <<real>> value
546 class PrimitiveInstance[E
: Object]
549 # The real value encapsulated
552 init(mtype
: MType, val
: E
)
560 if val
== true then return true
561 if val
== false then return false
567 if not o
isa PrimitiveInstance[Object] then return false
568 return self.val
== o
.val
573 if not o
isa PrimitiveInstance[Object] then return false
574 return self.val
.is_same_instance
(o
.val
)
577 redef fun to_s
do return "{mtype}#{val.object_id}({val})"
579 redef fun to_i
do return val
.as(Int)
581 redef fun to_f
do return val
.as(Float)
584 # Information about local variables in a running method
586 # The current visited node
587 # The node is stored by frame to keep a stack trace
588 var current_node
: ANode
589 # The executed property.
590 # A Method in case of a call, an attribute in case of a default initialization.
591 var mpropdef
: MPropDef
592 # Arguments of the method (the first is te receiver
593 var arguments
: Array[Instance]
594 # Mapping betwen a variable an the current value
595 var map
: Map[Variable, Instance] = new HashMap[Variable, Instance]
599 # Aborts the program with a message
600 # `v` is used to know if a colored message is displayed or not
601 private fun fatal
(v
: NaiveInterpreter, message
: String)
603 if v
.modelbuilder
.toolcontext
.opt_no_color
.value
== true then
604 sys
.stderr
.write
("Runtime error: {message} ({location.file.filename}:{location.line_start})\n")
606 sys
.stderr
.write
("{location}: Runtime error: {message}\n{location.colored_line("0;31")}\n")
607 sys
.stderr
.write
(v
.stack_trace
)
608 sys
.stderr
.write
("\n")
615 # Execute a `mpropdef` associated with the current node.
616 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
618 fatal
(v
, "NOT YET IMPLEMENTED method kind {class_name}. {mpropdef}")
623 redef class AMethPropdef
626 redef fun call
(v
, mpropdef
, args
)
628 var f
= new Frame(self, self.mpropdef
.as(not null), args
)
629 var res
= call_commons
(v
, mpropdef
, args
, f
)
631 if v
.returnmark
== f
then
640 private fun call_commons
(v
: NaiveInterpreter, mpropdef
: MMethodDef, arguments
: Array[Instance], f
: Frame): nullable Instance
642 for i
in [0..mpropdef
.msignature
.arity
[ do
643 var variable
= self.n_signature
.n_params
[i
].variable
644 assert variable
!= null
645 f
.map
[variable
] = arguments
[i
+1]
650 if mpropdef
.is_abstract
then
651 v
.fatal
("Abstract method `{mpropdef.mproperty.name}` called on `{arguments.first.mtype}`")
655 # Call the implicit super-init
656 var auto_super_inits
= self.auto_super_inits
657 if auto_super_inits
!= null then
658 var args
= [arguments
.first
]
659 for auto_super_init
in auto_super_inits
do
661 for i
in [0..auto_super_init
.msignature
.arity
+1[ do
662 args
.add
(arguments
[i
])
664 assert auto_super_init
.mproperty
!= mpropdef
.mproperty
665 v
.callsite
(auto_super_init
, args
)
668 if auto_super_call
then
669 # standard call-next-method
670 var superpd
= mpropdef
.lookup_next_definition
(v
.mainmodule
, arguments
.first
.mtype
)
671 v
.call_without_varargs
(superpd
, arguments
)
674 if n_block
!= null then
678 return intern_call
(v
, mpropdef
, arguments
)
682 private fun intern_call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
684 var pname
= mpropdef
.mproperty
.name
685 var cname
= mpropdef
.mclassdef
.mclass
.name
686 if pname
== "output" then
687 var recv
= args
.first
690 else if pname
== "object_id" then
691 var recv
= args
.first
692 if recv
isa PrimitiveInstance[Object] then
693 return v
.int_instance
(recv
.val
.object_id
)
695 return v
.int_instance
(recv
.object_id
)
697 else if pname
== "output_class_name" then
698 var recv
= args
.first
701 else if pname
== "native_class_name" then
702 var recv
= args
.first
703 var txt
= recv
.mtype
.to_s
704 return v
.native_string_instance
(txt
)
705 else if pname
== "==" then
706 # == is correclt redefined for instances
707 return v
.bool_instance
(args
[0] == args
[1])
708 else if pname
== "!=" then
709 return v
.bool_instance
(args
[0] != args
[1])
710 else if pname
== "is_same_type" then
711 return v
.bool_instance
(args
[0].mtype
== args
[1].mtype
)
712 else if pname
== "is_same_instance" then
713 return v
.bool_instance
(args
[1] != null and args
[0].eq_is
(args
[1]))
714 else if pname
== "exit" then
717 else if pname
== "sys" then
719 else if cname
== "Int" then
720 var recvval
= args
[0].to_i
721 if pname
== "unary -" then
722 return v
.int_instance
(-args
[0].to_i
)
723 else if pname
== "+" then
724 return v
.int_instance
(args
[0].to_i
+ args
[1].to_i
)
725 else if pname
== "-" then
726 return v
.int_instance
(args
[0].to_i
- args
[1].to_i
)
727 else if pname
== "*" then
728 return v
.int_instance
(args
[0].to_i
* args
[1].to_i
)
729 else if pname
== "%" then
730 return v
.int_instance
(args
[0].to_i
% args
[1].to_i
)
731 else if pname
== "/" then
732 return v
.int_instance
(args
[0].to_i
/ args
[1].to_i
)
733 else if pname
== "<" then
734 return v
.bool_instance
(args
[0].to_i
< args
[1].to_i
)
735 else if pname
== ">" then
736 return v
.bool_instance
(args
[0].to_i
> args
[1].to_i
)
737 else if pname
== "<=" then
738 return v
.bool_instance
(args
[0].to_i
<= args
[1].to_i
)
739 else if pname
== ">=" then
740 return v
.bool_instance
(args
[0].to_i
>= args
[1].to_i
)
741 else if pname
== "<=>" then
742 return v
.int_instance
(args
[0].to_i
<=> args
[1].to_i
)
743 else if pname
== "ascii" then
744 return v
.char_instance
(args
[0].to_i
.ascii
)
745 else if pname
== "to_f" then
746 return v
.float_instance
(args
[0].to_i
.to_f
)
747 else if pname
== "lshift" then
748 return v
.int_instance
(args
[0].to_i
.lshift
(args
[1].to_i
))
749 else if pname
== "rshift" then
750 return v
.int_instance
(args
[0].to_i
.rshift
(args
[1].to_i
))
751 else if pname
== "rand" then
752 var res
= recvval
.rand
753 return v
.int_instance
(res
)
754 else if pname
== "bin_and" then
755 return v
.int_instance
(args
[0].to_i
.bin_and
(args
[1].to_i
))
756 else if pname
== "bin_or" then
757 return v
.int_instance
(args
[0].to_i
.bin_or
(args
[1].to_i
))
758 else if pname
== "bin_xor" then
759 return v
.int_instance
(args
[0].to_i
.bin_xor
(args
[1].to_i
))
760 else if pname
== "native_int_to_s" then
761 return v
.native_string_instance
(recvval
.to_s
)
762 else if pname
== "strerror_ext" then
763 return v
.native_string_instance
(recvval
.strerror
)
765 else if cname
== "Char" then
766 var recv
= args
[0].val
.as(Char)
767 if pname
== "ascii" then
768 return v
.int_instance
(recv
.ascii
)
769 else if pname
== "successor" then
770 return v
.char_instance
(recv
.successor
(args
[1].to_i
))
771 else if pname
== "predecessor" then
772 return v
.char_instance
(recv
.predecessor
(args
[1].to_i
))
773 else if pname
== "<" then
774 return v
.bool_instance
(recv
< args
[1].val
.as(Char))
775 else if pname
== ">" then
776 return v
.bool_instance
(recv
> args
[1].val
.as(Char))
777 else if pname
== "<=" then
778 return v
.bool_instance
(recv
<= args
[1].val
.as(Char))
779 else if pname
== ">=" then
780 return v
.bool_instance
(recv
>= args
[1].val
.as(Char))
781 else if pname
== "<=>" then
782 return v
.int_instance
(recv
<=> args
[1].val
.as(Char))
784 else if cname
== "Float" then
785 var recv
= args
[0].to_f
786 if pname
== "unary -" then
787 return v
.float_instance
(-recv
)
788 else if pname
== "+" then
789 return v
.float_instance
(recv
+ args
[1].to_f
)
790 else if pname
== "-" then
791 return v
.float_instance
(recv
- args
[1].to_f
)
792 else if pname
== "*" then
793 return v
.float_instance
(recv
* args
[1].to_f
)
794 else if pname
== "/" then
795 return v
.float_instance
(recv
/ args
[1].to_f
)
796 else if pname
== "<" then
797 return v
.bool_instance
(recv
< args
[1].to_f
)
798 else if pname
== ">" then
799 return v
.bool_instance
(recv
> args
[1].to_f
)
800 else if pname
== "<=" then
801 return v
.bool_instance
(recv
<= args
[1].to_f
)
802 else if pname
== ">=" then
803 return v
.bool_instance
(recv
>= args
[1].to_f
)
804 else if pname
== "to_i" then
805 return v
.int_instance
(recv
.to_i
)
806 else if pname
== "cos" then
807 return v
.float_instance
(args
[0].to_f
.cos
)
808 else if pname
== "sin" then
809 return v
.float_instance
(args
[0].to_f
.sin
)
810 else if pname
== "tan" then
811 return v
.float_instance
(args
[0].to_f
.tan
)
812 else if pname
== "acos" then
813 return v
.float_instance
(args
[0].to_f
.acos
)
814 else if pname
== "asin" then
815 return v
.float_instance
(args
[0].to_f
.asin
)
816 else if pname
== "atan" then
817 return v
.float_instance
(args
[0].to_f
.atan
)
818 else if pname
== "sqrt" then
819 return v
.float_instance
(args
[0].to_f
.sqrt
)
820 else if pname
== "exp" then
821 return v
.float_instance
(args
[0].to_f
.exp
)
822 else if pname
== "log" then
823 return v
.float_instance
(args
[0].to_f
.log
)
824 else if pname
== "pow" then
825 return v
.float_instance
(args
[0].to_f
.pow
(args
[1].to_f
))
826 else if pname
== "rand" then
827 return v
.float_instance
(args
[0].to_f
.rand
)
828 else if pname
== "abs" then
829 return v
.float_instance
(args
[0].to_f
.abs
)
830 else if pname
== "hypot_with" then
831 return v
.float_instance
(args
[0].to_f
.hypot_with
(args
[1].to_f
))
832 else if pname
== "is_nan" then
833 return v
.bool_instance
(args
[0].to_f
.is_nan
)
834 else if pname
== "is_inf_extern" then
835 return v
.bool_instance
(args
[0].to_f
.is_inf
!= 0)
837 else if cname
== "NativeString" then
838 if pname
== "init" then
839 return v
.native_string_instance
("!" * args
[1].to_i
)
841 var recvval
= args
.first
.val
.as(Buffer)
842 if pname
== "[]" then
843 var arg1
= args
[1].to_i
844 if arg1
>= recvval
.length
or arg1
< 0 then
845 debug
("Illegal access on {recvval} for element {arg1}/{recvval.length}")
847 return v
.char_instance
(recvval
.chars
[arg1
])
848 else if pname
== "[]=" then
849 var arg1
= args
[1].to_i
850 if arg1
>= recvval
.length
or arg1
< 0 then
851 debug
("Illegal access on {recvval} for element {arg1}/{recvval.length}")
853 recvval
.chars
[arg1
] = args
[2].val
.as(Char)
855 else if pname
== "copy_to" then
856 # sig= copy_to(dest: NativeString, length: Int, from: Int, to: Int)
857 var destval
= args
[1].val
.as(FlatBuffer)
858 var lenval
= args
[2].to_i
859 var fromval
= args
[3].to_i
860 var toval
= args
[4].to_i
862 debug
("Illegal access on {recvval} for element {fromval}/{recvval.length}")
864 if fromval
+ lenval
>= recvval
.length
then
865 debug
("Illegal access on {recvval} for element {fromval}+{lenval}/{recvval.length}")
868 debug
("Illegal access on {destval} for element {toval}/{destval.length}")
870 if toval
+ lenval
>= destval
.length
then
871 debug
("Illegal access on {destval} for element {toval}+{lenval}/{destval.length}")
873 recvval
.as(FlatBuffer).copy
(fromval
, lenval
, destval
, toval
)
875 else if pname
== "atoi" then
876 return v
.int_instance
(recvval
.to_i
)
877 else if pname
== "file_exists" then
878 return v
.bool_instance
(recvval
.to_s
.file_exists
)
879 else if pname
== "file_mkdir" then
882 else if pname
== "file_chdir" then
885 else if pname
== "file_realpath" then
886 return v
.native_string_instance
(recvval
.to_s
.realpath
)
887 else if pname
== "get_environ" then
888 var txt
= recvval
.to_s
.environ
889 return v
.native_string_instance
(txt
)
890 else if pname
== "system" then
891 var res
= sys
.system
(recvval
.to_s
)
892 return v
.int_instance
(res
)
893 else if pname
== "atof" then
894 return v
.float_instance
(recvval
.to_f
)
896 else if pname
== "calloc_string" then
897 return v
.native_string_instance
("!" * args
[1].to_i
)
898 else if cname
== "NativeArray" then
899 if pname
== "init" then
900 var val
= new Array[Instance].filled_with
(v
.null_instance
, args
[1].to_i
)
901 return new PrimitiveInstance[Array[Instance]](args
[0].mtype
, val
)
903 var recvval
= args
.first
.val
.as(Array[Instance])
904 if pname
== "[]" then
905 if args
[1].to_i
>= recvval
.length
or args
[1].to_i
< 0 then
906 debug
("Illegal access on {recvval} for element {args[1].to_i}/{recvval.length}")
908 return recvval
[args
[1].to_i
]
909 else if pname
== "[]=" then
910 recvval
[args
[1].to_i
] = args
[2]
912 else if pname
== "length" then
913 return v
.int_instance
(recvval
.length
)
914 else if pname
== "copy_to" then
915 recvval
.copy
(0, args
[2].to_i
, args
[1].val
.as(Array[Instance]), 0)
918 else if cname
== "NativeFile" then
919 if pname
== "native_stdout" then
920 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, sys
.stdout
)
921 else if pname
== "native_stdin" then
922 return new PrimitiveInstance[IStream](mpropdef
.mclassdef
.mclass
.mclass_type
, sys
.stdin
)
923 else if pname
== "native_stderr" then
924 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, sys
.stderr
)
925 else if pname
== "io_open_read" then
926 var a1
= args
[1].val
.as(Buffer)
927 return new PrimitiveInstance[IStream](mpropdef
.mclassdef
.mclass
.mclass_type
, new IFStream.open
(a1
.to_s
))
928 else if pname
== "io_open_write" then
929 var a1
= args
[1].val
.as(Buffer)
930 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, new OFStream.open
(a1
.to_s
))
932 var recvval
= args
.first
.val
933 if pname
== "io_write" then
934 var a1
= args
[1].val
.as(Buffer)
935 recvval
.as(OStream).write
(a1
.substring
(0, args
[2].to_i
).to_s
)
937 else if pname
== "io_read" then
938 var str
= recvval
.as(IStream).read
(args
[2].to_i
)
939 var a1
= args
[1].val
.as(Buffer)
940 new FlatBuffer.from
(str
).copy
(0, str
.length
, a1
.as(FlatBuffer), 0)
941 return v
.int_instance
(str
.length
)
942 else if pname
== "io_close" then
943 recvval
.as(IOS).close
944 return v
.int_instance
(0)
945 else if pname
== "address_is_null" then
946 return v
.false_instance
948 else if pname
== "calloc_array" then
949 var recvtype
= args
.first
.mtype
.as(MClassType)
951 mtype
= recvtype
.supertype_to
(v
.mainmodule
, recvtype
, v
.mainmodule
.get_primitive_class
("ArrayCapable"))
952 mtype
= mtype
.arguments
.first
953 var val
= new Array[Instance].filled_with
(v
.null_instance
, args
[1].to_i
)
954 return new PrimitiveInstance[Array[Instance]](v
.mainmodule
.get_primitive_class
("NativeArray").get_mtype
([mtype
]), val
)
955 else if pname
== "native_argc" then
956 return v
.int_instance
(v
.arguments
.length
)
957 else if pname
== "native_argv" then
958 var txt
= v
.arguments
[args
[1].to_i
]
959 return v
.native_string_instance
(txt
)
960 else if pname
== "native_argc" then
961 return v
.int_instance
(v
.arguments
.length
)
962 else if pname
== "native_argv" then
963 var txt
= v
.arguments
[args
[1].to_i
]
964 return v
.native_string_instance
(txt
)
965 else if pname
== "get_time" then
966 return v
.int_instance
(get_time
)
967 else if pname
== "srand_from" then
968 srand_from
(args
[1].to_i
)
970 else if pname
== "atan2" then
971 return v
.float_instance
(atan2
(args
[1].to_f
, args
[2].to_f
))
972 else if pname
== "pi" then
973 return v
.float_instance
(pi
)
974 else if pname
== "lexer_goto" then
975 return v
.int_instance
(lexer_goto
(args
[1].to_i
, args
[2].to_i
))
976 else if pname
== "lexer_accept" then
977 return v
.int_instance
(lexer_accept
(args
[1].to_i
))
978 else if pname
== "parser_goto" then
979 return v
.int_instance
(parser_goto
(args
[1].to_i
, args
[2].to_i
))
980 else if pname
== "parser_action" then
981 return v
.int_instance
(parser_action
(args
[1].to_i
, args
[2].to_i
))
982 else if pname
== "file_getcwd" then
983 return v
.native_string_instance
(getcwd
)
984 else if pname
== "errno" then
985 return v
.int_instance
(sys
.errno
)
986 else if pname
== "address_is_null" then
987 return v
.false_instance
989 if mpropdef
.is_intern
then
990 fatal
(v
, "NOT YET IMPLEMENTED intern {mpropdef}")
991 else if mpropdef
.is_extern
then
992 fatal
(v
, "NOT YET IMPLEMENTED extern {mpropdef}")
994 fatal
(v
, "NOT YET IMPLEMENTED <wat?> {mpropdef}")
1000 redef class AbstractArray[E
]
1001 fun copy
(start
: Int, len
: Int, dest
: AbstractArray[E
], new_start
: Int)
1003 self.copy_to
(start
, len
, dest
, new_start
)
1007 redef class AAttrPropdef
1008 redef fun call
(v
, mpropdef
, args
)
1010 var recv
= args
.first
1011 assert recv
isa MutableInstance
1012 var attr
= self.mpropdef
.mproperty
1013 if mpropdef
== mreadpropdef
then
1014 assert args
.length
== 1
1015 if not is_lazy
or v
.isset_attribute
(attr
, recv
) then return v
.read_attribute
(attr
, recv
)
1016 return evaluate_expr
(v
, recv
)
1017 else if mpropdef
== mwritepropdef
then
1018 assert args
.length
== 2
1019 v
.write_attribute
(attr
, recv
, args
[1])
1026 # Evaluate and set the default value of the attribute in `recv`
1027 private fun init_expr
(v
: NaiveInterpreter, recv
: Instance)
1029 if is_lazy
then return
1030 var nexpr
= self.n_expr
1031 if nexpr
!= null then
1032 evaluate_expr
(v
, recv
)
1035 var mtype
= self.mpropdef
.static_mtype
.as(not null)
1036 mtype
= mtype
.anchor_to
(v
.mainmodule
, recv
.mtype
.as(MClassType))
1037 if mtype
isa MNullableType then
1038 v
.write_attribute
(self.mpropdef
.mproperty
, recv
, v
.null_instance
)
1042 private fun evaluate_expr
(v
: NaiveInterpreter, recv
: Instance): Instance
1044 assert recv
isa MutableInstance
1045 var nexpr
= self.n_expr
1046 assert nexpr
!= null
1047 var f
= new Frame(self, self.mpropdef
.as(not null), [recv
])
1049 var val
= v
.expr
(nexpr
)
1052 assert not v
.is_escaping
1053 v
.write_attribute
(self.mpropdef
.mproperty
, recv
, val
)
1058 redef class AClassdef
1059 # Execute an implicit `mpropdef` associated with the current node.
1060 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
1062 if mpropdef
.mproperty
.is_root_init
then
1063 assert self.super_inits
== null
1064 assert args
.length
== 1
1065 if not mpropdef
.is_intro
then
1066 # standard call-next-method
1067 var superpd
= mpropdef
.lookup_next_definition
(v
.mainmodule
, args
.first
.mtype
)
1068 v
.call_without_varargs
(superpd
, args
)
1073 var super_inits
= self.super_inits
1074 if super_inits
!= null then
1075 var args_of_super
= args
1076 if args
.length
> 1 then args_of_super
= [args
.first
]
1077 for su
in super_inits
do
1078 v
.send
(su
, args_of_super
)
1081 var recv
= args
.first
1082 assert recv
isa MutableInstance
1084 # Collect undefined attributes
1085 for npropdef
in self.n_propdefs
do
1086 if npropdef
isa AAttrPropdef and not npropdef
.noinit
and npropdef
.n_expr
== null then
1087 v
.write_attribute
(npropdef
.mpropdef
.mproperty
, recv
, args
[i
])
1096 # Evaluate the node as a possible expression.
1097 # Return a possible value
1098 # NOTE: Do not call this method directly, but use `v.expr`
1099 # This method is here to be implemented by subclasses.
1100 private fun expr
(v
: NaiveInterpreter): nullable Instance
1102 fatal
(v
, "NOT YET IMPLEMENTED expr {class_name}")
1106 # Evaluate the node as a statement.
1107 # NOTE: Do not call this method directly, but use `v.stmt`
1108 # This method is here to be implemented by subclasses (no need to return something).
1109 private fun stmt
(v
: NaiveInterpreter)
1116 redef class ABlockExpr
1119 var last
= self.n_expr
.last
1120 for e
in self.n_expr
do
1121 if e
== last
then break
1123 if v
.is_escaping
then return null
1130 for e
in self.n_expr
do
1132 if v
.is_escaping
then return
1137 redef class AVardeclExpr
1140 var ne
= self.n_expr
1143 if i
== null then return
1144 v
.frame
.map
[self.variable
.as(not null)] = i
1149 redef class AVarExpr
1152 return v
.frame
.map
[self.variable
.as(not null)]
1156 redef class AVarAssignExpr
1159 var i
= v
.expr
(self.n_value
)
1160 if i
== null then return null
1161 v
.frame
.map
[self.variable
.as(not null)] = i
1166 redef class AVarReassignExpr
1169 var vari
= v
.frame
.map
[self.variable
.as(not null)]
1170 var value
= v
.expr
(self.n_value
)
1171 if value
== null then return
1172 var res
= v
.callsite
(reassign_callsite
, [vari
, value
])
1174 v
.frame
.map
[self.variable
.as(not null)] = res
1178 redef class ASelfExpr
1181 return v
.frame
.arguments
.first
1185 redef class AContinueExpr
1188 var ne
= self.n_expr
1191 if i
== null then return
1194 v
.continuemark
= self.escapemark
1198 redef class ABreakExpr
1201 var ne
= self.n_expr
1204 if i
== null then return
1207 v
.breakmark
= self.escapemark
1211 redef class AReturnExpr
1214 var ne
= self.n_expr
1217 if i
== null then return
1220 v
.returnmark
= v
.frame
1224 redef class AAbortExpr
1235 var cond
= v
.expr
(self.n_expr
)
1236 if cond
== null then return null
1237 if cond
.is_true
then
1238 return v
.expr
(self.n_then
.as(not null))
1240 return v
.expr
(self.n_else
.as(not null))
1246 var cond
= v
.expr
(self.n_expr
)
1247 if cond
== null then return
1248 if cond
.is_true
then
1256 redef class AIfexprExpr
1259 var cond
= v
.expr
(self.n_expr
)
1260 if cond
== null then return null
1261 if cond
.is_true
then
1262 return v
.expr
(self.n_then
)
1264 return v
.expr
(self.n_else
)
1272 v
.stmt
(self.n_block
)
1273 v
.is_break
(self.escapemark
) # Clear the break (if any)
1277 redef class AWhileExpr
1281 var cond
= v
.expr
(self.n_expr
)
1282 if cond
== null then return
1283 if not cond
.is_true
then return
1284 v
.stmt
(self.n_block
)
1285 if v
.is_break
(self.escapemark
) then return
1286 v
.is_continue
(self.escapemark
) # Clear the break
1287 if v
.is_escaping
then return
1292 redef class ALoopExpr
1296 v
.stmt
(self.n_block
)
1297 if v
.is_break
(self.escapemark
) then return
1298 v
.is_continue
(self.escapemark
) # Clear the break
1299 if v
.is_escaping
then return
1304 redef class AForExpr
1307 var col
= v
.expr
(self.n_expr
)
1308 if col
== null then return
1309 if col
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1311 #self.debug("col {col}")
1312 var iter
= v
.callsite
(method_iterator
, [col
]).as(not null)
1313 #self.debug("iter {iter}")
1315 var isok
= v
.callsite
(method_is_ok
, [iter
]).as(not null)
1316 if not isok
.is_true
then return
1317 if self.variables
.length
== 1 then
1318 var item
= v
.callsite
(method_item
, [iter
]).as(not null)
1319 #self.debug("item {item}")
1320 v
.frame
.map
[self.variables
.first
] = item
1321 else if self.variables
.length
== 2 then
1322 var key
= v
.callsite
(method_key
, [iter
]).as(not null)
1323 v
.frame
.map
[self.variables
[0]] = key
1324 var item
= v
.callsite
(method_item
, [iter
]).as(not null)
1325 v
.frame
.map
[self.variables
[1]] = item
1329 v
.stmt
(self.n_block
)
1330 if v
.is_break
(self.escapemark
) then return
1331 v
.is_continue
(self.escapemark
) # Clear the break
1332 if v
.is_escaping
then return
1333 v
.callsite
(method_next
, [iter
])
1338 redef class AAssertExpr
1341 var cond
= v
.expr
(self.n_expr
)
1342 if cond
== null then return
1343 if not cond
.is_true
then
1345 if v
.is_escaping
then return
1348 fatal
(v
, "Assert '{nid.text}' failed")
1350 fatal
(v
, "Assert failed")
1360 var cond
= v
.expr
(self.n_expr
)
1361 if cond
== null then return null
1362 if cond
.is_true
then return cond
1363 return v
.expr
(self.n_expr2
)
1367 redef class AImpliesExpr
1370 var cond
= v
.expr
(self.n_expr
)
1371 if cond
== null then return null
1372 if not cond
.is_true
then return v
.true_instance
1373 return v
.expr
(self.n_expr2
)
1377 redef class AAndExpr
1380 var cond
= v
.expr
(self.n_expr
)
1381 if cond
== null then return null
1382 if not cond
.is_true
then return cond
1383 return v
.expr
(self.n_expr2
)
1387 redef class ANotExpr
1390 var cond
= v
.expr
(self.n_expr
)
1391 if cond
== null then return null
1392 return v
.bool_instance
(not cond
.is_true
)
1396 redef class AOrElseExpr
1399 var i
= v
.expr
(self.n_expr
)
1400 if i
== null then return null
1401 if i
!= v
.null_instance
then return i
1402 return v
.expr
(self.n_expr2
)
1406 redef class AIntExpr
1409 return v
.int_instance
(self.value
.as(not null))
1413 redef class AFloatExpr
1416 return v
.float_instance
(self.value
.as(not null))
1420 redef class ACharExpr
1423 return v
.char_instance
(self.value
.as(not null))
1427 redef class AArrayExpr
1430 var val
= new Array[Instance]
1431 for nexpr
in self.n_exprs
.n_exprs
do
1432 var i
= v
.expr
(nexpr
)
1433 if i
== null then return null
1436 var mtype
= v
.unanchor_type
(self.mtype
.as(not null)).as(MClassType)
1437 var elttype
= mtype
.arguments
.first
1438 return v
.array_instance
(val
, elttype
)
1442 redef class AStringFormExpr
1445 var txt
= self.value
.as(not null)
1446 var nat
= v
.native_string_instance
(txt
)
1447 var res
= v
.send
(v
.force_get_primitive_method
("to_s", nat
.mtype
), [nat
]).as(not null)
1452 redef class ASuperstringExpr
1455 var array
= new Array[Instance]
1456 for nexpr
in n_exprs
do
1457 var i
= v
.expr
(nexpr
)
1458 if i
== null then return null
1461 var i
= v
.array_instance
(array
, v
.mainmodule
.get_primitive_class
("Object").mclass_type
)
1462 var res
= v
.send
(v
.force_get_primitive_method
("to_s", i
.mtype
), [i
])
1468 redef class ACrangeExpr
1471 var e1
= v
.expr
(self.n_expr
)
1472 if e1
== null then return null
1473 var e2
= v
.expr
(self.n_expr2
)
1474 if e2
== null then return null
1475 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1476 var res
= new MutableInstance(mtype
)
1477 v
.init_instance
(res
)
1478 v
.callsite
(init_callsite
, [res
, e1
, e2
])
1483 redef class AOrangeExpr
1486 var e1
= v
.expr
(self.n_expr
)
1487 if e1
== null then return null
1488 var e2
= v
.expr
(self.n_expr2
)
1489 if e2
== null then return null
1490 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1491 var res
= new MutableInstance(mtype
)
1492 v
.init_instance
(res
)
1493 v
.callsite
(init_callsite
, [res
, e1
, e2
])
1498 redef class ATrueExpr
1501 return v
.bool_instance
(true)
1505 redef class AFalseExpr
1508 return v
.bool_instance
(false)
1512 redef class ANullExpr
1515 return v
.null_instance
1519 redef class AIsaExpr
1522 var i
= v
.expr
(self.n_expr
)
1523 if i
== null then return null
1524 var mtype
= v
.unanchor_type
(self.cast_type
.as(not null))
1525 return v
.bool_instance
(v
.is_subtype
(i
.mtype
, mtype
))
1529 redef class AAsCastExpr
1532 var i
= v
.expr
(self.n_expr
)
1533 if i
== null then return null
1534 var mtype
= self.mtype
.as(not null)
1535 var amtype
= v
.unanchor_type
(mtype
)
1536 if not v
.is_subtype
(i
.mtype
, amtype
) then
1537 fatal
(v
, "Cast failed. Expected `{amtype}`, got `{i.mtype}`")
1543 redef class AAsNotnullExpr
1546 var i
= v
.expr
(self.n_expr
)
1547 if i
== null then return null
1548 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1549 if i
.mtype
isa MNullType then
1550 fatal
(v
, "Cast failed")
1556 redef class AParExpr
1559 return v
.expr
(self.n_expr
)
1563 redef class AOnceExpr
1566 if v
.onces
.has_key
(self) then
1567 return v
.onces
[self]
1569 var res
= v
.expr
(self.n_expr
)
1570 if res
== null then return null
1577 redef class ASendExpr
1580 var recv
= v
.expr
(self.n_expr
)
1581 if recv
== null then return null
1583 for a
in self.raw_arguments
do
1585 if i
== null then return null
1589 var res
= v
.callsite
(callsite
, args
)
1594 redef class ASendReassignFormExpr
1597 var recv
= v
.expr
(self.n_expr
)
1598 if recv
== null then return
1600 for a
in self.raw_arguments
do
1602 if i
== null then return
1605 var value
= v
.expr
(self.n_value
)
1606 if value
== null then return
1608 var read
= v
.callsite
(callsite
, args
)
1611 var write
= v
.callsite
(reassign_callsite
, [read
, value
])
1612 assert write
!= null
1616 v
.callsite
(write_callsite
, args
)
1620 redef class ASuperExpr
1623 var recv
= v
.frame
.arguments
.first
1625 for a
in self.n_args
.n_exprs
do
1627 if i
== null then return null
1631 var callsite
= self.callsite
1632 if callsite
!= null then
1633 # Add additionnals arguments for the super init call
1634 if args
.length
== 1 then
1635 for i
in [0..callsite
.msignature
.arity
[ do
1636 args
.add
(v
.frame
.arguments
[i
+1])
1640 var res
= v
.callsite
(callsite
, args
)
1644 if args
.length
== 1 then
1645 args
= v
.frame
.arguments
1648 # stantard call-next-method
1649 var mpropdef
= self.mpropdef
1650 mpropdef
= mpropdef
.lookup_next_definition
(v
.mainmodule
, recv
.mtype
)
1651 var res
= v
.call_without_varargs
(mpropdef
, args
)
1656 redef class ANewExpr
1659 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1660 var recv
: Instance = new MutableInstance(mtype
)
1661 v
.init_instance
(recv
)
1663 for a
in self.n_args
.n_exprs
do
1665 if i
== null then return null
1668 var res2
= v
.callsite
(callsite
, args
)
1669 if res2
!= null then
1670 #self.debug("got {res2} from {mproperty}. drop {recv}")
1677 redef class AAttrExpr
1680 var recv
= v
.expr
(self.n_expr
)
1681 if recv
== null then return null
1682 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1683 var mproperty
= self.mproperty
.as(not null)
1684 return v
.read_attribute
(mproperty
, recv
)
1688 redef class AAttrAssignExpr
1691 var recv
= v
.expr
(self.n_expr
)
1692 if recv
== null then return
1693 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1694 var i
= v
.expr
(self.n_value
)
1695 if i
== null then return
1696 var mproperty
= self.mproperty
.as(not null)
1697 v
.write_attribute
(mproperty
, recv
, i
)
1701 redef class AAttrReassignExpr
1704 var recv
= v
.expr
(self.n_expr
)
1705 if recv
== null then return
1706 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1707 var value
= v
.expr
(self.n_value
)
1708 if value
== null then return
1709 var mproperty
= self.mproperty
.as(not null)
1710 var attr
= v
.read_attribute
(mproperty
, recv
)
1711 var res
= v
.callsite
(reassign_callsite
, [attr
, value
])
1713 v
.write_attribute
(mproperty
, recv
, res
)
1717 redef class AIssetAttrExpr
1720 var recv
= v
.expr
(self.n_expr
)
1721 if recv
== null then return null
1722 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1723 var mproperty
= self.mproperty
.as(not null)
1724 return v
.bool_instance
(v
.isset_attribute
(mproperty
, recv
))
1728 redef class ADebugTypeExpr