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
26 redef class ToolContext
27 # --discover-call-trace
28 var opt_discover_call_trace
: OptionBool = new OptionBool("Trace calls of the first invocation of a method", "--discover-call-trace")
33 self.option_context
.add_option
(self.opt_discover_call_trace
)
37 redef class ModelBuilder
38 # Execute the program from the entry point (`Sys::main`) of the `mainmodule`
39 # `arguments` are the command-line arguments in order
41 # 1. the AST is fully loaded.
42 # 2. the model is fully built.
43 # 3. the instructions are fully analysed.
44 fun run_naive_interpreter
(mainmodule
: MModule, arguments
: Array[String])
47 self.toolcontext
.info
("*** START INTERPRETING ***", 1)
49 var interpreter
= new NaiveInterpreter(self, mainmodule
, arguments
)
50 init_naive_interpreter
(interpreter
, mainmodule
)
53 self.toolcontext
.info
("*** END INTERPRETING: {time1-time0} ***", 2)
56 private fun init_naive_interpreter
(interpreter
: NaiveInterpreter, mainmodule
: MModule) do
57 var sys_type
= mainmodule
.sys_type
58 if sys_type
== null then return # no class Sys
59 var mainobj
= new MutableInstance(sys_type
)
60 interpreter
.mainobj
= mainobj
61 interpreter
.init_instance
(mainobj
)
62 var initprop
= mainmodule
.try_get_primitive_method
("init", sys_type
.mclass
)
63 if initprop
!= null then
64 interpreter
.send
(initprop
, [mainobj
])
66 var mainprop
= mainmodule
.try_get_primitive_method
("main", sys_type
.mclass
)
67 if mainprop
!= null then
68 interpreter
.send
(mainprop
, [mainobj
])
73 # The visitor that interprets the Nit Program by walking on the AST
74 private class NaiveInterpreter
75 # The modelbuilder that know the AST and its associations with the model
76 var modelbuilder
: ModelBuilder
78 # The main moduleof the program (used to lookup methoda
79 var mainmodule
: MModule
81 # The command line arguments of the interpreted program
82 # arguments.first is the program name
83 # arguments[1] is the first argument
84 var arguments
: Array[String]
86 var mainobj
: nullable Instance
88 init(modelbuilder
: ModelBuilder, mainmodule
: MModule, arguments
: Array[String])
90 self.modelbuilder
= modelbuilder
91 self.mainmodule
= mainmodule
92 self.arguments
= arguments
93 self.true_instance
= new PrimitiveInstance[Bool](mainmodule
.bool_type
, true)
94 self.false_instance
= new PrimitiveInstance[Bool](mainmodule
.bool_type
, false)
95 self.null_instance
= new MutableInstance(mainmodule
.model
.null_type
)
98 # Subtype test in the context of the mainmodule
99 fun is_subtype
(sub
, sup
: MType): Bool
101 return sub
.is_subtype
(self.mainmodule
, self.frame
.arguments
.first
.mtype
.as(MClassType), sup
)
104 fun force_get_primitive_method
(name
: String, recv
: MType): MMethod
106 assert recv
isa MClassType
107 return self.modelbuilder
.force_get_primitive_method
(self.frame
.current_node
, name
, recv
.mclass
, self.mainmodule
)
110 # Is a return executed?
111 # Set this mark to skip the evaluation until the end of the specified method frame
112 var returnmark
: nullable Frame = null
114 # Is a break executed?
115 # Set this mark to skip the evaluation until a labeled statement catch it with `is_break`
116 var breakmark
: nullable EscapeMark = null
118 # Is a continue executed?
119 # Set this mark to skip the evaluation until a labeled statement catch it with `is_continue`
120 var continuemark
: nullable EscapeMark = null
122 # Is a return or a break or a continue executed?
123 # Use this function to know if you must skip the evaluation of statements
124 fun is_escaping
: Bool do return returnmark
!= null or breakmark
!= null or continuemark
!= null
126 # The value associated with the current return/break/continue, if any.
127 # Set the value when you set a escapemark.
128 # Read the value when you catch a mark or reach the end of a method
129 var escapevalue
: nullable Instance = null
131 # If there is a break and is associated with `escapemark`, then return true an clear the mark.
132 # If there is no break or if `escapemark` is null then return false.
133 # Use this function to catch a potential break.
134 fun is_break
(escapemark
: nullable EscapeMark): Bool
136 if escapemark
!= null and self.breakmark
== escapemark
then
137 self.breakmark
= null
144 # If there is a continue and is associated with `escapemark`, then return true an clear the mark.
145 # If there is no continue or if `escapemark` is null then return false.
146 # Use this function to catch a potential continue.
147 fun is_continue
(escapemark
: nullable EscapeMark): Bool
149 if escapemark
!= null and self.continuemark
== escapemark
then
150 self.continuemark
= null
157 # Evaluate `n` as an expression in the current context.
158 # Return the value of the expression.
159 # If `n` cannot be evaluated, then aborts.
160 fun expr
(n
: AExpr): nullable Instance
162 var frame
= self.frame
163 var old
= frame
.current_node
164 frame
.current_node
= n
165 #n.debug("IN Execute expr")
167 if i
== null and not self.is_escaping
then
168 n
.debug
("inconsitance: no value and not escaping.")
170 var implicit_cast_to
= n
.implicit_cast_to
171 if implicit_cast_to
!= null then
172 var mtype
= self.unanchor_type
(implicit_cast_to
)
173 if not self.is_subtype
(i
.mtype
, mtype
) then n
.fatal
(self, "Cast failed. Expected `{implicit_cast_to}`, got `{i.mtype}`")
176 #n.debug("OUT Execute expr: value is {i}")
177 #if not is_subtype(i.mtype, n.mtype.as(not null)) then n.debug("Expected {n.mtype.as(not null)} got {i}")
178 frame
.current_node
= old
182 # Evaluate `n` as a statement in the current context.
183 # Do nothing if `n` is null.
184 # If `n` cannot be evaluated, then aborts.
185 fun stmt
(n
: nullable AExpr)
188 var frame
= self.frame
189 var old
= frame
.current_node
190 frame
.current_node
= n
191 #n.debug("Execute stmt")
193 frame
.current_node
= old
197 # Map used to store values of nodes that must be evaluated once in the system (`AOnceExpr`)
198 var onces
: Map[ANode, Instance] = new HashMap[ANode, Instance]
200 # Return the boolean instance associated with `val`.
201 fun bool_instance
(val
: Bool): Instance
203 if val
then return self.true_instance
else return self.false_instance
206 # Return the integer instance associated with `val`.
207 fun int_instance
(val
: Int): Instance
209 var ic
= self.mainmodule
.get_primitive_class
("Int")
210 return new PrimitiveInstance[Int](ic
.mclass_type
, val
)
213 # Return the char instance associated with `val`.
214 fun char_instance
(val
: Char): Instance
216 var ic
= self.mainmodule
.get_primitive_class
("Char")
217 return new PrimitiveInstance[Char](ic
.mclass_type
, val
)
220 # Return the float instance associated with `val`.
221 fun float_instance
(val
: Float): Instance
223 var ic
= self.mainmodule
.get_primitive_class
("Float")
224 return new PrimitiveInstance[Float](ic
.mclass_type
, val
)
227 # The unique intance of the `true` value.
228 var true_instance
: Instance
230 # The unique intance of the `false` value.
231 var false_instance
: Instance
233 # The unique intance of the `null` value.
234 var null_instance
: Instance
236 # Return a new array made of `values`.
237 # The dynamic type of the result is Array[elttype].
238 fun array_instance
(values
: Array[Instance], elttype
: MType): Instance
240 assert not elttype
.need_anchor
241 var nat
= new PrimitiveInstance[Array[Instance]](self.mainmodule
.get_primitive_class
("NativeArray").get_mtype
([elttype
]), values
)
242 var mtype
= self.mainmodule
.get_primitive_class
("Array").get_mtype
([elttype
])
243 var res
= new MutableInstance(mtype
)
244 self.init_instance
(res
)
245 self.send
(self.force_get_primitive_method
("with_native", mtype
), [res
, nat
, self.int_instance
(values
.length
)])
249 # Return a new native string initialized with `txt`
250 fun native_string_instance
(txt
: String): Instance
252 var val
= new FlatBuffer.from
(txt
)
254 var ic
= self.mainmodule
.get_primitive_class
("NativeString")
255 return new PrimitiveInstance[Buffer](ic
.mclass_type
, val
)
258 # The current frame used to store local variables of the current method executed
259 fun frame
: Frame do return frames
.first
261 # The stack of all frames. The first one is the current one.
262 var frames
: List[Frame] = new List[Frame]
264 # Return a stack stace. One line per function
265 fun stack_trace
: String
267 var b
= new FlatBuffer
268 b
.append
(",---- Stack trace -- - - -\n")
270 b
.append
("| {f.mpropdef} ({f.current_node.location})\n")
272 b
.append
("`------------------- - - -")
276 # Exit the program with a message
277 fun fatal
(message
: String)
279 if frames
.is_empty
then
282 self.frame
.current_node
.fatal
(self, message
)
287 # Debug on the current node
288 fun debug
(message
: String)
290 if frames
.is_empty
then
293 self.frame
.current_node
.debug
(message
)
297 # Store known method, used to trace methods as thez are reached
298 var discover_call_trace
: Set[MMethodDef] = new HashSet[MMethodDef]
300 # Common code for calls to injected methods and normal methods
301 fun call_commons
(mpropdef
: MMethodDef, args
: Array[Instance]): Array[Instance]
303 var vararg_rank
= mpropdef
.msignature
.vararg_rank
304 if vararg_rank
>= 0 then
305 assert args
.length
>= mpropdef
.msignature
.arity
+ 1 # because of self
307 args
= new Array[Instance]
309 args
.add
(rawargs
.first
) # recv
311 for i
in [0..vararg_rank
[ do
312 args
.add
(rawargs
[i
+1])
315 var vararg_lastrank
= vararg_rank
+ rawargs
.length-1-mpropdef
.msignature
.arity
316 var vararg
= new Array[Instance]
317 for i
in [vararg_rank
..vararg_lastrank
] do
318 vararg
.add
(rawargs
[i
+1])
320 # FIXME: its it to late to determine the vararg type, this should have been done during a previous analysis
321 var elttype
= mpropdef
.msignature
.mparameters
[vararg_rank
].mtype
.anchor_to
(self.mainmodule
, args
.first
.mtype
.as(MClassType))
322 args
.add
(self.array_instance
(vararg
, elttype
))
324 for i
in [vararg_lastrank
+1..rawargs
.length-1
[ do
325 args
.add
(rawargs
[i
+1])
331 # Execute `mpropdef` for a `args` (where `args[0]` is the receiver).
332 # Return a falue if `mpropdef` is a function, or null if it is a procedure.
333 # The call is direct/static. There is no message-seding/late-binding.
334 fun call
(mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
336 args
= call_commons
(mpropdef
, args
)
337 return call_without_varargs
(mpropdef
, args
)
340 # Common code to call and this function
342 # Call only executes the variadic part, this avoids
343 # double encapsulation of variadic parameters into an Array
344 fun call_without_varargs
(mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
346 if self.modelbuilder
.toolcontext
.opt_discover_call_trace
.value
and not self.discover_call_trace
.has
(mpropdef
) then
347 self.discover_call_trace
.add mpropdef
348 self.debug
("Discovered {mpropdef}")
350 assert args
.length
== mpropdef
.msignature
.arity
+ 1 else debug
("Invalid arity for {mpropdef}. {args.length} arguments given.")
352 # Look for the AST node that implements the property
353 var mproperty
= mpropdef
.mproperty
354 if self.modelbuilder
.mpropdef2npropdef
.has_key
(mpropdef
) then
355 var npropdef
= self.modelbuilder
.mpropdef2npropdef
[mpropdef
]
356 self.parameter_check
(npropdef
, mpropdef
, args
)
357 return npropdef
.call
(self, mpropdef
, args
)
358 else if mproperty
.name
== "init" then
359 var nclassdef
= self.modelbuilder
.mclassdef2nclassdef
[mpropdef
.mclassdef
]
360 self.parameter_check
(nclassdef
, mpropdef
, args
)
361 return nclassdef
.call
(self, mpropdef
, args
)
363 fatal
("Fatal Error: method {mpropdef} not found in the AST")
368 # Generate type checks in the C code to check covariant parameters
369 fun parameter_check
(node
: ANode, mpropdef
: MMethodDef, args
: Array[Instance])
371 var msignature
= mpropdef
.msignature
372 for i
in [0..msignature
.arity
[ do
373 # skip test for vararg since the array is instantiated with the correct polymorphic type
374 if msignature
.vararg_rank
== i
then continue
376 # skip if the cast is not required
377 var origmtype
= mpropdef
.mproperty
.intro
.msignature
.mparameters
[i
].mtype
378 if not origmtype
.need_anchor
then continue
380 # get the parameter type
381 var mtype
= msignature
.mparameters
[i
].mtype
382 var anchor
= args
.first
.mtype
.as(MClassType)
383 var amtype
= mtype
.anchor_to
(self.mainmodule
, anchor
)
384 if not args
[i
+1].mtype
.is_subtype
(self.mainmodule
, anchor
, amtype
) then
385 node
.fatal
(self, "Cast failed. Expected `{mtype}`, got `{args[i+1].mtype}`")
390 # Common code for runtime injected calls and normal calls
391 fun send_commons
(mproperty
: MMethod, args
: Array[Instance], mtype
: MType): nullable Instance
393 if mtype
isa MNullType then
394 if mproperty
.name
== "==" then
395 return self.bool_instance
(args
[0] == args
[1])
396 else if mproperty
.name
== "!=" then
397 return self.bool_instance
(args
[0] != args
[1])
399 #fatal("Receiver is null. {mproperty}. {args.join(" ")} {self.frame.current_node.class_name}")
400 fatal
("Receiver is null")
405 # Execute a full `callsite` for given `args`
406 # Use this method, instead of `send` to execute and control the aditionnal behavior of the call-sites
407 fun callsite
(callsite
: nullable CallSite, arguments
: Array[Instance]): nullable Instance
409 return send
(callsite
.mproperty
, arguments
)
412 # Execute `mproperty` for a `args` (where `args[0]` is the receiver).
413 # Return a falue if `mproperty` is a function, or null if it is a procedure.
414 # The call is polimotphic. There is a message-seding/late-bindng according to te receiver (args[0]).
415 fun send
(mproperty
: MMethod, args
: Array[Instance]): nullable Instance
417 var recv
= args
.first
418 var mtype
= recv
.mtype
419 var ret
= send_commons
(mproperty
, args
, mtype
)
420 if ret
!= null then return ret
421 var propdef
= mproperty
.lookup_first_definition
(self.mainmodule
, mtype
)
422 return self.call
(propdef
, args
)
425 # Read the attribute `mproperty` of an instance `recv` and return its value.
426 # If the attribute in not yet initialized, then aborts with an error message.
427 fun read_attribute
(mproperty
: MAttribute, recv
: Instance): Instance
429 assert recv
isa MutableInstance
430 if not recv
.attributes
.has_key
(mproperty
) then
431 fatal
("Uninitialized attribute {mproperty.name}")
434 return recv
.attributes
[mproperty
]
437 # Replace in `recv` the value of the attribute `mproperty` by `value`
438 fun write_attribute
(mproperty
: MAttribute, recv
: Instance, value
: Instance)
440 assert recv
isa MutableInstance
441 recv
.attributes
[mproperty
] = value
444 # Is the attribute `mproperty` initialized the instance `recv`?
445 fun isset_attribute
(mproperty
: MAttribute, recv
: Instance): Bool
447 assert recv
isa MutableInstance
448 return recv
.attributes
.has_key
(mproperty
)
451 # Collect attributes of a type in the order of their init
452 fun collect_attr_propdef
(mtype
: MType): Array[AAttrPropdef]
454 var cache
= self.collect_attr_propdef_cache
455 if cache
.has_key
(mtype
) then return cache
[mtype
]
457 var res
= new Array[AAttrPropdef]
458 var cds
= mtype
.collect_mclassdefs
(self.mainmodule
).to_a
459 self.mainmodule
.linearize_mclassdefs
(cds
)
461 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
462 for npropdef
in n
.n_propdefs
do
463 if npropdef
isa AAttrPropdef then
473 var collect_attr_propdef_cache
= new HashMap[MType, Array[AAttrPropdef]]
475 # Fill the initial values of the newly created instance `recv`.
476 # `recv.mtype` is used to know what must be filled.
477 fun init_instance
(recv
: Instance)
479 for npropdef
in collect_attr_propdef
(recv
.mtype
) do
480 npropdef
.init_expr
(self, recv
)
484 # This function determine the correct type according the reciever of the current definition (self).
485 fun unanchor_type
(mtype
: MType): MType
487 return mtype
.anchor_to
(self.mainmodule
, self.frame
.arguments
.first
.mtype
.as(MClassType))
491 # An instance represents a value of the executed program.
492 abstract class Instance
493 # The dynamic type of the instance
494 # ASSERT: not self.mtype.is_anchored
497 # return true if the instance is the true value.
498 # return false if the instance is the true value.
500 fun is_true
: Bool do abort
502 # Return true if `self` IS `o` (using the Nit semantic of is)
503 fun eq_is
(o
: Instance): Bool do return self.is_same_instance
(o
)
505 # Human readable object identity "Type#number"
506 redef fun to_s
do return "{mtype}"
508 # Return the integer value if the instance is an integer.
510 fun to_i
: Int do abort
512 # Return the integer value if the instance is a float.
514 fun to_f
: Float do abort
516 # The real value encapsulated if the instance is primitive.
518 fun val
: Object do abort
521 # A instance with attribute (standards objects)
522 class MutableInstance
525 # The values of the attributes
526 var attributes
: Map[MAttribute, Instance] = new HashMap[MAttribute, Instance]
529 # Special instance to handle primitives values (int, bool, etc.)
530 # The trick it just to encapsulate the <<real>> value
531 class PrimitiveInstance[E
: Object]
534 # The real value encapsulated
537 init(mtype
: MType, val
: E
)
545 if val
== true then return true
546 if val
== false then return false
552 if not o
isa PrimitiveInstance[Object] then return false
553 return self.val
== o
.val
558 if not o
isa PrimitiveInstance[Object] then return false
559 return self.val
.is_same_instance
(o
.val
)
562 redef fun to_s
do return "{mtype}#{val.object_id}({val})"
564 redef fun to_i
do return val
.as(Int)
566 redef fun to_f
do return val
.as(Float)
569 # Information about local variables in a running method
571 # The current visited node
572 # The node is stored by frame to keep a stack trace
573 var current_node
: ANode
574 # The executed property.
575 # A Method in case of a call, an attribute in case of a default initialization.
576 var mpropdef
: MPropDef
577 # Arguments of the method (the first is te receiver
578 var arguments
: Array[Instance]
579 # Mapping betwen a variable an the current value
580 var map
: Map[Variable, Instance] = new HashMap[Variable, Instance]
584 # Aborts the program with a message
585 # `v` is used to know if a colored message is displayed or not
586 private fun fatal
(v
: NaiveInterpreter, message
: String)
588 if v
.modelbuilder
.toolcontext
.opt_no_color
.value
== true then
589 sys
.stderr
.write
("Runtime error: {message} ({location.file.filename}:{location.line_start})\n")
591 sys
.stderr
.write
("{location}: Runtime error: {message}\n{location.colored_line("0;31")}\n")
592 sys
.stderr
.write
(v
.stack_trace
)
593 sys
.stderr
.write
("\n")
600 # Execute a `mpropdef` associated with the current node.
601 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
603 fatal
(v
, "NOT YET IMPLEMENTED method kind {class_name}. {mpropdef}")
608 redef class AMethPropdef
611 redef fun call
(v
, mpropdef
, args
)
613 var f
= new Frame(self, self.mpropdef
.as(not null), args
)
614 var res
= call_commons
(v
, mpropdef
, args
, f
)
616 if v
.returnmark
== f
then
625 private fun call_commons
(v
: NaiveInterpreter, mpropdef
: MMethodDef, arguments
: Array[Instance], f
: Frame): nullable Instance
627 for i
in [0..mpropdef
.msignature
.arity
[ do
628 var variable
= self.n_signature
.n_params
[i
].variable
629 assert variable
!= null
630 f
.map
[variable
] = arguments
[i
+1]
635 if mpropdef
.is_abstract
then
636 v
.fatal
("Abstract method `{mpropdef.mproperty.name}` called on `{arguments.first.mtype}`")
640 # Call the implicit super-init
641 var auto_super_inits
= self.auto_super_inits
642 if auto_super_inits
!= null then
643 var args
= [arguments
.first
]
644 for auto_super_init
in auto_super_inits
do
646 for i
in [0..auto_super_init
.msignature
.arity
+1[ do
647 args
.add
(arguments
[i
])
649 v
.callsite
(auto_super_init
, args
)
653 if n_block
!= null then
657 return intern_call
(v
, mpropdef
, arguments
)
661 private fun intern_call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
663 var pname
= mpropdef
.mproperty
.name
664 var cname
= mpropdef
.mclassdef
.mclass
.name
665 if pname
== "output" then
666 var recv
= args
.first
669 else if pname
== "object_id" then
670 var recv
= args
.first
671 if recv
isa PrimitiveInstance[Object] then
672 return v
.int_instance
(recv
.val
.object_id
)
674 return v
.int_instance
(recv
.object_id
)
676 else if pname
== "output_class_name" then
677 var recv
= args
.first
680 else if pname
== "native_class_name" then
681 var recv
= args
.first
682 var txt
= recv
.mtype
.to_s
683 return v
.native_string_instance
(txt
)
684 else if pname
== "==" then
685 # == is correclt redefined for instances
686 return v
.bool_instance
(args
[0] == args
[1])
687 else if pname
== "!=" then
688 return v
.bool_instance
(args
[0] != args
[1])
689 else if pname
== "is_same_type" then
690 return v
.bool_instance
(args
[0].mtype
== args
[1].mtype
)
691 else if pname
== "is_same_instance" then
692 return v
.bool_instance
(args
[1] != null and args
[0].eq_is
(args
[1]))
693 else if pname
== "exit" then
696 else if pname
== "sys" then
698 else if cname
== "Int" then
699 var recvval
= args
[0].to_i
700 if pname
== "unary -" then
701 return v
.int_instance
(-args
[0].to_i
)
702 else if pname
== "+" then
703 return v
.int_instance
(args
[0].to_i
+ args
[1].to_i
)
704 else if pname
== "-" then
705 return v
.int_instance
(args
[0].to_i
- args
[1].to_i
)
706 else if pname
== "*" then
707 return v
.int_instance
(args
[0].to_i
* args
[1].to_i
)
708 else if pname
== "%" then
709 return v
.int_instance
(args
[0].to_i
% args
[1].to_i
)
710 else if pname
== "/" then
711 return v
.int_instance
(args
[0].to_i
/ args
[1].to_i
)
712 else if pname
== "<" then
713 return v
.bool_instance
(args
[0].to_i
< args
[1].to_i
)
714 else if pname
== ">" then
715 return v
.bool_instance
(args
[0].to_i
> args
[1].to_i
)
716 else if pname
== "<=" then
717 return v
.bool_instance
(args
[0].to_i
<= args
[1].to_i
)
718 else if pname
== ">=" then
719 return v
.bool_instance
(args
[0].to_i
>= args
[1].to_i
)
720 else if pname
== "<=>" then
721 return v
.int_instance
(args
[0].to_i
<=> args
[1].to_i
)
722 else if pname
== "ascii" then
723 return v
.char_instance
(args
[0].to_i
.ascii
)
724 else if pname
== "to_f" then
725 return v
.float_instance
(args
[0].to_i
.to_f
)
726 else if pname
== "lshift" then
727 return v
.int_instance
(args
[0].to_i
.lshift
(args
[1].to_i
))
728 else if pname
== "rshift" then
729 return v
.int_instance
(args
[0].to_i
.rshift
(args
[1].to_i
))
730 else if pname
== "rand" then
731 var res
= recvval
.rand
732 return v
.int_instance
(res
)
733 else if pname
== "bin_and" then
734 return v
.int_instance
(args
[0].to_i
.bin_and
(args
[1].to_i
))
735 else if pname
== "bin_or" then
736 return v
.int_instance
(args
[0].to_i
.bin_or
(args
[1].to_i
))
737 else if pname
== "bin_xor" then
738 return v
.int_instance
(args
[0].to_i
.bin_xor
(args
[1].to_i
))
739 else if pname
== "native_int_to_s" then
740 return v
.native_string_instance
(recvval
.to_s
)
741 else if pname
== "strerror_ext" then
742 return v
.native_string_instance
(recvval
.strerror
)
744 else if cname
== "Char" then
745 var recv
= args
[0].val
.as(Char)
746 if pname
== "ascii" then
747 return v
.int_instance
(recv
.ascii
)
748 else if pname
== "successor" then
749 return v
.char_instance
(recv
.successor
(args
[1].to_i
))
750 else if pname
== "predecessor" then
751 return v
.char_instance
(recv
.predecessor
(args
[1].to_i
))
752 else if pname
== "<" then
753 return v
.bool_instance
(recv
< args
[1].val
.as(Char))
754 else if pname
== ">" then
755 return v
.bool_instance
(recv
> args
[1].val
.as(Char))
756 else if pname
== "<=" then
757 return v
.bool_instance
(recv
<= args
[1].val
.as(Char))
758 else if pname
== ">=" then
759 return v
.bool_instance
(recv
>= args
[1].val
.as(Char))
760 else if pname
== "<=>" then
761 return v
.int_instance
(recv
<=> args
[1].val
.as(Char))
763 else if cname
== "Float" then
764 var recv
= args
[0].to_f
765 if pname
== "unary -" then
766 return v
.float_instance
(-recv
)
767 else if pname
== "+" then
768 return v
.float_instance
(recv
+ args
[1].to_f
)
769 else if pname
== "-" then
770 return v
.float_instance
(recv
- args
[1].to_f
)
771 else if pname
== "*" then
772 return v
.float_instance
(recv
* args
[1].to_f
)
773 else if pname
== "/" then
774 return v
.float_instance
(recv
/ args
[1].to_f
)
775 else if pname
== "<" then
776 return v
.bool_instance
(recv
< args
[1].to_f
)
777 else if pname
== ">" then
778 return v
.bool_instance
(recv
> args
[1].to_f
)
779 else if pname
== "<=" then
780 return v
.bool_instance
(recv
<= args
[1].to_f
)
781 else if pname
== ">=" then
782 return v
.bool_instance
(recv
>= args
[1].to_f
)
783 else if pname
== "to_i" then
784 return v
.int_instance
(recv
.to_i
)
785 else if pname
== "cos" then
786 return v
.float_instance
(args
[0].to_f
.cos
)
787 else if pname
== "sin" then
788 return v
.float_instance
(args
[0].to_f
.sin
)
789 else if pname
== "tan" then
790 return v
.float_instance
(args
[0].to_f
.tan
)
791 else if pname
== "acos" then
792 return v
.float_instance
(args
[0].to_f
.acos
)
793 else if pname
== "asin" then
794 return v
.float_instance
(args
[0].to_f
.asin
)
795 else if pname
== "atan" then
796 return v
.float_instance
(args
[0].to_f
.atan
)
797 else if pname
== "sqrt" then
798 return v
.float_instance
(args
[0].to_f
.sqrt
)
799 else if pname
== "exp" then
800 return v
.float_instance
(args
[0].to_f
.exp
)
801 else if pname
== "log" then
802 return v
.float_instance
(args
[0].to_f
.log
)
803 else if pname
== "pow" then
804 return v
.float_instance
(args
[0].to_f
.pow
(args
[1].to_f
))
805 else if pname
== "rand" then
806 return v
.float_instance
(args
[0].to_f
.rand
)
807 else if pname
== "abs" then
808 return v
.float_instance
(args
[0].to_f
.abs
)
809 else if pname
== "hypot_with" then
810 return v
.float_instance
(args
[0].to_f
.hypot_with
(args
[1].to_f
))
811 else if pname
== "is_nan" then
812 return v
.bool_instance
(args
[0].to_f
.is_nan
)
813 else if pname
== "is_inf_extern" then
814 return v
.bool_instance
(args
[0].to_f
.is_inf
!= 0)
816 else if cname
== "NativeString" then
817 if pname
== "init" then
818 return v
.native_string_instance
("!" * args
[1].to_i
)
820 var recvval
= args
.first
.val
.as(Buffer)
821 if pname
== "[]" then
822 var arg1
= args
[1].to_i
823 if arg1
>= recvval
.length
or arg1
< 0 then
824 debug
("Illegal access on {recvval} for element {arg1}/{recvval.length}")
826 return v
.char_instance
(recvval
.chars
[arg1
])
827 else if pname
== "[]=" then
828 var arg1
= args
[1].to_i
829 if arg1
>= recvval
.length
or arg1
< 0 then
830 debug
("Illegal access on {recvval} for element {arg1}/{recvval.length}")
832 recvval
.chars
[arg1
] = args
[2].val
.as(Char)
834 else if pname
== "copy_to" then
835 # sig= copy_to(dest: NativeString, length: Int, from: Int, to: Int)
836 var destval
= args
[1].val
.as(FlatBuffer)
837 var lenval
= args
[2].to_i
838 var fromval
= args
[3].to_i
839 var toval
= args
[4].to_i
841 debug
("Illegal access on {recvval} for element {fromval}/{recvval.length}")
843 if fromval
+ lenval
>= recvval
.length
then
844 debug
("Illegal access on {recvval} for element {fromval}+{lenval}/{recvval.length}")
847 debug
("Illegal access on {destval} for element {toval}/{destval.length}")
849 if toval
+ lenval
>= destval
.length
then
850 debug
("Illegal access on {destval} for element {toval}+{lenval}/{destval.length}")
852 recvval
.as(FlatBuffer).copy
(fromval
, lenval
, destval
, toval
)
854 else if pname
== "atoi" then
855 return v
.int_instance
(recvval
.to_i
)
856 else if pname
== "file_exists" then
857 return v
.bool_instance
(recvval
.to_s
.file_exists
)
858 else if pname
== "file_mkdir" then
861 else if pname
== "file_chdir" then
864 else if pname
== "file_realpath" then
865 return v
.native_string_instance
(recvval
.to_s
.realpath
)
866 else if pname
== "get_environ" then
867 var txt
= recvval
.to_s
.environ
868 return v
.native_string_instance
(txt
)
869 else if pname
== "system" then
870 var res
= sys
.system
(recvval
.to_s
)
871 return v
.int_instance
(res
)
872 else if pname
== "atof" then
873 return v
.float_instance
(recvval
.to_f
)
875 else if pname
== "calloc_string" then
876 return v
.native_string_instance
("!" * args
[1].to_i
)
877 else if cname
== "NativeArray" then
878 if pname
== "init" then
879 var val
= new Array[Instance].filled_with
(v
.null_instance
, args
[1].to_i
)
880 return new PrimitiveInstance[Array[Instance]](args
[0].mtype
, val
)
882 var recvval
= args
.first
.val
.as(Array[Instance])
883 if pname
== "[]" then
884 if args
[1].to_i
>= recvval
.length
or args
[1].to_i
< 0 then
885 debug
("Illegal access on {recvval} for element {args[1].to_i}/{recvval.length}")
887 return recvval
[args
[1].to_i
]
888 else if pname
== "[]=" then
889 recvval
[args
[1].to_i
] = args
[2]
891 else if pname
== "length" then
892 return v
.int_instance
(recvval
.length
)
893 else if pname
== "copy_to" then
894 recvval
.copy
(0, args
[2].to_i
, args
[1].val
.as(Array[Instance]), 0)
897 else if cname
== "NativeFile" then
898 if pname
== "native_stdout" then
899 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, sys
.stdout
)
900 else if pname
== "native_stdin" then
901 return new PrimitiveInstance[IStream](mpropdef
.mclassdef
.mclass
.mclass_type
, sys
.stdin
)
902 else if pname
== "native_stderr" then
903 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, sys
.stderr
)
904 else if pname
== "io_open_read" then
905 var a1
= args
[1].val
.as(Buffer)
906 return new PrimitiveInstance[IStream](mpropdef
.mclassdef
.mclass
.mclass_type
, new IFStream.open
(a1
.to_s
))
907 else if pname
== "io_open_write" then
908 var a1
= args
[1].val
.as(Buffer)
909 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, new OFStream.open
(a1
.to_s
))
911 var recvval
= args
.first
.val
912 if pname
== "io_write" then
913 var a1
= args
[1].val
.as(Buffer)
914 recvval
.as(OStream).write
(a1
.substring
(0, args
[2].to_i
).to_s
)
916 else if pname
== "io_read" then
917 var str
= recvval
.as(IStream).read
(args
[2].to_i
)
918 var a1
= args
[1].val
.as(Buffer)
919 new FlatBuffer.from
(str
).copy
(0, str
.length
, a1
.as(FlatBuffer), 0)
920 return v
.int_instance
(str
.length
)
921 else if pname
== "io_close" then
922 recvval
.as(IOS).close
923 return v
.int_instance
(0)
924 else if pname
== "address_is_null" then
925 return v
.false_instance
927 else if pname
== "calloc_array" then
928 var recvtype
= args
.first
.mtype
.as(MClassType)
930 mtype
= recvtype
.supertype_to
(v
.mainmodule
, recvtype
, v
.mainmodule
.get_primitive_class
("ArrayCapable"))
931 mtype
= mtype
.arguments
.first
932 var val
= new Array[Instance].filled_with
(v
.null_instance
, args
[1].to_i
)
933 return new PrimitiveInstance[Array[Instance]](v
.mainmodule
.get_primitive_class
("NativeArray").get_mtype
([mtype
]), val
)
934 else if pname
== "native_argc" then
935 return v
.int_instance
(v
.arguments
.length
)
936 else if pname
== "native_argv" then
937 var txt
= v
.arguments
[args
[1].to_i
]
938 return v
.native_string_instance
(txt
)
939 else if pname
== "native_argc" then
940 return v
.int_instance
(v
.arguments
.length
)
941 else if pname
== "native_argv" then
942 var txt
= v
.arguments
[args
[1].to_i
]
943 return v
.native_string_instance
(txt
)
944 else if pname
== "get_time" then
945 return v
.int_instance
(get_time
)
946 else if pname
== "srand_from" then
947 srand_from
(args
[1].to_i
)
949 else if pname
== "atan2" then
950 return v
.float_instance
(atan2
(args
[1].to_f
, args
[2].to_f
))
951 else if pname
== "pi" then
952 return v
.float_instance
(pi
)
953 else if pname
== "lexer_goto" then
954 return v
.int_instance
(lexer_goto
(args
[1].to_i
, args
[2].to_i
))
955 else if pname
== "lexer_accept" then
956 return v
.int_instance
(lexer_accept
(args
[1].to_i
))
957 else if pname
== "parser_goto" then
958 return v
.int_instance
(parser_goto
(args
[1].to_i
, args
[2].to_i
))
959 else if pname
== "parser_action" then
960 return v
.int_instance
(parser_action
(args
[1].to_i
, args
[2].to_i
))
961 else if pname
== "file_getcwd" then
962 return v
.native_string_instance
(getcwd
)
963 else if pname
== "errno" then
964 return v
.int_instance
(sys
.errno
)
965 else if pname
== "address_is_null" then
966 return v
.false_instance
968 if mpropdef
.is_intern
then
969 fatal
(v
, "NOT YET IMPLEMENTED intern {mpropdef}")
970 else if mpropdef
.is_extern
then
971 fatal
(v
, "NOT YET IMPLEMENTED extern {mpropdef}")
973 fatal
(v
, "NOT YET IMPLEMENTED <wat?> {mpropdef}")
979 redef class AbstractArray[E
]
980 fun copy
(start
: Int, len
: Int, dest
: AbstractArray[E
], new_start
: Int)
982 self.copy_to
(start
, len
, dest
, new_start
)
986 redef class AAttrPropdef
987 redef fun call
(v
, mpropdef
, args
)
989 var recv
= args
.first
990 assert recv
isa MutableInstance
991 var attr
= self.mpropdef
.mproperty
992 if mpropdef
== mreadpropdef
then
993 assert args
.length
== 1
994 return v
.read_attribute
(attr
, recv
)
995 else if mpropdef
== mwritepropdef
then
996 assert args
.length
== 2
997 v
.write_attribute
(attr
, recv
, args
[1])
1004 # Evaluate and set the default value of the attribute in `recv`
1005 private fun init_expr
(v
: NaiveInterpreter, recv
: Instance)
1007 assert recv
isa MutableInstance
1008 var nexpr
= self.n_expr
1009 if nexpr
!= null then
1010 var f
= new Frame(self, self.mpropdef
.as(not null), [recv
])
1012 var val
= v
.expr
(nexpr
)
1015 assert not v
.is_escaping
1016 v
.write_attribute
(self.mpropdef
.mproperty
, recv
, val
)
1019 var mtype
= self.mpropdef
.static_mtype
.as(not null)
1020 mtype
= mtype
.anchor_to
(v
.mainmodule
, recv
.mtype
.as(MClassType))
1021 if mtype
isa MNullableType then
1022 v
.write_attribute
(self.mpropdef
.mproperty
, recv
, v
.null_instance
)
1027 redef class AClassdef
1028 # Execute an implicit `mpropdef` associated with the current node.
1029 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
1031 var super_inits
= self.super_inits
1032 if super_inits
!= null then
1033 var args_of_super
= args
1034 if args
.length
> 1 then args_of_super
= [args
.first
]
1035 for su
in super_inits
do
1036 v
.send
(su
, args_of_super
)
1039 var recv
= args
.first
1040 assert recv
isa MutableInstance
1042 # Collect undefined attributes
1043 for npropdef
in self.n_propdefs
do
1044 if npropdef
isa AAttrPropdef and not npropdef
.noinit
and npropdef
.n_expr
== null then
1045 v
.write_attribute
(npropdef
.mpropdef
.mproperty
, recv
, args
[i
])
1054 # Evaluate the node as a possible expression.
1055 # Return a possible value
1056 # NOTE: Do not call this method directly, but use `v.expr`
1057 # This method is here to be implemented by subclasses.
1058 private fun expr
(v
: NaiveInterpreter): nullable Instance
1060 fatal
(v
, "NOT YET IMPLEMENTED expr {class_name}")
1064 # Evaluate the node as a statement.
1065 # NOTE: Do not call this method directly, but use `v.stmt`
1066 # This method is here to be implemented by subclasses (no need to return something).
1067 private fun stmt
(v
: NaiveInterpreter)
1074 redef class ABlockExpr
1077 var last
= self.n_expr
.last
1078 for e
in self.n_expr
do
1079 if e
== last
then break
1081 if v
.is_escaping
then return null
1088 for e
in self.n_expr
do
1090 if v
.is_escaping
then return
1095 redef class AVardeclExpr
1098 var ne
= self.n_expr
1101 if i
== null then return
1102 v
.frame
.map
[self.variable
.as(not null)] = i
1107 redef class AVarExpr
1110 return v
.frame
.map
[self.variable
.as(not null)]
1114 redef class AVarAssignExpr
1117 var i
= v
.expr
(self.n_value
)
1118 if i
== null then return null
1119 v
.frame
.map
[self.variable
.as(not null)] = i
1124 redef class AVarReassignExpr
1127 var vari
= v
.frame
.map
[self.variable
.as(not null)]
1128 var value
= v
.expr
(self.n_value
)
1129 if value
== null then return
1130 var res
= v
.callsite
(reassign_callsite
, [vari
, value
])
1132 v
.frame
.map
[self.variable
.as(not null)] = res
1136 redef class ASelfExpr
1139 return v
.frame
.arguments
.first
1143 redef class AContinueExpr
1146 var ne
= self.n_expr
1149 if i
== null then return
1152 v
.continuemark
= self.escapemark
1156 redef class ABreakExpr
1159 var ne
= self.n_expr
1162 if i
== null then return
1165 v
.breakmark
= self.escapemark
1169 redef class AReturnExpr
1172 var ne
= self.n_expr
1175 if i
== null then return
1178 v
.returnmark
= v
.frame
1182 redef class AAbortExpr
1193 var cond
= v
.expr
(self.n_expr
)
1194 if cond
== null then return null
1195 if cond
.is_true
then
1196 return v
.expr
(self.n_then
.as(not null))
1198 return v
.expr
(self.n_else
.as(not null))
1204 var cond
= v
.expr
(self.n_expr
)
1205 if cond
== null then return
1206 if cond
.is_true
then
1214 redef class AIfexprExpr
1217 var cond
= v
.expr
(self.n_expr
)
1218 if cond
== null then return null
1219 if cond
.is_true
then
1220 return v
.expr
(self.n_then
)
1222 return v
.expr
(self.n_else
)
1230 v
.stmt
(self.n_block
)
1231 v
.is_break
(self.escapemark
) # Clear the break (if any)
1235 redef class AWhileExpr
1239 var cond
= v
.expr
(self.n_expr
)
1240 if cond
== null then return
1241 if not cond
.is_true
then return
1242 v
.stmt
(self.n_block
)
1243 if v
.is_break
(self.escapemark
) then return
1244 v
.is_continue
(self.escapemark
) # Clear the break
1245 if v
.is_escaping
then return
1250 redef class ALoopExpr
1254 v
.stmt
(self.n_block
)
1255 if v
.is_break
(self.escapemark
) then return
1256 v
.is_continue
(self.escapemark
) # Clear the break
1257 if v
.is_escaping
then return
1262 redef class AForExpr
1265 var col
= v
.expr
(self.n_expr
)
1266 if col
== null then return
1267 if col
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1269 #self.debug("col {col}")
1270 var iter
= v
.callsite
(method_iterator
, [col
]).as(not null)
1271 #self.debug("iter {iter}")
1273 var isok
= v
.callsite
(method_is_ok
, [iter
]).as(not null)
1274 if not isok
.is_true
then return
1275 if self.variables
.length
== 1 then
1276 var item
= v
.callsite
(method_item
, [iter
]).as(not null)
1277 #self.debug("item {item}")
1278 v
.frame
.map
[self.variables
.first
] = item
1279 else if self.variables
.length
== 2 then
1280 var key
= v
.callsite
(method_key
, [iter
]).as(not null)
1281 v
.frame
.map
[self.variables
[0]] = key
1282 var item
= v
.callsite
(method_item
, [iter
]).as(not null)
1283 v
.frame
.map
[self.variables
[1]] = item
1287 v
.stmt
(self.n_block
)
1288 if v
.is_break
(self.escapemark
) then return
1289 v
.is_continue
(self.escapemark
) # Clear the break
1290 if v
.is_escaping
then return
1291 v
.callsite
(method_next
, [iter
])
1296 redef class AAssertExpr
1299 var cond
= v
.expr
(self.n_expr
)
1300 if cond
== null then return
1301 if not cond
.is_true
then
1303 if v
.is_escaping
then return
1306 fatal
(v
, "Assert '{nid.text}' failed")
1308 fatal
(v
, "Assert failed")
1318 var cond
= v
.expr
(self.n_expr
)
1319 if cond
== null then return null
1320 if cond
.is_true
then return cond
1321 return v
.expr
(self.n_expr2
)
1325 redef class AImpliesExpr
1328 var cond
= v
.expr
(self.n_expr
)
1329 if cond
== null then return null
1330 if not cond
.is_true
then return v
.true_instance
1331 return v
.expr
(self.n_expr2
)
1335 redef class AAndExpr
1338 var cond
= v
.expr
(self.n_expr
)
1339 if cond
== null then return null
1340 if not cond
.is_true
then return cond
1341 return v
.expr
(self.n_expr2
)
1345 redef class ANotExpr
1348 var cond
= v
.expr
(self.n_expr
)
1349 if cond
== null then return null
1350 return v
.bool_instance
(not cond
.is_true
)
1354 redef class AOrElseExpr
1357 var i
= v
.expr
(self.n_expr
)
1358 if i
== null then return null
1359 if i
!= v
.null_instance
then return i
1360 return v
.expr
(self.n_expr2
)
1364 redef class AIntExpr
1367 return v
.int_instance
(self.value
.as(not null))
1371 redef class AFloatExpr
1374 return v
.float_instance
(self.value
.as(not null))
1378 redef class ACharExpr
1381 return v
.char_instance
(self.value
.as(not null))
1385 redef class AArrayExpr
1388 var val
= new Array[Instance]
1389 for nexpr
in self.n_exprs
.n_exprs
do
1390 var i
= v
.expr
(nexpr
)
1391 if i
== null then return null
1394 var mtype
= v
.unanchor_type
(self.mtype
.as(not null)).as(MClassType)
1395 var elttype
= mtype
.arguments
.first
1396 return v
.array_instance
(val
, elttype
)
1400 redef class AStringFormExpr
1403 var txt
= self.value
.as(not null)
1404 var nat
= v
.native_string_instance
(txt
)
1405 var res
= v
.send
(v
.force_get_primitive_method
("to_s", nat
.mtype
), [nat
]).as(not null)
1410 redef class ASuperstringExpr
1413 var array
= new Array[Instance]
1414 for nexpr
in n_exprs
do
1415 var i
= v
.expr
(nexpr
)
1416 if i
== null then return null
1419 var i
= v
.array_instance
(array
, v
.mainmodule
.get_primitive_class
("Object").mclass_type
)
1420 var res
= v
.send
(v
.force_get_primitive_method
("to_s", i
.mtype
), [i
])
1426 redef class ACrangeExpr
1429 var e1
= v
.expr
(self.n_expr
)
1430 if e1
== null then return null
1431 var e2
= v
.expr
(self.n_expr2
)
1432 if e2
== null then return null
1433 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1434 var res
= new MutableInstance(mtype
)
1435 v
.init_instance
(res
)
1436 v
.callsite
(init_callsite
, [res
, e1
, e2
])
1441 redef class AOrangeExpr
1444 var e1
= v
.expr
(self.n_expr
)
1445 if e1
== null then return null
1446 var e2
= v
.expr
(self.n_expr2
)
1447 if e2
== null then return null
1448 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1449 var res
= new MutableInstance(mtype
)
1450 v
.init_instance
(res
)
1451 v
.callsite
(init_callsite
, [res
, e1
, e2
])
1456 redef class ATrueExpr
1459 return v
.bool_instance
(true)
1463 redef class AFalseExpr
1466 return v
.bool_instance
(false)
1470 redef class ANullExpr
1473 return v
.null_instance
1477 redef class AIsaExpr
1480 var i
= v
.expr
(self.n_expr
)
1481 if i
== null then return null
1482 var mtype
= v
.unanchor_type
(self.cast_type
.as(not null))
1483 return v
.bool_instance
(v
.is_subtype
(i
.mtype
, mtype
))
1487 redef class AAsCastExpr
1490 var i
= v
.expr
(self.n_expr
)
1491 if i
== null then return null
1492 var mtype
= self.mtype
.as(not null)
1493 var amtype
= v
.unanchor_type
(mtype
)
1494 if not v
.is_subtype
(i
.mtype
, amtype
) then
1495 fatal
(v
, "Cast failed. Expected `{amtype}`, got `{i.mtype}`")
1501 redef class AAsNotnullExpr
1504 var i
= v
.expr
(self.n_expr
)
1505 if i
== null then return null
1506 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1507 if i
.mtype
isa MNullType then
1508 fatal
(v
, "Cast failed")
1514 redef class AParExpr
1517 return v
.expr
(self.n_expr
)
1521 redef class AOnceExpr
1524 if v
.onces
.has_key
(self) then
1525 return v
.onces
[self]
1527 var res
= v
.expr
(self.n_expr
)
1528 if res
== null then return null
1535 redef class ASendExpr
1538 var recv
= v
.expr
(self.n_expr
)
1539 if recv
== null then return null
1541 for a
in self.raw_arguments
do
1543 if i
== null then return null
1547 var res
= v
.callsite
(callsite
, args
)
1552 redef class ASendReassignFormExpr
1555 var recv
= v
.expr
(self.n_expr
)
1556 if recv
== null then return
1558 for a
in self.raw_arguments
do
1560 if i
== null then return
1563 var value
= v
.expr
(self.n_value
)
1564 if value
== null then return
1566 var read
= v
.callsite
(callsite
, args
)
1569 var write
= v
.callsite
(reassign_callsite
, [read
, value
])
1570 assert write
!= null
1574 v
.callsite
(write_callsite
, args
)
1578 redef class ASuperExpr
1581 var recv
= v
.frame
.arguments
.first
1583 for a
in self.n_args
.n_exprs
do
1585 if i
== null then return null
1589 var callsite
= self.callsite
1590 if callsite
!= null then
1591 # Add additionnals arguments for the super init call
1592 if args
.length
== 1 then
1593 for i
in [0..callsite
.msignature
.arity
[ do
1594 args
.add
(v
.frame
.arguments
[i
+1])
1598 var res
= v
.callsite
(callsite
, args
)
1602 if args
.length
== 1 then
1603 args
= v
.frame
.arguments
1606 # stantard call-next-method
1607 var mpropdef
= self.mpropdef
1608 mpropdef
= mpropdef
.lookup_next_definition
(v
.mainmodule
, recv
.mtype
)
1609 var res
= v
.call_without_varargs
(mpropdef
, args
)
1614 redef class ANewExpr
1617 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1618 var recv
: Instance = new MutableInstance(mtype
)
1619 v
.init_instance
(recv
)
1621 for a
in self.n_args
.n_exprs
do
1623 if i
== null then return null
1626 var res2
= v
.callsite
(callsite
, args
)
1627 if res2
!= null then
1628 #self.debug("got {res2} from {mproperty}. drop {recv}")
1635 redef class AAttrExpr
1638 var recv
= v
.expr
(self.n_expr
)
1639 if recv
== null then return null
1640 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1641 var mproperty
= self.mproperty
.as(not null)
1642 return v
.read_attribute
(mproperty
, recv
)
1646 redef class AAttrAssignExpr
1649 var recv
= v
.expr
(self.n_expr
)
1650 if recv
== null then return
1651 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1652 var i
= v
.expr
(self.n_value
)
1653 if i
== null then return
1654 var mproperty
= self.mproperty
.as(not null)
1655 v
.write_attribute
(mproperty
, recv
, i
)
1659 redef class AAttrReassignExpr
1662 var recv
= v
.expr
(self.n_expr
)
1663 if recv
== null then return
1664 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1665 var value
= v
.expr
(self.n_value
)
1666 if value
== null then return
1667 var mproperty
= self.mproperty
.as(not null)
1668 var attr
= v
.read_attribute
(mproperty
, recv
)
1669 var res
= v
.callsite
(reassign_callsite
, [attr
, value
])
1671 v
.write_attribute
(mproperty
, recv
, res
)
1675 redef class AIssetAttrExpr
1678 var recv
= v
.expr
(self.n_expr
)
1679 if recv
== null then return null
1680 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1681 var mproperty
= self.mproperty
.as(not null)
1682 return v
.bool_instance
(v
.isset_attribute
(mproperty
, recv
))
1686 redef class ADebugTypeExpr