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
25 redef class ToolContext
26 # --discover-call-trace
27 var opt_discover_call_trace
= new OptionBool("Trace calls of the first invocation of a method", "--discover-call-trace")
32 self.option_context
.add_option
(self.opt_discover_call_trace
)
36 redef class ModelBuilder
37 # Execute the program from the entry point (`Sys::main`) of the `mainmodule`
38 # `arguments` are the command-line arguments in order
40 # 1. the AST is fully loaded.
41 # 2. the model is fully built.
42 # 3. the instructions are fully analysed.
43 fun run_naive_interpreter
(mainmodule
: MModule, arguments
: Array[String])
46 self.toolcontext
.info
("*** START INTERPRETING ***", 1)
48 var interpreter
= new NaiveInterpreter(self, mainmodule
, arguments
)
49 interpreter
.start
(mainmodule
)
52 self.toolcontext
.info
("*** END INTERPRETING: {time1-time0} ***", 2)
56 # The visitor that interprets the Nit Program by walking on the AST
57 class NaiveInterpreter
58 # The modelbuilder that know the AST and its associations with the model
59 var modelbuilder
: ModelBuilder
61 # The main module of the program (used to lookup method)
62 var mainmodule
: MModule
64 # The command line arguments of the interpreted program
65 # arguments.first is the program name
66 # arguments[1] is the first argument
67 var arguments
: Array[String]
69 # The main Sys instance
70 var mainobj
: nullable Instance is noinit
74 if mainmodule
.model
.get_mclasses_by_name
("Bool") != null then
75 self.true_instance
= new PrimitiveInstance[Bool](mainmodule
.bool_type
, true)
76 init_instance_primitive
(self.true_instance
)
77 self.false_instance
= new PrimitiveInstance[Bool](mainmodule
.bool_type
, false)
78 init_instance_primitive
(self.false_instance
)
80 self.null_instance
= new PrimitiveInstance[nullable Object](mainmodule
.model
.null_type
, null)
83 # Starts the interpreter on the main module of a program
84 fun start
(mainmodule
: MModule) do
85 var interpreter
= self
86 var sys_type
= mainmodule
.sys_type
87 if sys_type
== null then return # no class Sys
88 var mainobj
= new MutableInstance(sys_type
)
89 interpreter
.mainobj
= mainobj
90 interpreter
.init_instance
(mainobj
)
91 var initprop
= mainmodule
.try_get_primitive_method
("init", sys_type
.mclass
)
92 if initprop
!= null then
93 interpreter
.send
(initprop
, [mainobj
])
95 var mainprop
= mainmodule
.try_get_primitive_method
("run", sys_type
.mclass
) or else
96 mainmodule
.try_get_primitive_method
("main", sys_type
.mclass
)
97 if mainprop
!= null then
98 interpreter
.send
(mainprop
, [mainobj
])
102 # Subtype test in the context of the mainmodule
103 fun is_subtype
(sub
, sup
: MType): Bool
105 return sub
.is_subtype
(self.mainmodule
, current_receiver_class
, sup
)
108 # Get a primitive method in the context of the main module
109 fun force_get_primitive_method
(name
: String, recv
: MType): MMethod
111 assert recv
isa MClassType
112 return self.modelbuilder
.force_get_primitive_method
(current_node
, name
, recv
.mclass
, self.mainmodule
)
115 # Is a return executed?
116 # Set this mark to skip the evaluation until the end of the specified method frame
117 var returnmark
: nullable Frame = null
119 # Is a break or a continue executed?
120 # Set this mark to skip the evaluation until a labeled statement catch it with `is_escape`
121 var escapemark
: nullable EscapeMark = null
123 # Is a return or a break or a continue executed?
124 # Use this function to know if you must skip the evaluation of statements
125 fun is_escaping
: Bool do return returnmark
!= null or escapemark
!= null
127 # The value associated with the current return/break/continue, if any.
128 # Set the value when you set a escapemark.
129 # Read the value when you catch a mark or reach the end of a method
130 var escapevalue
: nullable Instance = null
132 # If there is a break/continue and is associated with `escapemark`, then return true and clear the mark.
133 # If there is no break/continue or if `escapemark` is null then return false.
134 # Use this function to catch a potential break/continue.
135 fun is_escape
(escapemark
: nullable EscapeMark): Bool
137 if escapemark
!= null and self.escapemark
== escapemark
then
138 self.escapemark
= null
145 # Evaluate `n` as an expression in the current context.
146 # Return the value of the expression.
147 # If `n` cannot be evaluated, then aborts.
148 fun expr
(n
: AExpr): nullable Instance
150 var frame
= self.frame
151 var old
= frame
.current_node
152 frame
.current_node
= n
153 #n.debug("IN Execute expr")
155 if i
== null and not self.is_escaping
then
156 n
.debug
("inconsitance: no value and not escaping.")
158 var implicit_cast_to
= n
.implicit_cast_to
159 if implicit_cast_to
!= null then
160 var mtype
= self.unanchor_type
(implicit_cast_to
)
161 if not self.is_subtype
(i
.mtype
, mtype
) then n
.fatal
(self, "Cast failed. Expected `{implicit_cast_to}`, got `{i.mtype}`")
164 #n.debug("OUT Execute expr: value is {i}")
165 #if not is_subtype(i.mtype, n.mtype.as(not null)) then n.debug("Expected {n.mtype.as(not null)} got {i}")
166 frame
.current_node
= old
170 # Evaluate `n` as a statement in the current context.
171 # Do nothing if `n` is null.
172 # If `n` cannot be evaluated, then aborts.
173 fun stmt
(n
: nullable AExpr)
176 var frame
= self.frame
177 var old
= frame
.current_node
178 frame
.current_node
= n
179 #n.debug("Execute stmt")
181 frame
.current_node
= old
185 # Map used to store values of nodes that must be evaluated once in the system (`AOnceExpr`)
186 var onces
: Map[ANode, Instance] = new HashMap[ANode, Instance]
188 # Return the boolean instance associated with `val`.
189 fun bool_instance
(val
: Bool): Instance
191 if val
then return self.true_instance
else return self.false_instance
194 # Return the integer instance associated with `val`.
195 fun int_instance
(val
: Int): Instance
197 var ic
= get_primitive_class
("Int")
198 var instance
= new PrimitiveInstance[Int](ic
.mclass_type
, val
)
199 init_instance_primitive
(instance
)
203 # Return the char instance associated with `val`.
204 fun char_instance
(val
: Char): Instance
206 var ic
= get_primitive_class
("Char")
207 var instance
= new PrimitiveInstance[Char](ic
.mclass_type
, val
)
208 init_instance_primitive
(instance
)
212 # Return the float instance associated with `val`.
213 fun float_instance
(val
: Float): Instance
215 var ic
= get_primitive_class
("Float")
216 var instance
= new PrimitiveInstance[Float](ic
.mclass_type
, val
)
217 init_instance_primitive
(instance
)
221 # The unique instance of the `true` value.
222 var true_instance
: Instance is noinit
224 # The unique instance of the `false` value.
225 var false_instance
: Instance is noinit
227 # The unique instance of the `null` value.
228 var null_instance
: Instance is noinit
230 # Return a new array made of `values`.
231 # The dynamic type of the result is Array[elttype].
232 fun array_instance
(values
: Array[Instance], elttype
: MType): Instance
234 assert not elttype
.need_anchor
235 var nat
= new PrimitiveInstance[Array[Instance]](get_primitive_class
("NativeArray").get_mtype
([elttype
]), values
)
236 init_instance_primitive
(nat
)
237 var mtype
= get_primitive_class
("Array").get_mtype
([elttype
])
238 var res
= new MutableInstance(mtype
)
239 self.init_instance
(res
)
240 self.send
(self.force_get_primitive_method
("with_native", mtype
), [res
, nat
, self.int_instance
(values
.length
)])
244 # Return a instance associated to a primitive class
245 # Current primitive classes are `Int`, `Bool`, and `String`
246 fun value_instance
(object
: Object): Instance
248 if object
isa Int then
249 return int_instance
(object
)
250 else if object
isa Bool then
251 return bool_instance
(object
)
252 else if object
isa String then
253 return string_instance
(object
)
259 # Return a new native string initialized with `txt`
260 fun native_string_instance
(txt
: String): Instance
262 var val
= new FlatBuffer.from
(txt
)
264 var ic
= get_primitive_class
("NativeString")
265 var instance
= new PrimitiveInstance[Buffer](ic
.mclass_type
, val
)
266 init_instance_primitive
(instance
)
270 # Return a new String instance for `txt`
271 fun string_instance
(txt
: String): Instance
273 var nat
= native_string_instance
(txt
)
274 var res
= self.send
(self.force_get_primitive_method
("to_s_with_length", nat
.mtype
), [nat
, self.int_instance
(txt
.length
)])
279 # The current frame used to store local variables of the current method executed
280 fun frame
: Frame do return frames
.first
282 # The stack of all frames. The first one is the current one.
283 var frames
= new List[Frame]
285 # Return a stack trace. One line per function
286 fun stack_trace
: String
288 var b
= new FlatBuffer
289 b
.append
(",---- Stack trace -- - - -\n")
291 b
.append
("| {f.mpropdef} ({f.current_node.location})\n")
293 b
.append
("`------------------- - - -")
297 # The current node, used to print errors, debug and stack-traces
298 fun current_node
: nullable ANode
300 if frames
.is_empty
then return null
301 return frames
.first
.current_node
304 # The dynamic type of the current `self`
305 fun current_receiver_class
: MClassType
307 return frames
.first
.arguments
.first
.mtype
.as(MClassType)
310 # Exit the program with a message
311 fun fatal
(message
: String)
313 var node
= current_node
317 node
.fatal
(self, message
)
322 # Debug on the current node
323 fun debug
(message
: String)
325 var node
= current_node
333 # Retrieve the value of the variable in the current frame
334 fun read_variable
(v
: Variable): Instance
340 # Assign the value of the variable in the current frame
341 fun write_variable
(v
: Variable, value
: Instance)
347 # Store known methods, used to trace methods as they are reached
348 var discover_call_trace
: Set[MMethodDef] = new HashSet[MMethodDef]
350 # Evaluate `args` as expressions in the call of `mpropdef` on `recv`.
351 # This method is used to manage varargs in signatures and returns the real array
352 # of instances to use in the call.
353 # Return `null` if one of the evaluation of the arguments return null.
354 fun varargize
(mpropdef
: MMethodDef, recv
: Instance, args
: SequenceRead[AExpr]): nullable Array[Instance]
356 var msignature
= mpropdef
.new_msignature
or else mpropdef
.msignature
.as(not null)
357 var res
= new Array[Instance]
360 if args
.is_empty
then return res
362 var vararg_rank
= msignature
.vararg_rank
363 var vararg_len
= args
.length
- msignature
.arity
364 if vararg_len
< 0 then vararg_len
= 0
366 for i
in [0..msignature
.arity
[ do
367 if i
== vararg_rank
then
369 if ne
isa AVarargExpr then
370 var e
= self.expr
(ne
.n_expr
)
371 if e
== null then return null
375 var vararg
= new Array[Instance]
376 for j
in [vararg_rank
..vararg_rank
+vararg_len
] do
377 var e
= self.expr
(args
[j
])
378 if e
== null then return null
381 var elttype
= msignature
.mparameters
[vararg_rank
].mtype
.anchor_to
(self.mainmodule
, recv
.mtype
.as(MClassType))
382 res
.add
(self.array_instance
(vararg
, elttype
))
385 if i
> vararg_rank
then j
+= vararg_len
386 var e
= self.expr
(args
[j
])
387 if e
== null then return null
394 # Execute `mpropdef` for a `args` (where `args[0]` is the receiver).
395 # Return a value if `mpropdef` is a function, or null if it is a procedure.
396 # The call is direct/static. There is no message-sending/late-binding.
397 fun call
(mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
399 if self.modelbuilder
.toolcontext
.opt_discover_call_trace
.value
and not self.discover_call_trace
.has
(mpropdef
) then
400 self.discover_call_trace
.add mpropdef
401 self.debug
("Discovered {mpropdef}")
403 assert args
.length
== mpropdef
.msignature
.arity
+ 1 else debug
("Invalid arity for {mpropdef}. {args.length} arguments given.")
405 # Look for the AST node that implements the property
406 var val
= mpropdef
.constant_value
408 var node
= modelbuilder
.mpropdef2node
(mpropdef
)
409 if node
isa APropdef then
410 self.parameter_check
(node
, mpropdef
, args
)
411 return node
.call
(self, mpropdef
, args
)
412 else if node
isa AClassdef then
413 self.parameter_check
(node
, mpropdef
, args
)
414 return node
.call
(self, mpropdef
, args
)
415 else if node
!= null then
416 fatal
("Fatal Error: method {mpropdef} associated to unexpected AST node {node.location}")
418 else if val
!= null then
419 return value_instance
(val
)
421 fatal
("Fatal Error: method {mpropdef} not found in the AST")
426 # Execute type checks of covariant parameters
427 fun parameter_check
(node
: ANode, mpropdef
: MMethodDef, args
: Array[Instance])
429 var msignature
= mpropdef
.msignature
430 for i
in [0..msignature
.arity
[ do
431 # skip test for vararg since the array is instantiated with the correct polymorphic type
432 if msignature
.vararg_rank
== i
then continue
434 # skip if the cast is not required
435 var origmtype
= mpropdef
.mproperty
.intro
.msignature
.mparameters
[i
].mtype
436 if not origmtype
.need_anchor
then continue
438 #print "{mpropdef}: {mpropdef.mproperty.intro.msignature.mparameters[i]}"
440 # get the parameter type
441 var mtype
= msignature
.mparameters
[i
].mtype
442 var anchor
= args
.first
.mtype
.as(MClassType)
443 var amtype
= mtype
.anchor_to
(self.mainmodule
, anchor
)
444 if not args
[i
+1].mtype
.is_subtype
(self.mainmodule
, anchor
, amtype
) then
445 node
.fatal
(self, "Cast failed. Expected `{mtype}`, got `{args[i+1].mtype}`")
450 # Common code for runtime injected calls and normal calls
451 fun send_commons
(mproperty
: MMethod, args
: Array[Instance], mtype
: MType): nullable Instance
453 if mtype
isa MNullType then
454 if mproperty
.name
== "==" then
455 return self.bool_instance
(args
[0] == args
[1])
456 else if mproperty
.name
== "!=" then
457 return self.bool_instance
(args
[0] != args
[1])
459 #fatal("Receiver is null. {mproperty}. {args.join(" ")} {self.frame.current_node.class_name}")
460 fatal
("Receiver is null")
465 # Execute a full `callsite` for given `args`
466 # Use this method, instead of `send` to execute and control the additional behavior of the call-sites
467 fun callsite
(callsite
: nullable CallSite, arguments
: Array[Instance]): nullable Instance
469 var initializers
= callsite
.mpropdef
.initializers
470 if not initializers
.is_empty
then
471 var recv
= arguments
.first
473 for p
in initializers
do
474 if p
isa MMethod then
476 for x
in p
.intro
.msignature
.mparameters
do
477 args
.add arguments
[i
]
481 else if p
isa MAttribute then
482 assert recv
isa MutableInstance
483 write_attribute
(p
, recv
, arguments
[i
])
487 assert i
== arguments
.length
489 return send
(callsite
.mproperty
, [recv
])
491 return send
(callsite
.mproperty
, arguments
)
494 # Execute `mproperty` for a `args` (where `args[0]` is the receiver).
495 # Return a value if `mproperty` is a function, or null if it is a procedure.
496 # The call is polymorphic. There is a message-sending/late-binding according to the receiver (args[0]).
497 fun send
(mproperty
: MMethod, args
: Array[Instance]): nullable Instance
499 var recv
= args
.first
500 var mtype
= recv
.mtype
501 var ret
= send_commons
(mproperty
, args
, mtype
)
502 if ret
!= null then return ret
503 var propdef
= mproperty
.lookup_first_definition
(self.mainmodule
, mtype
)
504 return self.call
(propdef
, args
)
507 # Read the attribute `mproperty` of an instance `recv` and return its value.
508 # If the attribute in not yet initialized, then aborts with an error message.
509 fun read_attribute
(mproperty
: MAttribute, recv
: Instance): Instance
511 assert recv
isa MutableInstance
512 if not recv
.attributes
.has_key
(mproperty
) then
513 fatal
("Uninitialized attribute {mproperty.name}")
516 return recv
.attributes
[mproperty
]
519 # Replace in `recv` the value of the attribute `mproperty` by `value`
520 fun write_attribute
(mproperty
: MAttribute, recv
: Instance, value
: Instance)
522 assert recv
isa MutableInstance
523 recv
.attributes
[mproperty
] = value
526 # Is the attribute `mproperty` initialized the instance `recv`?
527 fun isset_attribute
(mproperty
: MAttribute, recv
: Instance): Bool
529 assert recv
isa MutableInstance
530 return recv
.attributes
.has_key
(mproperty
)
533 # Collect attributes of a type in the order of their init
534 fun collect_attr_propdef
(mtype
: MType): Array[AAttrPropdef]
536 var cache
= self.collect_attr_propdef_cache
537 if cache
.has_key
(mtype
) then return cache
[mtype
]
539 var res
= new Array[AAttrPropdef]
540 var cds
= mtype
.collect_mclassdefs
(self.mainmodule
).to_a
541 self.mainmodule
.linearize_mclassdefs
(cds
)
543 res
.add_all
(modelbuilder
.collect_attr_propdef
(cd
))
550 private var collect_attr_propdef_cache
= new HashMap[MType, Array[AAttrPropdef]]
552 # Fill the initial values of the newly created instance `recv`.
553 # `recv.mtype` is used to know what must be filled.
554 fun init_instance
(recv
: Instance)
556 for npropdef
in collect_attr_propdef
(recv
.mtype
) do
557 npropdef
.init_expr
(self, recv
)
561 # A hook to initialize a `PrimitiveInstance`
562 fun init_instance_primitive
(recv
: Instance) do end
564 # Return the primitive `MClass` corresponding to the `name` given in parameter
565 # `name` : name of the primitive class
566 fun get_primitive_class
(name
: String): MClass
568 return mainmodule
.get_primitive_class
(name
)
571 # This function determines the correct type according to the receiver of the current propdef (self).
572 fun unanchor_type
(mtype
: MType): MType
574 return mtype
.anchor_to
(self.mainmodule
, current_receiver_class
)
577 # Placebo instance used to mark internal error result when `null` already have a meaning.
578 # TODO: replace with multiple return or something better
579 var error_instance
= new MutableInstance(modelbuilder
.model
.null_type
) is lazy
582 # An instance represents a value of the executed program.
583 abstract class Instance
584 # The dynamic type of the instance
585 # ASSERT: not self.mtype.is_anchored
588 # return true if the instance is the true value.
589 # return false if the instance is the true value.
591 fun is_true
: Bool do abort
593 # Return true if `self` IS `o` (using the Nit semantic of is)
594 fun eq_is
(o
: Instance): Bool do return self.is_same_instance
(o
)
596 # Human readable object identity "Type#number"
597 redef fun to_s
do return "{mtype}"
599 # Return the integer value if the instance is an integer.
601 fun to_i
: Int do abort
603 # Return the integer value if the instance is a float.
605 fun to_f
: Float do abort
607 # The real value encapsulated if the instance is primitive.
609 fun val
: nullable Object do abort
612 # A instance with attribute (standards objects)
613 class MutableInstance
616 # The values of the attributes
617 var attributes
: Map[MAttribute, Instance] = new HashMap[MAttribute, Instance]
620 # Special instance to handle primitives values (int, bool, etc.)
621 # The trick it just to encapsulate the <<real>> value
622 class PrimitiveInstance[E
]
625 # The real value encapsulated
630 if val
== true then return true
631 if val
== false then return false
637 if not o
isa PrimitiveInstance[nullable Object] then return false
638 return self.val
== o
.val
643 if not o
isa PrimitiveInstance[nullable Object] then return false
644 return self.val
.is_same_instance
(o
.val
)
647 redef fun to_s
do return "{mtype}#{val.object_id}({val or else "null"})"
649 redef fun to_i
do return val
.as(Int)
651 redef fun to_f
do return val
.as(Float)
654 # Information about local variables in a running method
656 # The current visited node
657 # The node is stored by frame to keep a stack trace
658 var current_node
: ANode
659 # The executed property.
660 # A Method in case of a call, an attribute in case of a default initialization.
661 var mpropdef
: MPropDef
662 # Arguments of the method (the first is the receiver)
663 var arguments
: Array[Instance]
664 # Mapping between a variable and the current value
665 private var map
: Map[Variable, Instance] = new HashMap[Variable, Instance]
669 # Aborts the program with a message
670 # `v` is used to know if a colored message is displayed or not
671 fun fatal
(v
: NaiveInterpreter, message
: String)
673 if v
.modelbuilder
.toolcontext
.opt_no_color
.value
== true then
674 sys
.stderr
.write
("Runtime error: {message} ({location.file.filename}:{location.line_start})\n")
676 sys
.stderr
.write
("{location}: Runtime error: {message}\n{location.colored_line("0;31")}\n")
677 sys
.stderr
.write
(v
.stack_trace
)
678 sys
.stderr
.write
("\n")
685 # Execute a `mpropdef` associated with the current node.
686 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
688 fatal
(v
, "NOT YET IMPLEMENTED method kind {class_name}. {mpropdef}")
693 redef class AMethPropdef
696 redef fun call
(v
, mpropdef
, args
)
698 var f
= new Frame(self, self.mpropdef
.as(not null), args
)
699 var res
= call_commons
(v
, mpropdef
, args
, f
)
701 if v
.returnmark
== f
then
710 private fun call_commons
(v
: NaiveInterpreter, mpropdef
: MMethodDef, arguments
: Array[Instance], f
: Frame): nullable Instance
714 for i
in [0..mpropdef
.msignature
.arity
[ do
715 var variable
= self.n_signature
.n_params
[i
].variable
716 assert variable
!= null
717 v
.write_variable
(variable
, arguments
[i
+1])
720 if mpropdef
.is_abstract
then
721 v
.fatal
("Abstract method `{mpropdef.mproperty.name}` called on `{arguments.first.mtype}`")
725 # Call the implicit super-init
726 var auto_super_inits
= self.auto_super_inits
727 if auto_super_inits
!= null then
728 var args
= [arguments
.first
]
729 for auto_super_init
in auto_super_inits
do
731 for i
in [0..auto_super_init
.msignature
.arity
+1[ do
732 args
.add
(arguments
[i
])
734 assert auto_super_init
.mproperty
!= mpropdef
.mproperty
735 v
.callsite
(auto_super_init
, args
)
738 if auto_super_call
then
739 # standard call-next-method
740 var superpd
= mpropdef
.lookup_next_definition
(v
.mainmodule
, arguments
.first
.mtype
)
741 v
.call
(superpd
, arguments
)
744 if mpropdef
.is_intern
or mpropdef
.is_extern
then
745 var res
= intern_call
(v
, mpropdef
, arguments
)
746 if res
!= v
.error_instance
then return res
749 if n_block
!= null then
754 if mpropdef
.is_intern
then
755 fatal
(v
, "NOT YET IMPLEMENTED intern {mpropdef}")
756 else if mpropdef
.is_extern
then
757 fatal
(v
, "NOT YET IMPLEMENTED extern {mpropdef}")
759 fatal
(v
, "NOT YET IMPLEMENTED <wat?> {mpropdef}")
764 # Interprets a intern or a shortcut extern method.
765 # Returns the result for a function, `null` for a procedure, or `error_instance` if the method is unknown.
766 private fun intern_call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
768 var pname
= mpropdef
.mproperty
.name
769 var cname
= mpropdef
.mclassdef
.mclass
.name
770 if pname
== "output" then
771 var recv
= args
.first
774 else if pname
== "object_id" then
775 var recv
= args
.first
776 if recv
isa PrimitiveInstance[Object] then
777 return v
.int_instance
(recv
.val
.object_id
)
779 return v
.int_instance
(recv
.object_id
)
781 else if pname
== "output_class_name" then
782 var recv
= args
.first
785 else if pname
== "native_class_name" then
786 var recv
= args
.first
787 var txt
= recv
.mtype
.to_s
788 return v
.native_string_instance
(txt
)
789 else if pname
== "==" then
790 # == is correctly redefined for instances
791 return v
.bool_instance
(args
[0] == args
[1])
792 else if pname
== "!=" then
793 return v
.bool_instance
(args
[0] != args
[1])
794 else if pname
== "is_same_type" then
795 return v
.bool_instance
(args
[0].mtype
== args
[1].mtype
)
796 else if pname
== "is_same_instance" then
797 return v
.bool_instance
(args
[0].eq_is
(args
[1]))
798 else if pname
== "exit" then
801 else if pname
== "sys" then
803 else if cname
== "Int" then
804 var recvval
= args
[0].to_i
805 if pname
== "unary -" then
806 return v
.int_instance
(-args
[0].to_i
)
807 else if pname
== "+" then
808 return v
.int_instance
(args
[0].to_i
+ args
[1].to_i
)
809 else if pname
== "-" then
810 return v
.int_instance
(args
[0].to_i
- args
[1].to_i
)
811 else if pname
== "*" then
812 return v
.int_instance
(args
[0].to_i
* args
[1].to_i
)
813 else if pname
== "%" then
814 return v
.int_instance
(args
[0].to_i
% args
[1].to_i
)
815 else if pname
== "/" then
816 return v
.int_instance
(args
[0].to_i
/ args
[1].to_i
)
817 else if pname
== "<" then
818 return v
.bool_instance
(args
[0].to_i
< args
[1].to_i
)
819 else if pname
== ">" then
820 return v
.bool_instance
(args
[0].to_i
> args
[1].to_i
)
821 else if pname
== "<=" then
822 return v
.bool_instance
(args
[0].to_i
<= args
[1].to_i
)
823 else if pname
== ">=" then
824 return v
.bool_instance
(args
[0].to_i
>= args
[1].to_i
)
825 else if pname
== "<=>" then
826 return v
.int_instance
(args
[0].to_i
<=> args
[1].to_i
)
827 else if pname
== "ascii" then
828 return v
.char_instance
(args
[0].to_i
.ascii
)
829 else if pname
== "to_f" then
830 return v
.float_instance
(args
[0].to_i
.to_f
)
831 else if pname
== "lshift" then
832 return v
.int_instance
(args
[0].to_i
.lshift
(args
[1].to_i
))
833 else if pname
== "rshift" then
834 return v
.int_instance
(args
[0].to_i
.rshift
(args
[1].to_i
))
835 else if pname
== "rand" then
836 var res
= recvval
.rand
837 return v
.int_instance
(res
)
838 else if pname
== "bin_and" then
839 return v
.int_instance
(args
[0].to_i
.bin_and
(args
[1].to_i
))
840 else if pname
== "bin_or" then
841 return v
.int_instance
(args
[0].to_i
.bin_or
(args
[1].to_i
))
842 else if pname
== "bin_xor" then
843 return v
.int_instance
(args
[0].to_i
.bin_xor
(args
[1].to_i
))
844 else if pname
== "bin_not" then
845 return v
.int_instance
(args
[0].to_i
.bin_not
)
846 else if pname
== "native_int_to_s" then
847 return v
.native_string_instance
(recvval
.to_s
)
848 else if pname
== "strerror_ext" then
849 return v
.native_string_instance
(recvval
.strerror
)
851 else if cname
== "Char" then
852 var recv
= args
[0].val
.as(Char)
853 if pname
== "ascii" then
854 return v
.int_instance
(recv
.ascii
)
855 else if pname
== "successor" then
856 return v
.char_instance
(recv
.successor
(args
[1].to_i
))
857 else if pname
== "predecessor" then
858 return v
.char_instance
(recv
.predecessor
(args
[1].to_i
))
859 else if pname
== "<" then
860 return v
.bool_instance
(recv
< args
[1].val
.as(Char))
861 else if pname
== ">" then
862 return v
.bool_instance
(recv
> args
[1].val
.as(Char))
863 else if pname
== "<=" then
864 return v
.bool_instance
(recv
<= args
[1].val
.as(Char))
865 else if pname
== ">=" then
866 return v
.bool_instance
(recv
>= args
[1].val
.as(Char))
867 else if pname
== "<=>" then
868 return v
.int_instance
(recv
<=> args
[1].val
.as(Char))
870 else if cname
== "Float" then
871 var recv
= args
[0].to_f
872 if pname
== "unary -" then
873 return v
.float_instance
(-recv
)
874 else if pname
== "+" then
875 return v
.float_instance
(recv
+ args
[1].to_f
)
876 else if pname
== "-" then
877 return v
.float_instance
(recv
- args
[1].to_f
)
878 else if pname
== "*" then
879 return v
.float_instance
(recv
* args
[1].to_f
)
880 else if pname
== "/" then
881 return v
.float_instance
(recv
/ args
[1].to_f
)
882 else if pname
== "<" then
883 return v
.bool_instance
(recv
< args
[1].to_f
)
884 else if pname
== ">" then
885 return v
.bool_instance
(recv
> args
[1].to_f
)
886 else if pname
== "<=" then
887 return v
.bool_instance
(recv
<= args
[1].to_f
)
888 else if pname
== ">=" then
889 return v
.bool_instance
(recv
>= args
[1].to_f
)
890 else if pname
== "to_i" then
891 return v
.int_instance
(recv
.to_i
)
892 else if pname
== "cos" then
893 return v
.float_instance
(args
[0].to_f
.cos
)
894 else if pname
== "sin" then
895 return v
.float_instance
(args
[0].to_f
.sin
)
896 else if pname
== "tan" then
897 return v
.float_instance
(args
[0].to_f
.tan
)
898 else if pname
== "acos" then
899 return v
.float_instance
(args
[0].to_f
.acos
)
900 else if pname
== "asin" then
901 return v
.float_instance
(args
[0].to_f
.asin
)
902 else if pname
== "atan" then
903 return v
.float_instance
(args
[0].to_f
.atan
)
904 else if pname
== "sqrt" then
905 return v
.float_instance
(args
[0].to_f
.sqrt
)
906 else if pname
== "exp" then
907 return v
.float_instance
(args
[0].to_f
.exp
)
908 else if pname
== "log" then
909 return v
.float_instance
(args
[0].to_f
.log
)
910 else if pname
== "pow" then
911 return v
.float_instance
(args
[0].to_f
.pow
(args
[1].to_f
))
912 else if pname
== "rand" then
913 return v
.float_instance
(args
[0].to_f
.rand
)
914 else if pname
== "abs" then
915 return v
.float_instance
(args
[0].to_f
.abs
)
916 else if pname
== "hypot_with" then
917 return v
.float_instance
(args
[0].to_f
.hypot_with
(args
[1].to_f
))
918 else if pname
== "is_nan" then
919 return v
.bool_instance
(args
[0].to_f
.is_nan
)
920 else if pname
== "is_inf_extern" then
921 return v
.bool_instance
(args
[0].to_f
.is_inf
!= 0)
922 else if pname
== "round" then
923 return v
.float_instance
(args
[0].to_f
.round
)
925 else if cname
== "NativeString" then
926 if pname
== "new" then
927 return v
.native_string_instance
("!" * args
[1].to_i
)
929 var recvval
= args
.first
.val
.as(Buffer)
930 if pname
== "[]" then
931 var arg1
= args
[1].to_i
932 if arg1
>= recvval
.length
or arg1
< 0 then
933 debug
("Illegal access on {recvval} for element {arg1}/{recvval.length}")
935 return v
.char_instance
(recvval
.chars
[arg1
])
936 else if pname
== "[]=" then
937 var arg1
= args
[1].to_i
938 if arg1
>= recvval
.length
or arg1
< 0 then
939 debug
("Illegal access on {recvval} for element {arg1}/{recvval.length}")
941 recvval
.chars
[arg1
] = args
[2].val
.as(Char)
943 else if pname
== "copy_to" then
944 # sig= copy_to(dest: NativeString, length: Int, from: Int, to: Int)
945 var destval
= args
[1].val
.as(FlatBuffer)
946 var lenval
= args
[2].to_i
947 var fromval
= args
[3].to_i
948 var toval
= args
[4].to_i
950 debug
("Illegal access on {recvval} for element {fromval}/{recvval.length}")
952 if fromval
+ lenval
>= recvval
.length
then
953 debug
("Illegal access on {recvval} for element {fromval}+{lenval}/{recvval.length}")
956 debug
("Illegal access on {destval} for element {toval}/{destval.length}")
958 if toval
+ lenval
>= destval
.length
then
959 debug
("Illegal access on {destval} for element {toval}+{lenval}/{destval.length}")
961 recvval
.as(FlatBuffer).copy
(fromval
, lenval
, destval
, toval
)
963 else if pname
== "atoi" then
964 return v
.int_instance
(recvval
.to_i
)
965 else if pname
== "file_exists" then
966 return v
.bool_instance
(recvval
.to_s
.file_exists
)
967 else if pname
== "file_mkdir" then
970 else if pname
== "file_chdir" then
973 else if pname
== "file_realpath" then
974 return v
.native_string_instance
(recvval
.to_s
.realpath
)
975 else if pname
== "get_environ" then
976 var txt
= recvval
.to_s
.environ
977 return v
.native_string_instance
(txt
)
978 else if pname
== "system" then
979 var res
= sys
.system
(recvval
.to_s
)
980 return v
.int_instance
(res
)
981 else if pname
== "atof" then
982 return v
.float_instance
(recvval
.to_f
)
984 else if cname
== "String" then
985 var cs
= v
.send
(v
.force_get_primitive_method
("to_cstring", args
.first
.mtype
), [args
.first
])
986 var str
= cs
.val
.to_s
987 if pname
== "files" then
988 var res
= new Array[Instance]
989 for f
in str
.files
do res
.add v
.string_instance
(f
)
990 return v
.array_instance
(res
, v
.get_primitive_class
("String").mclass_type
)
992 else if pname
== "calloc_string" then
993 return v
.native_string_instance
("!" * args
[1].to_i
)
994 else if cname
== "NativeArray" then
995 if pname
== "new" then
996 var val
= new Array[Instance].filled_with
(v
.null_instance
, args
[1].to_i
)
997 var instance
= new PrimitiveInstance[Array[Instance]](args
[0].mtype
, val
)
998 v
.init_instance_primitive
(instance
)
1001 var recvval
= args
.first
.val
.as(Array[Instance])
1002 if pname
== "[]" then
1003 if args
[1].to_i
>= recvval
.length
or args
[1].to_i
< 0 then
1004 debug
("Illegal access on {recvval} for element {args[1].to_i}/{recvval.length}")
1006 return recvval
[args
[1].to_i
]
1007 else if pname
== "[]=" then
1008 recvval
[args
[1].to_i
] = args
[2]
1010 else if pname
== "length" then
1011 return v
.int_instance
(recvval
.length
)
1012 else if pname
== "copy_to" then
1013 recvval
.copy_to
(0, args
[2].to_i
, args
[1].val
.as(Array[Instance]), 0)
1016 else if cname
== "NativeFile" then
1017 if pname
== "native_stdout" then
1018 var instance
= new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, sys
.stdout
)
1019 v
.init_instance_primitive
(instance
)
1021 else if pname
== "native_stdin" then
1022 var instance
= new PrimitiveInstance[IStream](mpropdef
.mclassdef
.mclass
.mclass_type
, sys
.stdin
)
1023 v
.init_instance_primitive
(instance
)
1025 else if pname
== "native_stderr" then
1026 var instance
= new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, sys
.stderr
)
1027 v
.init_instance_primitive
(instance
)
1029 else if pname
== "io_open_read" then
1030 var a1
= args
[1].val
.as(Buffer)
1031 var instance
= new PrimitiveInstance[IStream](mpropdef
.mclassdef
.mclass
.mclass_type
, new IFStream.open
(a1
.to_s
))
1032 v
.init_instance_primitive
(instance
)
1034 else if pname
== "io_open_write" then
1035 var a1
= args
[1].val
.as(Buffer)
1036 var instance
= new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, new OFStream.open
(a1
.to_s
))
1037 v
.init_instance_primitive
(instance
)
1040 var recvval
= args
.first
.val
1041 if pname
== "io_write" then
1042 var a1
= args
[1].val
.as(Buffer)
1043 recvval
.as(OStream).write
(a1
.substring
(0, args
[2].to_i
).to_s
)
1045 else if pname
== "io_read" then
1046 var str
= recvval
.as(IStream).read
(args
[2].to_i
)
1047 var a1
= args
[1].val
.as(Buffer)
1048 new FlatBuffer.from
(str
).copy
(0, str
.length
, a1
.as(FlatBuffer), 0)
1049 return v
.int_instance
(str
.length
)
1050 else if pname
== "io_close" then
1051 recvval
.as(IOS).close
1052 return v
.int_instance
(0)
1053 else if pname
== "address_is_null" then
1054 return v
.false_instance
1056 else if pname
== "calloc_array" then
1057 var recvtype
= args
.first
.mtype
.as(MClassType)
1059 mtype
= recvtype
.supertype_to
(v
.mainmodule
, recvtype
, v
.get_primitive_class
("ArrayCapable"))
1060 mtype
= mtype
.arguments
.first
1061 var val
= new Array[Instance].filled_with
(v
.null_instance
, args
[1].to_i
)
1062 var instance
= new PrimitiveInstance[Array[Instance]](v
.get_primitive_class
("NativeArray").get_mtype
([mtype
]), val
)
1063 v
.init_instance_primitive
(instance
)
1065 else if pname
== "native_argc" then
1066 return v
.int_instance
(v
.arguments
.length
)
1067 else if pname
== "native_argv" then
1068 var txt
= v
.arguments
[args
[1].to_i
]
1069 return v
.native_string_instance
(txt
)
1070 else if pname
== "native_argc" then
1071 return v
.int_instance
(v
.arguments
.length
)
1072 else if pname
== "native_argv" then
1073 var txt
= v
.arguments
[args
[1].to_i
]
1074 return v
.native_string_instance
(txt
)
1075 else if pname
== "get_time" then
1076 return v
.int_instance
(get_time
)
1077 else if pname
== "srand" then
1080 else if pname
== "srand_from" then
1081 srand_from
(args
[1].to_i
)
1083 else if pname
== "atan2" then
1084 return v
.float_instance
(atan2
(args
[1].to_f
, args
[2].to_f
))
1085 else if pname
== "pi" then
1086 return v
.float_instance
(pi
)
1087 else if pname
== "lexer_goto" then
1088 return v
.int_instance
(lexer_goto
(args
[1].to_i
, args
[2].to_i
))
1089 else if pname
== "lexer_accept" then
1090 return v
.int_instance
(lexer_accept
(args
[1].to_i
))
1091 else if pname
== "parser_goto" then
1092 return v
.int_instance
(parser_goto
(args
[1].to_i
, args
[2].to_i
))
1093 else if pname
== "parser_action" then
1094 return v
.int_instance
(parser_action
(args
[1].to_i
, args
[2].to_i
))
1095 else if pname
== "file_getcwd" then
1096 return v
.native_string_instance
(getcwd
)
1097 else if pname
== "errno" then
1098 return v
.int_instance
(sys
.errno
)
1099 else if pname
== "address_is_null" then
1100 return v
.false_instance
1102 return v
.error_instance
1106 redef class AAttrPropdef
1107 redef fun call
(v
, mpropdef
, args
)
1109 var recv
= args
.first
1110 assert recv
isa MutableInstance
1111 var attr
= self.mpropdef
.mproperty
1112 if mpropdef
== mreadpropdef
then
1113 assert args
.length
== 1
1114 if not is_lazy
or v
.isset_attribute
(attr
, recv
) then return v
.read_attribute
(attr
, recv
)
1115 return evaluate_expr
(v
, recv
)
1116 else if mpropdef
== mwritepropdef
then
1117 assert args
.length
== 2
1118 v
.write_attribute
(attr
, recv
, args
[1])
1125 # Evaluate and set the default value of the attribute in `recv`
1126 private fun init_expr
(v
: NaiveInterpreter, recv
: Instance)
1128 if is_lazy
then return
1130 evaluate_expr
(v
, recv
)
1133 var mtype
= self.mpropdef
.static_mtype
.as(not null)
1134 mtype
= mtype
.anchor_to
(v
.mainmodule
, recv
.mtype
.as(MClassType))
1135 if mtype
isa MNullableType then
1136 v
.write_attribute
(self.mpropdef
.mproperty
, recv
, v
.null_instance
)
1140 private fun evaluate_expr
(v
: NaiveInterpreter, recv
: Instance): Instance
1142 assert recv
isa MutableInstance
1143 var f
= new Frame(self, self.mpropdef
.as(not null), [recv
])
1148 var nexpr
= self.n_expr
1149 var nblock
= self.n_block
1150 if nexpr
!= null then
1152 else if nblock
!= null then
1154 assert v
.returnmark
== f
1157 v
.escapevalue
= null
1164 assert not v
.is_escaping
1165 v
.write_attribute
(self.mpropdef
.mproperty
, recv
, val
)
1170 redef class AClassdef
1171 # Execute an implicit `mpropdef` associated with the current node.
1172 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
1174 if mpropdef
.mproperty
.is_root_init
then
1175 assert args
.length
== 1
1176 if not mpropdef
.is_intro
then
1177 # standard call-next-method
1178 var superpd
= mpropdef
.lookup_next_definition
(v
.mainmodule
, args
.first
.mtype
)
1179 v
.call
(superpd
, args
)
1189 # Evaluate the node as a possible expression.
1190 # Return a possible value
1191 # NOTE: Do not call this method directly, but use `v.expr`
1192 # This method is here to be implemented by subclasses.
1193 private fun expr
(v
: NaiveInterpreter): nullable Instance
1195 fatal
(v
, "NOT YET IMPLEMENTED expr {class_name}")
1199 # Evaluate the node as a statement.
1200 # NOTE: Do not call this method directly, but use `v.stmt`
1201 # This method is here to be implemented by subclasses (no need to return something).
1202 private fun stmt
(v
: NaiveInterpreter)
1209 redef class ABlockExpr
1212 var last
= self.n_expr
.last
1213 for e
in self.n_expr
do
1214 if e
== last
then break
1216 if v
.is_escaping
then return null
1223 for e
in self.n_expr
do
1225 if v
.is_escaping
then return
1230 redef class AVardeclExpr
1233 var ne
= self.n_expr
1236 if i
== null then return
1237 v
.write_variable
(self.variable
.as(not null), i
)
1242 redef class AVarExpr
1245 return v
.read_variable
(self.variable
.as(not null))
1249 redef class AVarAssignExpr
1252 var i
= v
.expr
(self.n_value
)
1253 if i
== null then return null
1254 v
.write_variable
(self.variable
.as(not null), i
)
1259 redef class AVarReassignExpr
1262 var variable
= self.variable
.as(not null)
1263 var vari
= v
.read_variable
(variable
)
1264 var value
= v
.expr
(self.n_value
)
1265 if value
== null then return
1266 var res
= v
.callsite
(reassign_callsite
, [vari
, value
])
1268 v
.write_variable
(variable
, res
)
1272 redef class ASelfExpr
1275 return v
.frame
.arguments
.first
1279 redef class AEscapeExpr
1282 var ne
= self.n_expr
1285 if i
== null then return
1288 v
.escapemark
= self.escapemark
1292 redef class AReturnExpr
1295 var ne
= self.n_expr
1298 if i
== null then return
1301 v
.returnmark
= v
.frame
1305 redef class AAbortExpr
1316 var cond
= v
.expr
(self.n_expr
)
1317 if cond
== null then return null
1318 if cond
.is_true
then
1319 return v
.expr
(self.n_then
.as(not null))
1321 return v
.expr
(self.n_else
.as(not null))
1327 var cond
= v
.expr
(self.n_expr
)
1328 if cond
== null then return
1329 if cond
.is_true
then
1337 redef class AIfexprExpr
1340 var cond
= v
.expr
(self.n_expr
)
1341 if cond
== null then return null
1342 if cond
.is_true
then
1343 return v
.expr
(self.n_then
)
1345 return v
.expr
(self.n_else
)
1353 v
.stmt
(self.n_block
)
1354 v
.is_escape
(self.break_mark
) # Clear the break (if any)
1358 redef class AWhileExpr
1362 var cond
= v
.expr
(self.n_expr
)
1363 if cond
== null then return
1364 if not cond
.is_true
then return
1365 v
.stmt
(self.n_block
)
1366 if v
.is_escape
(self.break_mark
) then return
1367 v
.is_escape
(self.continue_mark
) # Clear the break
1368 if v
.is_escaping
then return
1373 redef class ALoopExpr
1377 v
.stmt
(self.n_block
)
1378 if v
.is_escape
(self.break_mark
) then return
1379 v
.is_escape
(self.continue_mark
) # Clear the break
1380 if v
.is_escaping
then return
1385 redef class AForExpr
1388 var col
= v
.expr
(self.n_expr
)
1389 if col
== null then return
1390 if col
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1392 #self.debug("col {col}")
1393 var iter
= v
.callsite
(method_iterator
, [col
]).as(not null)
1394 #self.debug("iter {iter}")
1396 var isok
= v
.callsite
(method_is_ok
, [iter
]).as(not null)
1397 if not isok
.is_true
then break
1398 if self.variables
.length
== 1 then
1399 var item
= v
.callsite
(method_item
, [iter
]).as(not null)
1400 #self.debug("item {item}")
1401 v
.write_variable
(self.variables
.first
, item
)
1402 else if self.variables
.length
== 2 then
1403 var key
= v
.callsite
(method_key
, [iter
]).as(not null)
1404 v
.write_variable
(self.variables
[0], key
)
1405 var item
= v
.callsite
(method_item
, [iter
]).as(not null)
1406 v
.write_variable
(self.variables
[1], item
)
1410 v
.stmt
(self.n_block
)
1411 if v
.is_escape
(self.break_mark
) then break
1412 v
.is_escape
(self.continue_mark
) # Clear the break
1413 if v
.is_escaping
then break
1414 v
.callsite
(method_next
, [iter
])
1416 var method_finish
= self.method_finish
1417 if method_finish
!= null then
1418 v
.callsite
(method_finish
, [iter
])
1423 redef class AAssertExpr
1426 var cond
= v
.expr
(self.n_expr
)
1427 if cond
== null then return
1428 if not cond
.is_true
then
1430 if v
.is_escaping
then return
1433 fatal
(v
, "Assert '{nid.text}' failed")
1435 fatal
(v
, "Assert failed")
1445 var cond
= v
.expr
(self.n_expr
)
1446 if cond
== null then return null
1447 if cond
.is_true
then return cond
1448 return v
.expr
(self.n_expr2
)
1452 redef class AImpliesExpr
1455 var cond
= v
.expr
(self.n_expr
)
1456 if cond
== null then return null
1457 if not cond
.is_true
then return v
.true_instance
1458 return v
.expr
(self.n_expr2
)
1462 redef class AAndExpr
1465 var cond
= v
.expr
(self.n_expr
)
1466 if cond
== null then return null
1467 if not cond
.is_true
then return cond
1468 return v
.expr
(self.n_expr2
)
1472 redef class ANotExpr
1475 var cond
= v
.expr
(self.n_expr
)
1476 if cond
== null then return null
1477 return v
.bool_instance
(not cond
.is_true
)
1481 redef class AOrElseExpr
1484 var i
= v
.expr
(self.n_expr
)
1485 if i
== null then return null
1486 if i
!= v
.null_instance
then return i
1487 return v
.expr
(self.n_expr2
)
1491 redef class AIntExpr
1494 return v
.int_instance
(self.value
.as(not null))
1498 redef class AFloatExpr
1501 return v
.float_instance
(self.value
.as(not null))
1505 redef class ACharExpr
1508 return v
.char_instance
(self.value
.as(not null))
1512 redef class AArrayExpr
1515 var val
= new Array[Instance]
1516 for nexpr
in self.n_exprs
.n_exprs
do
1517 var i
= v
.expr
(nexpr
)
1518 if i
== null then return null
1521 var mtype
= v
.unanchor_type
(self.mtype
.as(not null)).as(MClassType)
1522 var elttype
= mtype
.arguments
.first
1523 return v
.array_instance
(val
, elttype
)
1527 redef class AStringFormExpr
1530 var txt
= self.value
.as(not null)
1531 return v
.string_instance
(txt
)
1535 redef class ASuperstringExpr
1538 var array
= new Array[Instance]
1539 for nexpr
in n_exprs
do
1540 var i
= v
.expr
(nexpr
)
1541 if i
== null then return null
1544 var i
= v
.array_instance
(array
, v
.get_primitive_class
("Object").mclass_type
)
1545 var res
= v
.send
(v
.force_get_primitive_method
("to_s", i
.mtype
), [i
])
1551 redef class ACrangeExpr
1554 var e1
= v
.expr
(self.n_expr
)
1555 if e1
== null then return null
1556 var e2
= v
.expr
(self.n_expr2
)
1557 if e2
== null then return null
1558 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1559 var res
= new MutableInstance(mtype
)
1560 v
.init_instance
(res
)
1561 v
.callsite
(init_callsite
, [res
, e1
, e2
])
1566 redef class AOrangeExpr
1569 var e1
= v
.expr
(self.n_expr
)
1570 if e1
== null then return null
1571 var e2
= v
.expr
(self.n_expr2
)
1572 if e2
== null then return null
1573 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1574 var res
= new MutableInstance(mtype
)
1575 v
.init_instance
(res
)
1576 v
.callsite
(init_callsite
, [res
, e1
, e2
])
1581 redef class ATrueExpr
1584 return v
.bool_instance
(true)
1588 redef class AFalseExpr
1591 return v
.bool_instance
(false)
1595 redef class ANullExpr
1598 return v
.null_instance
1602 redef class AIsaExpr
1605 var i
= v
.expr
(self.n_expr
)
1606 if i
== null then return null
1607 var mtype
= v
.unanchor_type
(self.cast_type
.as(not null))
1608 return v
.bool_instance
(v
.is_subtype
(i
.mtype
, mtype
))
1612 redef class AAsCastExpr
1615 var i
= v
.expr
(self.n_expr
)
1616 if i
== null then return null
1617 var mtype
= self.mtype
.as(not null)
1618 var amtype
= v
.unanchor_type
(mtype
)
1619 if not v
.is_subtype
(i
.mtype
, amtype
) then
1620 fatal
(v
, "Cast failed. Expected `{amtype}`, got `{i.mtype}`")
1626 redef class AAsNotnullExpr
1629 var i
= v
.expr
(self.n_expr
)
1630 if i
== null then return null
1631 if i
.mtype
isa MNullType then
1632 fatal
(v
, "Cast failed")
1638 redef class AParExpr
1641 return v
.expr
(self.n_expr
)
1645 redef class AOnceExpr
1648 if v
.onces
.has_key
(self) then
1649 return v
.onces
[self]
1651 var res
= v
.expr
(self.n_expr
)
1652 if res
== null then return null
1659 redef class ASendExpr
1662 var recv
= v
.expr
(self.n_expr
)
1663 if recv
== null then return null
1664 var args
= v
.varargize
(callsite
.mpropdef
, recv
, self.raw_arguments
)
1665 if args
== null then return null
1667 var res
= v
.callsite
(callsite
, args
)
1672 redef class ASendReassignFormExpr
1675 var recv
= v
.expr
(self.n_expr
)
1676 if recv
== null then return
1677 var args
= v
.varargize
(callsite
.mpropdef
, recv
, self.raw_arguments
)
1678 if args
== null then return
1679 var value
= v
.expr
(self.n_value
)
1680 if value
== null then return
1682 var read
= v
.callsite
(callsite
, args
)
1685 var write
= v
.callsite
(reassign_callsite
, [read
, value
])
1686 assert write
!= null
1690 v
.callsite
(write_callsite
, args
)
1694 redef class ASuperExpr
1697 var recv
= v
.frame
.arguments
.first
1699 var callsite
= self.callsite
1700 if callsite
!= null then
1701 var args
= v
.varargize
(callsite
.mpropdef
, recv
, self.n_args
.n_exprs
)
1702 if args
== null then return null
1703 # Add additional arguments for the super init call
1704 if args
.length
== 1 then
1705 for i
in [0..callsite
.msignature
.arity
[ do
1706 args
.add
(v
.frame
.arguments
[i
+1])
1710 var res
= v
.callsite
(callsite
, args
)
1714 # standard call-next-method
1715 var mpropdef
= self.mpropdef
1716 mpropdef
= mpropdef
.lookup_next_definition
(v
.mainmodule
, recv
.mtype
)
1718 var args
= v
.varargize
(mpropdef
, recv
, self.n_args
.n_exprs
)
1719 if args
== null then return null
1721 if args
.length
== 1 then
1722 args
= v
.frame
.arguments
1724 var res
= v
.call
(mpropdef
, args
)
1729 redef class ANewExpr
1732 var mtype
= v
.unanchor_type
(self.recvtype
.as(not null))
1733 var recv
: Instance = new MutableInstance(mtype
)
1734 v
.init_instance
(recv
)
1735 var args
= v
.varargize
(callsite
.mpropdef
, recv
, self.n_args
.n_exprs
)
1736 if args
== null then return null
1737 var res2
= v
.callsite
(callsite
, args
)
1738 if res2
!= null then
1739 #self.debug("got {res2} from {mproperty}. drop {recv}")
1746 redef class AAttrExpr
1749 var recv
= v
.expr
(self.n_expr
)
1750 if recv
== null then return null
1751 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1752 var mproperty
= self.mproperty
.as(not null)
1753 return v
.read_attribute
(mproperty
, recv
)
1757 redef class AAttrAssignExpr
1760 var recv
= v
.expr
(self.n_expr
)
1761 if recv
== null then return
1762 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1763 var i
= v
.expr
(self.n_value
)
1764 if i
== null then return
1765 var mproperty
= self.mproperty
.as(not null)
1766 v
.write_attribute
(mproperty
, recv
, i
)
1770 redef class AAttrReassignExpr
1773 var recv
= v
.expr
(self.n_expr
)
1774 if recv
== null then return
1775 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1776 var value
= v
.expr
(self.n_value
)
1777 if value
== null then return
1778 var mproperty
= self.mproperty
.as(not null)
1779 var attr
= v
.read_attribute
(mproperty
, recv
)
1780 var res
= v
.callsite
(reassign_callsite
, [attr
, value
])
1782 v
.write_attribute
(mproperty
, recv
, res
)
1786 redef class AIssetAttrExpr
1789 var recv
= v
.expr
(self.n_expr
)
1790 if recv
== null then return null
1791 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1792 var mproperty
= self.mproperty
.as(not null)
1793 return v
.bool_instance
(v
.isset_attribute
(mproperty
, recv
))
1797 redef class ADebugTypeExpr