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 import primitive_types
26 redef class ToolContext
27 # --discover-call-trace
28 var opt_discover_call_trace
= 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 interpreter
.start
(mainmodule
)
53 self.toolcontext
.info
("*** END INTERPRETING: {time1-time0} ***", 2)
57 # The visitor that interprets the Nit Program by walking on the AST
58 class NaiveInterpreter
59 # The modelbuilder that know the AST and its associations with the model
60 var modelbuilder
: ModelBuilder
62 # The main module of the program (used to lookup method)
63 var mainmodule
: MModule
65 # The command line arguments of the interpreted program
66 # arguments.first is the program name
67 # arguments[1] is the first argument
68 var arguments
: Array[String]
70 # The main Sys instance
71 var mainobj
: nullable Instance is noinit
75 if mainmodule
.model
.get_mclasses_by_name
("Bool") != null then
76 self.true_instance
= new PrimitiveInstance[Bool](mainmodule
.bool_type
, true)
77 init_instance_primitive
(self.true_instance
)
78 self.false_instance
= new PrimitiveInstance[Bool](mainmodule
.bool_type
, false)
79 init_instance_primitive
(self.false_instance
)
81 self.null_instance
= new PrimitiveInstance[nullable Object](mainmodule
.model
.null_type
, null)
84 # Starts the interpreter on the main module of a program
85 fun start
(mainmodule
: MModule) do
86 var interpreter
= self
87 var sys_type
= mainmodule
.sys_type
88 if sys_type
== null then return # no class Sys
89 var mainobj
= new MutableInstance(sys_type
)
90 interpreter
.mainobj
= mainobj
91 interpreter
.init_instance
(mainobj
)
92 var initprop
= mainmodule
.try_get_primitive_method
("init", sys_type
.mclass
)
93 if initprop
!= null then
94 interpreter
.send
(initprop
, [mainobj
])
96 var mainprop
= mainmodule
.try_get_primitive_method
("run", sys_type
.mclass
) or else
97 mainmodule
.try_get_primitive_method
("main", sys_type
.mclass
)
98 if mainprop
!= null then
99 interpreter
.send
(mainprop
, [mainobj
])
103 # Subtype test in the context of the mainmodule
104 fun is_subtype
(sub
, sup
: MType): Bool
106 return sub
.is_subtype
(self.mainmodule
, current_receiver_class
, sup
)
109 # Get a primitive method in the context of the main module
110 fun force_get_primitive_method
(name
: String, recv
: MType): MMethod
112 assert recv
isa MClassType
113 return self.modelbuilder
.force_get_primitive_method
(current_node
, name
, recv
.mclass
, self.mainmodule
)
116 # Is a return executed?
117 # Set this mark to skip the evaluation until the end of the specified method frame
118 var returnmark
: nullable FRAME = null
120 # Is a break or a continue executed?
121 # Set this mark to skip the evaluation until a labeled statement catch it with `is_escape`
122 var escapemark
: nullable EscapeMark = null
124 # Is a return or a break or a continue executed?
125 # Use this function to know if you must skip the evaluation of statements
126 fun is_escaping
: Bool do return returnmark
!= null or escapemark
!= null
128 # The value associated with the current return/break/continue, if any.
129 # Set the value when you set a escapemark.
130 # Read the value when you catch a mark or reach the end of a method
131 var escapevalue
: nullable Instance = null
133 # If there is a break/continue and is associated with `escapemark`, then return true and clear the mark.
134 # If there is no break/continue or if `escapemark` is null then return false.
135 # Use this function to catch a potential break/continue.
136 fun is_escape
(escapemark
: nullable EscapeMark): Bool
138 if escapemark
!= null and self.escapemark
== escapemark
then
139 self.escapemark
= null
146 # Evaluate `n` as an expression in the current context.
147 # Return the value of the expression.
148 # If `n` cannot be evaluated, then aborts.
149 fun expr
(n
: AExpr): nullable Instance
151 var frame
= self.frame
152 var old
= frame
.current_node
153 frame
.current_node
= n
154 #n.debug("IN Execute expr")
156 if i
== null and not self.is_escaping
then
157 n
.debug
("inconsitance: no value and not escaping.")
159 var implicit_cast_to
= n
.implicit_cast_to
160 if implicit_cast_to
!= null then
161 var mtype
= self.unanchor_type
(implicit_cast_to
)
162 if not self.is_subtype
(i
.mtype
, mtype
) then n
.fatal
(self, "Cast failed. Expected `{implicit_cast_to}`, got `{i.mtype}`")
165 #n.debug("OUT Execute expr: value is {i}")
166 #if not is_subtype(i.mtype, n.mtype.as(not null)) then n.debug("Expected {n.mtype.as(not null)} got {i}")
167 frame
.current_node
= old
171 # Evaluate `n` as a statement in the current context.
172 # Do nothing if `n` is null.
173 # If `n` cannot be evaluated, then aborts.
174 fun stmt
(n
: nullable AExpr)
176 if n
== null then return
178 if n
.comprehension
!= null then
179 var comprehension
= frame
.comprehension
.as(not null)
181 if i
!= null then comprehension
.add
(i
)
185 var frame
= self.frame
186 var old
= frame
.current_node
187 frame
.current_node
= n
189 frame
.current_node
= old
192 # Map used to store values of nodes that must be evaluated once in the system (`AOnceExpr`)
193 var onces
: Map[ANode, Instance] = new HashMap[ANode, Instance]
195 # Return the boolean instance associated with `val`.
196 fun bool_instance
(val
: Bool): Instance
198 if val
then return self.true_instance
else return self.false_instance
201 # Return the integer instance associated with `val`.
202 fun int_instance
(val
: Int): Instance
204 var t
= mainmodule
.int_type
205 var instance
= new PrimitiveInstance[Int](t
, val
)
206 init_instance_primitive
(instance
)
210 # Return the char instance associated with `val`.
211 fun char_instance
(val
: Char): Instance
213 var t
= mainmodule
.char_type
214 var instance
= new PrimitiveInstance[Char](t
, val
)
215 init_instance_primitive
(instance
)
219 # Return the float instance associated with `val`.
220 fun float_instance
(val
: Float): Instance
222 var t
= mainmodule
.float_type
223 var instance
= new PrimitiveInstance[Float](t
, val
)
224 init_instance_primitive
(instance
)
228 # The unique instance of the `true` value.
229 var true_instance
: Instance is noinit
231 # The unique instance of the `false` value.
232 var false_instance
: Instance is noinit
234 # The unique instance of the `null` value.
235 var null_instance
: Instance is noinit
237 # Return a new array made of `values`.
238 # The dynamic type of the result is Array[elttype].
239 fun array_instance
(values
: Array[Instance], elttype
: MType): Instance
241 assert not elttype
.need_anchor
242 var nat
= new PrimitiveInstance[Array[Instance]](mainmodule
.native_array_type
(elttype
), values
)
243 init_instance_primitive
(nat
)
244 var mtype
= mainmodule
.array_type
(elttype
)
245 var res
= new MutableInstance(mtype
)
246 self.init_instance
(res
)
247 self.send
(self.force_get_primitive_method
("with_native", mtype
), [res
, nat
, self.int_instance
(values
.length
)])
251 # Return a instance associated to a primitive class
252 # Current primitive classes are `Int`, `Bool`, and `String`
253 fun value_instance
(object
: Object): Instance
255 if object
isa Int then
256 return int_instance
(object
)
257 else if object
isa Bool then
258 return bool_instance
(object
)
259 else if object
isa String then
260 return string_instance
(object
)
266 # Return a new native string initialized with `txt`
267 fun native_string_instance
(txt
: String): Instance
269 var val
= new FlatBuffer.from
(txt
)
271 var t
= mainmodule
.native_string_type
272 var instance
= new PrimitiveInstance[Buffer](t
, val
)
273 init_instance_primitive
(instance
)
277 # Return a new String instance for `txt`
278 fun string_instance
(txt
: String): Instance
280 var nat
= native_string_instance
(txt
)
281 var res
= self.send
(self.force_get_primitive_method
("to_s_with_length", nat
.mtype
), [nat
, self.int_instance
(txt
.length
)])
286 # The virtual type of the frames used in the execution engine
289 # The current frame used to store local variables of the current method executed
290 fun frame
: FRAME do return frames
.first
292 # The stack of all frames. The first one is the current one.
293 var frames
= new List[FRAME]
295 # Return a stack trace. One line per function
296 fun stack_trace
: String
298 var b
= new FlatBuffer
299 b
.append
(",---- Stack trace -- - - -\n")
301 b
.append
("| {f.mpropdef} ({f.current_node.location})\n")
303 b
.append
("`------------------- - - -")
307 # The current node, used to print errors, debug and stack-traces
308 fun current_node
: nullable ANode
310 if frames
.is_empty
then return null
311 return frames
.first
.current_node
314 # The dynamic type of the current `self`
315 fun current_receiver_class
: MClassType
317 return frames
.first
.arguments
.first
.mtype
.as(MClassType)
320 # Initialize the environment for a call and return a new Frame
321 # *`node` The AST node
322 # *`mpropdef` The corresponding mpropdef
323 # *`args` Arguments of the call
324 fun new_frame
(node
: ANode, mpropdef
: MPropDef, args
: Array[Instance]): FRAME
326 return new InterpreterFrame(node
, mpropdef
, args
)
329 # Exit the program with a message
330 fun fatal
(message
: String)
332 var node
= current_node
336 node
.fatal
(self, message
)
341 # Debug on the current node
342 fun debug
(message
: String)
344 var node
= current_node
352 # Retrieve the value of the variable in the current frame
353 fun read_variable
(v
: Variable): Instance
355 var f
= frames
.first
.as(InterpreterFrame)
359 # Assign the value of the variable in the current frame
360 fun write_variable
(v
: Variable, value
: Instance)
362 var f
= frames
.first
.as(InterpreterFrame)
366 # Store known methods, used to trace methods as they are reached
367 var discover_call_trace
: Set[MMethodDef] = new HashSet[MMethodDef]
369 # Evaluate `args` as expressions in the call of `mpropdef` on `recv`.
370 # This method is used to manage varargs in signatures and returns the real array
371 # of instances to use in the call.
372 # Return `null` if one of the evaluation of the arguments return null.
373 fun varargize
(mpropdef
: MMethodDef, recv
: Instance, args
: SequenceRead[AExpr]): nullable Array[Instance]
375 var msignature
= mpropdef
.new_msignature
or else mpropdef
.msignature
.as(not null)
376 var res
= new Array[Instance]
379 if args
.is_empty
then return res
381 var vararg_rank
= msignature
.vararg_rank
382 var vararg_len
= args
.length
- msignature
.arity
383 if vararg_len
< 0 then vararg_len
= 0
385 for i
in [0..msignature
.arity
[ do
386 if i
== vararg_rank
then
388 if ne
isa AVarargExpr then
389 var e
= self.expr
(ne
.n_expr
)
390 if e
== null then return null
394 var vararg
= new Array[Instance]
395 for j
in [vararg_rank
..vararg_rank
+vararg_len
] do
396 var e
= self.expr
(args
[j
])
397 if e
== null then return null
400 var elttype
= msignature
.mparameters
[vararg_rank
].mtype
.anchor_to
(self.mainmodule
, recv
.mtype
.as(MClassType))
401 res
.add
(self.array_instance
(vararg
, elttype
))
404 if i
> vararg_rank
then j
+= vararg_len
405 var e
= self.expr
(args
[j
])
406 if e
== null then return null
413 # Execute `mpropdef` for a `args` (where `args[0]` is the receiver).
414 # Return a value if `mpropdef` is a function, or null if it is a procedure.
415 # The call is direct/static. There is no message-sending/late-binding.
416 fun call
(mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
418 if self.modelbuilder
.toolcontext
.opt_discover_call_trace
.value
and not self.discover_call_trace
.has
(mpropdef
) then
419 self.discover_call_trace
.add mpropdef
420 self.debug
("Discovered {mpropdef}")
422 assert args
.length
== mpropdef
.msignature
.arity
+ 1 else debug
("Invalid arity for {mpropdef}. {args.length} arguments given.")
424 # Look for the AST node that implements the property
425 var val
= mpropdef
.constant_value
427 var node
= modelbuilder
.mpropdef2node
(mpropdef
)
428 if mpropdef
.is_abstract
then
430 self.frames
.unshift new_frame
(node
, mpropdef
, args
)
432 fatal
("Abstract method `{mpropdef.mproperty.name}` called on `{args.first.mtype}`")
436 if node
isa APropdef then
437 self.parameter_check
(node
, mpropdef
, args
)
438 return node
.call
(self, mpropdef
, args
)
439 else if node
isa AClassdef then
440 self.parameter_check
(node
, mpropdef
, args
)
441 return node
.call
(self, mpropdef
, args
)
442 else if node
!= null then
443 fatal
("Fatal Error: method {mpropdef} associated to unexpected AST node {node.location}")
445 else if val
!= null then
446 return value_instance
(val
)
448 fatal
("Fatal Error: method {mpropdef} not found in the AST")
453 # Execute type checks of covariant parameters
454 fun parameter_check
(node
: ANode, mpropdef
: MMethodDef, args
: Array[Instance])
456 var msignature
= mpropdef
.msignature
457 for i
in [0..msignature
.arity
[ do
458 # skip test for vararg since the array is instantiated with the correct polymorphic type
459 if msignature
.vararg_rank
== i
then continue
461 # skip if the cast is not required
462 var origmtype
= mpropdef
.mproperty
.intro
.msignature
.mparameters
[i
].mtype
463 if not origmtype
.need_anchor
then continue
465 #print "{mpropdef}: {mpropdef.mproperty.intro.msignature.mparameters[i]}"
467 # get the parameter type
468 var mtype
= msignature
.mparameters
[i
].mtype
469 var anchor
= args
.first
.mtype
.as(MClassType)
470 var amtype
= mtype
.anchor_to
(self.mainmodule
, anchor
)
471 if not args
[i
+1].mtype
.is_subtype
(self.mainmodule
, anchor
, amtype
) then
472 node
.fatal
(self, "Cast failed. Expected `{mtype}`, got `{args[i+1].mtype}`")
477 # Common code for runtime injected calls and normal calls
478 fun send_commons
(mproperty
: MMethod, args
: Array[Instance], mtype
: MType): nullable Instance
480 if mtype
isa MNullType then
481 if mproperty
.name
== "==" or mproperty
.name
== "is_same_instance" then
482 return self.bool_instance
(args
[0] == args
[1])
483 else if mproperty
.name
== "!=" then
484 return self.bool_instance
(args
[0] != args
[1])
486 #fatal("Receiver is null. {mproperty}. {args.join(" ")} {self.frame.current_node.class_name}")
487 fatal
("Receiver is null")
492 # Execute a full `callsite` for given `args`
493 # Use this method, instead of `send` to execute and control the additional behavior of the call-sites
494 fun callsite
(callsite
: nullable CallSite, arguments
: Array[Instance]): nullable Instance
496 var initializers
= callsite
.mpropdef
.initializers
497 if not initializers
.is_empty
then
498 var recv
= arguments
.first
500 for p
in initializers
do
501 if p
isa MMethod then
503 for x
in p
.intro
.msignature
.mparameters
do
504 args
.add arguments
[i
]
508 else if p
isa MAttribute then
509 assert recv
isa MutableInstance
510 write_attribute
(p
, recv
, arguments
[i
])
514 assert i
== arguments
.length
516 return send
(callsite
.mproperty
, [recv
])
518 return send
(callsite
.mproperty
, arguments
)
521 # Execute `mproperty` for a `args` (where `args[0]` is the receiver).
522 # Return a value if `mproperty` is a function, or null if it is a procedure.
523 # The call is polymorphic. There is a message-sending/late-binding according to the receiver (args[0]).
524 fun send
(mproperty
: MMethod, args
: Array[Instance]): nullable Instance
526 var recv
= args
.first
527 var mtype
= recv
.mtype
528 var ret
= send_commons
(mproperty
, args
, mtype
)
529 if ret
!= null then return ret
530 var propdef
= mproperty
.lookup_first_definition
(self.mainmodule
, mtype
)
531 return self.call
(propdef
, args
)
534 # Read the attribute `mproperty` of an instance `recv` and return its value.
535 # If the attribute in not yet initialized, then aborts with an error message.
536 fun read_attribute
(mproperty
: MAttribute, recv
: Instance): Instance
538 assert recv
isa MutableInstance
539 if not recv
.attributes
.has_key
(mproperty
) then
540 fatal
("Uninitialized attribute {mproperty.name}")
543 return recv
.attributes
[mproperty
]
546 # Replace in `recv` the value of the attribute `mproperty` by `value`
547 fun write_attribute
(mproperty
: MAttribute, recv
: Instance, value
: Instance)
549 assert recv
isa MutableInstance
550 recv
.attributes
[mproperty
] = value
553 # Is the attribute `mproperty` initialized the instance `recv`?
554 fun isset_attribute
(mproperty
: MAttribute, recv
: Instance): Bool
556 assert recv
isa MutableInstance
557 return recv
.attributes
.has_key
(mproperty
)
560 # Collect attributes of a type in the order of their init
561 fun collect_attr_propdef
(mtype
: MType): Array[AAttrPropdef]
563 var cache
= self.collect_attr_propdef_cache
564 if cache
.has_key
(mtype
) then return cache
[mtype
]
566 var res
= new Array[AAttrPropdef]
567 var cds
= mtype
.collect_mclassdefs
(self.mainmodule
).to_a
568 self.mainmodule
.linearize_mclassdefs
(cds
)
570 res
.add_all
(modelbuilder
.collect_attr_propdef
(cd
))
577 private var collect_attr_propdef_cache
= new HashMap[MType, Array[AAttrPropdef]]
579 # Fill the initial values of the newly created instance `recv`.
580 # `recv.mtype` is used to know what must be filled.
581 fun init_instance
(recv
: Instance)
583 for npropdef
in collect_attr_propdef
(recv
.mtype
) do
584 npropdef
.init_expr
(self, recv
)
588 # A hook to initialize a `PrimitiveInstance`
589 fun init_instance_primitive
(recv
: Instance) do end
591 # This function determines the correct type according to the receiver of the current propdef (self).
592 fun unanchor_type
(mtype
: MType): MType
594 return mtype
.anchor_to
(self.mainmodule
, current_receiver_class
)
597 # Placebo instance used to mark internal error result when `null` already have a meaning.
598 # TODO: replace with multiple return or something better
599 var error_instance
= new MutableInstance(modelbuilder
.model
.null_type
) is lazy
602 # An instance represents a value of the executed program.
603 abstract class Instance
604 # The dynamic type of the instance
605 # ASSERT: not self.mtype.is_anchored
608 # return true if the instance is the true value.
609 # return false if the instance is the true value.
611 fun is_true
: Bool do abort
613 # Return true if `self` IS `o` (using the Nit semantic of is)
614 fun eq_is
(o
: Instance): Bool do return self.is_same_instance
(o
)
616 # Human readable object identity "Type#number"
617 redef fun to_s
do return "{mtype}"
619 # Return the integer value if the instance is an integer.
621 fun to_i
: Int do abort
623 # Return the integer value if the instance is a float.
625 fun to_f
: Float do abort
627 # The real value encapsulated if the instance is primitive.
629 fun val
: nullable Object do abort
632 # A instance with attribute (standards objects)
633 class MutableInstance
636 # The values of the attributes
637 var attributes
: Map[MAttribute, Instance] = new HashMap[MAttribute, Instance]
640 # Special instance to handle primitives values (int, bool, etc.)
641 # The trick it just to encapsulate the <<real>> value
642 class PrimitiveInstance[E
]
645 # The real value encapsulated
650 if val
== true then return true
651 if val
== false then return false
657 if not o
isa PrimitiveInstance[nullable Object] then return false
658 return self.val
== o
.val
663 if not o
isa PrimitiveInstance[nullable Object] then return false
664 return self.val
.is_same_instance
(o
.val
)
667 redef fun to_s
do return "{mtype}#{val.object_id}({val or else "null"})"
669 redef fun to_i
do return val
.as(Int)
671 redef fun to_f
do return val
.as(Float)
674 # Information about local variables in a running method
676 # The current visited node
677 # The node is stored by frame to keep a stack trace
678 var current_node
: ANode
679 # The executed property.
680 # A Method in case of a call, an attribute in case of a default initialization.
681 var mpropdef
: MPropDef
682 # Arguments of the method (the first is the receiver)
683 var arguments
: Array[Instance]
684 # Indicate if the expression has an array comprehension form
685 var comprehension
: nullable Array[Instance] = null
688 # Implementation of a Frame with a Hashmap to store local variables
689 class InterpreterFrame
692 # Mapping between a variable and the current value
693 private var map
: Map[Variable, Instance] = new HashMap[Variable, Instance]
697 # Aborts the program with a message
698 # `v` is used to know if a colored message is displayed or not
699 fun fatal
(v
: NaiveInterpreter, message
: String)
701 if v
.modelbuilder
.toolcontext
.opt_no_color
.value
== true then
702 sys
.stderr
.write
("Runtime error: {message} ({location.file.filename}:{location.line_start})\n")
704 sys
.stderr
.write
("{location}: Runtime error: {message}\n{location.colored_line("0;31")}\n")
705 sys
.stderr
.write
(v
.stack_trace
)
706 sys
.stderr
.write
("\n")
713 # Execute a `mpropdef` associated with the current node.
714 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
716 fatal
(v
, "NOT YET IMPLEMENTED method kind {class_name}. {mpropdef}")
721 redef class AMethPropdef
724 redef fun call
(v
, mpropdef
, args
)
726 var f
= v
.new_frame
(self, mpropdef
, args
)
727 var res
= call_commons
(v
, mpropdef
, args
, f
)
729 if v
.returnmark
== f
then
738 private fun call_commons
(v
: NaiveInterpreter, mpropdef
: MMethodDef, arguments
: Array[Instance], f
: Frame): nullable Instance
742 for i
in [0..mpropdef
.msignature
.arity
[ do
743 var variable
= self.n_signature
.n_params
[i
].variable
744 assert variable
!= null
745 v
.write_variable
(variable
, arguments
[i
+1])
748 # Call the implicit super-init
749 var auto_super_inits
= self.auto_super_inits
750 if auto_super_inits
!= null then
751 var args
= [arguments
.first
]
752 for auto_super_init
in auto_super_inits
do
754 for i
in [0..auto_super_init
.msignature
.arity
+1[ do
755 args
.add
(arguments
[i
])
757 assert auto_super_init
.mproperty
!= mpropdef
.mproperty
758 v
.callsite
(auto_super_init
, args
)
761 if auto_super_call
then
762 # standard call-next-method
763 var superpd
= mpropdef
.lookup_next_definition
(v
.mainmodule
, arguments
.first
.mtype
)
764 v
.call
(superpd
, arguments
)
767 if mpropdef
.is_intern
or mpropdef
.is_extern
then
768 var res
= intern_call
(v
, mpropdef
, arguments
)
769 if res
!= v
.error_instance
then return res
772 if n_block
!= null then
777 if mpropdef
.is_intern
then
778 fatal
(v
, "NOT YET IMPLEMENTED intern {mpropdef}")
779 else if mpropdef
.is_extern
then
780 fatal
(v
, "NOT YET IMPLEMENTED extern {mpropdef}")
782 fatal
(v
, "NOT YET IMPLEMENTED <wat?> {mpropdef}")
787 # Interprets a intern or a shortcut extern method.
788 # Returns the result for a function, `null` for a procedure, or `error_instance` if the method is unknown.
789 private fun intern_call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
791 var pname
= mpropdef
.mproperty
.name
792 var cname
= mpropdef
.mclassdef
.mclass
.name
793 if pname
== "output" then
794 var recv
= args
.first
797 else if pname
== "object_id" then
798 var recv
= args
.first
799 if recv
isa PrimitiveInstance[Object] then
800 return v
.int_instance
(recv
.val
.object_id
)
802 return v
.int_instance
(recv
.object_id
)
804 else if pname
== "output_class_name" then
805 var recv
= args
.first
808 else if pname
== "native_class_name" then
809 var recv
= args
.first
810 var txt
= recv
.mtype
.to_s
811 return v
.native_string_instance
(txt
)
812 else if pname
== "==" then
813 # == is correctly redefined for instances
814 return v
.bool_instance
(args
[0] == args
[1])
815 else if pname
== "!=" then
816 return v
.bool_instance
(args
[0] != args
[1])
817 else if pname
== "is_same_type" then
818 return v
.bool_instance
(args
[0].mtype
== args
[1].mtype
)
819 else if pname
== "is_same_instance" then
820 return v
.bool_instance
(args
[0].eq_is
(args
[1]))
821 else if pname
== "exit" then
824 else if pname
== "buffer_mode_full" then
825 return v
.int_instance
(sys
.buffer_mode_full
)
826 else if pname
== "buffer_mode_line" then
827 return v
.int_instance
(sys
.buffer_mode_line
)
828 else if pname
== "buffer_mode_none" then
829 return v
.int_instance
(sys
.buffer_mode_none
)
830 else if pname
== "sys" then
832 else if cname
== "Int" then
833 var recvval
= args
[0].to_i
834 if pname
== "unary -" then
835 return v
.int_instance
(-args
[0].to_i
)
836 else if pname
== "+" then
837 return v
.int_instance
(args
[0].to_i
+ args
[1].to_i
)
838 else if pname
== "-" then
839 return v
.int_instance
(args
[0].to_i
- args
[1].to_i
)
840 else if pname
== "*" then
841 return v
.int_instance
(args
[0].to_i
* args
[1].to_i
)
842 else if pname
== "%" then
843 return v
.int_instance
(args
[0].to_i
% args
[1].to_i
)
844 else if pname
== "/" then
845 return v
.int_instance
(args
[0].to_i
/ args
[1].to_i
)
846 else if pname
== "<" then
847 return v
.bool_instance
(args
[0].to_i
< args
[1].to_i
)
848 else if pname
== ">" then
849 return v
.bool_instance
(args
[0].to_i
> args
[1].to_i
)
850 else if pname
== "<=" then
851 return v
.bool_instance
(args
[0].to_i
<= args
[1].to_i
)
852 else if pname
== ">=" then
853 return v
.bool_instance
(args
[0].to_i
>= args
[1].to_i
)
854 else if pname
== "<=>" then
855 return v
.int_instance
(args
[0].to_i
<=> args
[1].to_i
)
856 else if pname
== "ascii" then
857 return v
.char_instance
(args
[0].to_i
.ascii
)
858 else if pname
== "to_f" then
859 return v
.float_instance
(args
[0].to_i
.to_f
)
860 else if pname
== "lshift" then
861 return v
.int_instance
(args
[0].to_i
.lshift
(args
[1].to_i
))
862 else if pname
== "rshift" then
863 return v
.int_instance
(args
[0].to_i
.rshift
(args
[1].to_i
))
864 else if pname
== "rand" then
865 var res
= recvval
.rand
866 return v
.int_instance
(res
)
867 else if pname
== "bin_and" then
868 return v
.int_instance
(args
[0].to_i
.bin_and
(args
[1].to_i
))
869 else if pname
== "bin_or" then
870 return v
.int_instance
(args
[0].to_i
.bin_or
(args
[1].to_i
))
871 else if pname
== "bin_xor" then
872 return v
.int_instance
(args
[0].to_i
.bin_xor
(args
[1].to_i
))
873 else if pname
== "bin_not" then
874 return v
.int_instance
(args
[0].to_i
.bin_not
)
875 else if pname
== "int_to_s_len" then
876 return v
.int_instance
(recvval
.to_s
.length
)
877 else if pname
== "native_int_to_s" then
879 var srecv
= args
[1].val
.as(Buffer)
884 else if pname
== "strerror_ext" then
885 return v
.native_string_instance
(recvval
.strerror
)
887 else if cname
== "Char" then
888 var recv
= args
[0].val
.as(Char)
889 if pname
== "ascii" then
890 return v
.int_instance
(recv
.ascii
)
891 else if pname
== "successor" then
892 return v
.char_instance
(recv
.successor
(args
[1].to_i
))
893 else if pname
== "predecessor" then
894 return v
.char_instance
(recv
.predecessor
(args
[1].to_i
))
895 else if pname
== "<" then
896 return v
.bool_instance
(recv
< args
[1].val
.as(Char))
897 else if pname
== ">" then
898 return v
.bool_instance
(recv
> args
[1].val
.as(Char))
899 else if pname
== "<=" then
900 return v
.bool_instance
(recv
<= args
[1].val
.as(Char))
901 else if pname
== ">=" then
902 return v
.bool_instance
(recv
>= args
[1].val
.as(Char))
903 else if pname
== "<=>" then
904 return v
.int_instance
(recv
<=> args
[1].val
.as(Char))
906 else if cname
== "Float" then
907 var recv
= args
[0].to_f
908 if pname
== "unary -" then
909 return v
.float_instance
(-recv
)
910 else if pname
== "+" then
911 return v
.float_instance
(recv
+ args
[1].to_f
)
912 else if pname
== "-" then
913 return v
.float_instance
(recv
- args
[1].to_f
)
914 else if pname
== "*" then
915 return v
.float_instance
(recv
* args
[1].to_f
)
916 else if pname
== "/" then
917 return v
.float_instance
(recv
/ args
[1].to_f
)
918 else if pname
== "<" then
919 return v
.bool_instance
(recv
< args
[1].to_f
)
920 else if pname
== ">" then
921 return v
.bool_instance
(recv
> args
[1].to_f
)
922 else if pname
== "<=" then
923 return v
.bool_instance
(recv
<= args
[1].to_f
)
924 else if pname
== ">=" then
925 return v
.bool_instance
(recv
>= args
[1].to_f
)
926 else if pname
== "to_i" then
927 return v
.int_instance
(recv
.to_i
)
928 else if pname
== "cos" then
929 return v
.float_instance
(args
[0].to_f
.cos
)
930 else if pname
== "sin" then
931 return v
.float_instance
(args
[0].to_f
.sin
)
932 else if pname
== "tan" then
933 return v
.float_instance
(args
[0].to_f
.tan
)
934 else if pname
== "acos" then
935 return v
.float_instance
(args
[0].to_f
.acos
)
936 else if pname
== "asin" then
937 return v
.float_instance
(args
[0].to_f
.asin
)
938 else if pname
== "atan" then
939 return v
.float_instance
(args
[0].to_f
.atan
)
940 else if pname
== "sqrt" then
941 return v
.float_instance
(args
[0].to_f
.sqrt
)
942 else if pname
== "exp" then
943 return v
.float_instance
(args
[0].to_f
.exp
)
944 else if pname
== "log" then
945 return v
.float_instance
(args
[0].to_f
.log
)
946 else if pname
== "pow" then
947 return v
.float_instance
(args
[0].to_f
.pow
(args
[1].to_f
))
948 else if pname
== "rand" then
949 return v
.float_instance
(args
[0].to_f
.rand
)
950 else if pname
== "abs" then
951 return v
.float_instance
(args
[0].to_f
.abs
)
952 else if pname
== "hypot_with" then
953 return v
.float_instance
(args
[0].to_f
.hypot_with
(args
[1].to_f
))
954 else if pname
== "is_nan" then
955 return v
.bool_instance
(args
[0].to_f
.is_nan
)
956 else if pname
== "is_inf_extern" then
957 return v
.bool_instance
(args
[0].to_f
.is_inf
!= 0)
958 else if pname
== "round" then
959 return v
.float_instance
(args
[0].to_f
.round
)
961 else if cname
== "NativeString" then
962 if pname
== "new" then
963 return v
.native_string_instance
("!" * args
[1].to_i
)
965 var recvval
= args
.first
.val
.as(Buffer)
966 if pname
== "[]" then
967 var arg1
= args
[1].to_i
968 if arg1
>= recvval
.length
or arg1
< 0 then
969 debug
("Illegal access on {recvval} for element {arg1}/{recvval.length}")
971 return v
.char_instance
(recvval
.chars
[arg1
])
972 else if pname
== "[]=" then
973 var arg1
= args
[1].to_i
974 if arg1
>= recvval
.length
or arg1
< 0 then
975 debug
("Illegal access on {recvval} for element {arg1}/{recvval.length}")
977 recvval
.chars
[arg1
] = args
[2].val
.as(Char)
979 else if pname
== "copy_to" then
980 # sig= copy_to(dest: NativeString, length: Int, from: Int, to: Int)
981 var destval
= args
[1].val
.as(FlatBuffer)
982 var lenval
= args
[2].to_i
983 var fromval
= args
[3].to_i
984 var toval
= args
[4].to_i
986 debug
("Illegal access on {recvval} for element {fromval}/{recvval.length}")
988 if fromval
+ lenval
> recvval
.length
then
989 debug
("Illegal access on {recvval} for element {fromval}+{lenval}/{recvval.length}")
992 debug
("Illegal access on {destval} for element {toval}/{destval.length}")
994 if toval
+ lenval
> destval
.length
then
995 debug
("Illegal access on {destval} for element {toval}+{lenval}/{destval.length}")
997 recvval
.as(FlatBuffer).copy
(fromval
, lenval
, destval
, toval
)
999 else if pname
== "atoi" then
1000 return v
.int_instance
(recvval
.to_i
)
1001 else if pname
== "file_exists" then
1002 return v
.bool_instance
(recvval
.to_s
.file_exists
)
1003 else if pname
== "file_mkdir" then
1006 else if pname
== "file_chdir" then
1009 else if pname
== "file_realpath" then
1010 return v
.native_string_instance
(recvval
.to_s
.realpath
)
1011 else if pname
== "get_environ" then
1012 var txt
= recvval
.to_s
.environ
1013 return v
.native_string_instance
(txt
)
1014 else if pname
== "system" then
1015 var res
= sys
.system
(recvval
.to_s
)
1016 return v
.int_instance
(res
)
1017 else if pname
== "atof" then
1018 return v
.float_instance
(recvval
.to_f
)
1020 else if cname
== "String" then
1021 var cs
= v
.send
(v
.force_get_primitive_method
("to_cstring", args
.first
.mtype
), [args
.first
])
1022 var str
= cs
.val
.to_s
1023 if pname
== "files" then
1024 var res
= new Array[Instance]
1025 for f
in str
.files
do res
.add v
.string_instance
(f
)
1026 return v
.array_instance
(res
, v
.mainmodule
.string_type
)
1028 else if pname
== "calloc_string" then
1029 return v
.native_string_instance
("!" * args
[1].to_i
)
1030 else if cname
== "NativeArray" then
1031 if pname
== "new" then
1032 var val
= new Array[Instance].filled_with
(v
.null_instance
, args
[1].to_i
)
1033 var instance
= new PrimitiveInstance[Array[Instance]](args
[0].mtype
, val
)
1034 v
.init_instance_primitive
(instance
)
1037 var recvval
= args
.first
.val
.as(Array[Instance])
1038 if pname
== "[]" then
1039 if args
[1].to_i
>= recvval
.length
or args
[1].to_i
< 0 then
1040 debug
("Illegal access on {recvval} for element {args[1].to_i}/{recvval.length}")
1042 return recvval
[args
[1].to_i
]
1043 else if pname
== "[]=" then
1044 recvval
[args
[1].to_i
] = args
[2]
1046 else if pname
== "length" then
1047 return v
.int_instance
(recvval
.length
)
1048 else if pname
== "copy_to" then
1049 recvval
.copy_to
(0, args
[2].to_i
, args
[1].val
.as(Array[Instance]), 0)
1052 else if cname
== "NativeFile" then
1053 if pname
== "native_stdout" then
1054 var inst
= new PrimitiveNativeFile.native_stdout
1055 var instance
= new PrimitiveInstance[PrimitiveNativeFile](mpropdef
.mclassdef
.mclass
.mclass_type
, inst
)
1056 v
.init_instance_primitive
(instance
)
1058 else if pname
== "native_stdin" then
1059 var inst
= new PrimitiveNativeFile.native_stdin
1060 var instance
= new PrimitiveInstance[PrimitiveNativeFile](mpropdef
.mclassdef
.mclass
.mclass_type
, inst
)
1061 v
.init_instance_primitive
(instance
)
1063 else if pname
== "native_stderr" then
1064 var inst
= new PrimitiveNativeFile.native_stderr
1065 var instance
= new PrimitiveInstance[PrimitiveNativeFile](mpropdef
.mclassdef
.mclass
.mclass_type
, inst
)
1066 v
.init_instance_primitive
(instance
)
1068 else if pname
== "io_open_read" then
1069 var a1
= args
[1].val
.as(Buffer)
1070 var inst
= new PrimitiveNativeFile.io_open_read
(a1
.to_s
)
1071 var instance
= new PrimitiveInstance[PrimitiveNativeFile](mpropdef
.mclassdef
.mclass
.mclass_type
, inst
)
1072 v
.init_instance_primitive
(instance
)
1074 else if pname
== "io_open_write" then
1075 var a1
= args
[1].val
.as(Buffer)
1076 var inst
= new PrimitiveNativeFile.io_open_write
(a1
.to_s
)
1077 var instance
= new PrimitiveInstance[PrimitiveNativeFile](mpropdef
.mclassdef
.mclass
.mclass_type
, inst
)
1078 v
.init_instance_primitive
(instance
)
1081 var recvval
= args
.first
.val
1082 if pname
== "io_write" then
1083 var a1
= args
[1].val
.as(Buffer)
1084 return v
.int_instance
(recvval
.as(PrimitiveNativeFile).io_write
(a1
.to_cstring
, args
[2].to_i
))
1085 else if pname
== "io_read" then
1086 var a1
= args
[1].val
.as(Buffer)
1087 var ns
= new NativeString(a1
.length
)
1088 var len
= recvval
.as(PrimitiveNativeFile).io_read
(ns
, args
[2].to_i
)
1090 a1
.append
(ns
.to_s_with_length
(len
))
1091 return v
.int_instance
(len
)
1092 else if pname
== "flush" then
1093 recvval
.as(PrimitiveNativeFile).flush
1095 else if pname
== "io_close" then
1096 return v
.int_instance
(recvval
.as(PrimitiveNativeFile).io_close
)
1097 else if pname
== "set_buffering_type" then
1098 return v
.int_instance
(recvval
.as(PrimitiveNativeFile).set_buffering_type
(args
[1].to_i
, args
[2].to_i
))
1100 else if pname
== "native_argc" then
1101 return v
.int_instance
(v
.arguments
.length
)
1102 else if pname
== "native_argv" then
1103 var txt
= v
.arguments
[args
[1].to_i
]
1104 return v
.native_string_instance
(txt
)
1105 else if pname
== "native_argc" then
1106 return v
.int_instance
(v
.arguments
.length
)
1107 else if pname
== "native_argv" then
1108 var txt
= v
.arguments
[args
[1].to_i
]
1109 return v
.native_string_instance
(txt
)
1110 else if pname
== "get_time" then
1111 return v
.int_instance
(get_time
)
1112 else if pname
== "srand" then
1115 else if pname
== "srand_from" then
1116 srand_from
(args
[1].to_i
)
1118 else if pname
== "atan2" then
1119 return v
.float_instance
(atan2
(args
[1].to_f
, args
[2].to_f
))
1120 else if pname
== "pi" then
1121 return v
.float_instance
(pi
)
1122 else if pname
== "lexer_goto" then
1123 return v
.int_instance
(lexer_goto
(args
[1].to_i
, args
[2].to_i
))
1124 else if pname
== "lexer_accept" then
1125 return v
.int_instance
(lexer_accept
(args
[1].to_i
))
1126 else if pname
== "parser_goto" then
1127 return v
.int_instance
(parser_goto
(args
[1].to_i
, args
[2].to_i
))
1128 else if pname
== "parser_action" then
1129 return v
.int_instance
(parser_action
(args
[1].to_i
, args
[2].to_i
))
1130 else if pname
== "file_getcwd" then
1131 return v
.native_string_instance
(getcwd
)
1132 else if pname
== "errno" then
1133 return v
.int_instance
(sys
.errno
)
1134 else if pname
== "address_is_null" then
1136 if recv
isa PrimitiveInstance[PrimitiveNativeFile] then
1137 return v
.bool_instance
(recv
.val
.address_is_null
)
1139 return v
.false_instance
1141 return v
.error_instance
1145 redef class AAttrPropdef
1146 redef fun call
(v
, mpropdef
, args
)
1148 var recv
= args
.first
1149 assert recv
isa MutableInstance
1150 var attr
= self.mpropdef
.mproperty
1151 if mpropdef
== mreadpropdef
then
1152 assert args
.length
== 1
1153 if not is_lazy
or v
.isset_attribute
(attr
, recv
) then return v
.read_attribute
(attr
, recv
)
1154 var f
= v
.new_frame
(self, mpropdef
, args
)
1155 return evaluate_expr
(v
, recv
, f
)
1156 else if mpropdef
== mwritepropdef
then
1157 assert args
.length
== 2
1158 v
.write_attribute
(attr
, recv
, args
[1])
1165 # Evaluate and set the default value of the attribute in `recv`
1166 private fun init_expr
(v
: NaiveInterpreter, recv
: Instance)
1168 if is_lazy
then return
1170 var f
= v
.new_frame
(self, mpropdef
.as(not null), [recv
])
1171 evaluate_expr
(v
, recv
, f
)
1174 var mpropdef
= self.mpropdef
1175 if mpropdef
== null then return
1176 var mtype
= mpropdef
.static_mtype
.as(not null)
1177 mtype
= mtype
.anchor_to
(v
.mainmodule
, recv
.mtype
.as(MClassType))
1178 if mtype
isa MNullableType then
1179 v
.write_attribute
(self.mpropdef
.mproperty
, recv
, v
.null_instance
)
1183 private fun evaluate_expr
(v
: NaiveInterpreter, recv
: Instance, f
: Frame): Instance
1185 assert recv
isa MutableInstance
1190 var nexpr
= self.n_expr
1191 var nblock
= self.n_block
1192 if nexpr
!= null then
1194 else if nblock
!= null then
1196 assert v
.returnmark
== f
1199 v
.escapevalue
= null
1206 assert not v
.is_escaping
1207 v
.write_attribute
(self.mpropdef
.mproperty
, recv
, val
)
1212 redef class AClassdef
1213 # Execute an implicit `mpropdef` associated with the current node.
1214 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
1216 if mpropdef
.mproperty
.is_root_init
then
1217 assert args
.length
== 1
1218 if not mpropdef
.is_intro
then
1219 # standard call-next-method
1220 var superpd
= mpropdef
.lookup_next_definition
(v
.mainmodule
, args
.first
.mtype
)
1221 v
.call
(superpd
, args
)
1231 # Evaluate the node as a possible expression.
1232 # Return a possible value
1233 # NOTE: Do not call this method directly, but use `v.expr`
1234 # This method is here to be implemented by subclasses.
1235 protected fun expr
(v
: NaiveInterpreter): nullable Instance
1237 fatal
(v
, "NOT YET IMPLEMENTED expr {class_name}")
1241 # Evaluate the node as a statement.
1242 # NOTE: Do not call this method directly, but use `v.stmt`
1243 # This method is here to be implemented by subclasses (no need to return something).
1244 protected fun stmt
(v
: NaiveInterpreter)
1251 redef class ABlockExpr
1254 var last
= self.n_expr
.last
1255 for e
in self.n_expr
do
1256 if e
== last
then break
1258 if v
.is_escaping
then return null
1265 for e
in self.n_expr
do
1267 if v
.is_escaping
then return
1272 redef class AVardeclExpr
1275 var ne
= self.n_expr
1278 if i
== null then return
1279 v
.write_variable
(self.variable
.as(not null), i
)
1284 redef class AVarExpr
1287 return v
.read_variable
(self.variable
.as(not null))
1291 redef class AVarAssignExpr
1294 var i
= v
.expr
(self.n_value
)
1295 if i
== null then return null
1296 v
.write_variable
(self.variable
.as(not null), i
)
1301 redef class AVarReassignExpr
1304 var variable
= self.variable
.as(not null)
1305 var vari
= v
.read_variable
(variable
)
1306 var value
= v
.expr
(self.n_value
)
1307 if value
== null then return
1308 var res
= v
.callsite
(reassign_callsite
, [vari
, value
])
1310 v
.write_variable
(variable
, res
)
1314 redef class ASelfExpr
1317 return v
.frame
.arguments
.first
1321 redef class AEscapeExpr
1324 var ne
= self.n_expr
1327 if i
== null then return
1330 v
.escapemark
= self.escapemark
1334 redef class AReturnExpr
1337 var ne
= self.n_expr
1340 if i
== null then return
1343 v
.returnmark
= v
.frame
1347 redef class AAbortExpr
1358 var cond
= v
.expr
(self.n_expr
)
1359 if cond
== null then return null
1360 if cond
.is_true
then
1361 return v
.expr
(self.n_then
.as(not null))
1363 return v
.expr
(self.n_else
.as(not null))
1369 var cond
= v
.expr
(self.n_expr
)
1370 if cond
== null then return
1371 if cond
.is_true
then
1379 redef class AIfexprExpr
1382 var cond
= v
.expr
(self.n_expr
)
1383 if cond
== null then return null
1384 if cond
.is_true
then
1385 return v
.expr
(self.n_then
)
1387 return v
.expr
(self.n_else
)
1395 v
.stmt
(self.n_block
)
1396 v
.is_escape
(self.break_mark
) # Clear the break (if any)
1400 redef class AWhileExpr
1404 var cond
= v
.expr
(self.n_expr
)
1405 if cond
== null then return
1406 if not cond
.is_true
then return
1407 v
.stmt
(self.n_block
)
1408 if v
.is_escape
(self.break_mark
) then return
1409 v
.is_escape
(self.continue_mark
) # Clear the break
1410 if v
.is_escaping
then return
1415 redef class ALoopExpr
1419 v
.stmt
(self.n_block
)
1420 if v
.is_escape
(self.break_mark
) then return
1421 v
.is_escape
(self.continue_mark
) # Clear the break
1422 if v
.is_escaping
then return
1427 redef class AForExpr
1430 var col
= v
.expr
(self.n_expr
)
1431 if col
== null then return
1432 if col
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1434 #self.debug("col {col}")
1435 var iter
= v
.callsite
(method_iterator
, [col
]).as(not null)
1436 #self.debug("iter {iter}")
1438 var isok
= v
.callsite
(method_is_ok
, [iter
]).as(not null)
1439 if not isok
.is_true
then break
1440 if self.variables
.length
== 1 then
1441 var item
= v
.callsite
(method_item
, [iter
]).as(not null)
1442 #self.debug("item {item}")
1443 v
.write_variable
(self.variables
.first
, item
)
1444 else if self.variables
.length
== 2 then
1445 var key
= v
.callsite
(method_key
, [iter
]).as(not null)
1446 v
.write_variable
(self.variables
[0], key
)
1447 var item
= v
.callsite
(method_item
, [iter
]).as(not null)
1448 v
.write_variable
(self.variables
[1], item
)
1452 v
.stmt
(self.n_block
)
1453 if v
.is_escape
(self.break_mark
) then break
1454 v
.is_escape
(self.continue_mark
) # Clear the break
1455 if v
.is_escaping
then break
1456 v
.callsite
(method_next
, [iter
])
1458 var method_finish
= self.method_finish
1459 if method_finish
!= null then
1460 v
.callsite
(method_finish
, [iter
])
1465 redef class AAssertExpr
1468 var cond
= v
.expr
(self.n_expr
)
1469 if cond
== null then return
1470 if not cond
.is_true
then
1472 if v
.is_escaping
then return
1475 fatal
(v
, "Assert '{nid.text}' failed")
1477 fatal
(v
, "Assert failed")
1487 var cond
= v
.expr
(self.n_expr
)
1488 if cond
== null then return null
1489 if cond
.is_true
then return cond
1490 return v
.expr
(self.n_expr2
)
1494 redef class AImpliesExpr
1497 var cond
= v
.expr
(self.n_expr
)
1498 if cond
== null then return null
1499 if not cond
.is_true
then return v
.true_instance
1500 return v
.expr
(self.n_expr2
)
1504 redef class AAndExpr
1507 var cond
= v
.expr
(self.n_expr
)
1508 if cond
== null then return null
1509 if not cond
.is_true
then return cond
1510 return v
.expr
(self.n_expr2
)
1514 redef class ANotExpr
1517 var cond
= v
.expr
(self.n_expr
)
1518 if cond
== null then return null
1519 return v
.bool_instance
(not cond
.is_true
)
1523 redef class AOrElseExpr
1526 var i
= v
.expr
(self.n_expr
)
1527 if i
== null then return null
1528 if i
!= v
.null_instance
then return i
1529 return v
.expr
(self.n_expr2
)
1533 redef class AIntExpr
1536 return v
.int_instance
(self.value
.as(not null))
1540 redef class AFloatExpr
1543 return v
.float_instance
(self.value
.as(not null))
1547 redef class ACharExpr
1550 return v
.char_instance
(self.value
.as(not null))
1554 redef class AArrayExpr
1557 var val
= new Array[Instance]
1558 var old_comprehension
= v
.frame
.comprehension
1559 v
.frame
.comprehension
= val
1560 for nexpr
in self.n_exprs
do
1561 if nexpr
isa AForExpr then
1564 var i
= v
.expr
(nexpr
)
1565 if i
== null then return null
1569 v
.frame
.comprehension
= old_comprehension
1570 var mtype
= v
.unanchor_type
(self.mtype
.as(not null)).as(MClassType)
1571 var elttype
= mtype
.arguments
.first
1572 return v
.array_instance
(val
, elttype
)
1576 redef class AStringFormExpr
1579 var txt
= self.value
.as(not null)
1580 return v
.string_instance
(txt
)
1584 redef class ASuperstringExpr
1587 var array
= new Array[Instance]
1588 for nexpr
in n_exprs
do
1589 var i
= v
.expr
(nexpr
)
1590 if i
== null then return null
1593 var i
= v
.array_instance
(array
, v
.mainmodule
.object_type
)
1594 var res
= v
.send
(v
.force_get_primitive_method
("to_s", i
.mtype
), [i
])
1600 redef class ACrangeExpr
1603 var e1
= v
.expr
(self.n_expr
)
1604 if e1
== null then return null
1605 var e2
= v
.expr
(self.n_expr2
)
1606 if e2
== null then return null
1607 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1608 var res
= new MutableInstance(mtype
)
1609 v
.init_instance
(res
)
1610 v
.callsite
(init_callsite
, [res
, e1
, e2
])
1615 redef class AOrangeExpr
1618 var e1
= v
.expr
(self.n_expr
)
1619 if e1
== null then return null
1620 var e2
= v
.expr
(self.n_expr2
)
1621 if e2
== null then return null
1622 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1623 var res
= new MutableInstance(mtype
)
1624 v
.init_instance
(res
)
1625 v
.callsite
(init_callsite
, [res
, e1
, e2
])
1630 redef class ATrueExpr
1633 return v
.bool_instance
(true)
1637 redef class AFalseExpr
1640 return v
.bool_instance
(false)
1644 redef class ANullExpr
1647 return v
.null_instance
1651 redef class AIsaExpr
1654 var i
= v
.expr
(self.n_expr
)
1655 if i
== null then return null
1656 var mtype
= v
.unanchor_type
(self.cast_type
.as(not null))
1657 return v
.bool_instance
(v
.is_subtype
(i
.mtype
, mtype
))
1661 redef class AAsCastExpr
1664 var i
= v
.expr
(self.n_expr
)
1665 if i
== null then return null
1666 var mtype
= self.mtype
.as(not null)
1667 var amtype
= v
.unanchor_type
(mtype
)
1668 if not v
.is_subtype
(i
.mtype
, amtype
) then
1669 fatal
(v
, "Cast failed. Expected `{amtype}`, got `{i.mtype}`")
1675 redef class AAsNotnullExpr
1678 var i
= v
.expr
(self.n_expr
)
1679 if i
== null then return null
1680 if i
.mtype
isa MNullType then
1681 fatal
(v
, "Cast failed")
1687 redef class AParExpr
1690 return v
.expr
(self.n_expr
)
1694 redef class AOnceExpr
1697 if v
.onces
.has_key
(self) then
1698 return v
.onces
[self]
1700 var res
= v
.expr
(self.n_expr
)
1701 if res
== null then return null
1708 redef class ASendExpr
1711 var recv
= v
.expr
(self.n_expr
)
1712 if recv
== null then return null
1713 var args
= v
.varargize
(callsite
.mpropdef
, recv
, self.raw_arguments
)
1714 if args
== null then return null
1716 var res
= v
.callsite
(callsite
, args
)
1721 redef class ASendReassignFormExpr
1724 var recv
= v
.expr
(self.n_expr
)
1725 if recv
== null then return
1726 var args
= v
.varargize
(callsite
.mpropdef
, recv
, self.raw_arguments
)
1727 if args
== null then return
1728 var value
= v
.expr
(self.n_value
)
1729 if value
== null then return
1731 var read
= v
.callsite
(callsite
, args
)
1734 var write
= v
.callsite
(reassign_callsite
, [read
, value
])
1735 assert write
!= null
1739 v
.callsite
(write_callsite
, args
)
1743 redef class ASuperExpr
1746 var recv
= v
.frame
.arguments
.first
1748 var callsite
= self.callsite
1749 if callsite
!= null then
1750 var args
= v
.varargize
(callsite
.mpropdef
, recv
, self.n_args
.n_exprs
)
1751 if args
== null then return null
1752 # Add additional arguments for the super init call
1753 if args
.length
== 1 then
1754 for i
in [0..callsite
.msignature
.arity
[ do
1755 args
.add
(v
.frame
.arguments
[i
+1])
1759 var res
= v
.callsite
(callsite
, args
)
1763 # standard call-next-method
1764 var mpropdef
= self.mpropdef
1765 mpropdef
= mpropdef
.lookup_next_definition
(v
.mainmodule
, recv
.mtype
)
1767 var args
= v
.varargize
(mpropdef
, recv
, self.n_args
.n_exprs
)
1768 if args
== null then return null
1770 if args
.length
== 1 then
1771 args
= v
.frame
.arguments
1773 var res
= v
.call
(mpropdef
, args
)
1778 redef class ANewExpr
1781 var mtype
= v
.unanchor_type
(self.recvtype
.as(not null))
1782 var recv
: Instance = new MutableInstance(mtype
)
1783 v
.init_instance
(recv
)
1784 var args
= v
.varargize
(callsite
.mpropdef
, recv
, self.n_args
.n_exprs
)
1785 if args
== null then return null
1786 var res2
= v
.callsite
(callsite
, args
)
1787 if res2
!= null then
1788 #self.debug("got {res2} from {mproperty}. drop {recv}")
1795 redef class AAttrExpr
1798 var recv
= v
.expr
(self.n_expr
)
1799 if recv
== null then return null
1800 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1801 var mproperty
= self.mproperty
.as(not null)
1802 return v
.read_attribute
(mproperty
, recv
)
1806 redef class AAttrAssignExpr
1809 var recv
= v
.expr
(self.n_expr
)
1810 if recv
== null then return
1811 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1812 var i
= v
.expr
(self.n_value
)
1813 if i
== null then return
1814 var mproperty
= self.mproperty
.as(not null)
1815 v
.write_attribute
(mproperty
, recv
, i
)
1819 redef class AAttrReassignExpr
1822 var recv
= v
.expr
(self.n_expr
)
1823 if recv
== null then return
1824 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1825 var value
= v
.expr
(self.n_value
)
1826 if value
== null then return
1827 var mproperty
= self.mproperty
.as(not null)
1828 var attr
= v
.read_attribute
(mproperty
, recv
)
1829 var res
= v
.callsite
(reassign_callsite
, [attr
, value
])
1831 v
.write_attribute
(mproperty
, recv
, res
)
1835 redef class AIssetAttrExpr
1838 var recv
= v
.expr
(self.n_expr
)
1839 if recv
== null then return null
1840 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1841 var mproperty
= self.mproperty
.as(not null)
1842 return v
.bool_instance
(v
.isset_attribute
(mproperty
, recv
))
1846 redef class ADebugTypeExpr