1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Interpretation of a Nit program directly on the AST
18 module naive_interpreter
22 import auto_super_init
26 redef class ToolContext
27 # --discover-call-trace
28 var opt_discover_call_trace
: OptionBool = new OptionBool("Trace calls of the first invocation of a method", "--discover-call-trace")
33 self.option_context
.add_option
(self.opt_discover_call_trace
)
37 redef class ModelBuilder
38 # Execute the program from the entry point (`Sys::main`) of the `mainmodule`
39 # `arguments` are the command-line arguments in order
41 # 1. the AST is fully loaded.
42 # 2. the model is fully built.
43 # 3. the instructions are fully analysed.
44 fun run_naive_interpreter
(mainmodule
: MModule, arguments
: Array[String])
47 self.toolcontext
.info
("*** START INTERPRETING ***", 1)
49 var interpreter
= new NaiveInterpreter(self, mainmodule
, arguments
)
50 init_naive_interpreter
(interpreter
, mainmodule
)
53 self.toolcontext
.info
("*** END INTERPRETING: {time1-time0} ***", 2)
56 private fun init_naive_interpreter
(interpreter
: NaiveInterpreter, mainmodule
: MModule) do
57 var sys_type
= mainmodule
.sys_type
58 if sys_type
== null then return # no class Sys
59 var mainobj
= new MutableInstance(sys_type
)
60 interpreter
.mainobj
= mainobj
61 interpreter
.init_instance
(mainobj
)
62 var initprop
= mainmodule
.try_get_primitive_method
("init", sys_type
.mclass
)
63 if initprop
!= null then
64 interpreter
.send
(initprop
, [mainobj
])
66 var mainprop
= mainmodule
.try_get_primitive_method
("main", sys_type
.mclass
)
67 if mainprop
!= null then
68 interpreter
.send
(mainprop
, [mainobj
])
73 # The visitor that interprets the Nit Program by walking on the AST
74 private class NaiveInterpreter
75 # The modelbuilder that know the AST and its associations with the model
76 var modelbuilder
: ModelBuilder
78 # The main moduleof the program (used to lookup methoda
79 var mainmodule
: MModule
81 # The command line arguments of the interpreted program
82 # arguments.first is the program name
83 # arguments[1] is the first argument
84 var arguments
: Array[String]
86 var mainobj
: nullable Instance
88 init(modelbuilder
: ModelBuilder, mainmodule
: MModule, arguments
: Array[String])
90 self.modelbuilder
= modelbuilder
91 self.mainmodule
= mainmodule
92 self.arguments
= arguments
93 self.true_instance
= new PrimitiveInstance[Bool](mainmodule
.bool_type
, true)
94 self.false_instance
= new PrimitiveInstance[Bool](mainmodule
.bool_type
, false)
95 self.null_instance
= new MutableInstance(mainmodule
.model
.null_type
)
98 # Subtype test in the context of the mainmodule
99 fun is_subtype
(sub
, sup
: MType): Bool
101 return sub
.is_subtype
(self.mainmodule
, self.frame
.arguments
.first
.mtype
.as(MClassType), sup
)
104 fun force_get_primitive_method
(name
: String, recv
: MType): MMethod
106 assert recv
isa MClassType
107 return self.modelbuilder
.force_get_primitive_method
(self.frame
.current_node
, name
, recv
.mclass
, self.mainmodule
)
110 # Is a return executed?
111 # Set this mark to skip the evaluation until the end of the specified method frame
112 var returnmark
: nullable Frame = null
114 # Is a break executed?
115 # Set this mark to skip the evaluation until a labeled statement catch it with `is_break`
116 var breakmark
: nullable EscapeMark = null
118 # Is a continue executed?
119 # Set this mark to skip the evaluation until a labeled statement catch it with `is_continue`
120 var continuemark
: nullable EscapeMark = null
122 # Is a return or a break or a continue executed?
123 # Use this function to know if you must skip the evaluation of statements
124 fun is_escaping
: Bool do return returnmark
!= null or breakmark
!= null or continuemark
!= null
126 # The value associated with the current return/break/continue, if any.
127 # Set the value when you set a escapemark.
128 # Read the value when you catch a mark or reach the end of a method
129 var escapevalue
: nullable Instance = null
131 # If there is a break and is associated with `escapemark`, then return true an clear the mark.
132 # If there is no break or if `escapemark` is null then return false.
133 # Use this function to catch a potential break.
134 fun is_break
(escapemark
: nullable EscapeMark): Bool
136 if escapemark
!= null and self.breakmark
== escapemark
then
137 self.breakmark
= null
144 # If there is a continue and is associated with `escapemark`, then return true an clear the mark.
145 # If there is no continue or if `escapemark` is null then return false.
146 # Use this function to catch a potential continue.
147 fun is_continue
(escapemark
: nullable EscapeMark): Bool
149 if escapemark
!= null and self.continuemark
== escapemark
then
150 self.continuemark
= null
157 # Evaluate `n` as an expression in the current context.
158 # Return the value of the expression.
159 # If `n` cannot be evaluated, then aborts.
160 fun expr
(n
: AExpr): nullable Instance
162 var frame
= self.frame
163 var old
= frame
.current_node
164 frame
.current_node
= n
165 #n.debug("IN Execute expr")
167 if i
== null and not self.is_escaping
then
168 n
.debug
("inconsitance: no value and not escaping.")
170 var implicit_cast_to
= n
.implicit_cast_to
171 if implicit_cast_to
!= null then
172 var mtype
= self.unanchor_type
(implicit_cast_to
)
173 if not self.is_subtype
(i
.mtype
, mtype
) then n
.fatal
(self, "Cast failed. Expected `{implicit_cast_to}`, got `{i.mtype}`")
176 #n.debug("OUT Execute expr: value is {i}")
177 #if not is_subtype(i.mtype, n.mtype.as(not null)) then n.debug("Expected {n.mtype.as(not null)} got {i}")
178 frame
.current_node
= old
182 # Evaluate `n` as a statement in the current context.
183 # Do nothing if `n` is null.
184 # If `n` cannot be evaluated, then aborts.
185 fun stmt
(n
: nullable AExpr)
188 var frame
= self.frame
189 var old
= frame
.current_node
190 frame
.current_node
= n
191 #n.debug("Execute stmt")
193 frame
.current_node
= old
197 # Map used to store values of nodes that must be evaluated once in the system (`AOnceExpr`)
198 var onces
: Map[ANode, Instance] = new HashMap[ANode, Instance]
200 # Return the boolean instance associated with `val`.
201 fun bool_instance
(val
: Bool): Instance
203 if val
then return self.true_instance
else return self.false_instance
206 # Return the integer instance associated with `val`.
207 fun int_instance
(val
: Int): Instance
209 var ic
= self.mainmodule
.get_primitive_class
("Int")
210 return new PrimitiveInstance[Int](ic
.mclass_type
, val
)
213 # Return the char instance associated with `val`.
214 fun char_instance
(val
: Char): Instance
216 var ic
= self.mainmodule
.get_primitive_class
("Char")
217 return new PrimitiveInstance[Char](ic
.mclass_type
, val
)
220 # Return the float instance associated with `val`.
221 fun float_instance
(val
: Float): Instance
223 var ic
= self.mainmodule
.get_primitive_class
("Float")
224 return new PrimitiveInstance[Float](ic
.mclass_type
, val
)
227 # The unique intance of the `true` value.
228 var true_instance
: Instance
230 # The unique intance of the `false` value.
231 var false_instance
: Instance
233 # The unique intance of the `null` value.
234 var null_instance
: Instance
236 # Return a new array made of `values`.
237 # The dynamic type of the result is Array[elttype].
238 fun array_instance
(values
: Array[Instance], elttype
: MType): Instance
240 assert not elttype
.need_anchor
241 var nat
= new PrimitiveInstance[Array[Instance]](self.mainmodule
.get_primitive_class
("NativeArray").get_mtype
([elttype
]), values
)
242 var mtype
= self.mainmodule
.get_primitive_class
("Array").get_mtype
([elttype
])
243 var res
= new MutableInstance(mtype
)
244 self.init_instance
(res
)
245 self.send
(self.force_get_primitive_method
("with_native", mtype
), [res
, nat
, self.int_instance
(values
.length
)])
249 # Return a new native string initialized with `txt`
250 fun native_string_instance
(txt
: String): Instance
252 var val
= new Buffer.from
(txt
)
254 var ic
= self.mainmodule
.get_primitive_class
("NativeString")
255 return new PrimitiveInstance[Buffer](ic
.mclass_type
, val
)
258 # The current frame used to store local variables of the current method executed
259 fun frame
: Frame do return frames
.first
261 # The stack of all frames. The first one is the current one.
262 var frames
: List[Frame] = new List[Frame]
264 # Return a stack stace. One line per function
265 fun stack_trace
: String
268 b
.append
(",---- Stack trace -- - - -\n")
270 b
.append
("| {f.mpropdef} ({f.current_node.location})\n")
272 b
.append
("`------------------- - - -")
276 # Exit the program with a message
277 fun fatal
(message
: String)
279 if frames
.is_empty
then
282 self.frame
.current_node
.fatal
(self, message
)
287 # Debug on the current node
288 fun debug
(message
: String)
290 if frames
.is_empty
then
293 self.frame
.current_node
.debug
(message
)
297 # Store known method, used to trace methods as thez are reached
298 var discover_call_trace
: Set[MMethodDef] = new HashSet[MMethodDef]
300 # Common code for calls to injected methods and normal methods
301 fun call_commons
(mpropdef
: MMethodDef, args
: Array[Instance]): Array[Instance]
303 var vararg_rank
= mpropdef
.msignature
.vararg_rank
304 if vararg_rank
>= 0 then
305 assert args
.length
>= mpropdef
.msignature
.arity
+ 1 # because of self
307 args
= new Array[Instance]
309 args
.add
(rawargs
.first
) # recv
311 for i
in [0..vararg_rank
[ do
312 args
.add
(rawargs
[i
+1])
315 var vararg_lastrank
= vararg_rank
+ rawargs
.length-1-mpropdef
.msignature
.arity
316 var vararg
= new Array[Instance]
317 for i
in [vararg_rank
..vararg_lastrank
] do
318 vararg
.add
(rawargs
[i
+1])
320 # FIXME: its it to late to determine the vararg type, this should have been done during a previous analysis
321 var elttype
= mpropdef
.msignature
.mparameters
[vararg_rank
].mtype
.anchor_to
(self.mainmodule
, args
.first
.mtype
.as(MClassType))
322 args
.add
(self.array_instance
(vararg
, elttype
))
324 for i
in [vararg_lastrank
+1..rawargs
.length-1
[ do
325 args
.add
(rawargs
[i
+1])
331 # Execute `mpropdef` for a `args` (where `args[0]` is the receiver).
332 # Return a falue if `mpropdef` is a function, or null if it is a procedure.
333 # The call is direct/static. There is no message-seding/late-binding.
334 fun call
(mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
336 args
= call_commons
(mpropdef
, args
)
337 return call_without_varargs
(mpropdef
, args
)
340 # Common code to call and this function
342 # Call only executes the variadic part, this avoids
343 # double encapsulation of variadic parameters into an Array
344 fun call_without_varargs
(mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
346 if self.modelbuilder
.toolcontext
.opt_discover_call_trace
.value
and not self.discover_call_trace
.has
(mpropdef
) then
347 self.discover_call_trace
.add mpropdef
348 self.debug
("Discovered {mpropdef}")
350 if args
.length
< mpropdef
.msignature
.arity
+ 1 or args
.length
> mpropdef
.msignature
.arity
+ 1 then
351 fatal
("NOT YET IMPLEMENTED: Invalid arity for {mpropdef}. {args.length} arguments given.")
354 # Look for the AST node that implements the property
355 var mproperty
= mpropdef
.mproperty
356 if self.modelbuilder
.mpropdef2npropdef
.has_key
(mpropdef
) then
357 var npropdef
= self.modelbuilder
.mpropdef2npropdef
[mpropdef
]
358 self.parameter_check
(npropdef
, mpropdef
, args
)
359 return npropdef
.call
(self, mpropdef
, args
)
360 else if mproperty
.name
== "init" then
361 var nclassdef
= self.modelbuilder
.mclassdef2nclassdef
[mpropdef
.mclassdef
]
362 self.parameter_check
(nclassdef
, mpropdef
, args
)
363 return nclassdef
.call
(self, mpropdef
, args
)
365 fatal
("Fatal Error: method {mpropdef} not found in the AST")
370 # Generate type checks in the C code to check covariant parameters
371 fun parameter_check
(node
: ANode, mpropdef
: MMethodDef, args
: Array[Instance])
373 var msignature
= mpropdef
.msignature
374 for i
in [0..msignature
.arity
[ do
375 # skip test for vararg since the array is instantiated with the correct polymorphic type
376 if msignature
.vararg_rank
== i
then continue
378 # skip if the cast is not required
379 var origmtype
= mpropdef
.mproperty
.intro
.msignature
.mparameters
[i
].mtype
380 if not origmtype
.need_anchor
then continue
382 # get the parameter type
383 var mtype
= msignature
.mparameters
[i
].mtype
384 var anchor
= args
.first
.mtype
.as(MClassType)
385 var amtype
= mtype
.anchor_to
(self.mainmodule
, anchor
)
386 if not args
[i
+1].mtype
.is_subtype
(self.mainmodule
, anchor
, amtype
) then
387 node
.fatal
(self, "Cast failed. Expected `{mtype}`, got `{args[i+1].mtype}`")
392 # Common code for runtime injected calls and normal calls
393 fun send_commons
(mproperty
: MMethod, args
: Array[Instance], mtype
: MType): nullable Instance
395 if mtype
isa MNullType then
396 if mproperty
.name
== "==" then
397 return self.bool_instance
(args
[0] == args
[1])
398 else if mproperty
.name
== "!=" then
399 return self.bool_instance
(args
[0] != args
[1])
401 #fatal("Reciever is null. {mproperty}. {args.join(" ")} {self.frame.current_node.class_name}")
402 fatal
("Reciever is null")
407 # Execute `mproperty` for a `args` (where `args[0]` is the receiver).
408 # Return a falue if `mproperty` is a function, or null if it is a procedure.
409 # The call is polimotphic. There is a message-seding/late-bindng according to te receiver (args[0]).
410 fun send
(mproperty
: MMethod, args
: Array[Instance]): nullable Instance
412 var recv
= args
.first
413 var mtype
= recv
.mtype
414 var ret
= send_commons
(mproperty
, args
, mtype
)
415 if ret
!= null then return ret
416 var propdef
= mproperty
.lookup_first_definition
(self.mainmodule
, mtype
)
417 return self.call
(propdef
, args
)
420 # Read the attribute `mproperty` of an instance `recv` and return its value.
421 # If the attribute in not yet initialized, then aborts with an error message.
422 fun read_attribute
(mproperty
: MAttribute, recv
: Instance): Instance
424 assert recv
isa MutableInstance
425 if not recv
.attributes
.has_key
(mproperty
) then
426 fatal
("Uninitialized attribute {mproperty.name}")
429 return recv
.attributes
[mproperty
]
432 # Collect attributes of a type in the order of their init
433 fun collect_attr_propdef
(mtype
: MType): Array[AAttrPropdef]
435 var cache
= self.collect_attr_propdef_cache
436 if cache
.has_key
(mtype
) then return cache
[mtype
]
438 var res
= new Array[AAttrPropdef]
439 var cds
= mtype
.collect_mclassdefs
(self.mainmodule
).to_a
440 self.mainmodule
.linearize_mclassdefs
(cds
)
442 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
443 for npropdef
in n
.n_propdefs
do
444 if npropdef
isa AAttrPropdef then
454 var collect_attr_propdef_cache
= new HashMap[MType, Array[AAttrPropdef]]
456 # Fill the initial values of the newly created instance `recv`.
457 # `recv.mtype` is used to know what must be filled.
458 fun init_instance
(recv
: Instance)
460 for npropdef
in collect_attr_propdef
(recv
.mtype
) do
461 npropdef
.init_expr
(self, recv
)
465 # This function determine the correct type according the reciever of the current definition (self).
466 fun unanchor_type
(mtype
: MType): MType
468 return mtype
.anchor_to
(self.mainmodule
, self.frame
.arguments
.first
.mtype
.as(MClassType))
472 # An instance represents a value of the executed program.
473 abstract class Instance
474 # The dynamic type of the instance
475 # ASSERT: not self.mtype.is_anchored
478 # return true if the instance is the true value.
479 # return false if the instance is the true value.
481 fun is_true
: Bool do abort
483 # Return true if `self` IS `o` (using the Nit semantic of is)
484 fun eq_is
(o
: Instance): Bool do return self.is_same_instance
(o
)
486 # Human readable object identity "Type#number"
487 redef fun to_s
do return "{mtype}"
489 # Return the integer value if the instance is an integer.
491 fun to_i
: Int do abort
493 # Return the integer value if the instance is a float.
495 fun to_f
: Float do abort
497 # The real value encapsulated if the instance is primitive.
499 fun val
: Object do abort
502 # A instance with attribute (standards objects)
503 class MutableInstance
506 # The values of the attributes
507 var attributes
: Map[MAttribute, Instance] = new HashMap[MAttribute, Instance]
510 # Special instance to handle primitives values (int, bool, etc.)
511 # The trick it just to encapsulate the <<real>> value
512 class PrimitiveInstance[E
: Object]
515 # The real value encapsulated
518 init(mtype
: MType, val
: E
)
526 if val
== true then return true
527 if val
== false then return false
533 if not o
isa PrimitiveInstance[Object] then return false
534 return self.val
== o
.val
539 if not o
isa PrimitiveInstance[Object] then return false
540 return self.val
.is_same_instance
(o
.val
)
543 redef fun to_s
do return "{mtype}#{val.object_id}({val})"
545 redef fun to_i
do return val
.as(Int)
547 redef fun to_f
do return val
.as(Float)
550 # Information about local variables in a running method
552 # The current visited node
553 # The node is stored by frame to keep a stack trace
554 var current_node
: ANode
555 # The executed property.
556 # A Method in case of a call, an attribute in case of a default initialization.
557 var mpropdef
: MPropDef
558 # Arguments of the method (the first is te receiver
559 var arguments
: Array[Instance]
560 # Mapping betwen a variable an the current value
561 var map
: Map[Variable, Instance] = new HashMap[Variable, Instance]
565 # Aborts the program with a message
566 # `v` is used to know if a colored message is displayed or not
567 private fun fatal
(v
: NaiveInterpreter, message
: String)
569 if v
.modelbuilder
.toolcontext
.opt_no_color
.value
== true then
570 stderr
.write
("Runtime error: {message} ({location.file.filename}:{location.line_start})\n")
572 stderr
.write
("{location}: Runtime error: {message}\n{location.colored_line("0;31")}\n")
573 stderr
.write
(v
.stack_trace
)
581 # Execute a `mpropdef` associated with the current node.
582 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
584 fatal
(v
, "NOT YET IMPLEMENTED method kind {class_name}. {mpropdef}")
589 redef class AConcreteMethPropdef
591 redef fun call
(v
, mpropdef
, args
)
593 var f
= new Frame(self, self.mpropdef
.as(not null), args
)
594 call_commons
(v
, mpropdef
, args
, f
)
596 if v
.returnmark
== f
then
598 var res
= v
.escapevalue
605 private fun call_commons
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance], f
: Frame)
607 for i
in [0..mpropdef
.msignature
.arity
[ do
608 var variable
= self.n_signature
.n_params
[i
].variable
609 assert variable
!= null
610 f
.map
[variable
] = args
[i
+1]
615 # Call the implicit super-init
616 var auto_super_inits
= self.auto_super_inits
617 if auto_super_inits
!= null then
618 var selfarg
= [args
.first
]
619 for auto_super_init
in auto_super_inits
do
620 if auto_super_init
.intro
.msignature
.arity
== 0 then
621 v
.send
(auto_super_init
, selfarg
)
623 v
.send
(auto_super_init
, args
)
632 redef class AInternMethPropdef
633 redef fun call
(v
, mpropdef
, args
)
635 var pname
= mpropdef
.mproperty
.name
636 var cname
= mpropdef
.mclassdef
.mclass
.name
637 if pname
== "output" then
638 var recv
= args
.first
641 else if pname
== "object_id" then
642 var recv
= args
.first
643 if recv
isa PrimitiveInstance[Object] then
644 return v
.int_instance
(recv
.val
.object_id
)
646 return v
.int_instance
(recv
.object_id
)
648 else if pname
== "output_class_name" then
649 var recv
= args
.first
652 else if pname
== "native_class_name" then
653 var recv
= args
.first
654 var txt
= recv
.mtype
.to_s
655 return v
.native_string_instance
(txt
)
656 else if pname
== "==" then
657 # == is correclt redefined for instances
658 return v
.bool_instance
(args
[0] == args
[1])
659 else if pname
== "!=" then
660 return v
.bool_instance
(args
[0] != args
[1])
661 else if pname
== "is_same_type" then
662 return v
.bool_instance
(args
[0].mtype
== args
[1].mtype
)
663 else if pname
== "is_same_instance" then
664 return v
.bool_instance
(args
[1] != null and args
[0].eq_is
(args
[1]))
665 else if pname
== "exit" then
668 else if pname
== "sys" then
670 else if cname
== "Int" then
671 if pname
== "unary -" then
672 return v
.int_instance
(-args
[0].to_i
)
673 else if pname
== "succ" then
674 return v
.int_instance
(args
[0].to_i
+ 1)
675 else if pname
== "prec" then
676 return v
.int_instance
(args
[0].to_i
- 1)
677 else if pname
== "+" then
678 return v
.int_instance
(args
[0].to_i
+ args
[1].to_i
)
679 else if pname
== "-" then
680 return v
.int_instance
(args
[0].to_i
- args
[1].to_i
)
681 else if pname
== "*" then
682 return v
.int_instance
(args
[0].to_i
* args
[1].to_i
)
683 else if pname
== "%" then
684 return v
.int_instance
(args
[0].to_i
% args
[1].to_i
)
685 else if pname
== "/" then
686 return v
.int_instance
(args
[0].to_i
/ args
[1].to_i
)
687 else if pname
== "<" then
688 return v
.bool_instance
(args
[0].to_i
< args
[1].to_i
)
689 else if pname
== ">" then
690 return v
.bool_instance
(args
[0].to_i
> args
[1].to_i
)
691 else if pname
== "<=" then
692 return v
.bool_instance
(args
[0].to_i
<= args
[1].to_i
)
693 else if pname
== ">=" then
694 return v
.bool_instance
(args
[0].to_i
>= args
[1].to_i
)
695 else if pname
== "<=>" then
696 return v
.int_instance
(args
[0].to_i
<=> args
[1].to_i
)
697 else if pname
== "ascii" then
698 return v
.char_instance
(args
[0].to_i
.ascii
)
699 else if pname
== "to_f" then
700 return v
.float_instance
(args
[0].to_i
.to_f
)
701 else if pname
== "lshift" then
702 return v
.int_instance
(args
[0].to_i
.lshift
(args
[1].to_i
))
703 else if pname
== "rshift" then
704 return v
.int_instance
(args
[0].to_i
.rshift
(args
[1].to_i
))
706 else if cname
== "Char" then
707 var recv
= args
[0].val
.as(Char)
708 if pname
== "ascii" then
709 return v
.int_instance
(recv
.ascii
)
710 else if pname
== "succ" then
711 return v
.char_instance
(recv
.succ
)
712 else if pname
== "prec" then
713 return v
.char_instance
(recv
.prec
)
714 else if pname
== "<" then
715 return v
.bool_instance
(recv
< args
[1].val
.as(Char))
716 else if pname
== ">" then
717 return v
.bool_instance
(recv
> args
[1].val
.as(Char))
718 else if pname
== "<=" then
719 return v
.bool_instance
(recv
<= args
[1].val
.as(Char))
720 else if pname
== ">=" then
721 return v
.bool_instance
(recv
>= args
[1].val
.as(Char))
722 else if pname
== "<=>" then
723 return v
.int_instance
(recv
<=> args
[1].val
.as(Char))
725 else if cname
== "Float" then
726 var recv
= args
[0].to_f
727 if pname
== "unary -" then
728 return v
.float_instance
(-recv
)
729 else if pname
== "+" then
730 return v
.float_instance
(recv
+ args
[1].to_f
)
731 else if pname
== "-" then
732 return v
.float_instance
(recv
- args
[1].to_f
)
733 else if pname
== "*" then
734 return v
.float_instance
(recv
* args
[1].to_f
)
735 else if pname
== "/" then
736 return v
.float_instance
(recv
/ args
[1].to_f
)
737 else if pname
== "<" then
738 return v
.bool_instance
(recv
< args
[1].to_f
)
739 else if pname
== ">" then
740 return v
.bool_instance
(recv
> args
[1].to_f
)
741 else if pname
== "<=" then
742 return v
.bool_instance
(recv
<= args
[1].to_f
)
743 else if pname
== ">=" then
744 return v
.bool_instance
(recv
>= args
[1].to_f
)
745 else if pname
== "to_i" then
746 return v
.int_instance
(recv
.to_i
)
748 else if cname
== "NativeString" then
749 var recvval
= args
.first
.val
.as(Buffer)
750 if pname
== "[]" then
751 var arg1
= args
[1].to_i
752 if arg1
>= recvval
.length
or arg1
< 0 then
753 debug
("Illegal access on {recvval} for element {arg1}/{recvval.length}")
755 return v
.char_instance
(recvval
.chars
[arg1
])
756 else if pname
== "[]=" then
757 var arg1
= args
[1].to_i
758 if arg1
>= recvval
.length
or arg1
< 0 then
759 debug
("Illegal access on {recvval} for element {arg1}/{recvval.length}")
761 recvval
.chars
[arg1
] = args
[2].val
.as(Char)
763 else if pname
== "copy_to" then
764 # sig= copy_to(dest: NativeString, length: Int, from: Int, to: Int)
765 var destval
= args
[1].val
.as(Buffer)
766 var lenval
= args
[2].to_i
767 var fromval
= args
[3].to_i
768 var toval
= args
[4].to_i
770 debug
("Illegal access on {recvval} for element {fromval}/{recvval.length}")
772 if fromval
+ lenval
>= recvval
.length
then
773 debug
("Illegal access on {recvval} for element {fromval}+{lenval}/{recvval.length}")
776 debug
("Illegal access on {destval} for element {toval}/{destval.length}")
778 if toval
+ lenval
>= destval
.length
then
779 debug
("Illegal access on {destval} for element {toval}+{lenval}/{destval.length}")
781 recvval
.copy
(fromval
, lenval
, destval
, toval
)
783 else if pname
== "atoi" then
784 return v
.int_instance
(recvval
.to_i
)
786 else if pname
== "calloc_string" then
787 return v
.native_string_instance
("!" * args
[1].to_i
)
788 else if cname
== "NativeArray" then
789 var recvval
= args
.first
.val
.as(Array[Instance])
790 if pname
== "[]" then
791 if args
[1].to_i
>= recvval
.length
or args
[1].to_i
< 0 then
792 debug
("Illegal access on {recvval} for element {args[1].to_i}/{recvval.length}")
794 return recvval
[args
[1].to_i
]
795 else if pname
== "[]=" then
796 recvval
[args
[1].to_i
] = args
[2]
798 else if pname
== "copy_to" then
799 recvval
.copy
(0, args
[2].to_i
, args
[1].val
.as(Array[Instance]), 0)
802 else if pname
== "calloc_array" then
803 var recvtype
= args
.first
.mtype
.as(MClassType)
805 mtype
= recvtype
.supertype_to
(v
.mainmodule
, recvtype
, v
.mainmodule
.get_primitive_class
("ArrayCapable"))
806 mtype
= mtype
.arguments
.first
807 var val
= new Array[Instance].filled_with
(v
.null_instance
, args
[1].to_i
)
808 return new PrimitiveInstance[Array[Instance]](v
.mainmodule
.get_primitive_class
("NativeArray").get_mtype
([mtype
]), val
)
809 else if pname
== "native_argc" then
810 return v
.int_instance
(v
.arguments
.length
)
811 else if pname
== "native_argv" then
812 var txt
= v
.arguments
[args
[1].to_i
]
813 return v
.native_string_instance
(txt
)
815 fatal
(v
, "NOT YET IMPLEMENTED intern {mpropdef}")
820 redef class AbstractArray[E
]
821 fun copy
(start
: Int, len
: Int, dest
: AbstractArray[E
], new_start
: Int)
823 self.copy_to
(start
, len
, dest
, new_start
)
827 redef class AExternInitPropdef
828 redef fun call
(v
, mpropdef
, args
)
830 var pname
= mpropdef
.mproperty
.name
831 var cname
= mpropdef
.mclassdef
.mclass
.name
832 if pname
== "native_stdout" then
833 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, stdout
)
834 else if pname
== "native_stdin" then
835 return new PrimitiveInstance[IStream](mpropdef
.mclassdef
.mclass
.mclass_type
, stdin
)
836 else if pname
== "native_stderr" then
837 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, stderr
)
838 else if pname
== "io_open_read" then
839 var a1
= args
[1].val
.as(Buffer)
840 return new PrimitiveInstance[IStream](mpropdef
.mclassdef
.mclass
.mclass_type
, new IFStream.open
(a1
.to_s
))
841 else if pname
== "io_open_write" then
842 var a1
= args
[1].val
.as(Buffer)
843 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, new OFStream.open
(a1
.to_s
))
845 fatal
(v
, "NOT YET IMPLEMENTED extern init {mpropdef}")
850 redef class AExternMethPropdef
852 redef fun call
(v
, mpropdef
, args
)
854 var pname
= mpropdef
.mproperty
.name
855 var cname
= mpropdef
.mclassdef
.mclass
.name
856 if cname
== "Int" then
857 var recvval
= args
.first
.val
.as(Int)
858 if pname
== "rand" then
859 var res
= recvval
.rand
860 return v
.int_instance
(res
)
861 else if pname
== "native_int_to_s" then
862 return v
.native_string_instance
(recvval
.to_s
)
864 else if cname
== "NativeFile" then
865 var recvval
= args
.first
.val
866 if pname
== "io_write" then
867 var a1
= args
[1].val
.as(Buffer)
868 recvval
.as(OStream).write
(a1
.substring
(0, args
[2].to_i
))
870 else if pname
== "io_read" then
871 var str
= recvval
.as(IStream).read
(args
[2].to_i
)
872 var a1
= args
[1].val
.as(Buffer)
873 new Buffer.from
(str
).copy
(0, str
.length
, a1
, 0)
874 return v
.int_instance
(str
.length
)
875 else if pname
== "io_close" then
876 recvval
.as(IOS).close
877 return v
.int_instance
(0)
879 else if cname
== "NativeString" then
880 var recvval
= args
.first
.val
.as(Buffer)
881 if pname
== "file_exists" then
882 return v
.bool_instance
(recvval
.to_s
.file_exists
)
883 else if pname
== "file_mkdir" then
886 else if pname
== "file_chdir" then
889 else if pname
== "file_realpath" then
890 return v
.native_string_instance
(recvval
.to_s
.realpath
)
891 else if pname
== "get_environ" then
892 var txt
= recvval
.to_s
.environ
893 return v
.native_string_instance
(txt
)
894 else if pname
== "system" then
895 var res
= sys
.system
(recvval
.to_s
)
896 return v
.int_instance
(res
)
897 else if pname
== "atof" then
898 return v
.float_instance
(recvval
.to_f
)
900 else if cname
== "Int" then
901 if pname
== "rand" then
902 return v
.int_instance
(args
[0].to_i
.rand
)
904 else if cname
== "Float" then
905 if pname
== "cos" then
906 return v
.float_instance
(args
[0].to_f
.cos
)
907 else if pname
== "sin" then
908 return v
.float_instance
(args
[0].to_f
.sin
)
909 else if pname
== "tan" then
910 return v
.float_instance
(args
[0].to_f
.tan
)
911 else if pname
== "acos" then
912 return v
.float_instance
(args
[0].to_f
.acos
)
913 else if pname
== "asin" then
914 return v
.float_instance
(args
[0].to_f
.asin
)
915 else if pname
== "atan" then
916 return v
.float_instance
(args
[0].to_f
.atan
)
917 else if pname
== "sqrt" then
918 return v
.float_instance
(args
[0].to_f
.sqrt
)
919 else if pname
== "exp" then
920 return v
.float_instance
(args
[0].to_f
.exp
)
921 else if pname
== "log" then
922 return v
.float_instance
(args
[0].to_f
.log
)
923 else if pname
== "pow" then
924 return v
.float_instance
(args
[0].to_f
.pow
(args
[1].to_f
))
925 else if pname
== "rand" then
926 return v
.float_instance
(args
[0].to_f
.rand
)
928 else if pname
== "native_argc" then
929 return v
.int_instance
(v
.arguments
.length
)
930 else if pname
== "native_argv" then
931 var txt
= v
.arguments
[args
[1].to_i
]
932 return v
.native_string_instance
(txt
)
933 else if pname
== "get_time" then
934 return v
.int_instance
(get_time
)
935 else if pname
== "srand_from" then
936 srand_from
(args
[1].to_i
)
938 else if pname
== "atan2" then
939 return v
.float_instance
(atan2
(args
[1].to_f
, args
[2].to_f
))
940 else if pname
== "pi" then
941 return v
.float_instance
(pi
)
942 else if pname
== "lexer_goto" then
943 return v
.int_instance
(lexer_goto
(args
[1].to_i
, args
[2].to_i
))
944 else if pname
== "lexer_accept" then
945 return v
.int_instance
(lexer_accept
(args
[1].to_i
))
946 else if pname
== "parser_goto" then
947 return v
.int_instance
(parser_goto
(args
[1].to_i
, args
[2].to_i
))
948 else if pname
== "parser_action" then
949 return v
.int_instance
(parser_action
(args
[1].to_i
, args
[2].to_i
))
950 else if pname
== "file_getcwd" then
951 return v
.native_string_instance
(getcwd
)
953 fatal
(v
, "NOT YET IMPLEMENTED extern {mpropdef}")
958 redef class AAttrPropdef
959 redef fun call
(v
, mpropdef
, args
)
961 var recv
= args
.first
962 assert recv
isa MutableInstance
963 var attr
= self.mpropdef
.mproperty
964 if args
.length
== 1 then
965 return v
.read_attribute
(attr
, recv
)
967 assert args
.length
== 2
968 recv
.attributes
[attr
] = args
[1]
973 # Evaluate and set the default value of the attribute in `recv`
974 private fun init_expr
(v
: NaiveInterpreter, recv
: Instance)
976 assert recv
isa MutableInstance
977 var nexpr
= self.n_expr
978 if nexpr
!= null then
979 var f
= new Frame(self, self.mpropdef
.as(not null), [recv
])
981 var val
= v
.expr
(nexpr
)
984 assert not v
.is_escaping
985 recv
.attributes
[self.mpropdef
.mproperty
] = val
988 var mtype
= self.mpropdef
.static_mtype
.as(not null)
989 mtype
= mtype
.anchor_to
(v
.mainmodule
, recv
.mtype
.as(MClassType))
990 if mtype
isa MNullableType then
991 recv
.attributes
[self.mpropdef
.mproperty
] = v
.null_instance
996 redef class ADeferredMethPropdef
997 redef fun call
(v
, mpropdef
, args
)
999 fatal
(v
, "Abstract method `{mpropdef.mproperty.name}` called on `{args.first.mtype}`")
1004 redef class AClassdef
1005 # Execute an implicit `mpropdef` associated with the current node.
1006 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
1008 var super_inits
= self.super_inits
1009 if super_inits
!= null then
1010 assert args
.length
== 1
1011 for su
in super_inits
do
1016 var recv
= args
.first
1017 assert recv
isa MutableInstance
1019 # Collect undefined attributes
1020 for npropdef
in self.n_propdefs
do
1021 if npropdef
isa AAttrPropdef and npropdef
.n_expr
== null then
1022 recv
.attributes
[npropdef
.mpropdef
.mproperty
] = args
[i
]
1031 # Evaluate the node as a possible expression.
1032 # Return a possible value
1033 # NOTE: Do not call this method directly, but use `v.expr`
1034 # This method is here to be implemented by subclasses.
1035 private fun expr
(v
: NaiveInterpreter): nullable Instance
1037 fatal
(v
, "NOT YET IMPLEMENTED expr {class_name}")
1041 # Evaluate the node as a statement.
1042 # NOTE: Do not call this method directly, but use `v.stmt`
1043 # This method is here to be implemented by subclasses (no need to return something).
1044 private fun stmt
(v
: NaiveInterpreter)
1051 redef class ABlockExpr
1054 var last
= self.n_expr
.last
1055 for e
in self.n_expr
do
1056 if e
== last
then break
1058 if v
.is_escaping
then return null
1065 for e
in self.n_expr
do
1067 if v
.is_escaping
then return
1072 redef class AVardeclExpr
1075 var ne
= self.n_expr
1078 if i
== null then return
1079 v
.frame
.map
[self.variable
.as(not null)] = i
1084 redef class AVarExpr
1087 return v
.frame
.map
[self.variable
.as(not null)]
1091 redef class AVarAssignExpr
1094 var i
= v
.expr
(self.n_value
)
1095 if i
== null then return null
1096 v
.frame
.map
[self.variable
.as(not null)] = i
1101 redef class AVarReassignExpr
1104 var vari
= v
.frame
.map
[self.variable
.as(not null)]
1105 var value
= v
.expr
(self.n_value
)
1106 if value
== null then return
1107 var res
= v
.send
(reassign_callsite
.mproperty
, [vari
, value
])
1109 v
.frame
.map
[self.variable
.as(not null)] = res
1113 redef class ASelfExpr
1116 return v
.frame
.arguments
.first
1120 redef class AContinueExpr
1123 var ne
= self.n_expr
1126 if i
== null then return
1129 v
.continuemark
= self.escapemark
1133 redef class ABreakExpr
1136 var ne
= self.n_expr
1139 if i
== null then return
1142 v
.breakmark
= self.escapemark
1146 redef class AReturnExpr
1149 var ne
= self.n_expr
1152 if i
== null then return
1155 v
.returnmark
= v
.frame
1159 redef class AAbortExpr
1170 var cond
= v
.expr
(self.n_expr
)
1171 if cond
== null then return null
1172 if cond
.is_true
then
1173 return v
.expr
(self.n_then
.as(not null))
1175 return v
.expr
(self.n_else
.as(not null))
1181 var cond
= v
.expr
(self.n_expr
)
1182 if cond
== null then return
1183 if cond
.is_true
then
1191 redef class AIfexprExpr
1194 var cond
= v
.expr
(self.n_expr
)
1195 if cond
== null then return null
1196 if cond
.is_true
then
1197 return v
.expr
(self.n_then
)
1199 return v
.expr
(self.n_else
)
1207 v
.stmt
(self.n_block
)
1208 v
.is_break
(self.escapemark
) # Clear the break (if any)
1212 redef class AWhileExpr
1216 var cond
= v
.expr
(self.n_expr
)
1217 if cond
== null then return
1218 if not cond
.is_true
then return
1219 v
.stmt
(self.n_block
)
1220 if v
.is_break
(self.escapemark
) then return
1221 v
.is_continue
(self.escapemark
) # Clear the break
1222 if v
.is_escaping
then return
1227 redef class ALoopExpr
1231 v
.stmt
(self.n_block
)
1232 if v
.is_break
(self.escapemark
) then return
1233 v
.is_continue
(self.escapemark
) # Clear the break
1234 if v
.is_escaping
then return
1239 redef class AForExpr
1242 var col
= v
.expr
(self.n_expr
)
1243 if col
== null then return
1244 #self.debug("col {col}")
1245 var iter
= v
.send
(v
.force_get_primitive_method
("iterator", col
.mtype
), [col
]).as(not null)
1246 #self.debug("iter {iter}")
1248 var isok
= v
.send
(v
.force_get_primitive_method
("is_ok", iter
.mtype
), [iter
]).as(not null)
1249 if not isok
.is_true
then return
1250 if self.variables
.length
== 1 then
1251 var item
= v
.send
(v
.force_get_primitive_method
("item", iter
.mtype
), [iter
]).as(not null)
1252 #self.debug("item {item}")
1253 v
.frame
.map
[self.variables
.first
] = item
1254 else if self.variables
.length
== 2 then
1255 var key
= v
.send
(v
.force_get_primitive_method
("key", iter
.mtype
), [iter
]).as(not null)
1256 v
.frame
.map
[self.variables
[0]] = key
1257 var item
= v
.send
(v
.force_get_primitive_method
("item", iter
.mtype
), [iter
]).as(not null)
1258 v
.frame
.map
[self.variables
[1]] = item
1262 v
.stmt
(self.n_block
)
1263 if v
.is_break
(self.escapemark
) then return
1264 v
.is_continue
(self.escapemark
) # Clear the break
1265 if v
.is_escaping
then return
1266 v
.send
(v
.force_get_primitive_method
("next", iter
.mtype
), [iter
])
1271 redef class AAssertExpr
1274 var cond
= v
.expr
(self.n_expr
)
1275 if cond
== null then return
1276 if not cond
.is_true
then
1278 if v
.is_escaping
then return
1281 fatal
(v
, "Assert '{nid.text}' failed")
1283 fatal
(v
, "Assert failed")
1293 var cond
= v
.expr
(self.n_expr
)
1294 if cond
== null then return null
1295 if cond
.is_true
then return cond
1296 return v
.expr
(self.n_expr2
)
1300 redef class AImpliesExpr
1303 var cond
= v
.expr
(self.n_expr
)
1304 if cond
== null then return null
1305 if not cond
.is_true
then return v
.true_instance
1306 return v
.expr
(self.n_expr2
)
1310 redef class AAndExpr
1313 var cond
= v
.expr
(self.n_expr
)
1314 if cond
== null then return null
1315 if not cond
.is_true
then return cond
1316 return v
.expr
(self.n_expr2
)
1320 redef class ANotExpr
1323 var cond
= v
.expr
(self.n_expr
)
1324 if cond
== null then return null
1325 return v
.bool_instance
(not cond
.is_true
)
1329 redef class AOrElseExpr
1332 var i
= v
.expr
(self.n_expr
)
1333 if i
== null then return null
1334 if i
!= v
.null_instance
then return i
1335 return v
.expr
(self.n_expr2
)
1339 redef class AIntExpr
1342 return v
.int_instance
(self.value
.as(not null))
1346 redef class AFloatExpr
1349 return v
.float_instance
(self.value
.as(not null))
1353 redef class ACharExpr
1356 return v
.char_instance
(self.value
.as(not null))
1360 redef class AArrayExpr
1363 var val
= new Array[Instance]
1364 for nexpr
in self.n_exprs
.n_exprs
do
1365 var i
= v
.expr
(nexpr
)
1366 if i
== null then return null
1369 var mtype
= v
.unanchor_type
(self.mtype
.as(not null)).as(MClassType)
1370 var elttype
= mtype
.arguments
.first
1371 return v
.array_instance
(val
, elttype
)
1375 redef class AStringFormExpr
1378 var txt
= self.value
.as(not null)
1379 var nat
= v
.native_string_instance
(txt
)
1380 var res
= v
.send
(v
.force_get_primitive_method
("to_s", nat
.mtype
), [nat
]).as(not null)
1385 redef class ASuperstringExpr
1388 var array
= new Array[Instance]
1389 for nexpr
in n_exprs
do
1390 var i
= v
.expr
(nexpr
)
1391 if i
== null then return null
1394 var i
= v
.array_instance
(array
, v
.mainmodule
.get_primitive_class
("Object").mclass_type
)
1395 var res
= v
.send
(v
.force_get_primitive_method
("to_s", i
.mtype
), [i
])
1401 redef class ACrangeExpr
1404 var e1
= v
.expr
(self.n_expr
)
1405 if e1
== null then return null
1406 var e2
= v
.expr
(self.n_expr2
)
1407 if e2
== null then return null
1408 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1409 var res
= new MutableInstance(mtype
)
1410 v
.init_instance
(res
)
1411 v
.send
(v
.force_get_primitive_method
("init", mtype
), [res
, e1
, e2
])
1416 redef class AOrangeExpr
1419 var e1
= v
.expr
(self.n_expr
)
1420 if e1
== null then return null
1421 var e2
= v
.expr
(self.n_expr2
)
1422 if e2
== null then return null
1423 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1424 var res
= new MutableInstance(mtype
)
1425 v
.init_instance
(res
)
1426 v
.send
(v
.force_get_primitive_method
("without_last", mtype
), [res
, e1
, e2
])
1431 redef class ATrueExpr
1434 return v
.bool_instance
(true)
1438 redef class AFalseExpr
1441 return v
.bool_instance
(false)
1445 redef class ANullExpr
1448 return v
.null_instance
1452 redef class AIsaExpr
1455 var i
= v
.expr
(self.n_expr
)
1456 if i
== null then return null
1457 var mtype
= v
.unanchor_type
(self.cast_type
.as(not null))
1458 return v
.bool_instance
(v
.is_subtype
(i
.mtype
, mtype
))
1462 redef class AAsCastExpr
1465 var i
= v
.expr
(self.n_expr
)
1466 if i
== null then return null
1467 var mtype
= self.mtype
.as(not null)
1468 var amtype
= v
.unanchor_type
(mtype
)
1469 if not v
.is_subtype
(i
.mtype
, amtype
) then
1470 fatal
(v
, "Cast failed. Expected `{amtype}`, got `{i.mtype}`")
1476 redef class AAsNotnullExpr
1479 var i
= v
.expr
(self.n_expr
)
1480 if i
== null then return null
1481 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1482 if i
.mtype
isa MNullType then
1483 fatal
(v
, "Cast failed")
1489 redef class AParExpr
1492 return v
.expr
(self.n_expr
)
1496 redef class AOnceExpr
1499 if v
.onces
.has_key
(self) then
1500 return v
.onces
[self]
1502 var res
= v
.expr
(self.n_expr
)
1503 if res
== null then return null
1510 redef class ASendExpr
1513 var recv
= v
.expr
(self.n_expr
)
1514 if recv
== null then return null
1516 for a
in self.raw_arguments
.as(not null) do
1518 if i
== null then return null
1522 var res
= v
.send
(callsite
.mproperty
, args
)
1527 redef class ASendReassignFormExpr
1530 var recv
= v
.expr
(self.n_expr
)
1531 if recv
== null then return
1533 for a
in self.raw_arguments
.as(not null) do
1535 if i
== null then return
1538 var value
= v
.expr
(self.n_value
)
1539 if value
== null then return
1541 var read
= v
.send
(callsite
.mproperty
, args
)
1544 var write
= v
.send
(reassign_callsite
.mproperty
, [read
, value
])
1545 assert write
!= null
1549 v
.send
(write_callsite
.mproperty
, args
)
1553 redef class ASuperExpr
1556 var recv
= v
.frame
.arguments
.first
1558 for a
in self.n_args
.n_exprs
do
1560 if i
== null then return null
1563 if args
.length
== 1 then
1564 args
= v
.frame
.arguments
1567 var callsite
= self.callsite
1568 if callsite
!= null then
1569 if callsite
.mproperty
.intro
.msignature
.arity
== 0 then
1573 var res
= v
.send
(callsite
.mproperty
, args
)
1577 # stantard call-next-method
1578 var mpropdef
= v
.frame
.mpropdef
1579 mpropdef
= mpropdef
.lookup_next_definition
(v
.mainmodule
, recv
.mtype
)
1580 assert mpropdef
isa MMethodDef
1581 var res
= v
.call_without_varargs
(mpropdef
, args
)
1586 redef class ANewExpr
1589 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1590 var recv
: Instance = new MutableInstance(mtype
)
1591 v
.init_instance
(recv
)
1593 for a
in self.n_args
.n_exprs
do
1595 if i
== null then return null
1598 var res2
= v
.send
(callsite
.mproperty
, args
)
1599 if res2
!= null then
1600 #self.debug("got {res2} from {mproperty}. drop {recv}")
1607 redef class AAttrExpr
1610 var recv
= v
.expr
(self.n_expr
)
1611 if recv
== null then return null
1612 if recv
.mtype
isa MNullType then fatal
(v
, "Reciever is null")
1613 var mproperty
= self.mproperty
.as(not null)
1614 return v
.read_attribute
(mproperty
, recv
)
1618 redef class AAttrAssignExpr
1621 var recv
= v
.expr
(self.n_expr
)
1622 if recv
== null then return
1623 if recv
.mtype
isa MNullType then fatal
(v
, "Reciever is null")
1624 var i
= v
.expr
(self.n_value
)
1625 if i
== null then return
1626 var mproperty
= self.mproperty
.as(not null)
1627 assert recv
isa MutableInstance
1628 recv
.attributes
[mproperty
] = i
1632 redef class AAttrReassignExpr
1635 var recv
= v
.expr
(self.n_expr
)
1636 if recv
== null then return
1637 if recv
.mtype
isa MNullType then fatal
(v
, "Reciever is null")
1638 var value
= v
.expr
(self.n_value
)
1639 if value
== null then return
1640 var mproperty
= self.mproperty
.as(not null)
1641 var attr
= v
.read_attribute
(mproperty
, recv
)
1642 var res
= v
.send
(reassign_callsite
.mproperty
, [attr
, value
])
1644 assert recv
isa MutableInstance
1645 recv
.attributes
[mproperty
] = res
1649 redef class AIssetAttrExpr
1652 var recv
= v
.expr
(self.n_expr
)
1653 if recv
== null then return null
1654 if recv
.mtype
isa MNullType then fatal
(v
, "Reciever is null")
1655 var mproperty
= self.mproperty
.as(not null)
1656 assert recv
isa MutableInstance
1657 return v
.bool_instance
(recv
.attributes
.has_key
(mproperty
))
1661 redef class ADebugTypeExpr