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 byte instance associated with `val`.
211 fun byte_instance
(val
: Byte): Instance
213 var t
= mainmodule
.byte_type
214 var instance
= new PrimitiveInstance[Byte](t
, val
)
215 init_instance_primitive
(instance
)
219 # Return the char instance associated with `val`.
220 fun char_instance
(val
: Char): Instance
222 var t
= mainmodule
.char_type
223 var instance
= new PrimitiveInstance[Char](t
, val
)
224 init_instance_primitive
(instance
)
228 # Return the float instance associated with `val`.
229 fun float_instance
(val
: Float): Instance
231 var t
= mainmodule
.float_type
232 var instance
= new PrimitiveInstance[Float](t
, val
)
233 init_instance_primitive
(instance
)
237 # The unique instance of the `true` value.
238 var true_instance
: Instance is noinit
240 # The unique instance of the `false` value.
241 var false_instance
: Instance is noinit
243 # The unique instance of the `null` value.
244 var null_instance
: Instance is noinit
246 # Return a new array made of `values`.
247 # The dynamic type of the result is Array[elttype].
248 fun array_instance
(values
: Array[Instance], elttype
: MType): Instance
250 assert not elttype
.need_anchor
251 var nat
= new PrimitiveInstance[Array[Instance]](mainmodule
.native_array_type
(elttype
), values
)
252 init_instance_primitive
(nat
)
253 var mtype
= mainmodule
.array_type
(elttype
)
254 var res
= new MutableInstance(mtype
)
255 self.init_instance
(res
)
256 self.send
(self.force_get_primitive_method
("with_native", mtype
), [res
, nat
, self.int_instance
(values
.length
)])
260 # Return a instance associated to a primitive class
261 # Current primitive classes are `Int`, `Bool`, and `String`
262 fun value_instance
(object
: Object): Instance
264 if object
isa Int then
265 return int_instance
(object
)
266 else if object
isa Bool then
267 return bool_instance
(object
)
268 else if object
isa String then
269 return string_instance
(object
)
275 # Return a new native string initialized with `txt`
276 fun native_string_instance
(txt
: String): Instance
278 var val
= new FlatBuffer.from
(txt
)
280 var t
= mainmodule
.native_string_type
281 var instance
= new PrimitiveInstance[Buffer](t
, val
)
282 init_instance_primitive
(instance
)
286 # Return a new String instance for `txt`
287 fun string_instance
(txt
: String): Instance
289 var nat
= native_string_instance
(txt
)
290 var res
= self.send
(self.force_get_primitive_method
("to_s_with_length", nat
.mtype
), [nat
, self.int_instance
(txt
.length
)])
295 # The virtual type of the frames used in the execution engine
298 # The current frame used to store local variables of the current method executed
299 fun frame
: FRAME do return frames
.first
301 # The stack of all frames. The first one is the current one.
302 var frames
= new List[FRAME]
304 # Return a stack trace. One line per function
305 fun stack_trace
: String
307 var b
= new FlatBuffer
308 b
.append
(",---- Stack trace -- - - -\n")
310 b
.append
("| {f.mpropdef} ({f.current_node.location})\n")
312 b
.append
("`------------------- - - -")
316 # The current node, used to print errors, debug and stack-traces
317 fun current_node
: nullable ANode
319 if frames
.is_empty
then return null
320 return frames
.first
.current_node
323 # The dynamic type of the current `self`
324 fun current_receiver_class
: MClassType
326 return frames
.first
.arguments
.first
.mtype
.as(MClassType)
329 # Initialize the environment for a call and return a new Frame
330 # *`node` The AST node
331 # *`mpropdef` The corresponding mpropdef
332 # *`args` Arguments of the call
333 fun new_frame
(node
: ANode, mpropdef
: MPropDef, args
: Array[Instance]): FRAME
335 return new InterpreterFrame(node
, mpropdef
, args
)
338 # Exit the program with a message
339 fun fatal
(message
: String)
341 var node
= current_node
345 node
.fatal
(self, message
)
350 # Debug on the current node
351 fun debug
(message
: String)
353 var node
= current_node
361 # Retrieve the value of the variable in the current frame
362 fun read_variable
(v
: Variable): Instance
364 var f
= frames
.first
.as(InterpreterFrame)
368 # Assign the value of the variable in the current frame
369 fun write_variable
(v
: Variable, value
: Instance)
371 var f
= frames
.first
.as(InterpreterFrame)
375 # Store known methods, used to trace methods as they are reached
376 var discover_call_trace
: Set[MMethodDef] = new HashSet[MMethodDef]
378 # Evaluate `args` as expressions in the call of `mpropdef` on `recv`.
379 # This method is used to manage varargs in signatures and returns the real array
380 # of instances to use in the call.
381 # Return `null` if one of the evaluation of the arguments return null.
382 fun varargize
(mpropdef
: MMethodDef, map
: nullable SignatureMap, recv
: Instance, args
: SequenceRead[AExpr]): nullable Array[Instance]
384 var msignature
= mpropdef
.new_msignature
or else mpropdef
.msignature
.as(not null)
385 var res
= new Array[Instance]
388 if msignature
.arity
== 0 then return res
391 assert args
.length
== msignature
.arity
else debug
("Expected {msignature.arity} args, got {args.length}")
393 var e
= self.expr
(ne
)
394 if e
== null then return null
400 # Eval in order of arguments, not parameters
401 var exprs
= new Array[Instance].with_capacity
(args
.length
)
403 var e
= self.expr
(ne
)
404 if e
== null then return null
409 # Fill `res` with the result of the evaluation according to the mapping
410 for i
in [0..msignature
.arity
[ do
411 var param
= msignature
.mparameters
[i
]
412 var j
= map
.map
.get_or_null
(i
)
415 res
.add
(null_instance
)
418 if param
.is_vararg
and map
.vararg_decl
> 0 then
419 var vararg
= exprs
.sub
(j
, map
.vararg_decl
)
420 var elttype
= param
.mtype
.anchor_to
(self.mainmodule
, recv
.mtype
.as(MClassType))
421 var arg
= self.array_instance
(vararg
, elttype
)
430 # Execute `mpropdef` for a `args` (where `args[0]` is the receiver).
431 # Return a value if `mpropdef` is a function, or null if it is a procedure.
432 # The call is direct/static. There is no message-sending/late-binding.
433 fun call
(mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
435 if self.modelbuilder
.toolcontext
.opt_discover_call_trace
.value
and not self.discover_call_trace
.has
(mpropdef
) then
436 self.discover_call_trace
.add mpropdef
437 self.debug
("Discovered {mpropdef}")
439 assert args
.length
== mpropdef
.msignature
.arity
+ 1 else debug
("Invalid arity for {mpropdef}. {args.length} arguments given.")
441 # Look for the AST node that implements the property
442 var val
= mpropdef
.constant_value
444 var node
= modelbuilder
.mpropdef2node
(mpropdef
)
445 if mpropdef
.is_abstract
then
447 self.frames
.unshift new_frame
(node
, mpropdef
, args
)
449 fatal
("Abstract method `{mpropdef.mproperty.name}` called on `{args.first.mtype}`")
453 if node
isa APropdef then
454 self.parameter_check
(node
, mpropdef
, args
)
455 return node
.call
(self, mpropdef
, args
)
456 else if node
isa AClassdef then
457 self.parameter_check
(node
, mpropdef
, args
)
458 return node
.call
(self, mpropdef
, args
)
459 else if node
!= null then
460 fatal
("Fatal Error: method {mpropdef} associated to unexpected AST node {node.location}")
462 else if val
!= null then
463 return value_instance
(val
)
465 fatal
("Fatal Error: method {mpropdef} not found in the AST")
470 # Execute type checks of covariant parameters
471 fun parameter_check
(node
: ANode, mpropdef
: MMethodDef, args
: Array[Instance])
473 var msignature
= mpropdef
.msignature
474 for i
in [0..msignature
.arity
[ do
475 # skip test for vararg since the array is instantiated with the correct polymorphic type
476 if msignature
.vararg_rank
== i
then continue
478 # skip if the cast is not required
479 var origmtype
= mpropdef
.mproperty
.intro
.msignature
.mparameters
[i
].mtype
480 if not origmtype
.need_anchor
then continue
482 #print "{mpropdef}: {mpropdef.mproperty.intro.msignature.mparameters[i]}"
484 # get the parameter type
485 var mtype
= msignature
.mparameters
[i
].mtype
486 var anchor
= args
.first
.mtype
.as(MClassType)
487 var amtype
= mtype
.anchor_to
(self.mainmodule
, anchor
)
488 if not args
[i
+1].mtype
.is_subtype
(self.mainmodule
, anchor
, amtype
) then
489 node
.fatal
(self, "Cast failed. Expected `{mtype}`, got `{args[i+1].mtype}`")
494 # Common code for runtime injected calls and normal calls
495 fun send_commons
(mproperty
: MMethod, args
: Array[Instance], mtype
: MType): nullable Instance
497 if mtype
isa MNullType then
498 if mproperty
.name
== "==" or mproperty
.name
== "is_same_instance" then
499 return self.bool_instance
(args
[0] == args
[1])
500 else if mproperty
.name
== "!=" then
501 return self.bool_instance
(args
[0] != args
[1])
503 #fatal("Receiver is null. {mproperty}. {args.join(" ")} {self.frame.current_node.class_name}")
504 fatal
("Receiver is null")
509 # Execute a full `callsite` for given `args`
510 # Use this method, instead of `send` to execute and control the additional behavior of the call-sites
511 fun callsite
(callsite
: nullable CallSite, arguments
: Array[Instance]): nullable Instance
513 var initializers
= callsite
.mpropdef
.initializers
514 if not initializers
.is_empty
then
515 var recv
= arguments
.first
517 for p
in initializers
do
518 if p
isa MMethod then
520 for x
in p
.intro
.msignature
.mparameters
do
521 args
.add arguments
[i
]
525 else if p
isa MAttribute then
526 assert recv
isa MutableInstance
527 write_attribute
(p
, recv
, arguments
[i
])
531 assert i
== arguments
.length
533 return send
(callsite
.mproperty
, [recv
])
535 return send
(callsite
.mproperty
, arguments
)
538 # Execute `mproperty` for a `args` (where `args[0]` is the receiver).
539 # Return a value if `mproperty` is a function, or null if it is a procedure.
540 # The call is polymorphic. There is a message-sending/late-binding according to the receiver (args[0]).
541 fun send
(mproperty
: MMethod, args
: Array[Instance]): nullable Instance
543 var recv
= args
.first
544 var mtype
= recv
.mtype
545 var ret
= send_commons
(mproperty
, args
, mtype
)
546 if ret
!= null then return ret
547 var propdef
= mproperty
.lookup_first_definition
(self.mainmodule
, mtype
)
548 return self.call
(propdef
, args
)
551 # Read the attribute `mproperty` of an instance `recv` and return its value.
552 # If the attribute in not yet initialized, then aborts with an error message.
553 fun read_attribute
(mproperty
: MAttribute, recv
: Instance): Instance
555 assert recv
isa MutableInstance
556 if not recv
.attributes
.has_key
(mproperty
) then
557 fatal
("Uninitialized attribute {mproperty.name}")
560 return recv
.attributes
[mproperty
]
563 # Replace in `recv` the value of the attribute `mproperty` by `value`
564 fun write_attribute
(mproperty
: MAttribute, recv
: Instance, value
: Instance)
566 assert recv
isa MutableInstance
567 recv
.attributes
[mproperty
] = value
570 # Is the attribute `mproperty` initialized the instance `recv`?
571 fun isset_attribute
(mproperty
: MAttribute, recv
: Instance): Bool
573 assert recv
isa MutableInstance
574 return recv
.attributes
.has_key
(mproperty
)
577 # Collect attributes of a type in the order of their init
578 fun collect_attr_propdef
(mtype
: MType): Array[AAttrPropdef]
580 var cache
= self.collect_attr_propdef_cache
581 if cache
.has_key
(mtype
) then return cache
[mtype
]
583 var res
= new Array[AAttrPropdef]
584 var cds
= mtype
.collect_mclassdefs
(self.mainmodule
).to_a
585 self.mainmodule
.linearize_mclassdefs
(cds
)
587 res
.add_all
(modelbuilder
.collect_attr_propdef
(cd
))
594 private var collect_attr_propdef_cache
= new HashMap[MType, Array[AAttrPropdef]]
596 # Fill the initial values of the newly created instance `recv`.
597 # `recv.mtype` is used to know what must be filled.
598 fun init_instance
(recv
: Instance)
600 for npropdef
in collect_attr_propdef
(recv
.mtype
) do
601 npropdef
.init_expr
(self, recv
)
605 # A hook to initialize a `PrimitiveInstance`
606 fun init_instance_primitive
(recv
: Instance) do end
608 # This function determines the correct type according to the receiver of the current propdef (self).
609 fun unanchor_type
(mtype
: MType): MType
611 return mtype
.anchor_to
(self.mainmodule
, current_receiver_class
)
614 # Placebo instance used to mark internal error result when `null` already have a meaning.
615 # TODO: replace with multiple return or something better
616 var error_instance
= new MutableInstance(modelbuilder
.model
.null_type
) is lazy
619 # An instance represents a value of the executed program.
620 abstract class Instance
621 # The dynamic type of the instance
622 # ASSERT: not self.mtype.is_anchored
625 # return true if the instance is the true value.
626 # return false if the instance is the true value.
628 fun is_true
: Bool do abort
630 # Return true if `self` IS `o` (using the Nit semantic of is)
631 fun eq_is
(o
: Instance): Bool do return self.is_same_instance
(o
)
633 # Human readable object identity "Type#number"
634 redef fun to_s
do return "{mtype}"
636 # Return the integer value if the instance is an integer.
638 fun to_i
: Int do abort
640 # Return the integer value if the instance is a float.
642 fun to_f
: Float do abort
644 # Return the integer value if the instance is a byte.
646 fun to_b
: Byte do abort
648 # The real value encapsulated if the instance is primitive.
650 fun val
: nullable Object do abort
653 # A instance with attribute (standards objects)
654 class MutableInstance
657 # The values of the attributes
658 var attributes
: Map[MAttribute, Instance] = new HashMap[MAttribute, Instance]
661 # Special instance to handle primitives values (int, bool, etc.)
662 # The trick it just to encapsulate the <<real>> value
663 class PrimitiveInstance[E
]
666 # The real value encapsulated
671 if val
== true then return true
672 if val
== false then return false
678 if not o
isa PrimitiveInstance[nullable Object] then return false
679 return self.val
== o
.val
684 if not o
isa PrimitiveInstance[nullable Object] then return false
685 return self.val
.is_same_instance
(o
.val
)
688 redef fun to_s
do return "{mtype}#{val.object_id}({val or else "null"})"
690 redef fun to_i
do return val
.as(Int)
692 redef fun to_f
do return val
.as(Float)
694 redef fun to_b
do return val
.as(Byte)
697 # Information about local variables in a running method
699 # The current visited node
700 # The node is stored by frame to keep a stack trace
701 var current_node
: ANode
702 # The executed property.
703 # A Method in case of a call, an attribute in case of a default initialization.
704 var mpropdef
: MPropDef
705 # Arguments of the method (the first is the receiver)
706 var arguments
: Array[Instance]
707 # Indicate if the expression has an array comprehension form
708 var comprehension
: nullable Array[Instance] = null
711 # Implementation of a Frame with a Hashmap to store local variables
712 class InterpreterFrame
715 # Mapping between a variable and the current value
716 private var map
: Map[Variable, Instance] = new HashMap[Variable, Instance]
720 # Aborts the program with a message
721 # `v` is used to know if a colored message is displayed or not
722 fun fatal
(v
: NaiveInterpreter, message
: String)
724 if v
.modelbuilder
.toolcontext
.opt_no_color
.value
== true then
725 sys
.stderr
.write
("Runtime error: {message} ({location.file.filename}:{location.line_start})\n")
727 sys
.stderr
.write
("{location}: Runtime error: {message}\n{location.colored_line("0;31")}\n")
728 sys
.stderr
.write
(v
.stack_trace
)
729 sys
.stderr
.write
("\n")
736 # Execute a `mpropdef` associated with the current node.
737 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
739 fatal
(v
, "NOT YET IMPLEMENTED method kind {class_name}. {mpropdef}")
744 redef class AMethPropdef
747 redef fun call
(v
, mpropdef
, args
)
749 var f
= v
.new_frame
(self, mpropdef
, args
)
750 var res
= call_commons
(v
, mpropdef
, args
, f
)
752 if v
.returnmark
== f
then
761 private fun call_commons
(v
: NaiveInterpreter, mpropdef
: MMethodDef, arguments
: Array[Instance], f
: Frame): nullable Instance
765 for i
in [0..mpropdef
.msignature
.arity
[ do
766 var variable
= self.n_signature
.n_params
[i
].variable
767 assert variable
!= null
768 v
.write_variable
(variable
, arguments
[i
+1])
771 # Call the implicit super-init
772 var auto_super_inits
= self.auto_super_inits
773 if auto_super_inits
!= null then
774 var args
= [arguments
.first
]
775 for auto_super_init
in auto_super_inits
do
777 for i
in [0..auto_super_init
.msignature
.arity
+1[ do
778 args
.add
(arguments
[i
])
780 assert auto_super_init
.mproperty
!= mpropdef
.mproperty
781 v
.callsite
(auto_super_init
, args
)
784 if auto_super_call
then
785 # standard call-next-method
786 var superpd
= mpropdef
.lookup_next_definition
(v
.mainmodule
, arguments
.first
.mtype
)
787 v
.call
(superpd
, arguments
)
790 if mpropdef
.is_intern
or mpropdef
.is_extern
then
791 var res
= intern_call
(v
, mpropdef
, arguments
)
792 if res
!= v
.error_instance
then return res
795 if n_block
!= null then
800 if mpropdef
.is_intern
then
801 fatal
(v
, "NOT YET IMPLEMENTED intern {mpropdef}")
802 else if mpropdef
.is_extern
then
803 var res
= call_extern
(v
, mpropdef
, arguments
, f
)
804 if res
!= v
.error_instance
then return res
806 fatal
(v
, "NOT YET IMPLEMENTED <wat?> {mpropdef}")
811 # Call this extern method
812 protected fun call_extern
(v
: NaiveInterpreter, mpropdef
: MMethodDef, arguments
: Array[Instance], f
: Frame): nullable Instance
814 fatal
(v
, "NOT YET IMPLEMENTED extern {mpropdef}")
815 return v
.error_instance
818 # Interprets a intern or a shortcut extern method.
819 # Returns the result for a function, `null` for a procedure, or `error_instance` if the method is unknown.
820 private fun intern_call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
822 var pname
= mpropdef
.mproperty
.name
823 var cname
= mpropdef
.mclassdef
.mclass
.name
824 if pname
== "output" then
825 var recv
= args
.first
828 else if pname
== "object_id" then
829 var recv
= args
.first
830 if recv
isa PrimitiveInstance[Object] then
831 return v
.int_instance
(recv
.val
.object_id
)
833 return v
.int_instance
(recv
.object_id
)
835 else if pname
== "output_class_name" then
836 var recv
= args
.first
839 else if pname
== "native_class_name" then
840 var recv
= args
.first
841 var txt
= recv
.mtype
.to_s
842 return v
.native_string_instance
(txt
)
843 else if pname
== "==" then
844 # == is correctly redefined for instances
845 return v
.bool_instance
(args
[0] == args
[1])
846 else if pname
== "!=" then
847 return v
.bool_instance
(args
[0] != args
[1])
848 else if pname
== "is_same_type" then
849 return v
.bool_instance
(args
[0].mtype
== args
[1].mtype
)
850 else if pname
== "is_same_instance" then
851 return v
.bool_instance
(args
[0].eq_is
(args
[1]))
852 else if pname
== "exit" then
855 else if pname
== "buffer_mode_full" then
856 return v
.int_instance
(sys
.buffer_mode_full
)
857 else if pname
== "buffer_mode_line" then
858 return v
.int_instance
(sys
.buffer_mode_line
)
859 else if pname
== "buffer_mode_none" then
860 return v
.int_instance
(sys
.buffer_mode_none
)
861 else if pname
== "sys" then
863 else if cname
== "Int" then
864 var recvval
= args
[0].to_i
865 if pname
== "unary -" then
866 return v
.int_instance
(-args
[0].to_i
)
867 else if pname
== "unary +" then
869 else if pname
== "+" then
870 return v
.int_instance
(args
[0].to_i
+ args
[1].to_i
)
871 else if pname
== "-" then
872 return v
.int_instance
(args
[0].to_i
- args
[1].to_i
)
873 else if pname
== "*" then
874 return v
.int_instance
(args
[0].to_i
* args
[1].to_i
)
875 else if pname
== "%" then
876 return v
.int_instance
(args
[0].to_i
% args
[1].to_i
)
877 else if pname
== "/" then
878 return v
.int_instance
(args
[0].to_i
/ args
[1].to_i
)
879 else if pname
== "<" then
880 return v
.bool_instance
(args
[0].to_i
< args
[1].to_i
)
881 else if pname
== ">" then
882 return v
.bool_instance
(args
[0].to_i
> args
[1].to_i
)
883 else if pname
== "<=" then
884 return v
.bool_instance
(args
[0].to_i
<= args
[1].to_i
)
885 else if pname
== ">=" then
886 return v
.bool_instance
(args
[0].to_i
>= args
[1].to_i
)
887 else if pname
== "<=>" then
888 return v
.int_instance
(args
[0].to_i
<=> args
[1].to_i
)
889 else if pname
== "ascii" then
890 return v
.char_instance
(args
[0].to_i
.ascii
)
891 else if pname
== "to_f" then
892 return v
.float_instance
(args
[0].to_i
.to_f
)
893 else if pname
== "to_b" then
894 return v
.byte_instance
(args
[0].to_i
.to_b
)
895 else if pname
== "lshift" then
896 return v
.int_instance
(args
[0].to_i
.lshift
(args
[1].to_i
))
897 else if pname
== "rshift" then
898 return v
.int_instance
(args
[0].to_i
.rshift
(args
[1].to_i
))
899 else if pname
== "rand" then
900 var res
= recvval
.rand
901 return v
.int_instance
(res
)
902 else if pname
== "bin_and" then
903 return v
.int_instance
(args
[0].to_i
.bin_and
(args
[1].to_i
))
904 else if pname
== "bin_or" then
905 return v
.int_instance
(args
[0].to_i
.bin_or
(args
[1].to_i
))
906 else if pname
== "bin_xor" then
907 return v
.int_instance
(args
[0].to_i
.bin_xor
(args
[1].to_i
))
908 else if pname
== "bin_not" then
909 return v
.int_instance
(args
[0].to_i
.bin_not
)
910 else if pname
== "int_to_s_len" then
911 return v
.int_instance
(recvval
.to_s
.length
)
912 else if pname
== "native_int_to_s" then
914 var srecv
= args
[1].val
.as(Buffer)
919 else if pname
== "strerror_ext" then
920 return v
.native_string_instance
(recvval
.strerror
)
922 else if cname
== "Byte" then
923 var recvval
= args
[0].to_b
924 if pname
== "unary -" then
925 return v
.byte_instance
(-args
[0].to_b
)
926 else if pname
== "unary +" then
928 else if pname
== "+" then
929 return v
.byte_instance
(args
[0].to_b
+ args
[1].to_b
)
930 else if pname
== "-" then
931 return v
.byte_instance
(args
[0].to_b
- args
[1].to_b
)
932 else if pname
== "*" then
933 return v
.byte_instance
(args
[0].to_b
* args
[1].to_b
)
934 else if pname
== "%" then
935 return v
.byte_instance
(args
[0].to_b
% args
[1].to_b
)
936 else if pname
== "/" then
937 return v
.byte_instance
(args
[0].to_b
/ args
[1].to_b
)
938 else if pname
== "<" then
939 return v
.bool_instance
(args
[0].to_b
< args
[1].to_b
)
940 else if pname
== ">" then
941 return v
.bool_instance
(args
[0].to_b
> args
[1].to_b
)
942 else if pname
== "<=" then
943 return v
.bool_instance
(args
[0].to_b
<= args
[1].to_b
)
944 else if pname
== ">=" then
945 return v
.bool_instance
(args
[0].to_b
>= args
[1].to_b
)
946 else if pname
== "<=>" then
947 return v
.int_instance
(args
[0].to_b
<=> args
[1].to_b
)
948 else if pname
== "to_f" then
949 return v
.float_instance
(args
[0].to_b
.to_f
)
950 else if pname
== "to_i" then
951 return v
.int_instance
(args
[0].to_b
.to_i
)
952 else if pname
== "lshift" then
953 return v
.byte_instance
(args
[0].to_b
.lshift
(args
[1].to_i
))
954 else if pname
== "rshift" then
955 return v
.byte_instance
(args
[0].to_b
.rshift
(args
[1].to_i
))
956 else if pname
== "byte_to_s_len" then
957 return v
.int_instance
(recvval
.to_s
.length
)
958 else if pname
== "native_byte_to_s" then
960 var srecv
= args
[1].val
.as(Buffer)
966 else if cname
== "Char" then
967 var recv
= args
[0].val
.as(Char)
968 if pname
== "ascii" then
969 return v
.int_instance
(recv
.ascii
)
970 else if pname
== "successor" then
971 return v
.char_instance
(recv
.successor
(args
[1].to_i
))
972 else if pname
== "predecessor" then
973 return v
.char_instance
(recv
.predecessor
(args
[1].to_i
))
974 else if pname
== "<" then
975 return v
.bool_instance
(recv
< args
[1].val
.as(Char))
976 else if pname
== ">" then
977 return v
.bool_instance
(recv
> args
[1].val
.as(Char))
978 else if pname
== "<=" then
979 return v
.bool_instance
(recv
<= args
[1].val
.as(Char))
980 else if pname
== ">=" then
981 return v
.bool_instance
(recv
>= args
[1].val
.as(Char))
982 else if pname
== "<=>" then
983 return v
.int_instance
(recv
<=> args
[1].val
.as(Char))
985 else if cname
== "Float" then
986 var recv
= args
[0].to_f
987 if pname
== "unary -" then
988 return v
.float_instance
(-recv
)
989 else if pname
== "unary +" then
991 else if pname
== "+" then
992 return v
.float_instance
(recv
+ args
[1].to_f
)
993 else if pname
== "-" then
994 return v
.float_instance
(recv
- args
[1].to_f
)
995 else if pname
== "*" then
996 return v
.float_instance
(recv
* args
[1].to_f
)
997 else if pname
== "/" then
998 return v
.float_instance
(recv
/ args
[1].to_f
)
999 else if pname
== "<" then
1000 return v
.bool_instance
(recv
< args
[1].to_f
)
1001 else if pname
== ">" then
1002 return v
.bool_instance
(recv
> args
[1].to_f
)
1003 else if pname
== "<=" then
1004 return v
.bool_instance
(recv
<= args
[1].to_f
)
1005 else if pname
== ">=" then
1006 return v
.bool_instance
(recv
>= args
[1].to_f
)
1007 else if pname
== "to_i" then
1008 return v
.int_instance
(recv
.to_i
)
1009 else if pname
== "to_b" then
1010 return v
.byte_instance
(recv
.to_b
)
1011 else if pname
== "cos" then
1012 return v
.float_instance
(args
[0].to_f
.cos
)
1013 else if pname
== "sin" then
1014 return v
.float_instance
(args
[0].to_f
.sin
)
1015 else if pname
== "tan" then
1016 return v
.float_instance
(args
[0].to_f
.tan
)
1017 else if pname
== "acos" then
1018 return v
.float_instance
(args
[0].to_f
.acos
)
1019 else if pname
== "asin" then
1020 return v
.float_instance
(args
[0].to_f
.asin
)
1021 else if pname
== "atan" then
1022 return v
.float_instance
(args
[0].to_f
.atan
)
1023 else if pname
== "sqrt" then
1024 return v
.float_instance
(args
[0].to_f
.sqrt
)
1025 else if pname
== "exp" then
1026 return v
.float_instance
(args
[0].to_f
.exp
)
1027 else if pname
== "log" then
1028 return v
.float_instance
(args
[0].to_f
.log
)
1029 else if pname
== "pow" then
1030 return v
.float_instance
(args
[0].to_f
.pow
(args
[1].to_f
))
1031 else if pname
== "rand" then
1032 return v
.float_instance
(args
[0].to_f
.rand
)
1033 else if pname
== "abs" then
1034 return v
.float_instance
(args
[0].to_f
.abs
)
1035 else if pname
== "hypot_with" then
1036 return v
.float_instance
(args
[0].to_f
.hypot_with
(args
[1].to_f
))
1037 else if pname
== "is_nan" then
1038 return v
.bool_instance
(args
[0].to_f
.is_nan
)
1039 else if pname
== "is_inf_extern" then
1040 return v
.bool_instance
(args
[0].to_f
.is_inf
!= 0)
1041 else if pname
== "round" then
1042 return v
.float_instance
(args
[0].to_f
.round
)
1044 else if cname
== "NativeString" then
1045 if pname
== "new" then
1046 return v
.native_string_instance
("!" * args
[1].to_i
)
1048 var recvval
= args
.first
.val
.as(Buffer)
1049 if pname
== "[]" then
1050 var arg1
= args
[1].to_i
1051 if arg1
>= recvval
.length
or arg1
< 0 then
1052 debug
("Illegal access on {recvval} for element {arg1}/{recvval.length}")
1054 return v
.char_instance
(recvval
.chars
[arg1
])
1055 else if pname
== "[]=" then
1056 var arg1
= args
[1].to_i
1057 if arg1
>= recvval
.length
or arg1
< 0 then
1058 debug
("Illegal access on {recvval} for element {arg1}/{recvval.length}")
1060 recvval
.chars
[arg1
] = args
[2].val
.as(Char)
1062 else if pname
== "copy_to" then
1063 # sig= copy_to(dest: NativeString, length: Int, from: Int, to: Int)
1064 var destval
= args
[1].val
.as(FlatBuffer)
1065 var lenval
= args
[2].to_i
1066 var fromval
= args
[3].to_i
1067 var toval
= args
[4].to_i
1069 debug
("Illegal access on {recvval} for element {fromval}/{recvval.length}")
1071 if fromval
+ lenval
> recvval
.length
then
1072 debug
("Illegal access on {recvval} for element {fromval}+{lenval}/{recvval.length}")
1075 debug
("Illegal access on {destval} for element {toval}/{destval.length}")
1077 if toval
+ lenval
> destval
.length
then
1078 debug
("Illegal access on {destval} for element {toval}+{lenval}/{destval.length}")
1080 recvval
.as(FlatBuffer).copy
(fromval
, lenval
, destval
, toval
)
1082 else if pname
== "atoi" then
1083 return v
.int_instance
(recvval
.to_i
)
1084 else if pname
== "file_exists" then
1085 return v
.bool_instance
(recvval
.to_s
.file_exists
)
1086 else if pname
== "file_mkdir" then
1087 var res
= recvval
.to_s
.mkdir
1088 return v
.bool_instance
(res
== null)
1089 else if pname
== "file_chdir" then
1090 var res
= recvval
.to_s
.chdir
1091 return v
.bool_instance
(res
== null)
1092 else if pname
== "file_realpath" then
1093 return v
.native_string_instance
(recvval
.to_s
.realpath
)
1094 else if pname
== "get_environ" then
1095 var txt
= recvval
.to_s
.environ
1096 return v
.native_string_instance
(txt
)
1097 else if pname
== "system" then
1098 var res
= sys
.system
(recvval
.to_s
)
1099 return v
.int_instance
(res
)
1100 else if pname
== "atof" then
1101 return v
.float_instance
(recvval
.to_f
)
1102 else if pname
== "fast_cstring" then
1103 var ns
= recvval
.to_cstring
.to_s
.substring_from
(args
[1].to_i
)
1104 return v
.native_string_instance
(ns
)
1106 else if cname
== "String" then
1107 var cs
= v
.send
(v
.force_get_primitive_method
("to_cstring", args
.first
.mtype
), [args
.first
])
1108 var str
= cs
.val
.to_s
1109 if pname
== "files" then
1110 var res
= new Array[Instance]
1111 for f
in str
.files
do res
.add v
.string_instance
(f
)
1112 return v
.array_instance
(res
, v
.mainmodule
.string_type
)
1114 else if pname
== "calloc_string" then
1115 return v
.native_string_instance
("!" * args
[1].to_i
)
1116 else if cname
== "NativeArray" then
1117 if pname
== "new" then
1118 var val
= new Array[Instance].filled_with
(v
.null_instance
, args
[1].to_i
)
1119 var instance
= new PrimitiveInstance[Array[Instance]](args
[0].mtype
, val
)
1120 v
.init_instance_primitive
(instance
)
1123 var recvval
= args
.first
.val
.as(Array[Instance])
1124 if pname
== "[]" then
1125 if args
[1].to_i
>= recvval
.length
or args
[1].to_i
< 0 then
1126 debug
("Illegal access on {recvval} for element {args[1].to_i}/{recvval.length}")
1128 return recvval
[args
[1].to_i
]
1129 else if pname
== "[]=" then
1130 recvval
[args
[1].to_i
] = args
[2]
1132 else if pname
== "length" then
1133 return v
.int_instance
(recvval
.length
)
1134 else if pname
== "copy_to" then
1135 recvval
.copy_to
(0, args
[2].to_i
, args
[1].val
.as(Array[Instance]), 0)
1138 else if cname
== "NativeFile" then
1139 if pname
== "native_stdout" then
1140 var inst
= new PrimitiveNativeFile.native_stdout
1141 var instance
= new PrimitiveInstance[PrimitiveNativeFile](mpropdef
.mclassdef
.mclass
.mclass_type
, inst
)
1142 v
.init_instance_primitive
(instance
)
1144 else if pname
== "native_stdin" then
1145 var inst
= new PrimitiveNativeFile.native_stdin
1146 var instance
= new PrimitiveInstance[PrimitiveNativeFile](mpropdef
.mclassdef
.mclass
.mclass_type
, inst
)
1147 v
.init_instance_primitive
(instance
)
1149 else if pname
== "native_stderr" then
1150 var inst
= new PrimitiveNativeFile.native_stderr
1151 var instance
= new PrimitiveInstance[PrimitiveNativeFile](mpropdef
.mclassdef
.mclass
.mclass_type
, inst
)
1152 v
.init_instance_primitive
(instance
)
1154 else if pname
== "io_open_read" then
1155 var a1
= args
[1].val
.as(Buffer)
1156 var inst
= new PrimitiveNativeFile.io_open_read
(a1
.to_s
)
1157 var instance
= new PrimitiveInstance[PrimitiveNativeFile](mpropdef
.mclassdef
.mclass
.mclass_type
, inst
)
1158 v
.init_instance_primitive
(instance
)
1160 else if pname
== "io_open_write" then
1161 var a1
= args
[1].val
.as(Buffer)
1162 var inst
= new PrimitiveNativeFile.io_open_write
(a1
.to_s
)
1163 var instance
= new PrimitiveInstance[PrimitiveNativeFile](mpropdef
.mclassdef
.mclass
.mclass_type
, inst
)
1164 v
.init_instance_primitive
(instance
)
1167 var recvval
= args
.first
.val
1168 if pname
== "io_write" then
1169 var a1
= args
[1].val
.as(Buffer)
1170 return v
.int_instance
(recvval
.as(PrimitiveNativeFile).io_write
(a1
.to_cstring
, args
[2].to_i
))
1171 else if pname
== "io_read" then
1172 var a1
= args
[1].val
.as(Buffer)
1173 var ns
= new NativeString(a1
.length
)
1174 var len
= recvval
.as(PrimitiveNativeFile).io_read
(ns
, args
[2].to_i
)
1176 a1
.append
(ns
.to_s_with_length
(len
))
1177 return v
.int_instance
(len
)
1178 else if pname
== "flush" then
1179 recvval
.as(PrimitiveNativeFile).flush
1181 else if pname
== "io_close" then
1182 return v
.int_instance
(recvval
.as(PrimitiveNativeFile).io_close
)
1183 else if pname
== "set_buffering_type" then
1184 return v
.int_instance
(recvval
.as(PrimitiveNativeFile).set_buffering_type
(args
[1].to_i
, args
[2].to_i
))
1186 else if pname
== "native_argc" then
1187 return v
.int_instance
(v
.arguments
.length
)
1188 else if pname
== "native_argv" then
1189 var txt
= v
.arguments
[args
[1].to_i
]
1190 return v
.native_string_instance
(txt
)
1191 else if pname
== "native_argc" then
1192 return v
.int_instance
(v
.arguments
.length
)
1193 else if pname
== "native_argv" then
1194 var txt
= v
.arguments
[args
[1].to_i
]
1195 return v
.native_string_instance
(txt
)
1196 else if pname
== "get_time" then
1197 return v
.int_instance
(get_time
)
1198 else if pname
== "srand" then
1201 else if pname
== "srand_from" then
1202 srand_from
(args
[1].to_i
)
1204 else if pname
== "atan2" then
1205 return v
.float_instance
(atan2
(args
[1].to_f
, args
[2].to_f
))
1206 else if pname
== "pi" then
1207 return v
.float_instance
(pi
)
1208 else if pname
== "lexer_goto" then
1209 return v
.int_instance
(lexer_goto
(args
[1].to_i
, args
[2].to_i
))
1210 else if pname
== "lexer_accept" then
1211 return v
.int_instance
(lexer_accept
(args
[1].to_i
))
1212 else if pname
== "parser_goto" then
1213 return v
.int_instance
(parser_goto
(args
[1].to_i
, args
[2].to_i
))
1214 else if pname
== "parser_action" then
1215 return v
.int_instance
(parser_action
(args
[1].to_i
, args
[2].to_i
))
1216 else if pname
== "file_getcwd" then
1217 return v
.native_string_instance
(getcwd
)
1218 else if pname
== "errno" then
1219 return v
.int_instance
(sys
.errno
)
1220 else if pname
== "address_is_null" then
1222 if recv
isa PrimitiveInstance[PrimitiveNativeFile] then
1223 return v
.bool_instance
(recv
.val
.address_is_null
)
1225 return v
.false_instance
1227 return v
.error_instance
1231 redef class AAttrPropdef
1232 redef fun call
(v
, mpropdef
, args
)
1234 var recv
= args
.first
1235 assert recv
isa MutableInstance
1236 var attr
= self.mpropdef
.mproperty
1237 if mpropdef
== mreadpropdef
then
1238 assert args
.length
== 1
1239 if not is_lazy
or v
.isset_attribute
(attr
, recv
) then return v
.read_attribute
(attr
, recv
)
1240 var f
= v
.new_frame
(self, mpropdef
, args
)
1241 return evaluate_expr
(v
, recv
, f
)
1242 else if mpropdef
== mwritepropdef
then
1243 assert args
.length
== 2
1244 v
.write_attribute
(attr
, recv
, args
[1])
1251 # Evaluate and set the default value of the attribute in `recv`
1252 private fun init_expr
(v
: NaiveInterpreter, recv
: Instance)
1254 if is_lazy
then return
1256 var f
= v
.new_frame
(self, mreadpropdef
.as(not null), [recv
])
1257 evaluate_expr
(v
, recv
, f
)
1260 var mpropdef
= self.mpropdef
1261 if mpropdef
== null then return
1262 var mtype
= self.mtype
.as(not null)
1263 mtype
= mtype
.anchor_to
(v
.mainmodule
, recv
.mtype
.as(MClassType))
1264 if mtype
isa MNullableType then
1265 v
.write_attribute
(self.mpropdef
.mproperty
, recv
, v
.null_instance
)
1269 private fun evaluate_expr
(v
: NaiveInterpreter, recv
: Instance, f
: Frame): Instance
1271 assert recv
isa MutableInstance
1276 var nexpr
= self.n_expr
1277 var nblock
= self.n_block
1278 if nexpr
!= null then
1280 else if nblock
!= null then
1282 assert v
.returnmark
== f
1285 v
.escapevalue
= null
1292 assert not v
.is_escaping
1293 v
.write_attribute
(self.mpropdef
.mproperty
, recv
, val
)
1298 redef class AClassdef
1299 # Execute an implicit `mpropdef` associated with the current node.
1300 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
1302 if mpropdef
.mproperty
.is_root_init
then
1303 assert args
.length
== 1
1304 if not mpropdef
.is_intro
then
1305 # standard call-next-method
1306 var superpd
= mpropdef
.lookup_next_definition
(v
.mainmodule
, args
.first
.mtype
)
1307 v
.call
(superpd
, args
)
1317 # Evaluate the node as a possible expression.
1318 # Return a possible value
1319 # NOTE: Do not call this method directly, but use `v.expr`
1320 # This method is here to be implemented by subclasses.
1321 protected fun expr
(v
: NaiveInterpreter): nullable Instance
1323 fatal
(v
, "NOT YET IMPLEMENTED expr {class_name}")
1327 # Evaluate the node as a statement.
1328 # NOTE: Do not call this method directly, but use `v.stmt`
1329 # This method is here to be implemented by subclasses (no need to return something).
1330 protected fun stmt
(v
: NaiveInterpreter)
1337 redef class ABlockExpr
1340 var last
= self.n_expr
.last
1341 for e
in self.n_expr
do
1342 if e
== last
then break
1344 if v
.is_escaping
then return null
1351 for e
in self.n_expr
do
1353 if v
.is_escaping
then return
1358 redef class AVardeclExpr
1361 var ne
= self.n_expr
1364 if i
== null then return null
1365 v
.write_variable
(self.variable
.as(not null), i
)
1372 redef class AVarExpr
1375 return v
.read_variable
(self.variable
.as(not null))
1379 redef class AVarAssignExpr
1382 var i
= v
.expr
(self.n_value
)
1383 if i
== null then return null
1384 v
.write_variable
(self.variable
.as(not null), i
)
1389 redef class AVarReassignExpr
1392 var variable
= self.variable
.as(not null)
1393 var vari
= v
.read_variable
(variable
)
1394 var value
= v
.expr
(self.n_value
)
1395 if value
== null then return
1396 var res
= v
.callsite
(reassign_callsite
, [vari
, value
])
1398 v
.write_variable
(variable
, res
)
1402 redef class ASelfExpr
1405 return v
.frame
.arguments
.first
1409 redef class AImplicitSelfExpr
1412 if not is_sys
then return super
1417 redef class AEscapeExpr
1420 var ne
= self.n_expr
1423 if i
== null then return
1426 v
.escapemark
= self.escapemark
1430 redef class AReturnExpr
1433 var ne
= self.n_expr
1436 if i
== null then return
1439 v
.returnmark
= v
.frame
1443 redef class AAbortExpr
1454 var cond
= v
.expr
(self.n_expr
)
1455 if cond
== null then return null
1456 if cond
.is_true
then
1457 return v
.expr
(self.n_then
.as(not null))
1459 return v
.expr
(self.n_else
.as(not null))
1465 var cond
= v
.expr
(self.n_expr
)
1466 if cond
== null then return
1467 if cond
.is_true
then
1475 redef class AIfexprExpr
1478 var cond
= v
.expr
(self.n_expr
)
1479 if cond
== null then return null
1480 if cond
.is_true
then
1481 return v
.expr
(self.n_then
)
1483 return v
.expr
(self.n_else
)
1491 v
.stmt
(self.n_block
)
1492 v
.is_escape
(self.break_mark
) # Clear the break (if any)
1496 redef class AWhileExpr
1500 var cond
= v
.expr
(self.n_expr
)
1501 if cond
== null then return
1502 if not cond
.is_true
then return
1503 v
.stmt
(self.n_block
)
1504 if v
.is_escape
(self.break_mark
) then return
1505 v
.is_escape
(self.continue_mark
) # Clear the break
1506 if v
.is_escaping
then return
1511 redef class ALoopExpr
1515 v
.stmt
(self.n_block
)
1516 if v
.is_escape
(self.break_mark
) then return
1517 v
.is_escape
(self.continue_mark
) # Clear the break
1518 if v
.is_escaping
then return
1523 redef class AForExpr
1526 var col
= v
.expr
(self.n_expr
)
1527 if col
== null then return
1528 if col
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1530 #self.debug("col {col}")
1531 var iter
= v
.callsite
(method_iterator
, [col
]).as(not null)
1532 #self.debug("iter {iter}")
1534 var isok
= v
.callsite
(method_is_ok
, [iter
]).as(not null)
1535 if not isok
.is_true
then break
1536 if self.variables
.length
== 1 then
1537 var item
= v
.callsite
(method_item
, [iter
]).as(not null)
1538 #self.debug("item {item}")
1539 v
.write_variable
(self.variables
.first
, item
)
1540 else if self.variables
.length
== 2 then
1541 var key
= v
.callsite
(method_key
, [iter
]).as(not null)
1542 v
.write_variable
(self.variables
[0], key
)
1543 var item
= v
.callsite
(method_item
, [iter
]).as(not null)
1544 v
.write_variable
(self.variables
[1], item
)
1548 v
.stmt
(self.n_block
)
1549 if v
.is_escape
(self.break_mark
) then break
1550 v
.is_escape
(self.continue_mark
) # Clear the break
1551 if v
.is_escaping
then break
1552 v
.callsite
(method_next
, [iter
])
1554 var method_finish
= self.method_finish
1555 if method_finish
!= null then
1556 v
.callsite
(method_finish
, [iter
])
1561 redef class AWithExpr
1564 var expr
= v
.expr
(self.n_expr
)
1565 if expr
== null then return
1567 v
.callsite
(method_start
, [expr
])
1568 v
.stmt
(self.n_block
)
1569 v
.is_escape
(self.break_mark
) # Clear the break
1570 v
.callsite
(method_finish
, [expr
])
1574 redef class AAssertExpr
1577 var cond
= v
.expr
(self.n_expr
)
1578 if cond
== null then return
1579 if not cond
.is_true
then
1581 if v
.is_escaping
then return
1584 fatal
(v
, "Assert '{nid.text}' failed")
1586 fatal
(v
, "Assert failed")
1596 var cond
= v
.expr
(self.n_expr
)
1597 if cond
== null then return null
1598 if cond
.is_true
then return cond
1599 return v
.expr
(self.n_expr2
)
1603 redef class AImpliesExpr
1606 var cond
= v
.expr
(self.n_expr
)
1607 if cond
== null then return null
1608 if not cond
.is_true
then return v
.true_instance
1609 return v
.expr
(self.n_expr2
)
1613 redef class AAndExpr
1616 var cond
= v
.expr
(self.n_expr
)
1617 if cond
== null then return null
1618 if not cond
.is_true
then return cond
1619 return v
.expr
(self.n_expr2
)
1623 redef class ANotExpr
1626 var cond
= v
.expr
(self.n_expr
)
1627 if cond
== null then return null
1628 return v
.bool_instance
(not cond
.is_true
)
1632 redef class AOrElseExpr
1635 var i
= v
.expr
(self.n_expr
)
1636 if i
== null then return null
1637 if i
!= v
.null_instance
then return i
1638 return v
.expr
(self.n_expr2
)
1642 redef class AIntExpr
1645 return v
.int_instance
(self.value
.as(not null))
1649 redef class AByteExpr
1652 return v
.byte_instance
(self.value
.as(not null))
1656 redef class AFloatExpr
1659 return v
.float_instance
(self.value
.as(not null))
1663 redef class ACharExpr
1666 return v
.char_instance
(self.value
.as(not null))
1670 redef class AArrayExpr
1673 var val
= new Array[Instance]
1674 var old_comprehension
= v
.frame
.comprehension
1675 v
.frame
.comprehension
= val
1676 for nexpr
in self.n_exprs
do
1677 if nexpr
isa AForExpr then
1680 var i
= v
.expr
(nexpr
)
1681 if i
== null then return null
1685 v
.frame
.comprehension
= old_comprehension
1686 var mtype
= v
.unanchor_type
(self.mtype
.as(not null)).as(MClassType)
1687 var elttype
= mtype
.arguments
.first
1688 return v
.array_instance
(val
, elttype
)
1692 redef class AStringFormExpr
1695 var txt
= self.value
.as(not null)
1696 return v
.string_instance
(txt
)
1700 redef class ASuperstringExpr
1703 var array
= new Array[Instance]
1704 for nexpr
in n_exprs
do
1705 var i
= v
.expr
(nexpr
)
1706 if i
== null then return null
1709 var i
= v
.array_instance
(array
, v
.mainmodule
.object_type
)
1710 var res
= v
.send
(v
.force_get_primitive_method
("plain_to_s", i
.mtype
), [i
])
1716 redef class ACrangeExpr
1719 var e1
= v
.expr
(self.n_expr
)
1720 if e1
== null then return null
1721 var e2
= v
.expr
(self.n_expr2
)
1722 if e2
== null then return null
1723 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1724 var res
= new MutableInstance(mtype
)
1725 v
.init_instance
(res
)
1726 v
.callsite
(init_callsite
, [res
, e1
, e2
])
1731 redef class AOrangeExpr
1734 var e1
= v
.expr
(self.n_expr
)
1735 if e1
== null then return null
1736 var e2
= v
.expr
(self.n_expr2
)
1737 if e2
== null then return null
1738 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1739 var res
= new MutableInstance(mtype
)
1740 v
.init_instance
(res
)
1741 v
.callsite
(init_callsite
, [res
, e1
, e2
])
1746 redef class ATrueExpr
1749 return v
.bool_instance
(true)
1753 redef class AFalseExpr
1756 return v
.bool_instance
(false)
1760 redef class ANullExpr
1763 return v
.null_instance
1767 redef class AIsaExpr
1770 var i
= v
.expr
(self.n_expr
)
1771 if i
== null then return null
1772 var mtype
= v
.unanchor_type
(self.cast_type
.as(not null))
1773 return v
.bool_instance
(v
.is_subtype
(i
.mtype
, mtype
))
1777 redef class AAsCastExpr
1780 var i
= v
.expr
(self.n_expr
)
1781 if i
== null then return null
1782 var mtype
= self.mtype
.as(not null)
1783 var amtype
= v
.unanchor_type
(mtype
)
1784 if not v
.is_subtype
(i
.mtype
, amtype
) then
1785 fatal
(v
, "Cast failed. Expected `{amtype}`, got `{i.mtype}`")
1791 redef class AAsNotnullExpr
1794 var i
= v
.expr
(self.n_expr
)
1795 if i
== null then return null
1796 if i
.mtype
isa MNullType then
1797 fatal
(v
, "Cast failed")
1803 redef class AParExpr
1806 return v
.expr
(self.n_expr
)
1810 redef class AOnceExpr
1813 if v
.onces
.has_key
(self) then
1814 return v
.onces
[self]
1816 var res
= v
.expr
(self.n_expr
)
1817 if res
== null then return null
1824 redef class ASendExpr
1827 var recv
= v
.expr
(self.n_expr
)
1828 if recv
== null then return null
1829 var args
= v
.varargize
(callsite
.mpropdef
, callsite
.signaturemap
, recv
, self.raw_arguments
)
1830 if args
== null then return null
1832 var res
= v
.callsite
(callsite
, args
)
1837 redef class ASendReassignFormExpr
1840 var recv
= v
.expr
(self.n_expr
)
1841 if recv
== null then return
1842 var args
= v
.varargize
(callsite
.mpropdef
, callsite
.signaturemap
, recv
, self.raw_arguments
)
1843 if args
== null then return
1844 var value
= v
.expr
(self.n_value
)
1845 if value
== null then return
1847 var read
= v
.callsite
(callsite
, args
)
1850 var write
= v
.callsite
(reassign_callsite
, [read
, value
])
1851 assert write
!= null
1855 v
.callsite
(write_callsite
, args
)
1859 redef class ASuperExpr
1862 var recv
= v
.frame
.arguments
.first
1864 var callsite
= self.callsite
1865 if callsite
!= null then
1867 if self.n_args
.n_exprs
.is_empty
then
1868 # Add automatic arguments for the super init call
1870 for i
in [0..callsite
.msignature
.arity
[ do
1871 args
.add
(v
.frame
.arguments
[i
+1])
1874 args
= v
.varargize
(callsite
.mpropdef
, callsite
.signaturemap
, recv
, self.n_args
.n_exprs
)
1875 if args
== null then return null
1879 var res
= v
.callsite
(callsite
, args
)
1883 # Standard call-next-method
1884 var mpropdef
= self.mpropdef
1885 mpropdef
= mpropdef
.lookup_next_definition
(v
.mainmodule
, recv
.mtype
)
1888 if self.n_args
.n_exprs
.is_empty
then
1889 args
= v
.frame
.arguments
1891 args
= v
.varargize
(mpropdef
, signaturemap
, recv
, self.n_args
.n_exprs
)
1892 if args
== null then return null
1895 var res
= v
.call
(mpropdef
, args
)
1900 redef class ANewExpr
1903 var mtype
= v
.unanchor_type
(self.recvtype
.as(not null))
1904 var recv
: Instance = new MutableInstance(mtype
)
1905 v
.init_instance
(recv
)
1906 var callsite
= self.callsite
1907 if callsite
== null then return recv
1909 var args
= v
.varargize
(callsite
.mpropdef
, callsite
.signaturemap
, recv
, self.n_args
.n_exprs
)
1910 if args
== null then return null
1911 var res2
= v
.callsite
(callsite
, args
)
1912 if res2
!= null then
1913 #self.debug("got {res2} from {mproperty}. drop {recv}")
1920 redef class AAttrExpr
1923 var recv
= v
.expr
(self.n_expr
)
1924 if recv
== null then return null
1925 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1926 var mproperty
= self.mproperty
.as(not null)
1927 return v
.read_attribute
(mproperty
, recv
)
1931 redef class AAttrAssignExpr
1934 var recv
= v
.expr
(self.n_expr
)
1935 if recv
== null then return
1936 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1937 var i
= v
.expr
(self.n_value
)
1938 if i
== null then return
1939 var mproperty
= self.mproperty
.as(not null)
1940 v
.write_attribute
(mproperty
, recv
, i
)
1944 redef class AAttrReassignExpr
1947 var recv
= v
.expr
(self.n_expr
)
1948 if recv
== null then return
1949 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1950 var value
= v
.expr
(self.n_value
)
1951 if value
== null then return
1952 var mproperty
= self.mproperty
.as(not null)
1953 var attr
= v
.read_attribute
(mproperty
, recv
)
1954 var res
= v
.callsite
(reassign_callsite
, [attr
, value
])
1956 v
.write_attribute
(mproperty
, recv
, res
)
1960 redef class AIssetAttrExpr
1963 var recv
= v
.expr
(self.n_expr
)
1964 if recv
== null then return null
1965 if recv
.mtype
isa MNullType then fatal
(v
, "Receiver is null")
1966 var mproperty
= self.mproperty
.as(not null)
1967 return v
.bool_instance
(v
.isset_attribute
(mproperty
, recv
))
1971 redef class AVarargExpr
1974 return v
.expr
(self.n_expr
)
1978 redef class ANamedargExpr
1981 return v
.expr
(self.n_expr
)
1985 redef class ADebugTypeExpr