interpreter: implements srand
[nit.git] / src / interpreter / naive_interpreter.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Interpretation of a Nit program directly on the AST
18 module naive_interpreter
19
20 import literal
21 import semantize
22 private import parser::tables
23 import mixin
24
25 redef class ToolContext
26 # --discover-call-trace
27 var opt_discover_call_trace = new OptionBool("Trace calls of the first invocation of a method", "--discover-call-trace")
28
29 redef init
30 do
31 super
32 self.option_context.add_option(self.opt_discover_call_trace)
33 end
34 end
35
36 redef class ModelBuilder
37 # Execute the program from the entry point (`Sys::main`) of the `mainmodule`
38 # `arguments` are the command-line arguments in order
39 # REQUIRE that:
40 # 1. the AST is fully loaded.
41 # 2. the model is fully built.
42 # 3. the instructions are fully analysed.
43 fun run_naive_interpreter(mainmodule: MModule, arguments: Array[String])
44 do
45 var time0 = get_time
46 self.toolcontext.info("*** START INTERPRETING ***", 1)
47
48 var interpreter = new NaiveInterpreter(self, mainmodule, arguments)
49 interpreter.start(mainmodule)
50
51 var time1 = get_time
52 self.toolcontext.info("*** END INTERPRETING: {time1-time0} ***", 2)
53 end
54 end
55
56 # The visitor that interprets the Nit Program by walking on the AST
57 class NaiveInterpreter
58 # The modelbuilder that know the AST and its associations with the model
59 var modelbuilder: ModelBuilder
60
61 # The main module of the program (used to lookup method)
62 var mainmodule: MModule
63
64 # The command line arguments of the interpreted program
65 # arguments.first is the program name
66 # arguments[1] is the first argument
67 var arguments: Array[String]
68
69 # The main Sys instance
70 var mainobj: nullable Instance is noinit
71
72 init
73 do
74 self.true_instance = new PrimitiveInstance[Bool](mainmodule.bool_type, true)
75 init_instance_primitive(self.true_instance)
76 self.false_instance = new PrimitiveInstance[Bool](mainmodule.bool_type, false)
77 init_instance_primitive(self.false_instance)
78 self.null_instance = new MutableInstance(mainmodule.model.null_type)
79 end
80
81 # Starts the interpreter on the main module of a program
82 fun start(mainmodule: MModule) do
83 var interpreter = self
84 var sys_type = mainmodule.sys_type
85 if sys_type == null then return # no class Sys
86 var mainobj = new MutableInstance(sys_type)
87 interpreter.mainobj = mainobj
88 interpreter.init_instance(mainobj)
89 var initprop = mainmodule.try_get_primitive_method("init", sys_type.mclass)
90 if initprop != null then
91 interpreter.send(initprop, [mainobj])
92 end
93 var mainprop = mainmodule.try_get_primitive_method("run", sys_type.mclass) or else
94 mainmodule.try_get_primitive_method("main", sys_type.mclass)
95 if mainprop != null then
96 interpreter.send(mainprop, [mainobj])
97 end
98 end
99
100 # Subtype test in the context of the mainmodule
101 fun is_subtype(sub, sup: MType): Bool
102 do
103 return sub.is_subtype(self.mainmodule, self.frame.arguments.first.mtype.as(MClassType), sup)
104 end
105
106 fun force_get_primitive_method(name: String, recv: MType): MMethod
107 do
108 assert recv isa MClassType
109 return self.modelbuilder.force_get_primitive_method(self.frame.current_node, name, recv.mclass, self.mainmodule)
110 end
111
112 # Is a return executed?
113 # Set this mark to skip the evaluation until the end of the specified method frame
114 var returnmark: nullable Frame = null
115
116 # Is a break or a continue executed?
117 # Set this mark to skip the evaluation until a labeled statement catch it with `is_escape`
118 var escapemark: nullable EscapeMark = null
119
120 # Is a return or a break or a continue executed?
121 # Use this function to know if you must skip the evaluation of statements
122 fun is_escaping: Bool do return returnmark != null or escapemark != null
123
124 # The value associated with the current return/break/continue, if any.
125 # Set the value when you set a escapemark.
126 # Read the value when you catch a mark or reach the end of a method
127 var escapevalue: nullable Instance = null
128
129 # If there is a break/continue and is associated with `escapemark`, then return true and clear the mark.
130 # If there is no break/continue or if `escapemark` is null then return false.
131 # Use this function to catch a potential break/continue.
132 fun is_escape(escapemark: nullable EscapeMark): Bool
133 do
134 if escapemark != null and self.escapemark == escapemark then
135 self.escapemark = null
136 return true
137 else
138 return false
139 end
140 end
141
142 # Evaluate `n` as an expression in the current context.
143 # Return the value of the expression.
144 # If `n` cannot be evaluated, then aborts.
145 fun expr(n: AExpr): nullable Instance
146 do
147 var frame = self.frame
148 var old = frame.current_node
149 frame.current_node = n
150 #n.debug("IN Execute expr")
151 var i = n.expr(self)
152 if i == null and not self.is_escaping then
153 n.debug("inconsitance: no value and not escaping.")
154 end
155 var implicit_cast_to = n.implicit_cast_to
156 if implicit_cast_to != null then
157 var mtype = self.unanchor_type(implicit_cast_to)
158 if not self.is_subtype(i.mtype, mtype) then n.fatal(self, "Cast failed. Expected `{implicit_cast_to}`, got `{i.mtype}`")
159 end
160
161 #n.debug("OUT Execute expr: value is {i}")
162 #if not is_subtype(i.mtype, n.mtype.as(not null)) then n.debug("Expected {n.mtype.as(not null)} got {i}")
163 frame.current_node = old
164 return i
165 end
166
167 # Evaluate `n` as a statement in the current context.
168 # Do nothing if `n` is null.
169 # If `n` cannot be evaluated, then aborts.
170 fun stmt(n: nullable AExpr)
171 do
172 if n != null then
173 var frame = self.frame
174 var old = frame.current_node
175 frame.current_node = n
176 #n.debug("Execute stmt")
177 n.stmt(self)
178 frame.current_node = old
179 end
180 end
181
182 # Map used to store values of nodes that must be evaluated once in the system (`AOnceExpr`)
183 var onces: Map[ANode, Instance] = new HashMap[ANode, Instance]
184
185 # Return the boolean instance associated with `val`.
186 fun bool_instance(val: Bool): Instance
187 do
188 if val then return self.true_instance else return self.false_instance
189 end
190
191 # Return the integer instance associated with `val`.
192 fun int_instance(val: Int): Instance
193 do
194 var ic = get_primitive_class("Int")
195 var instance = new PrimitiveInstance[Int](ic.mclass_type, val)
196 init_instance_primitive(instance)
197 return instance
198 end
199
200 # Return the char instance associated with `val`.
201 fun char_instance(val: Char): Instance
202 do
203 var ic = get_primitive_class("Char")
204 var instance = new PrimitiveInstance[Char](ic.mclass_type, val)
205 init_instance_primitive(instance)
206 return instance
207 end
208
209 # Return the float instance associated with `val`.
210 fun float_instance(val: Float): Instance
211 do
212 var ic = get_primitive_class("Float")
213 var instance = new PrimitiveInstance[Float](ic.mclass_type, val)
214 init_instance_primitive(instance)
215 return instance
216 end
217
218 # The unique instance of the `true` value.
219 var true_instance: Instance is noinit
220
221 # The unique instance of the `false` value.
222 var false_instance: Instance is noinit
223
224 # The unique instance of the `null` value.
225 var null_instance: Instance is noinit
226
227 # Return a new array made of `values`.
228 # The dynamic type of the result is Array[elttype].
229 fun array_instance(values: Array[Instance], elttype: MType): Instance
230 do
231 assert not elttype.need_anchor
232 var nat = new PrimitiveInstance[Array[Instance]](get_primitive_class("NativeArray").get_mtype([elttype]), values)
233 init_instance_primitive(nat)
234 var mtype = get_primitive_class("Array").get_mtype([elttype])
235 var res = new MutableInstance(mtype)
236 self.init_instance(res)
237 self.send(self.force_get_primitive_method("with_native", mtype), [res, nat, self.int_instance(values.length)])
238 return res
239 end
240
241 fun value_instance(object: Object): Instance
242 do
243 if object isa Int then
244 return int_instance(object)
245 else if object isa Bool then
246 return bool_instance(object)
247 else if object isa String then
248 return string_instance(object)
249 else
250 abort
251 end
252 end
253
254 # Return a new native string initialized with `txt`
255 fun native_string_instance(txt: String): Instance
256 do
257 var val = new FlatBuffer.from(txt)
258 val.add('\0')
259 var ic = get_primitive_class("NativeString")
260 var instance = new PrimitiveInstance[Buffer](ic.mclass_type, val)
261 init_instance_primitive(instance)
262 return instance
263 end
264
265 # Return a new String instance for `txt`
266 fun string_instance(txt: String): Instance
267 do
268 var nat = native_string_instance(txt)
269 var res = self.send(self.force_get_primitive_method("to_s_with_length", nat.mtype), [nat, self.int_instance(txt.length)])
270 assert res != null
271 return res
272 end
273
274 # The current frame used to store local variables of the current method executed
275 fun frame: Frame do return frames.first
276
277 # The stack of all frames. The first one is the current one.
278 var frames = new List[Frame]
279
280 # Return a stack trace. One line per function
281 fun stack_trace: String
282 do
283 var b = new FlatBuffer
284 b.append(",---- Stack trace -- - - -\n")
285 for f in frames do
286 b.append("| {f.mpropdef} ({f.current_node.location})\n")
287 end
288 b.append("`------------------- - - -")
289 return b.to_s
290 end
291
292 # Exit the program with a message
293 fun fatal(message: String)
294 do
295 if frames.is_empty then
296 print message
297 else
298 self.frame.current_node.fatal(self, message)
299 end
300 exit(1)
301 end
302
303 # Debug on the current node
304 fun debug(message: String)
305 do
306 if frames.is_empty then
307 print message
308 else
309 self.frame.current_node.debug(message)
310 end
311 end
312
313 # Retrieve the value of the variable in the current frame
314 fun read_variable(v: Variable): Instance
315 do
316 var f = frames.first
317 return f.map[v]
318 end
319
320 # Assign the value of the variable in the current frame
321 fun write_variable(v: Variable, value: Instance)
322 do
323 var f = frames.first
324 f.map[v] = value
325 end
326
327 # Store known methods, used to trace methods as they are reached
328 var discover_call_trace: Set[MMethodDef] = new HashSet[MMethodDef]
329
330 # Evaluate `args` as expressions in the call of `mpropdef` on `recv`.
331 # This method is used to manage varargs in signatures and returns the real array
332 # of instances to use in the call.
333 # Return `null` if one of the evaluation of the arguments return null.
334 fun varargize(mpropdef: MMethodDef, recv: Instance, args: SequenceRead[AExpr]): nullable Array[Instance]
335 do
336 var msignature = mpropdef.new_msignature or else mpropdef.msignature.as(not null)
337 var res = new Array[Instance]
338 res.add(recv)
339
340 if args.is_empty then return res
341
342 var vararg_rank = msignature.vararg_rank
343 var vararg_len = args.length - msignature.arity
344 if vararg_len < 0 then vararg_len = 0
345
346 for i in [0..msignature.arity[ do
347 if i == vararg_rank then
348 var ne = args[i]
349 if ne isa AVarargExpr then
350 var e = self.expr(ne.n_expr)
351 if e == null then return null
352 res.add(e)
353 continue
354 end
355 var vararg = new Array[Instance]
356 for j in [vararg_rank..vararg_rank+vararg_len] do
357 var e = self.expr(args[j])
358 if e == null then return null
359 vararg.add(e)
360 end
361 var elttype = msignature.mparameters[vararg_rank].mtype.anchor_to(self.mainmodule, recv.mtype.as(MClassType))
362 res.add(self.array_instance(vararg, elttype))
363 else
364 var j = i
365 if i > vararg_rank then j += vararg_len
366 var e = self.expr(args[j])
367 if e == null then return null
368 res.add(e)
369 end
370 end
371 return res
372 end
373
374 # Execute `mpropdef` for a `args` (where `args[0]` is the receiver).
375 # Return a value if `mpropdef` is a function, or null if it is a procedure.
376 # The call is direct/static. There is no message-sending/late-binding.
377 fun call(mpropdef: MMethodDef, args: Array[Instance]): nullable Instance
378 do
379 if self.modelbuilder.toolcontext.opt_discover_call_trace.value and not self.discover_call_trace.has(mpropdef) then
380 self.discover_call_trace.add mpropdef
381 self.debug("Discovered {mpropdef}")
382 end
383 assert args.length == mpropdef.msignature.arity + 1 else debug("Invalid arity for {mpropdef}. {args.length} arguments given.")
384
385 # Look for the AST node that implements the property
386 var mproperty = mpropdef.mproperty
387 var val = mpropdef.constant_value
388 if self.modelbuilder.mpropdef2npropdef.has_key(mpropdef) then
389 var npropdef = self.modelbuilder.mpropdef2npropdef[mpropdef]
390 self.parameter_check(npropdef, mpropdef, args)
391 return npropdef.call(self, mpropdef, args)
392 else if mproperty.is_root_init then
393 var nclassdef = self.modelbuilder.mclassdef2nclassdef[mpropdef.mclassdef]
394 self.parameter_check(nclassdef, mpropdef, args)
395 return nclassdef.call(self, mpropdef, args)
396 else if val != null then
397 return value_instance(val)
398 else
399 fatal("Fatal Error: method {mpropdef} not found in the AST")
400 abort
401 end
402 end
403
404 # Generate type checks in the C code to check covariant parameters
405 fun parameter_check(node: ANode, mpropdef: MMethodDef, args: Array[Instance])
406 do
407 var msignature = mpropdef.msignature
408 for i in [0..msignature.arity[ do
409 # skip test for vararg since the array is instantiated with the correct polymorphic type
410 if msignature.vararg_rank == i then continue
411
412 # skip if the cast is not required
413 var origmtype = mpropdef.mproperty.intro.msignature.mparameters[i].mtype
414 if not origmtype.need_anchor then continue
415
416 # get the parameter type
417 var mtype = msignature.mparameters[i].mtype
418 var anchor = args.first.mtype.as(MClassType)
419 var amtype = mtype.anchor_to(self.mainmodule, anchor)
420 if not args[i+1].mtype.is_subtype(self.mainmodule, anchor, amtype) then
421 node.fatal(self, "Cast failed. Expected `{mtype}`, got `{args[i+1].mtype}`")
422 end
423 end
424 end
425
426 # Common code for runtime injected calls and normal calls
427 fun send_commons(mproperty: MMethod, args: Array[Instance], mtype: MType): nullable Instance
428 do
429 if mtype isa MNullType then
430 if mproperty.name == "==" then
431 return self.bool_instance(args[0] == args[1])
432 else if mproperty.name == "!=" then
433 return self.bool_instance(args[0] != args[1])
434 end
435 #fatal("Receiver is null. {mproperty}. {args.join(" ")} {self.frame.current_node.class_name}")
436 fatal("Receiver is null")
437 end
438 return null
439 end
440
441 # Execute a full `callsite` for given `args`
442 # Use this method, instead of `send` to execute and control the additional behavior of the call-sites
443 fun callsite(callsite: nullable CallSite, arguments: Array[Instance]): nullable Instance
444 do
445 var initializers = callsite.mpropdef.initializers
446 if not initializers.is_empty then
447 var recv = arguments.first
448 var i = 1
449 for p in initializers do
450 if p isa MMethod then
451 var args = [recv]
452 for x in p.intro.msignature.mparameters do
453 args.add arguments[i]
454 i += 1
455 end
456 self.send(p, args)
457 else if p isa MAttribute then
458 assert recv isa MutableInstance
459 recv.attributes[p] = arguments[i]
460 i += 1
461 else abort
462 end
463 assert i == arguments.length
464
465 return send(callsite.mproperty, [recv])
466 end
467 return send(callsite.mproperty, arguments)
468 end
469
470 # Execute `mproperty` for a `args` (where `args[0]` is the receiver).
471 # Return a value if `mproperty` is a function, or null if it is a procedure.
472 # The call is polymorphic. There is a message-sending/late-binding according to the receiver (args[0]).
473 fun send(mproperty: MMethod, args: Array[Instance]): nullable Instance
474 do
475 var recv = args.first
476 var mtype = recv.mtype
477 var ret = send_commons(mproperty, args, mtype)
478 if ret != null then return ret
479 var propdef = mproperty.lookup_first_definition(self.mainmodule, mtype)
480 return self.call(propdef, args)
481 end
482
483 # Read the attribute `mproperty` of an instance `recv` and return its value.
484 # If the attribute in not yet initialized, then aborts with an error message.
485 fun read_attribute(mproperty: MAttribute, recv: Instance): Instance
486 do
487 assert recv isa MutableInstance
488 if not recv.attributes.has_key(mproperty) then
489 fatal("Uninitialized attribute {mproperty.name}")
490 abort
491 end
492 return recv.attributes[mproperty]
493 end
494
495 # Replace in `recv` the value of the attribute `mproperty` by `value`
496 fun write_attribute(mproperty: MAttribute, recv: Instance, value: Instance)
497 do
498 assert recv isa MutableInstance
499 recv.attributes[mproperty] = value
500 end
501
502 # Is the attribute `mproperty` initialized the instance `recv`?
503 fun isset_attribute(mproperty: MAttribute, recv: Instance): Bool
504 do
505 assert recv isa MutableInstance
506 return recv.attributes.has_key(mproperty)
507 end
508
509 # Collect attributes of a type in the order of their init
510 fun collect_attr_propdef(mtype: MType): Array[AAttrPropdef]
511 do
512 var cache = self.collect_attr_propdef_cache
513 if cache.has_key(mtype) then return cache[mtype]
514
515 var res = new Array[AAttrPropdef]
516 var cds = mtype.collect_mclassdefs(self.mainmodule).to_a
517 self.mainmodule.linearize_mclassdefs(cds)
518 for cd in cds do
519 if not self.modelbuilder.mclassdef2nclassdef.has_key(cd) then continue
520 var n = self.modelbuilder.mclassdef2nclassdef[cd]
521 for npropdef in n.n_propdefs do
522 if npropdef isa AAttrPropdef then
523 res.add(npropdef)
524 end
525 end
526 end
527
528 cache[mtype] = res
529 return res
530 end
531
532 private var collect_attr_propdef_cache = new HashMap[MType, Array[AAttrPropdef]]
533
534 # Fill the initial values of the newly created instance `recv`.
535 # `recv.mtype` is used to know what must be filled.
536 fun init_instance(recv: Instance)
537 do
538 for npropdef in collect_attr_propdef(recv.mtype) do
539 npropdef.init_expr(self, recv)
540 end
541 end
542
543 # A hook to initialize a `PrimitiveInstance`
544 fun init_instance_primitive(recv: Instance) do end
545
546 # Return the primitive `MClass` corresponding to the `name` given in parameter
547 # `name` : name of the primitive class
548 fun get_primitive_class(name: String): MClass
549 do
550 return mainmodule.get_primitive_class(name)
551 end
552
553 # This function determine the correct type according the reciever of the current definition (self).
554 fun unanchor_type(mtype: MType): MType
555 do
556 return mtype.anchor_to(self.mainmodule, self.frame.arguments.first.mtype.as(MClassType))
557 end
558
559 # Placebo instance used to mark internal error result when `null` already have a meaning.
560 # TODO: replace with multiple return or something better
561 var error_instance = new MutableInstance(modelbuilder.model.null_type) is lazy
562 end
563
564 # An instance represents a value of the executed program.
565 abstract class Instance
566 # The dynamic type of the instance
567 # ASSERT: not self.mtype.is_anchored
568 var mtype: MType
569
570 # return true if the instance is the true value.
571 # return false if the instance is the true value.
572 # else aborts
573 fun is_true: Bool do abort
574
575 # Return true if `self` IS `o` (using the Nit semantic of is)
576 fun eq_is(o: Instance): Bool do return self.is_same_instance(o)
577
578 # Human readable object identity "Type#number"
579 redef fun to_s do return "{mtype}"
580
581 # Return the integer value if the instance is an integer.
582 # else aborts
583 fun to_i: Int do abort
584
585 # Return the integer value if the instance is a float.
586 # else aborts
587 fun to_f: Float do abort
588
589 # The real value encapsulated if the instance is primitive.
590 # Else aborts.
591 fun val: Object do abort
592 end
593
594 # A instance with attribute (standards objects)
595 class MutableInstance
596 super Instance
597
598 # The values of the attributes
599 var attributes: Map[MAttribute, Instance] = new HashMap[MAttribute, Instance]
600 end
601
602 # Special instance to handle primitives values (int, bool, etc.)
603 # The trick it just to encapsulate the <<real>> value
604 class PrimitiveInstance[E: Object]
605 super Instance
606
607 # The real value encapsulated
608 redef var val: E
609
610 redef fun is_true
611 do
612 if val == true then return true
613 if val == false then return false
614 abort
615 end
616
617 redef fun ==(o)
618 do
619 if not o isa PrimitiveInstance[Object] then return false
620 return self.val == o.val
621 end
622
623 redef fun eq_is(o)
624 do
625 if not o isa PrimitiveInstance[Object] then return false
626 return self.val.is_same_instance(o.val)
627 end
628
629 redef fun to_s do return "{mtype}#{val.object_id}({val})"
630
631 redef fun to_i do return val.as(Int)
632
633 redef fun to_f do return val.as(Float)
634 end
635
636 # Information about local variables in a running method
637 class Frame
638 # The current visited node
639 # The node is stored by frame to keep a stack trace
640 var current_node: ANode
641 # The executed property.
642 # A Method in case of a call, an attribute in case of a default initialization.
643 var mpropdef: MPropDef
644 # Arguments of the method (the first is the receiver)
645 var arguments: Array[Instance]
646 # Mapping between a variable and the current value
647 private var map: Map[Variable, Instance] = new HashMap[Variable, Instance]
648 end
649
650 redef class ANode
651 # Aborts the program with a message
652 # `v` is used to know if a colored message is displayed or not
653 fun fatal(v: NaiveInterpreter, message: String)
654 do
655 if v.modelbuilder.toolcontext.opt_no_color.value == true then
656 sys.stderr.write("Runtime error: {message} ({location.file.filename}:{location.line_start})\n")
657 else
658 sys.stderr.write("{location}: Runtime error: {message}\n{location.colored_line("0;31")}\n")
659 sys.stderr.write(v.stack_trace)
660 sys.stderr.write("\n")
661 end
662 exit(1)
663 end
664 end
665
666 redef class APropdef
667 # Execute a `mpropdef` associated with the current node.
668 private fun call(v: NaiveInterpreter, mpropdef: MMethodDef, args: Array[Instance]): nullable Instance
669 do
670 fatal(v, "NOT YET IMPLEMENTED method kind {class_name}. {mpropdef}")
671 abort
672 end
673 end
674
675 redef class AMethPropdef
676 super TablesCapable
677
678 redef fun call(v, mpropdef, args)
679 do
680 var f = new Frame(self, self.mpropdef.as(not null), args)
681 var res = call_commons(v, mpropdef, args, f)
682 v.frames.shift
683 if v.returnmark == f then
684 v.returnmark = null
685 res = v.escapevalue
686 v.escapevalue = null
687 return res
688 end
689 return res
690 end
691
692 private fun call_commons(v: NaiveInterpreter, mpropdef: MMethodDef, arguments: Array[Instance], f: Frame): nullable Instance
693 do
694 v.frames.unshift(f)
695
696 for i in [0..mpropdef.msignature.arity[ do
697 var variable = self.n_signature.n_params[i].variable
698 assert variable != null
699 v.write_variable(variable, arguments[i+1])
700 end
701
702 if mpropdef.is_abstract then
703 v.fatal("Abstract method `{mpropdef.mproperty.name}` called on `{arguments.first.mtype}`")
704 abort
705 end
706
707 # Call the implicit super-init
708 var auto_super_inits = self.auto_super_inits
709 if auto_super_inits != null then
710 var args = [arguments.first]
711 for auto_super_init in auto_super_inits do
712 args.clear
713 for i in [0..auto_super_init.msignature.arity+1[ do
714 args.add(arguments[i])
715 end
716 assert auto_super_init.mproperty != mpropdef.mproperty
717 v.callsite(auto_super_init, args)
718 end
719 end
720 if auto_super_call then
721 # standard call-next-method
722 var superpd = mpropdef.lookup_next_definition(v.mainmodule, arguments.first.mtype)
723 v.call(superpd, arguments)
724 end
725
726 if mpropdef.is_intern or mpropdef.is_extern then
727 var res = intern_call(v, mpropdef, arguments)
728 if res != v.error_instance then return res
729 end
730
731 if n_block != null then
732 v.stmt(self.n_block)
733 return null
734 end
735
736 if mpropdef.is_intern then
737 fatal(v, "NOT YET IMPLEMENTED intern {mpropdef}")
738 else if mpropdef.is_extern then
739 fatal(v, "NOT YET IMPLEMENTED extern {mpropdef}")
740 else
741 fatal(v, "NOT YET IMPLEMENTED <wat?> {mpropdef}")
742 end
743 abort
744 end
745
746 # Interprets a intern or a shortcut extern method.
747 # Returns the result for a function, `null` for a procedure, or `error_instance` if the method is unknown.
748 private fun intern_call(v: NaiveInterpreter, mpropdef: MMethodDef, args: Array[Instance]): nullable Instance
749 do
750 var pname = mpropdef.mproperty.name
751 var cname = mpropdef.mclassdef.mclass.name
752 if pname == "output" then
753 var recv = args.first
754 recv.val.output
755 return null
756 else if pname == "object_id" then
757 var recv = args.first
758 if recv isa PrimitiveInstance[Object] then
759 return v.int_instance(recv.val.object_id)
760 else
761 return v.int_instance(recv.object_id)
762 end
763 else if pname == "output_class_name" then
764 var recv = args.first
765 print recv.mtype
766 return null
767 else if pname == "native_class_name" then
768 var recv = args.first
769 var txt = recv.mtype.to_s
770 return v.native_string_instance(txt)
771 else if pname == "==" then
772 # == is correctly redefined for instances
773 return v.bool_instance(args[0] == args[1])
774 else if pname == "!=" then
775 return v.bool_instance(args[0] != args[1])
776 else if pname == "is_same_type" then
777 return v.bool_instance(args[0].mtype == args[1].mtype)
778 else if pname == "is_same_instance" then
779 return v.bool_instance(args[0].eq_is(args[1]))
780 else if pname == "exit" then
781 exit(args[1].to_i)
782 abort
783 else if pname == "sys" then
784 return v.mainobj
785 else if cname == "Int" then
786 var recvval = args[0].to_i
787 if pname == "unary -" then
788 return v.int_instance(-args[0].to_i)
789 else if pname == "+" then
790 return v.int_instance(args[0].to_i + args[1].to_i)
791 else if pname == "-" then
792 return v.int_instance(args[0].to_i - args[1].to_i)
793 else if pname == "*" then
794 return v.int_instance(args[0].to_i * args[1].to_i)
795 else if pname == "%" then
796 return v.int_instance(args[0].to_i % args[1].to_i)
797 else if pname == "/" then
798 return v.int_instance(args[0].to_i / args[1].to_i)
799 else if pname == "<" then
800 return v.bool_instance(args[0].to_i < args[1].to_i)
801 else if pname == ">" then
802 return v.bool_instance(args[0].to_i > args[1].to_i)
803 else if pname == "<=" then
804 return v.bool_instance(args[0].to_i <= args[1].to_i)
805 else if pname == ">=" then
806 return v.bool_instance(args[0].to_i >= args[1].to_i)
807 else if pname == "<=>" then
808 return v.int_instance(args[0].to_i <=> args[1].to_i)
809 else if pname == "ascii" then
810 return v.char_instance(args[0].to_i.ascii)
811 else if pname == "to_f" then
812 return v.float_instance(args[0].to_i.to_f)
813 else if pname == "lshift" then
814 return v.int_instance(args[0].to_i.lshift(args[1].to_i))
815 else if pname == "rshift" then
816 return v.int_instance(args[0].to_i.rshift(args[1].to_i))
817 else if pname == "rand" then
818 var res = recvval.rand
819 return v.int_instance(res)
820 else if pname == "bin_and" then
821 return v.int_instance(args[0].to_i.bin_and(args[1].to_i))
822 else if pname == "bin_or" then
823 return v.int_instance(args[0].to_i.bin_or(args[1].to_i))
824 else if pname == "bin_xor" then
825 return v.int_instance(args[0].to_i.bin_xor(args[1].to_i))
826 else if pname == "bin_not" then
827 return v.int_instance(args[0].to_i.bin_not)
828 else if pname == "native_int_to_s" then
829 return v.native_string_instance(recvval.to_s)
830 else if pname == "strerror_ext" then
831 return v.native_string_instance(recvval.strerror)
832 end
833 else if cname == "Char" then
834 var recv = args[0].val.as(Char)
835 if pname == "ascii" then
836 return v.int_instance(recv.ascii)
837 else if pname == "successor" then
838 return v.char_instance(recv.successor(args[1].to_i))
839 else if pname == "predecessor" then
840 return v.char_instance(recv.predecessor(args[1].to_i))
841 else if pname == "<" then
842 return v.bool_instance(recv < args[1].val.as(Char))
843 else if pname == ">" then
844 return v.bool_instance(recv > args[1].val.as(Char))
845 else if pname == "<=" then
846 return v.bool_instance(recv <= args[1].val.as(Char))
847 else if pname == ">=" then
848 return v.bool_instance(recv >= args[1].val.as(Char))
849 else if pname == "<=>" then
850 return v.int_instance(recv <=> args[1].val.as(Char))
851 end
852 else if cname == "Float" then
853 var recv = args[0].to_f
854 if pname == "unary -" then
855 return v.float_instance(-recv)
856 else if pname == "+" then
857 return v.float_instance(recv + args[1].to_f)
858 else if pname == "-" then
859 return v.float_instance(recv - args[1].to_f)
860 else if pname == "*" then
861 return v.float_instance(recv * args[1].to_f)
862 else if pname == "/" then
863 return v.float_instance(recv / args[1].to_f)
864 else if pname == "<" then
865 return v.bool_instance(recv < args[1].to_f)
866 else if pname == ">" then
867 return v.bool_instance(recv > args[1].to_f)
868 else if pname == "<=" then
869 return v.bool_instance(recv <= args[1].to_f)
870 else if pname == ">=" then
871 return v.bool_instance(recv >= args[1].to_f)
872 else if pname == "to_i" then
873 return v.int_instance(recv.to_i)
874 else if pname == "cos" then
875 return v.float_instance(args[0].to_f.cos)
876 else if pname == "sin" then
877 return v.float_instance(args[0].to_f.sin)
878 else if pname == "tan" then
879 return v.float_instance(args[0].to_f.tan)
880 else if pname == "acos" then
881 return v.float_instance(args[0].to_f.acos)
882 else if pname == "asin" then
883 return v.float_instance(args[0].to_f.asin)
884 else if pname == "atan" then
885 return v.float_instance(args[0].to_f.atan)
886 else if pname == "sqrt" then
887 return v.float_instance(args[0].to_f.sqrt)
888 else if pname == "exp" then
889 return v.float_instance(args[0].to_f.exp)
890 else if pname == "log" then
891 return v.float_instance(args[0].to_f.log)
892 else if pname == "pow" then
893 return v.float_instance(args[0].to_f.pow(args[1].to_f))
894 else if pname == "rand" then
895 return v.float_instance(args[0].to_f.rand)
896 else if pname == "abs" then
897 return v.float_instance(args[0].to_f.abs)
898 else if pname == "hypot_with" then
899 return v.float_instance(args[0].to_f.hypot_with(args[1].to_f))
900 else if pname == "is_nan" then
901 return v.bool_instance(args[0].to_f.is_nan)
902 else if pname == "is_inf_extern" then
903 return v.bool_instance(args[0].to_f.is_inf != 0)
904 end
905 else if cname == "NativeString" then
906 if pname == "new" then
907 return v.native_string_instance("!" * args[1].to_i)
908 end
909 var recvval = args.first.val.as(Buffer)
910 if pname == "[]" then
911 var arg1 = args[1].to_i
912 if arg1 >= recvval.length or arg1 < 0 then
913 debug("Illegal access on {recvval} for element {arg1}/{recvval.length}")
914 end
915 return v.char_instance(recvval.chars[arg1])
916 else if pname == "[]=" then
917 var arg1 = args[1].to_i
918 if arg1 >= recvval.length or arg1 < 0 then
919 debug("Illegal access on {recvval} for element {arg1}/{recvval.length}")
920 end
921 recvval.chars[arg1] = args[2].val.as(Char)
922 return null
923 else if pname == "copy_to" then
924 # sig= copy_to(dest: NativeString, length: Int, from: Int, to: Int)
925 var destval = args[1].val.as(FlatBuffer)
926 var lenval = args[2].to_i
927 var fromval = args[3].to_i
928 var toval = args[4].to_i
929 if fromval < 0 then
930 debug("Illegal access on {recvval} for element {fromval}/{recvval.length}")
931 end
932 if fromval + lenval >= recvval.length then
933 debug("Illegal access on {recvval} for element {fromval}+{lenval}/{recvval.length}")
934 end
935 if toval < 0 then
936 debug("Illegal access on {destval} for element {toval}/{destval.length}")
937 end
938 if toval + lenval >= destval.length then
939 debug("Illegal access on {destval} for element {toval}+{lenval}/{destval.length}")
940 end
941 recvval.as(FlatBuffer).copy(fromval, lenval, destval, toval)
942 return null
943 else if pname == "atoi" then
944 return v.int_instance(recvval.to_i)
945 else if pname == "file_exists" then
946 return v.bool_instance(recvval.to_s.file_exists)
947 else if pname == "file_mkdir" then
948 recvval.to_s.mkdir
949 return null
950 else if pname == "file_chdir" then
951 recvval.to_s.chdir
952 return null
953 else if pname == "file_realpath" then
954 return v.native_string_instance(recvval.to_s.realpath)
955 else if pname == "get_environ" then
956 var txt = recvval.to_s.environ
957 return v.native_string_instance(txt)
958 else if pname == "system" then
959 var res = sys.system(recvval.to_s)
960 return v.int_instance(res)
961 else if pname == "atof" then
962 return v.float_instance(recvval.to_f)
963 end
964 else if pname == "calloc_string" then
965 return v.native_string_instance("!" * args[1].to_i)
966 else if cname == "NativeArray" then
967 if pname == "new" then
968 var val = new Array[Instance].filled_with(v.null_instance, args[1].to_i)
969 var instance = new PrimitiveInstance[Array[Instance]](args[0].mtype, val)
970 v.init_instance_primitive(instance)
971 return instance
972 end
973 var recvval = args.first.val.as(Array[Instance])
974 if pname == "[]" then
975 if args[1].to_i >= recvval.length or args[1].to_i < 0 then
976 debug("Illegal access on {recvval} for element {args[1].to_i}/{recvval.length}")
977 end
978 return recvval[args[1].to_i]
979 else if pname == "[]=" then
980 recvval[args[1].to_i] = args[2]
981 return null
982 else if pname == "length" then
983 return v.int_instance(recvval.length)
984 else if pname == "copy_to" then
985 recvval.copy(0, args[2].to_i, args[1].val.as(Array[Instance]), 0)
986 return null
987 end
988 else if cname == "NativeFile" then
989 if pname == "native_stdout" then
990 var instance = new PrimitiveInstance[OStream](mpropdef.mclassdef.mclass.mclass_type, sys.stdout)
991 v.init_instance_primitive(instance)
992 return instance
993 else if pname == "native_stdin" then
994 var instance = new PrimitiveInstance[IStream](mpropdef.mclassdef.mclass.mclass_type, sys.stdin)
995 v.init_instance_primitive(instance)
996 return instance
997 else if pname == "native_stderr" then
998 var instance = new PrimitiveInstance[OStream](mpropdef.mclassdef.mclass.mclass_type, sys.stderr)
999 v.init_instance_primitive(instance)
1000 return instance
1001 else if pname == "io_open_read" then
1002 var a1 = args[1].val.as(Buffer)
1003 var instance = new PrimitiveInstance[IStream](mpropdef.mclassdef.mclass.mclass_type, new IFStream.open(a1.to_s))
1004 v.init_instance_primitive(instance)
1005 return instance
1006 else if pname == "io_open_write" then
1007 var a1 = args[1].val.as(Buffer)
1008 var instance = new PrimitiveInstance[OStream](mpropdef.mclassdef.mclass.mclass_type, new OFStream.open(a1.to_s))
1009 v.init_instance_primitive(instance)
1010 return instance
1011 end
1012 var recvval = args.first.val
1013 if pname == "io_write" then
1014 var a1 = args[1].val.as(Buffer)
1015 recvval.as(OStream).write(a1.substring(0, args[2].to_i).to_s)
1016 return args[2]
1017 else if pname == "io_read" then
1018 var str = recvval.as(IStream).read(args[2].to_i)
1019 var a1 = args[1].val.as(Buffer)
1020 new FlatBuffer.from(str).copy(0, str.length, a1.as(FlatBuffer), 0)
1021 return v.int_instance(str.length)
1022 else if pname == "io_close" then
1023 recvval.as(IOS).close
1024 return v.int_instance(0)
1025 else if pname == "address_is_null" then
1026 return v.false_instance
1027 end
1028 else if pname == "calloc_array" then
1029 var recvtype = args.first.mtype.as(MClassType)
1030 var mtype: MType
1031 mtype = recvtype.supertype_to(v.mainmodule, recvtype, v.get_primitive_class("ArrayCapable"))
1032 mtype = mtype.arguments.first
1033 var val = new Array[Instance].filled_with(v.null_instance, args[1].to_i)
1034 var instance = new PrimitiveInstance[Array[Instance]](v.get_primitive_class("NativeArray").get_mtype([mtype]), val)
1035 v.init_instance_primitive(instance)
1036 return instance
1037 else if pname == "native_argc" then
1038 return v.int_instance(v.arguments.length)
1039 else if pname == "native_argv" then
1040 var txt = v.arguments[args[1].to_i]
1041 return v.native_string_instance(txt)
1042 else if pname == "native_argc" then
1043 return v.int_instance(v.arguments.length)
1044 else if pname == "native_argv" then
1045 var txt = v.arguments[args[1].to_i]
1046 return v.native_string_instance(txt)
1047 else if pname == "get_time" then
1048 return v.int_instance(get_time)
1049 else if pname == "srand" then
1050 srand
1051 return null
1052 else if pname == "srand_from" then
1053 srand_from(args[1].to_i)
1054 return null
1055 else if pname == "atan2" then
1056 return v.float_instance(atan2(args[1].to_f, args[2].to_f))
1057 else if pname == "pi" then
1058 return v.float_instance(pi)
1059 else if pname == "lexer_goto" then
1060 return v.int_instance(lexer_goto(args[1].to_i, args[2].to_i))
1061 else if pname == "lexer_accept" then
1062 return v.int_instance(lexer_accept(args[1].to_i))
1063 else if pname == "parser_goto" then
1064 return v.int_instance(parser_goto(args[1].to_i, args[2].to_i))
1065 else if pname == "parser_action" then
1066 return v.int_instance(parser_action(args[1].to_i, args[2].to_i))
1067 else if pname == "file_getcwd" then
1068 return v.native_string_instance(getcwd)
1069 else if pname == "errno" then
1070 return v.int_instance(sys.errno)
1071 else if pname == "address_is_null" then
1072 return v.false_instance
1073 end
1074 return v.error_instance
1075 end
1076 end
1077
1078 redef class AbstractArray[E]
1079 fun copy(start: Int, len: Int, dest: AbstractArray[E], new_start: Int)
1080 do
1081 self.copy_to(start, len, dest, new_start)
1082 end
1083 end
1084
1085 redef class AAttrPropdef
1086 redef fun call(v, mpropdef, args)
1087 do
1088 var recv = args.first
1089 assert recv isa MutableInstance
1090 var attr = self.mpropdef.mproperty
1091 if mpropdef == mreadpropdef then
1092 assert args.length == 1
1093 if not is_lazy or v.isset_attribute(attr, recv) then return v.read_attribute(attr, recv)
1094 return evaluate_expr(v, recv)
1095 else if mpropdef == mwritepropdef then
1096 assert args.length == 2
1097 v.write_attribute(attr, recv, args[1])
1098 return null
1099 else
1100 abort
1101 end
1102 end
1103
1104 # Evaluate and set the default value of the attribute in `recv`
1105 private fun init_expr(v: NaiveInterpreter, recv: Instance)
1106 do
1107 if is_lazy then return
1108 if has_value then
1109 evaluate_expr(v, recv)
1110 return
1111 end
1112 var mtype = self.mpropdef.static_mtype.as(not null)
1113 mtype = mtype.anchor_to(v.mainmodule, recv.mtype.as(MClassType))
1114 if mtype isa MNullableType then
1115 v.write_attribute(self.mpropdef.mproperty, recv, v.null_instance)
1116 end
1117 end
1118
1119 private fun evaluate_expr(v: NaiveInterpreter, recv: Instance): Instance
1120 do
1121 assert recv isa MutableInstance
1122 var f = new Frame(self, self.mpropdef.as(not null), [recv])
1123 v.frames.unshift(f)
1124
1125 var val
1126
1127 var nexpr = self.n_expr
1128 var nblock = self.n_block
1129 if nexpr != null then
1130 val = v.expr(nexpr)
1131 else if nblock != null then
1132 v.stmt(nblock)
1133 assert v.returnmark == f
1134 val = v.escapevalue
1135 v.returnmark = null
1136 v.escapevalue = null
1137 else
1138 abort
1139 end
1140 assert val != null
1141
1142 v.frames.shift
1143 assert not v.is_escaping
1144 v.write_attribute(self.mpropdef.mproperty, recv, val)
1145 return val
1146 end
1147 end
1148
1149 redef class AClassdef
1150 # Execute an implicit `mpropdef` associated with the current node.
1151 private fun call(v: NaiveInterpreter, mpropdef: MMethodDef, args: Array[Instance]): nullable Instance
1152 do
1153 if mpropdef.mproperty.is_root_init then
1154 assert args.length == 1
1155 if not mpropdef.is_intro then
1156 # standard call-next-method
1157 var superpd = mpropdef.lookup_next_definition(v.mainmodule, args.first.mtype)
1158 v.call(superpd, args)
1159 end
1160 return null
1161 else
1162 abort
1163 end
1164 end
1165 end
1166
1167 redef class AExpr
1168 # Evaluate the node as a possible expression.
1169 # Return a possible value
1170 # NOTE: Do not call this method directly, but use `v.expr`
1171 # This method is here to be implemented by subclasses.
1172 private fun expr(v: NaiveInterpreter): nullable Instance
1173 do
1174 fatal(v, "NOT YET IMPLEMENTED expr {class_name}")
1175 abort
1176 end
1177
1178 # Evaluate the node as a statement.
1179 # NOTE: Do not call this method directly, but use `v.stmt`
1180 # This method is here to be implemented by subclasses (no need to return something).
1181 private fun stmt(v: NaiveInterpreter)
1182 do
1183 expr(v)
1184 end
1185
1186 end
1187
1188 redef class ABlockExpr
1189 redef fun expr(v)
1190 do
1191 var last = self.n_expr.last
1192 for e in self.n_expr do
1193 if e == last then break
1194 v.stmt(e)
1195 if v.is_escaping then return null
1196 end
1197 return last.expr(v)
1198 end
1199
1200 redef fun stmt(v)
1201 do
1202 for e in self.n_expr do
1203 v.stmt(e)
1204 if v.is_escaping then return
1205 end
1206 end
1207 end
1208
1209 redef class AVardeclExpr
1210 redef fun stmt(v)
1211 do
1212 var ne = self.n_expr
1213 if ne != null then
1214 var i = v.expr(ne)
1215 if i == null then return
1216 v.write_variable(self.variable.as(not null), i)
1217 end
1218 end
1219 end
1220
1221 redef class AVarExpr
1222 redef fun expr(v)
1223 do
1224 return v.read_variable(self.variable.as(not null))
1225 end
1226 end
1227
1228 redef class AVarAssignExpr
1229 redef fun expr(v)
1230 do
1231 var i = v.expr(self.n_value)
1232 if i == null then return null
1233 v.write_variable(self.variable.as(not null), i)
1234 return i
1235 end
1236 end
1237
1238 redef class AVarReassignExpr
1239 redef fun stmt(v)
1240 do
1241 var variable = self.variable.as(not null)
1242 var vari = v.read_variable(variable)
1243 var value = v.expr(self.n_value)
1244 if value == null then return
1245 var res = v.callsite(reassign_callsite, [vari, value])
1246 assert res != null
1247 v.write_variable(variable, res)
1248 end
1249 end
1250
1251 redef class ASelfExpr
1252 redef fun expr(v)
1253 do
1254 return v.frame.arguments.first
1255 end
1256 end
1257
1258 redef class AEscapeExpr
1259 redef fun stmt(v)
1260 do
1261 var ne = self.n_expr
1262 if ne != null then
1263 var i = v.expr(ne)
1264 if i == null then return
1265 v.escapevalue = i
1266 end
1267 v.escapemark = self.escapemark
1268 end
1269 end
1270
1271 redef class AReturnExpr
1272 redef fun stmt(v)
1273 do
1274 var ne = self.n_expr
1275 if ne != null then
1276 var i = v.expr(ne)
1277 if i == null then return
1278 v.escapevalue = i
1279 end
1280 v.returnmark = v.frame
1281 end
1282 end
1283
1284 redef class AAbortExpr
1285 redef fun stmt(v)
1286 do
1287 fatal(v, "Aborted")
1288 exit(1)
1289 end
1290 end
1291
1292 redef class AIfExpr
1293 redef fun expr(v)
1294 do
1295 var cond = v.expr(self.n_expr)
1296 if cond == null then return null
1297 if cond.is_true then
1298 return v.expr(self.n_then.as(not null))
1299 else
1300 return v.expr(self.n_else.as(not null))
1301 end
1302 end
1303
1304 redef fun stmt(v)
1305 do
1306 var cond = v.expr(self.n_expr)
1307 if cond == null then return
1308 if cond.is_true then
1309 v.stmt(self.n_then)
1310 else
1311 v.stmt(self.n_else)
1312 end
1313 end
1314 end
1315
1316 redef class AIfexprExpr
1317 redef fun expr(v)
1318 do
1319 var cond = v.expr(self.n_expr)
1320 if cond == null then return null
1321 if cond.is_true then
1322 return v.expr(self.n_then)
1323 else
1324 return v.expr(self.n_else)
1325 end
1326 end
1327 end
1328
1329 redef class ADoExpr
1330 redef fun stmt(v)
1331 do
1332 v.stmt(self.n_block)
1333 v.is_escape(self.break_mark) # Clear the break (if any)
1334 end
1335 end
1336
1337 redef class AWhileExpr
1338 redef fun stmt(v)
1339 do
1340 loop
1341 var cond = v.expr(self.n_expr)
1342 if cond == null then return
1343 if not cond.is_true then return
1344 v.stmt(self.n_block)
1345 if v.is_escape(self.break_mark) then return
1346 v.is_escape(self.continue_mark) # Clear the break
1347 if v.is_escaping then return
1348 end
1349 end
1350 end
1351
1352 redef class ALoopExpr
1353 redef fun stmt(v)
1354 do
1355 loop
1356 v.stmt(self.n_block)
1357 if v.is_escape(self.break_mark) then return
1358 v.is_escape(self.continue_mark) # Clear the break
1359 if v.is_escaping then return
1360 end
1361 end
1362 end
1363
1364 redef class AForExpr
1365 redef fun stmt(v)
1366 do
1367 var col = v.expr(self.n_expr)
1368 if col == null then return
1369 if col.mtype isa MNullType then fatal(v, "Receiver is null")
1370
1371 #self.debug("col {col}")
1372 var iter = v.callsite(method_iterator, [col]).as(not null)
1373 #self.debug("iter {iter}")
1374 loop
1375 var isok = v.callsite(method_is_ok, [iter]).as(not null)
1376 if not isok.is_true then break
1377 if self.variables.length == 1 then
1378 var item = v.callsite(method_item, [iter]).as(not null)
1379 #self.debug("item {item}")
1380 v.write_variable(self.variables.first, item)
1381 else if self.variables.length == 2 then
1382 var key = v.callsite(method_key, [iter]).as(not null)
1383 v.write_variable(self.variables[0], key)
1384 var item = v.callsite(method_item, [iter]).as(not null)
1385 v.write_variable(self.variables[1], item)
1386 else
1387 abort
1388 end
1389 v.stmt(self.n_block)
1390 if v.is_escape(self.break_mark) then break
1391 v.is_escape(self.continue_mark) # Clear the break
1392 if v.is_escaping then break
1393 v.callsite(method_next, [iter])
1394 end
1395 var method_finish = self.method_finish
1396 if method_finish != null then
1397 v.callsite(method_finish, [iter])
1398 end
1399 end
1400 end
1401
1402 redef class AAssertExpr
1403 redef fun stmt(v)
1404 do
1405 var cond = v.expr(self.n_expr)
1406 if cond == null then return
1407 if not cond.is_true then
1408 v.stmt(self.n_else)
1409 if v.is_escaping then return
1410 var nid = self.n_id
1411 if nid != null then
1412 fatal(v, "Assert '{nid.text}' failed")
1413 else
1414 fatal(v, "Assert failed")
1415 end
1416 exit(1)
1417 end
1418 end
1419 end
1420
1421 redef class AOrExpr
1422 redef fun expr(v)
1423 do
1424 var cond = v.expr(self.n_expr)
1425 if cond == null then return null
1426 if cond.is_true then return cond
1427 return v.expr(self.n_expr2)
1428 end
1429 end
1430
1431 redef class AImpliesExpr
1432 redef fun expr(v)
1433 do
1434 var cond = v.expr(self.n_expr)
1435 if cond == null then return null
1436 if not cond.is_true then return v.true_instance
1437 return v.expr(self.n_expr2)
1438 end
1439 end
1440
1441 redef class AAndExpr
1442 redef fun expr(v)
1443 do
1444 var cond = v.expr(self.n_expr)
1445 if cond == null then return null
1446 if not cond.is_true then return cond
1447 return v.expr(self.n_expr2)
1448 end
1449 end
1450
1451 redef class ANotExpr
1452 redef fun expr(v)
1453 do
1454 var cond = v.expr(self.n_expr)
1455 if cond == null then return null
1456 return v.bool_instance(not cond.is_true)
1457 end
1458 end
1459
1460 redef class AOrElseExpr
1461 redef fun expr(v)
1462 do
1463 var i = v.expr(self.n_expr)
1464 if i == null then return null
1465 if i != v.null_instance then return i
1466 return v.expr(self.n_expr2)
1467 end
1468 end
1469
1470 redef class AIntExpr
1471 redef fun expr(v)
1472 do
1473 return v.int_instance(self.value.as(not null))
1474 end
1475 end
1476
1477 redef class AFloatExpr
1478 redef fun expr(v)
1479 do
1480 return v.float_instance(self.value.as(not null))
1481 end
1482 end
1483
1484 redef class ACharExpr
1485 redef fun expr(v)
1486 do
1487 return v.char_instance(self.value.as(not null))
1488 end
1489 end
1490
1491 redef class AArrayExpr
1492 redef fun expr(v)
1493 do
1494 var val = new Array[Instance]
1495 for nexpr in self.n_exprs.n_exprs do
1496 var i = v.expr(nexpr)
1497 if i == null then return null
1498 val.add(i)
1499 end
1500 var mtype = v.unanchor_type(self.mtype.as(not null)).as(MClassType)
1501 var elttype = mtype.arguments.first
1502 return v.array_instance(val, elttype)
1503 end
1504 end
1505
1506 redef class AStringFormExpr
1507 redef fun expr(v)
1508 do
1509 var txt = self.value.as(not null)
1510 return v.string_instance(txt)
1511 end
1512 end
1513
1514 redef class ASuperstringExpr
1515 redef fun expr(v)
1516 do
1517 var array = new Array[Instance]
1518 for nexpr in n_exprs do
1519 var i = v.expr(nexpr)
1520 if i == null then return null
1521 array.add(i)
1522 end
1523 var i = v.array_instance(array, v.get_primitive_class("Object").mclass_type)
1524 var res = v.send(v.force_get_primitive_method("to_s", i.mtype), [i])
1525 assert res != null
1526 return res
1527 end
1528 end
1529
1530 redef class ACrangeExpr
1531 redef fun expr(v)
1532 do
1533 var e1 = v.expr(self.n_expr)
1534 if e1 == null then return null
1535 var e2 = v.expr(self.n_expr2)
1536 if e2 == null then return null
1537 var mtype = v.unanchor_type(self.mtype.as(not null))
1538 var res = new MutableInstance(mtype)
1539 v.init_instance(res)
1540 v.callsite(init_callsite, [res, e1, e2])
1541 return res
1542 end
1543 end
1544
1545 redef class AOrangeExpr
1546 redef fun expr(v)
1547 do
1548 var e1 = v.expr(self.n_expr)
1549 if e1 == null then return null
1550 var e2 = v.expr(self.n_expr2)
1551 if e2 == null then return null
1552 var mtype = v.unanchor_type(self.mtype.as(not null))
1553 var res = new MutableInstance(mtype)
1554 v.init_instance(res)
1555 v.callsite(init_callsite, [res, e1, e2])
1556 return res
1557 end
1558 end
1559
1560 redef class ATrueExpr
1561 redef fun expr(v)
1562 do
1563 return v.bool_instance(true)
1564 end
1565 end
1566
1567 redef class AFalseExpr
1568 redef fun expr(v)
1569 do
1570 return v.bool_instance(false)
1571 end
1572 end
1573
1574 redef class ANullExpr
1575 redef fun expr(v)
1576 do
1577 return v.null_instance
1578 end
1579 end
1580
1581 redef class AIsaExpr
1582 redef fun expr(v)
1583 do
1584 var i = v.expr(self.n_expr)
1585 if i == null then return null
1586 var mtype = v.unanchor_type(self.cast_type.as(not null))
1587 return v.bool_instance(v.is_subtype(i.mtype, mtype))
1588 end
1589 end
1590
1591 redef class AAsCastExpr
1592 redef fun expr(v)
1593 do
1594 var i = v.expr(self.n_expr)
1595 if i == null then return null
1596 var mtype = self.mtype.as(not null)
1597 var amtype = v.unanchor_type(mtype)
1598 if not v.is_subtype(i.mtype, amtype) then
1599 fatal(v, "Cast failed. Expected `{amtype}`, got `{i.mtype}`")
1600 end
1601 return i
1602 end
1603 end
1604
1605 redef class AAsNotnullExpr
1606 redef fun expr(v)
1607 do
1608 var i = v.expr(self.n_expr)
1609 if i == null then return null
1610 if i.mtype isa MNullType then
1611 fatal(v, "Cast failed")
1612 end
1613 return i
1614 end
1615 end
1616
1617 redef class AParExpr
1618 redef fun expr(v)
1619 do
1620 return v.expr(self.n_expr)
1621 end
1622 end
1623
1624 redef class AOnceExpr
1625 redef fun expr(v)
1626 do
1627 if v.onces.has_key(self) then
1628 return v.onces[self]
1629 else
1630 var res = v.expr(self.n_expr)
1631 if res == null then return null
1632 v.onces[self] = res
1633 return res
1634 end
1635 end
1636 end
1637
1638 redef class ASendExpr
1639 redef fun expr(v)
1640 do
1641 var recv = v.expr(self.n_expr)
1642 if recv == null then return null
1643 var args = v.varargize(callsite.mpropdef, recv, self.raw_arguments)
1644 if args == null then return null
1645
1646 var res = v.callsite(callsite, args)
1647 return res
1648 end
1649 end
1650
1651 redef class ASendReassignFormExpr
1652 redef fun stmt(v)
1653 do
1654 var recv = v.expr(self.n_expr)
1655 if recv == null then return
1656 var args = v.varargize(callsite.mpropdef, recv, self.raw_arguments)
1657 if args == null then return
1658 var value = v.expr(self.n_value)
1659 if value == null then return
1660
1661 var read = v.callsite(callsite, args)
1662 assert read != null
1663
1664 var write = v.callsite(reassign_callsite, [read, value])
1665 assert write != null
1666
1667 args.add(write)
1668
1669 v.callsite(write_callsite, args)
1670 end
1671 end
1672
1673 redef class ASuperExpr
1674 redef fun expr(v)
1675 do
1676 var recv = v.frame.arguments.first
1677
1678 var callsite = self.callsite
1679 if callsite != null then
1680 var args = v.varargize(callsite.mpropdef, recv, self.n_args.n_exprs)
1681 if args == null then return null
1682 # Add additional arguments for the super init call
1683 if args.length == 1 then
1684 for i in [0..callsite.msignature.arity[ do
1685 args.add(v.frame.arguments[i+1])
1686 end
1687 end
1688 # Super init call
1689 var res = v.callsite(callsite, args)
1690 return res
1691 end
1692
1693 # standard call-next-method
1694 var mpropdef = self.mpropdef
1695 mpropdef = mpropdef.lookup_next_definition(v.mainmodule, recv.mtype)
1696
1697 var args = v.varargize(mpropdef, recv, self.n_args.n_exprs)
1698 if args == null then return null
1699
1700 if args.length == 1 then
1701 args = v.frame.arguments
1702 end
1703 var res = v.call(mpropdef, args)
1704 return res
1705 end
1706 end
1707
1708 redef class ANewExpr
1709 redef fun expr(v)
1710 do
1711 var mtype = v.unanchor_type(self.recvtype.as(not null))
1712 var recv: Instance = new MutableInstance(mtype)
1713 v.init_instance(recv)
1714 var args = v.varargize(callsite.mpropdef, recv, self.n_args.n_exprs)
1715 if args == null then return null
1716 var res2 = v.callsite(callsite, args)
1717 if res2 != null then
1718 #self.debug("got {res2} from {mproperty}. drop {recv}")
1719 return res2
1720 end
1721 return recv
1722 end
1723 end
1724
1725 redef class AAttrExpr
1726 redef fun expr(v)
1727 do
1728 var recv = v.expr(self.n_expr)
1729 if recv == null then return null
1730 if recv.mtype isa MNullType then fatal(v, "Receiver is null")
1731 var mproperty = self.mproperty.as(not null)
1732 return v.read_attribute(mproperty, recv)
1733 end
1734 end
1735
1736 redef class AAttrAssignExpr
1737 redef fun stmt(v)
1738 do
1739 var recv = v.expr(self.n_expr)
1740 if recv == null then return
1741 if recv.mtype isa MNullType then fatal(v, "Receiver is null")
1742 var i = v.expr(self.n_value)
1743 if i == null then return
1744 var mproperty = self.mproperty.as(not null)
1745 v.write_attribute(mproperty, recv, i)
1746 end
1747 end
1748
1749 redef class AAttrReassignExpr
1750 redef fun stmt(v)
1751 do
1752 var recv = v.expr(self.n_expr)
1753 if recv == null then return
1754 if recv.mtype isa MNullType then fatal(v, "Receiver is null")
1755 var value = v.expr(self.n_value)
1756 if value == null then return
1757 var mproperty = self.mproperty.as(not null)
1758 var attr = v.read_attribute(mproperty, recv)
1759 var res = v.callsite(reassign_callsite, [attr, value])
1760 assert res != null
1761 v.write_attribute(mproperty, recv, res)
1762 end
1763 end
1764
1765 redef class AIssetAttrExpr
1766 redef fun expr(v)
1767 do
1768 var recv = v.expr(self.n_expr)
1769 if recv == null then return null
1770 if recv.mtype isa MNullType then fatal(v, "Receiver is null")
1771 var mproperty = self.mproperty.as(not null)
1772 return v.bool_instance(v.isset_attribute(mproperty, recv))
1773 end
1774 end
1775
1776 redef class ADebugTypeExpr
1777 redef fun stmt(v)
1778 do
1779 # do nothing
1780 end
1781 end