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 naiveinterpreter
22 import auto_super_init
24 redef class ModelBuilder
25 # Execute the program from the entry point (Sys::main) of the `mainmodule'
26 # `arguments' are the command-line arguments in order
28 # 1. the AST is fully loaded.
29 # 2. the model is fully built.
30 # 3. the instructions are fully analysed.
31 fun run_naive_interpreter
(mainmodule
: MModule, arguments
: Array[String])
34 self.toolcontext
.info
("*** START INTERPRETING ***", 1)
36 var interpreter
= new NaiveInterpreter(self, mainmodule
, arguments
)
37 var mainclasses
= model
.get_mclasses_by_name
("Sys")
38 if mainclasses
== null then return
39 assert mainclasses
.length
== 1
40 var mainclass
= mainclasses
.first
41 var props
= model
.get_mproperties_by_name
("main")
42 assert props
.length
== 1
43 var methods
= props
.first
.lookup_definitions
(mainmodule
, mainclass
.mclass_type
)
44 assert methods
.length
== 1 else print methods
.join
(", ")
45 var mainobj
= new Instance(mainclass
.mclass_type
)
46 interpreter
.mainobj
= mainobj
47 interpreter
.init_instance
(mainobj
)
48 var initprop
= try_get_mproperty_by_name2
(nmodules
.first
, mainmodule
, mainclass
.mclass_type
, "init")
49 if initprop
!= null then
50 assert initprop
isa MMethod
51 interpreter
.send
(initprop
, [mainobj
])
53 interpreter
.check_init_instance
(mainobj
)
54 interpreter
.send
(interpreter
.get_property
("main", mainobj
), [mainobj
])
57 self.toolcontext
.info
("*** END INTERPRETING: {time1-time0} ***", 2)
61 # The visitor that interprets the Nit Program by walking on the AST
62 private class NaiveInterpreter
63 # The modelbuilder that know the AST and its associations with the model
64 var modelbuilder
: ModelBuilder
66 # The main moduleof the program (used to lookup methoda
67 var mainmodule
: MModule
69 # The command line arguments of the interpreted program
70 # arguments.first is the program name
71 # arguments[1] is the first argument
72 var arguments
: Array[String]
74 var mainobj
: nullable Instance
76 init(modelbuilder
: ModelBuilder, mainmodule
: MModule, arguments
: Array[String])
78 self.modelbuilder
= modelbuilder
79 self.mainmodule
= mainmodule
80 self.arguments
= arguments
81 self.true_instance
= new PrimitiveInstance[Bool](get_class
("Bool").mclass_type
, true)
82 self.false_instance
= new PrimitiveInstance[Bool](get_class
("Bool").mclass_type
, false)
83 self.null_instance
= new Instance(mainmodule
.model
.null_type
)
86 # Force to get the primitive class named `name' or abort
87 fun get_class
(name
: String): MClass
89 var cla
= mainmodule
.model
.get_mclasses_by_name
(name
)
91 if name
== "Bool" then
92 var c
= new MClass(mainmodule
, name
, 0, enum_kind
, public_visibility
)
93 var cladef
= new MClassDef(mainmodule
, c
.mclass_type
, new Location(null, 0,0,0,0), new Array[String])
96 fatal
("Fatal Error: no primitive class {name}")
99 assert cla
.length
== 1 else print cla
.join
(", ")
103 # Force to get the primitive property named `name' in the instance `recv' or abort
104 fun get_property
(name
: String, recv
: Instance): MMethod
106 var props
= self.mainmodule
.model
.get_mproperties_by_name
(name
)
107 if props
== null then
108 fatal
("Fatal Error: no primitive property {name} on {recv}")
111 var mtype
= recv
.mtype
112 var res
: nullable MMethod = null
113 for mprop
in props
do
114 assert mprop
isa MMethod
115 if not mtype
.has_mproperty
(self.mainmodule
, mprop
) then continue
119 fatal
("Fatal Error: ambigous property name '{name}'; conflict between {mprop.full_name} and {res.full_name}")
124 fatal
("Fatal Error: no primitive property {name} on {recv}")
130 # Subtype test in the context of the mainmodule
131 fun is_subtype
(sub
, sup
: MType): Bool
133 return sub
.is_subtype
(self.mainmodule
, self.frame
.arguments
.first
.mtype
.as(MClassType), sup
)
136 # Is a return executed?
137 # Set this mark to skip the evaluation until the end of the current method
138 var returnmark
: Bool = false
140 # Is a break executed?
141 # Set this mark to skip the evaluation until a labeled statement catch it with `is_break'
142 var breakmark
: nullable EscapeMark = null
144 # Is a continue executed?
145 # Set this mark to skip the evaluation until a labeled statement catch it with `is_continue'
146 var continuemark
: nullable EscapeMark = null
148 # Is a return or a break or a continue executed?
149 # Use this function to know if you must skip the evaluation of statements
150 fun is_escaping
: Bool do return returnmark
or breakmark
!= null or continuemark
!= null
152 # The value associated with the current return/break/continue, if any.
153 # Set the value when you set a escapemark.
154 # Read the value when you catch a mark or reach the end of a method
155 var escapevalue
: nullable Instance = null
157 # If there is a break and is associated with `escapemark', then return true an clear the mark.
158 # If there is no break or if `escapemark' is null then return false.
159 # Use this function to catch a potential break.
160 fun is_break
(escapemark
: nullable EscapeMark): Bool
162 if escapemark
!= null and self.breakmark
== escapemark
then
163 self.breakmark
= null
170 # If there is a continue and is associated with `escapemark', then return true an clear the mark.
171 # If there is no continue or if `escapemark' is null then return false.
172 # Use this function to catch a potential continue.
173 fun is_continue
(escapemark
: nullable EscapeMark): Bool
175 if escapemark
!= null and self.continuemark
== escapemark
then
176 self.continuemark
= null
183 # Evaluate `n' as an expression in the current context.
184 # Return the value of the expression.
185 # If `n' cannot be evaluated, then aborts.
186 fun expr
(n
: AExpr): Instance
188 var old
= self.frame
.current_node
189 self.frame
.current_node
= n
190 #n.debug("IN Execute expr")
191 var i
= n
.expr
(self).as(not null)
192 #n.debug("OUT Execute expr: value is {i}")
193 #if not is_subtype(i.mtype, n.mtype.as(not null)) then n.debug("Expected {n.mtype.as(not null)} got {i}")
194 self.frame
.current_node
= old
198 # Evaluate `n' as a statement in the current context.
199 # Do nothing if `n' is sull.
200 # If `n' cannot be evaluated, then aborts.
201 fun stmt
(n
: nullable AExpr)
204 var old
= self.frame
.current_node
205 self.frame
.current_node
= n
206 #n.debug("Execute stmt")
208 self.frame
.current_node
= old
212 # Map used to store values of nodes that must be evaluated once in the system (AOnceExpr)
213 var onces
: Map[ANode, Instance] = new HashMap[ANode, Instance]
215 # Return the boolean instance associated with `val'.
216 fun bool_instance
(val
: Bool): Instance
218 if val
then return self.true_instance
else return self.false_instance
221 # Return the integer instance associated with `val'.
222 fun int_instance
(val
: Int): Instance
224 var ic
= get_class
("Int")
225 return new PrimitiveInstance[Int](ic
.mclass_type
, val
)
228 # Return the char instance associated with `val'.
229 fun char_instance
(val
: Char): Instance
231 var ic
= get_class
("Char")
232 return new PrimitiveInstance[Char](ic
.mclass_type
, val
)
235 # Return the float instance associated with `val'.
236 fun float_instance
(val
: Float): Instance
238 var ic
= get_class
("Float")
239 return new PrimitiveInstance[Float](ic
.mclass_type
, val
)
242 # The unique intance of the `true' value.
243 var true_instance
: Instance
245 # The unique intance of the `false' value.
246 var false_instance
: Instance
248 # The unique intance of the `null' value.
249 var null_instance
: Instance
251 # Return a new array made of `values'.
252 # The dynamic type of the result is Array[elttype].
253 fun array_instance
(values
: Array[Instance], elttype
: MType): Instance
255 assert not elttype
.need_anchor
256 var nat
= new PrimitiveInstance[Array[Instance]](self.get_class
("NativeArray").get_mtype
([elttype
]), values
)
257 var mtype
= self.get_class
("Array").get_mtype
([elttype
])
258 var res
= new Instance(mtype
)
259 self.init_instance
(res
)
260 self.send
(self.get_property
("with_native", res
), [res
, nat
, self.int_instance
(values
.length
)])
261 self.check_init_instance
(res
)
265 # Return a new native string initialized with `txt'
266 fun native_string_instance
(txt
: String): Instance
268 var val
= new Buffer.from
(txt
)
269 var ic
= get_class
("NativeString")
270 return new PrimitiveInstance[Buffer](ic
.mclass_type
, val
)
273 # The current frame used to store local variables of the current method executed
274 fun frame
: Frame do return frames
.first
276 # The stack of all frames. The first one is the current one.
277 var frames
: List[Frame] = new List[Frame]
279 # Return a stack stace. One line per function
280 fun stack_trace
: String
283 b
.append
(",---- Stack trace -- - - -\n")
285 b
.append
("| {f.mpropdef} ({f.current_node.location})\n")
287 b
.append
("`------------------- - - -")
291 # Exit the program with a message
292 fun fatal
(message
: String)
294 if frames
.is_empty
then
297 self.frame
.current_node
.fatal
(self, message
)
302 # Execute `mpropdef' for a `args' (where args[0] is the receiver).
303 # Return a falue if `mpropdef' is a function, or null if it is a procedure.
304 # The call is direct/static. There is no message-seding/late-bindng.
305 fun call
(mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
307 var vararg_rank
= mpropdef
.msignature
.vararg_rank
308 if vararg_rank
>= 0 then
309 assert args
.length
>= mpropdef
.msignature
.arity
+ 1 # because of self
311 args
= new Array[Instance]
313 args
.add
(rawargs
.first
) # recv
315 for i
in [0..vararg_rank
[ do
316 args
.add
(rawargs
[i
+1])
319 var vararg_lastrank
= vararg_rank
+ rawargs
.length-1-mpropdef
.msignature
.arity
320 var vararg
= new Array[Instance]
321 for i
in [vararg_rank
..vararg_lastrank
] do
322 vararg
.add
(rawargs
[i
+1])
324 # FIXME: its it to late to determine the vararg type, this should have been done during a previous analysis
325 var elttype
= mpropdef
.msignature
.parameter_mtypes
[vararg_rank
].anchor_to
(self.mainmodule
, args
.first
.mtype
.as(MClassType))
326 args
.add
(self.array_instance
(vararg
, elttype
))
328 for i
in [vararg_lastrank
+1..rawargs
.length-1
[ do
329 args
.add
(rawargs
[i
+1])
332 assert args
.length
== mpropdef
.msignature
.arity
+ 1 # because of self
334 # Look for the AST node that implements the property
335 var mproperty
= mpropdef
.mproperty
336 if self.modelbuilder
.mpropdef2npropdef
.has_key
(mpropdef
) then
337 var npropdef
= self.modelbuilder
.mpropdef2npropdef
[mpropdef
]
338 return npropdef
.call
(self, mpropdef
, args
)
339 else if mproperty
.name
== "init" then
340 var nclassdef
= self.modelbuilder
.mclassdef2nclassdef
[mpropdef
.mclassdef
]
341 return nclassdef
.call
(self, mpropdef
, args
)
343 fatal
("Fatal Error: method {mpropdef} not found in the AST")
348 # Execute `mproperty' for a `args' (where args[0] is the receiver).
349 # Return a falue if `mproperty' is a function, or null if it is a procedure.
350 # The call is polimotphic. There is a message-seding/late-bindng according to te receiver (args[0]).
351 fun send
(mproperty
: MMethod, args
: Array[Instance]): nullable Instance
353 var recv
= args
.first
354 var mtype
= recv
.mtype
355 if mtype
isa MNullType then
356 if mproperty
.name
== "==" then
357 return self.bool_instance
(args
[0] == args
[1])
358 else if mproperty
.name
== "!=" then
359 return self.bool_instance
(args
[0] != args
[1])
361 #fatal("Reciever is null. {mproperty}. {args.join(" ")} {self.frame.current_node.class_name}")
362 fatal
("Reciever is null")
365 var propdefs
= mproperty
.lookup_definitions
(self.mainmodule
, mtype
)
366 if propdefs
.length
> 1 then
367 fatal
("NOT YET IMPLEMETED ERROR: Property conflict: {propdefs.join(", ")}")
370 assert propdefs
.length
== 1 else
371 fatal
("Fatal Error: No property '{mproperty}' for '{recv}'")
374 var propdef
= propdefs
.first
375 return self.call
(propdef
, args
)
378 # Read the attribute `mproperty' of an instance `recv' and return its value.
379 # If the attribute in not yet initialized, then aborts with an error message.
380 fun read_attribute
(mproperty
: MAttribute, recv
: Instance): Instance
382 if not recv
.attributes
.has_key
(mproperty
) then
383 fatal
("Uninitialized attribute {mproperty.name}")
386 return recv
.attributes
[mproperty
]
389 # Fill the initial values of the newly created instance `recv'.
390 # `recv.mtype' is used to know what must be filled.
391 fun init_instance
(recv
: Instance)
393 for cd
in recv
.mtype
.collect_mclassdefs
(self.mainmodule
)
395 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
396 for npropdef
in n
.n_propdefs
do
397 if npropdef
isa AAttrPropdef then
398 npropdef
.init_expr
(self, recv
)
404 # Check that non nullable attributes of `recv' are correctly initialized.
405 # This function is used as the last instruction of a new
406 # FIXME: this will work better once there is nullable types
407 fun check_init_instance
(recv
: Instance)
409 for cd
in recv
.mtype
.collect_mclassdefs
(self.mainmodule
)
411 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
412 for npropdef
in n
.n_propdefs
do
413 if npropdef
isa AAttrPropdef and npropdef
.n_expr
== null then
414 # Force read to check the initialization
415 self.read_attribute
(npropdef
.mpropdef
.mproperty
, recv
)
421 # This function determine the correct type according the reciever of the current definition (self).
422 fun unanchor_type
(mtype
: MType): MType
424 return mtype
.anchor_to
(self.mainmodule
, self.frame
.arguments
.first
.mtype
.as(MClassType))
428 # An instance represents a value of the executed program.
430 # The dynamic type of the instance
431 # ASSERT: not self.mtype.is_anchored
434 # The values of the attributes
435 var attributes
: Map[MAttribute, Instance] = new HashMap[MAttribute, Instance]
437 # return true if the instance is the true value.
438 # return false if the instance is the true value.
440 fun is_true
: Bool do abort
442 # Return true if `self' IS `o' (using the Nit semantic of is)
443 fun eq_is
(o
: Instance): Bool do return self is o
445 # Human readable object identity "Type#number"
446 redef fun to_s
do return "{mtype}#{object_id}"
448 # Return the integer valur is the instance is an integer.
450 fun to_i
: Int do abort
452 # The real value encapsulated if the instance is primitive.
454 fun val
: Object do abort
457 # Special instance to handle primitives values (int, bool, etc.)
458 # The trick it just to encapsulate the <<real>> value
459 class PrimitiveInstance[E
: Object]
462 # The real value encapsulated
465 init(mtype
: MType, val
: E
)
473 if val
== true then return true
474 if val
== false then return false
480 if not o
isa PrimitiveInstance[Object] then return false
481 return self.val
== o
.val
486 if not o
isa PrimitiveInstance[Object] then return false
487 return self.val
is o
.val
490 redef fun to_s
do return "{mtype}#{val.object_id}({val})"
492 redef fun to_i
do return val
.as(Int)
495 # Information about local variables in a running method
497 # The current visited node
498 # The node is stored by frame to keep a stack trace
499 var current_node
: ANode
500 # The executed property.
501 # A Method in case of a call, an attribute in case of a default initialization.
502 var mpropdef
: MPropDef
503 # Arguments of the method (the first is te receiver
504 var arguments
: Array[Instance]
505 # Mapping betwen a variable an the current value
506 var map
: Map[Variable, Instance] = new HashMap[Variable, Instance]
510 # Aborts the program with a message
511 # `v' is used to know if a colored message is displayed or not
512 private fun fatal
(v
: NaiveInterpreter, message
: String)
514 if v
.modelbuilder
.toolcontext
.opt_no_color
.value
== true then
515 print
("{message} ({location.file.filename}:{location.line_start})")
517 print
("{location}: {message}\n{location.colored_line("0;31")}")
525 # Execute a `mpropdef' associated with the current node.
526 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
528 fatal
(v
, "Unimplemented {mpropdef}")
533 redef class AConcreteMethPropdef
534 redef fun call
(v
, mpropdef
, args
)
536 var f
= new Frame(self, self.mpropdef
.as(not null), args
)
537 for i
in [0..mpropdef
.msignature
.arity
[ do
538 var variable
= self.n_signature
.n_params
[i
].variable
539 assert variable
!= null
540 f
.map
[variable
] = args
[i
+1]
545 # Call the implicit super-init
546 var auto_super_inits
= self.auto_super_inits
547 if auto_super_inits
!= null then
548 var selfarg
= [args
.first
]
549 for auto_super_init
in auto_super_inits
do
550 if auto_super_init
.intro
.msignature
.arity
== 0 then
551 v
.send
(auto_super_init
, selfarg
)
553 v
.send
(auto_super_init
, args
)
561 var res
= v
.escapevalue
567 redef class AInternMethPropdef
568 redef fun call
(v
, mpropdef
, args
)
570 var pname
= mpropdef
.mproperty
.name
571 var cname
= mpropdef
.mclassdef
.mclass
.name
572 if pname
== "output" then
573 var recv
= args
.first
576 else if pname
== "object_id" then
577 var recv
= args
.first
578 if recv
isa PrimitiveInstance[Object] then
579 return v
.int_instance
(recv
.val
.object_id
)
581 return v
.int_instance
(recv
.object_id
)
583 else if pname
== "output_class_name" then
584 var recv
= args
.first
585 print recv
.mtype
.as(MClassType).mclass
587 else if pname
== "native_class_name" then
588 var recv
= args
.first
589 var txt
= recv
.mtype
.as(MClassType).mclass
.to_s
590 return v
.native_string_instance
(txt
)
591 else if pname
== "==" then
592 # == is correclt redefined for instances
593 return v
.bool_instance
(args
[0] == args
[1])
594 else if pname
== "!=" then
595 return v
.bool_instance
(args
[0] != args
[1])
596 else if pname
== "is_same_type" then
597 return v
.bool_instance
(args
[0].mtype
== args
[1].mtype
)
598 else if pname
== "exit" then
601 else if pname
== "sys" then
603 else if cname
== "Int" then
604 if pname
== "unary -" then
605 return v
.int_instance
(-args
[0].to_i
)
606 else if pname
== "succ" then
607 return v
.int_instance
(args
[0].to_i
+ 1)
608 else if pname
== "prec" then
609 return v
.int_instance
(args
[0].to_i
- 1)
610 else if pname
== "+" then
611 return v
.int_instance
(args
[0].to_i
+ args
[1].to_i
)
612 else if pname
== "-" then
613 return v
.int_instance
(args
[0].to_i
- args
[1].to_i
)
614 else if pname
== "*" then
615 return v
.int_instance
(args
[0].to_i
* args
[1].to_i
)
616 else if pname
== "%" then
617 return v
.int_instance
(args
[0].to_i
% args
[1].to_i
)
618 else if pname
== "/" then
619 return v
.int_instance
(args
[0].to_i
/ args
[1].to_i
)
620 else if pname
== "<" then
621 return v
.bool_instance
(args
[0].to_i
< args
[1].to_i
)
622 else if pname
== ">" then
623 return v
.bool_instance
(args
[0].to_i
> args
[1].to_i
)
624 else if pname
== "<=" then
625 return v
.bool_instance
(args
[0].to_i
<= args
[1].to_i
)
626 else if pname
== ">=" then
627 return v
.bool_instance
(args
[0].to_i
>= args
[1].to_i
)
628 else if pname
== "<=>" then
629 return v
.int_instance
(args
[0].to_i
<=> args
[1].to_i
)
630 else if pname
== "ascii" then
631 return v
.char_instance
(args
[0].to_i
.ascii
)
632 else if pname
== "to_f" then
633 return v
.float_instance
(args
[0].to_i
.to_f
)
634 else if pname
== "lshift" then
635 return v
.int_instance
(args
[0].to_i
.lshift
(args
[1].to_i
))
636 else if pname
== "rshift" then
637 return v
.int_instance
(args
[0].to_i
.rshift
(args
[1].to_i
))
639 else if cname
== "Char" then
640 var recv
= args
[0].val
.as(Char)
641 if pname
== "ascii" then
642 return v
.int_instance
(recv
.ascii
)
643 else if pname
== "succ" then
644 return v
.char_instance
(recv
.succ
)
645 else if pname
== "prec" then
646 return v
.char_instance
(recv
.prec
)
647 else if pname
== "<" then
648 return v
.bool_instance
(recv
< args
[1].val
.as(Char))
649 else if pname
== ">" then
650 return v
.bool_instance
(recv
> args
[1].val
.as(Char))
651 else if pname
== "<=" then
652 return v
.bool_instance
(recv
<= args
[1].val
.as(Char))
653 else if pname
== ">=" then
654 return v
.bool_instance
(recv
>= args
[1].val
.as(Char))
655 else if pname
== "<=>" then
656 return v
.int_instance
(recv
<=> args
[1].val
.as(Char))
658 else if cname
== "Float" then
660 return v
.float_instance
(args
[0].val
.as(Float) + args
[1].val
.as(Float))
661 else if pname
== "-" then
662 return v
.float_instance
(args
[0].val
.as(Float) - args
[1].val
.as(Float))
663 else if pname
== "*" then
664 return v
.float_instance
(args
[0].val
.as(Float) * args
[1].val
.as(Float))
665 else if pname
== "/" then
666 return v
.float_instance
(args
[0].val
.as(Float) / args
[1].val
.as(Float))
667 else if pname
== "to_i" then
668 return v
.int_instance
(args
[0].val
.as(Float).to_i
)
670 else if cname
== "NativeString" then
671 var recvval
= args
.first
.val
.as(Buffer)
672 if pname
== "[]" then
673 return v
.char_instance
(recvval
[args
[1].to_i
])
674 else if pname
== "[]=" then
675 recvval
[args
[1].to_i
] = args
[2].val
.as(Char)
677 else if pname
== "copy_to" then
678 # sig= copy_to(dest: NativeString, length: Int, from: Int, to: Int)
679 recvval
.copy
(args
[3].to_i
, args
[2].to_i
, args
[1].val
.as(Buffer), args
[4].to_i
)
681 else if pname
== "atoi" then
682 return v
.int_instance
(recvval
.to_i
)
684 else if pname
== "calloc_string" then
685 return v
.native_string_instance
("!" * args
[1].to_i
)
686 else if cname
== "NativeArray" then
687 var recvval
= args
.first
.val
.as(Array[Instance])
688 if pname
== "[]" then
689 if args
[1].to_i
>= recvval
.length
then
690 debug
("Illegal access on {recvval} for element {args[1].to_i}/{recvval.length}")
692 return recvval
[args
[1].to_i
]
693 else if pname
== "[]=" then
694 recvval
[args
[1].to_i
] = args
[2]
696 else if pname
== "copy_to" then
697 recvval
.copy
(0, args
[2].to_i
, args
[1].val
.as(Array[Instance]), 0)
700 else if pname
== "calloc_array" then
701 var recvtype
= args
.first
.mtype
.as(MClassType)
702 var mtype
: MType = recvtype
.supertype_to
(v
.mainmodule
, recvtype
, v
.get_class
("ArrayCapable"))
703 mtype
= mtype
.as(MGenericType).arguments
.first
704 var val
= new Array[Instance].filled_with
(v
.null_instance
, args
[1].to_i
)
705 return new PrimitiveInstance[Array[Instance]](v
.get_class
("NativeArray").get_mtype
([mtype
]), val
)
707 fatal
(v
, "Unimplemented intern {mpropdef}")
712 redef class AbstractArray[E
]
713 fun copy
(start
: Int, len
: Int, dest
: AbstractArray[E
], new_start
: Int)
715 self.copy_to
(start
, len
, dest
, new_start
)
719 redef class AExternInitPropdef
720 redef fun call
(v
, mpropdef
, args
)
722 var pname
= mpropdef
.mproperty
.name
723 var cname
= mpropdef
.mclassdef
.mclass
.name
724 if pname
== "native_stdout" then
725 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, stdout
)
726 else if pname
== "native_stdin" then
727 return new PrimitiveInstance[IStream](mpropdef
.mclassdef
.mclass
.mclass_type
, stdin
)
728 else if pname
== "native_stderr" then
729 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, stderr
)
730 else if pname
== "io_open_read" then
731 var a1
= args
[1].val
.as(Buffer)
732 return new PrimitiveInstance[IStream](mpropdef
.mclassdef
.mclass
.mclass_type
, new IFStream.open
(a1
.to_s
))
733 else if pname
== "io_open_write" then
734 var a1
= args
[1].val
.as(Buffer)
735 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, new OFStream.open
(a1
.to_s
))
737 fatal
(v
, "Unimplemented extern init {mpropdef}")
742 redef class AExternMethPropdef
744 redef fun call
(v
, mpropdef
, args
)
746 var pname
= mpropdef
.mproperty
.name
747 var cname
= mpropdef
.mclassdef
.mclass
.name
748 if cname
== "NativeFile" then
749 var recvval
= args
.first
.val
750 if pname
== "io_write" then
751 var a1
= args
[1].val
.as(Buffer)
752 recvval
.as(OStream).write
(a1
.substring
(0, args
[2].to_i
))
754 else if pname
== "io_read" then
755 var str
= recvval
.as(IStream).read
(args
[2].to_i
)
756 var a1
= args
[1].val
.as(Buffer)
757 new Buffer.from
(str
).copy
(0, str
.length
, a1
, 0)
758 return v
.int_instance
(str
.length
)
759 else if pname
== "io_close" then
760 recvval
.as(IOS).close
761 return v
.int_instance
(0)
763 else if cname
== "NativeString" then
764 var recvval
= args
.first
.val
.as(Buffer)
765 if pname
== "file_exists" then
766 return v
.bool_instance
(recvval
.to_s
.file_exists
)
767 else if pname
== "file_mkdir" then
770 else if pname
== "get_environ" then
771 var txt
= args
.first
.val
.as(Buffer).to_s
.to_symbol
.environ
772 return v
.native_string_instance
(txt
)
774 else if pname
== "native_argc" then
775 return v
.int_instance
(v
.arguments
.length
)
776 else if pname
== "native_argv" then
777 var txt
= v
.arguments
[args
[1].to_i
]
778 return v
.native_string_instance
(txt
)
779 else if pname
== "get_time" then
780 return v
.int_instance
(get_time
)
781 else if pname
== "lexer_goto" then
782 return v
.int_instance
(lexer_goto
(args
[1].to_i
, args
[2].to_i
))
783 else if pname
== "lexer_accept" then
784 return v
.int_instance
(lexer_accept
(args
[1].to_i
))
785 else if pname
== "parser_goto" then
786 return v
.int_instance
(parser_goto
(args
[1].to_i
, args
[2].to_i
))
787 else if pname
== "parser_action" then
788 return v
.int_instance
(parser_action
(args
[1].to_i
, args
[2].to_i
))
790 fatal
(v
, "Unimplemented extern {mpropdef}")
795 redef class AAttrPropdef
796 redef fun call
(v
, mpropdef
, args
)
798 var attr
= self.mpropdef
.mproperty
799 if args
.length
== 1 then
800 return v
.read_attribute
(attr
, args
.first
)
802 assert args
.length
== 2
803 args
.first
.attributes
[attr
] = args
[1]
808 # Evaluate and set the default value of the attribute in `recv'
809 private fun init_expr
(v
: NaiveInterpreter, recv
: Instance)
811 var nexpr
= self.n_expr
812 if nexpr
!= null then
813 var f
= new Frame(self, self.mpropdef
.as(not null), [recv
])
815 var val
= v
.expr
(nexpr
)
817 assert not v
.is_escaping
818 recv
.attributes
[self.mpropdef
.mproperty
] = val
821 var mtype
= self.mpropdef
.static_mtype
.as(not null)
822 # TODO The needinit info is statically computed, move it to modelbuilder or whatever
823 mtype
= mtype
.resolve_for
(self.mpropdef
.mclassdef
.bound_mtype
, self.mpropdef
.mclassdef
.bound_mtype
, self.mpropdef
.mclassdef
.mmodule
, true)
824 if mtype
isa MNullableType then
825 recv
.attributes
[self.mpropdef
.mproperty
] = v
.null_instance
830 redef class AClassdef
831 # Execute an implicit `mpropdef' associated with the current node.
832 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
834 var super_inits
= self.super_inits
835 if super_inits
!= null then
836 assert args
.length
== 1
837 for su
in super_inits
do
842 var recv
= args
.first
844 # Collect undefined attributes
845 for npropdef
in self.n_propdefs
do
846 if npropdef
isa AAttrPropdef and npropdef
.n_expr
== null then
847 recv
.attributes
[npropdef
.mpropdef
.mproperty
] = args
[i
]
856 # Evaluate the node as a possible expression.
857 # Return a possible value
858 # NOTE: Do not call this method directly, but use `v.expr'
859 # This method is here to be implemented by subclasses.
860 private fun expr
(v
: NaiveInterpreter): nullable Instance
862 fatal
(v
, "Unimplemented expr {class_name}")
866 # Evaluate the node as a statement.
867 # NOTE: Do not call this method directly, but use `v.stmt'
868 # This method is here to be implemented by subclasses (no need to return something).
869 private fun stmt
(v
: NaiveInterpreter)
876 redef class ABlockExpr
879 for e
in self.n_expr
do
881 if v
.is_escaping
then return
886 redef class AVardeclExpr
892 v
.frame
.map
[self.variable
.as(not null)] = i
900 return v
.frame
.map
[self.variable
.as(not null)]
904 redef class AVarAssignExpr
907 var i
= v
.expr
(self.n_value
)
908 v
.frame
.map
[self.variable
.as(not null)] = i
912 redef class AVarReassignExpr
915 var vari
= v
.frame
.map
[self.variable
.as(not null)]
916 var value
= v
.expr
(self.n_value
)
917 var res
= v
.send
(reassign_property
.mproperty
, [vari
, value
])
919 v
.frame
.map
[self.variable
.as(not null)] = res
923 redef class ASelfExpr
926 return v
.frame
.arguments
.first
930 redef class AContinueExpr
933 v
.continuemark
= self.escapemark
937 redef class ABreakExpr
940 v
.breakmark
= self.escapemark
944 redef class AReturnExpr
956 redef class AAbortExpr
967 var cond
= v
.expr
(self.n_expr
)
976 redef class AIfexprExpr
979 var cond
= v
.expr
(self.n_expr
)
981 return v
.expr
(self.n_then
)
983 return v
.expr
(self.n_else
)
992 v
.is_break
(self.escapemark
) # Clear the break (if any)
996 redef class AWhileExpr
1000 var cond
= v
.expr
(self.n_expr
)
1001 if not cond
.is_true
then return
1002 v
.stmt
(self.n_block
)
1003 if v
.is_break
(self.escapemark
) then return
1004 v
.is_continue
(self.escapemark
) # Clear the break
1005 if v
.is_escaping
then return
1010 redef class ALoopExpr
1014 v
.stmt
(self.n_block
)
1015 if v
.is_break
(self.escapemark
) then return
1016 v
.is_continue
(self.escapemark
) # Clear the break
1017 if v
.is_escaping
then return
1022 redef class AForExpr
1025 var col
= v
.expr
(self.n_expr
)
1026 #self.debug("col {col}")
1027 var iter
= v
.send
(v
.get_property
("iterator", col
), [col
]).as(not null)
1028 #self.debug("iter {iter}")
1030 var isok
= v
.send
(v
.get_property
("is_ok", iter
), [iter
]).as(not null)
1031 if not isok
.is_true
then return
1032 var item
= v
.send
(v
.get_property
("item", iter
), [iter
]).as(not null)
1033 #self.debug("item {item}")
1034 v
.frame
.map
[self.variables
.first
] = item
1035 v
.stmt
(self.n_block
)
1036 if v
.is_break
(self.escapemark
) then return
1037 v
.is_continue
(self.escapemark
) # Clear the break
1038 if v
.is_escaping
then return
1039 v
.send
(v
.get_property
("next", iter
), [iter
])
1044 redef class AAssertExpr
1047 var cond
= v
.expr
(self.n_expr
)
1048 if not cond
.is_true
then
1050 if v
.is_escaping
then return
1053 fatal
(v
, "Assert '{nid.text}' failed")
1055 fatal
(v
, "Assert failed")
1065 var cond
= v
.expr
(self.n_expr
)
1066 if cond
.is_true
then return cond
1067 return v
.expr
(self.n_expr2
)
1071 redef class AAndExpr
1074 var cond
= v
.expr
(self.n_expr
)
1075 if not cond
.is_true
then return cond
1076 return v
.expr
(self.n_expr2
)
1080 redef class ANotExpr
1083 var cond
= v
.expr
(self.n_expr
)
1084 return v
.bool_instance
(not cond
.is_true
)
1088 redef class AOrElseExpr
1091 var i
= v
.expr
(self.n_expr
)
1092 if i
!= v
.null_instance
then return i
1093 return v
.expr
(self.n_expr2
)
1100 var i
= v
.expr
(self.n_expr
)
1101 var i2
= v
.expr
(self.n_expr2
)
1102 return v
.bool_instance
(i
.eq_is
(i2
))
1106 redef class AIntExpr
1109 return v
.int_instance
(self.value
.as(not null))
1113 redef class AFloatExpr
1116 return v
.float_instance
(self.value
.as(not null))
1120 redef class ACharExpr
1123 return v
.char_instance
(self.value
.as(not null))
1127 redef class AArrayExpr
1130 var val
= new Array[Instance]
1131 for nexpr
in self.n_exprs
.n_exprs
do
1132 val
.add
(v
.expr
(nexpr
))
1134 var mtype
= v
.unanchor_type
(self.mtype
.as(not null)).as(MGenericType)
1135 var elttype
= mtype
.arguments
.first
1136 return v
.array_instance
(val
, elttype
)
1140 redef class AStringFormExpr
1143 var txt
= self.value
.as(not null)
1144 var nat
= v
.native_string_instance
(txt
)
1145 var res
= new Instance(v
.get_class
("String").mclass_type
)
1146 v
.init_instance
(res
)
1147 v
.send
(v
.get_property
("from_cstring", res
), [res
, nat
])
1148 v
.check_init_instance
(res
)
1153 redef class ASuperstringExpr
1156 var array
= new Array[Instance]
1157 for nexpr
in n_exprs
do
1158 array
.add
(v
.expr
(nexpr
))
1160 var i
= v
.array_instance
(array
, v
.get_class
("Object").mclass_type
)
1161 var res
= v
.send
(v
.get_property
("to_s", i
), [i
])
1167 redef class ACrangeExpr
1170 var e1
= v
.expr
(self.n_expr
)
1171 var e2
= v
.expr
(self.n_expr2
)
1172 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1173 var res
= new Instance(mtype
)
1174 v
.init_instance
(res
)
1175 v
.send
(v
.get_property
("init", res
), [res
, e1
, e2
])
1176 v
.check_init_instance
(res
)
1181 redef class AOrangeExpr
1184 var e1
= v
.expr
(self.n_expr
)
1185 var e2
= v
.expr
(self.n_expr2
)
1186 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1187 var res
= new Instance(mtype
)
1188 v
.init_instance
(res
)
1189 v
.send
(v
.get_property
("without_last", res
), [res
, e1
, e2
])
1190 v
.check_init_instance
(res
)
1195 redef class ATrueExpr
1198 return v
.bool_instance
(true)
1202 redef class AFalseExpr
1205 return v
.bool_instance
(false)
1209 redef class ANullExpr
1212 return v
.null_instance
1216 redef class AIsaExpr
1219 var i
= v
.expr
(self.n_expr
)
1220 var mtype
= v
.unanchor_type
(self.cast_type
.as(not null))
1221 return v
.bool_instance
(v
.is_subtype
(i
.mtype
, mtype
))
1225 redef class AAsCastExpr
1228 var i
= v
.expr
(self.n_expr
)
1229 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1230 if not v
.is_subtype
(i
.mtype
, mtype
) then
1231 #fatal(v, "Cast failed expected {mtype}, got {i}")
1232 fatal
(v
, "Cast failed")
1238 redef class AAsNotnullExpr
1241 var i
= v
.expr
(self.n_expr
)
1242 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1243 if i
.mtype
isa MNullType then
1244 fatal
(v
, "Cast failed")
1250 redef class AParExpr
1253 return v
.expr
(self.n_expr
)
1257 redef class AOnceExpr
1260 if v
.onces
.has_key
(self) then
1261 return v
.onces
[self]
1263 var res
= v
.expr
(self.n_expr
)
1270 redef class ASendExpr
1273 var recv
= v
.expr
(self.n_expr
)
1275 for a
in compute_raw_arguments
do
1278 var mproperty
= self.mproperty
.as(not null)
1279 return v
.send
(mproperty
, args
)
1283 redef class ASendReassignFormExpr
1286 var recv
= v
.expr
(self.n_expr
)
1288 for a
in compute_raw_arguments
do
1291 var value
= v
.expr
(self.n_value
)
1293 var mproperty
= self.mproperty
.as(not null)
1294 var read
= v
.send
(mproperty
, args
)
1297 var write
= v
.send
(self.reassign_property
.mproperty
, [read
, value
])
1298 assert write
!= null
1302 v
.send
(self.write_mproperty
.as(not null), args
)
1306 redef class ASuperExpr
1309 var recv
= v
.frame
.arguments
.first
1311 for a
in self.n_args
.n_exprs
do
1314 if args
.length
== 1 then
1315 args
= v
.frame
.arguments
1318 var mproperty
= self.mproperty
1319 if mproperty
!= null then
1320 if mproperty
.intro
.msignature
.arity
== 0 then
1324 var res
= v
.send
(mproperty
, args
)
1328 # stantard call-next-method
1329 var mpropdef
= v
.frame
.mpropdef
1330 # FIXME: we do not want an ugly static call!
1331 var mpropdefs
= mpropdef
.mproperty
.lookup_super_definitions
(mpropdef
.mclassdef
.mmodule
, mpropdef
.mclassdef
.bound_mtype
)
1332 if mpropdefs
.length
!= 1 then
1333 debug
("MPRODFEFS for super {mpropdef} for {recv}: {mpropdefs.join(", ")}")
1335 mpropdef
= mpropdefs
.first
1336 assert mpropdef
isa MMethodDef
1337 var res
= v
.call
(mpropdef
, args
)
1342 redef class ANewExpr
1345 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1346 var recv
= new Instance(mtype
)
1347 v
.init_instance
(recv
)
1349 for a
in self.n_args
.n_exprs
do
1352 var mproperty
= self.mproperty
.as(not null)
1353 var res2
= v
.send
(mproperty
, args
)
1354 if res2
!= null then
1355 #self.debug("got {res2} from {mproperty}. drop {recv}")
1358 v
.check_init_instance
(recv
)
1363 redef class AAttrExpr
1366 var recv
= v
.expr
(self.n_expr
)
1367 var mproperty
= self.mproperty
.as(not null)
1368 return v
.read_attribute
(mproperty
, recv
)
1372 redef class AAttrAssignExpr
1375 var recv
= v
.expr
(self.n_expr
)
1376 var i
= v
.expr
(self.n_value
)
1377 var mproperty
= self.mproperty
.as(not null)
1378 recv
.attributes
[mproperty
] = i
1382 redef class AAttrReassignExpr
1385 var recv
= v
.expr
(self.n_expr
)
1386 var value
= v
.expr
(self.n_value
)
1387 var mproperty
= self.mproperty
.as(not null)
1388 var attr
= v
.read_attribute
(mproperty
, recv
)
1389 var res
= v
.send
(reassign_property
.mproperty
, [attr
, value
])
1391 recv
.attributes
[mproperty
] = res
1395 redef class AIssetAttrExpr
1398 var recv
= v
.expr
(self.n_expr
)
1399 var mproperty
= self.mproperty
.as(not null)
1400 return v
.bool_instance
(recv
.attributes
.has_key
(mproperty
))
1404 redef class ADebugTypeExpr