1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Interpretation of a Nit program directly on the AST
18 module naive_interpreter
22 import auto_super_init
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 sys_type
= mainmodule
.sys_type
38 if sys_type
== null then return # no class Sys
39 var mainobj
= new Instance(sys_type
)
40 interpreter
.mainobj
= mainobj
41 interpreter
.init_instance
(mainobj
)
42 var initprop
= mainmodule
.try_get_primitive_method
("init", sys_type
)
43 if initprop
!= null then
44 interpreter
.send
(initprop
, [mainobj
])
46 interpreter
.check_init_instance
(mainobj
)
47 var mainprop
= mainmodule
.try_get_primitive_method
("main", sys_type
)
48 if mainprop
!= null then
49 interpreter
.send
(mainprop
, [mainobj
])
53 self.toolcontext
.info
("*** END INTERPRETING: {time1-time0} ***", 2)
57 # The visitor that interprets the Nit Program by walking on the AST
58 private class NaiveInterpreter
59 # The modelbuilder that know the AST and its associations with the model
60 var modelbuilder
: ModelBuilder
62 # The main moduleof the program (used to lookup methoda
63 var mainmodule
: MModule
65 # The command line arguments of the interpreted program
66 # arguments.first is the program name
67 # arguments[1] is the first argument
68 var arguments
: Array[String]
70 var mainobj
: nullable Instance
72 init(modelbuilder
: ModelBuilder, mainmodule
: MModule, arguments
: Array[String])
74 self.modelbuilder
= modelbuilder
75 self.mainmodule
= mainmodule
76 self.arguments
= arguments
77 self.true_instance
= new PrimitiveInstance[Bool](mainmodule
.bool_type
, true)
78 self.false_instance
= new PrimitiveInstance[Bool](mainmodule
.bool_type
, false)
79 self.null_instance
= new Instance(mainmodule
.model
.null_type
)
82 # Subtype test in the context of the mainmodule
83 fun is_subtype
(sub
, sup
: MType): Bool
85 return sub
.is_subtype
(self.mainmodule
, self.frame
.arguments
.first
.mtype
.as(MClassType), sup
)
88 # Is a return executed?
89 # Set this mark to skip the evaluation until the end of the current method
90 var returnmark
: Bool = false
92 # Is a break executed?
93 # Set this mark to skip the evaluation until a labeled statement catch it with `is_break'
94 var breakmark
: nullable EscapeMark = null
96 # Is a continue executed?
97 # Set this mark to skip the evaluation until a labeled statement catch it with `is_continue'
98 var continuemark
: nullable EscapeMark = null
100 # Is a return or a break or a continue executed?
101 # Use this function to know if you must skip the evaluation of statements
102 fun is_escaping
: Bool do return returnmark
or breakmark
!= null or continuemark
!= null
104 # The value associated with the current return/break/continue, if any.
105 # Set the value when you set a escapemark.
106 # Read the value when you catch a mark or reach the end of a method
107 var escapevalue
: nullable Instance = null
109 # If there is a break and is associated with `escapemark', then return true an clear the mark.
110 # If there is no break or if `escapemark' is null then return false.
111 # Use this function to catch a potential break.
112 fun is_break
(escapemark
: nullable EscapeMark): Bool
114 if escapemark
!= null and self.breakmark
== escapemark
then
115 self.breakmark
= null
122 # If there is a continue and is associated with `escapemark', then return true an clear the mark.
123 # If there is no continue or if `escapemark' is null then return false.
124 # Use this function to catch a potential continue.
125 fun is_continue
(escapemark
: nullable EscapeMark): Bool
127 if escapemark
!= null and self.continuemark
== escapemark
then
128 self.continuemark
= null
135 # Evaluate `n' as an expression in the current context.
136 # Return the value of the expression.
137 # If `n' cannot be evaluated, then aborts.
138 fun expr
(n
: AExpr): Instance
140 var old
= self.frame
.current_node
141 self.frame
.current_node
= n
142 #n.debug("IN Execute expr")
143 var i
= n
.expr
(self).as(not null)
144 #n.debug("OUT Execute expr: value is {i}")
145 #if not is_subtype(i.mtype, n.mtype.as(not null)) then n.debug("Expected {n.mtype.as(not null)} got {i}")
146 self.frame
.current_node
= old
150 # Evaluate `n' as a statement in the current context.
151 # Do nothing if `n' is sull.
152 # If `n' cannot be evaluated, then aborts.
153 fun stmt
(n
: nullable AExpr)
156 var old
= self.frame
.current_node
157 self.frame
.current_node
= n
158 #n.debug("Execute stmt")
160 self.frame
.current_node
= old
164 # Map used to store values of nodes that must be evaluated once in the system (AOnceExpr)
165 var onces
: Map[ANode, Instance] = new HashMap[ANode, Instance]
167 # Return the boolean instance associated with `val'.
168 fun bool_instance
(val
: Bool): Instance
170 if val
then return self.true_instance
else return self.false_instance
173 # Return the integer instance associated with `val'.
174 fun int_instance
(val
: Int): Instance
176 var ic
= self.mainmodule
.get_primitive_class
("Int")
177 return new PrimitiveInstance[Int](ic
.mclass_type
, val
)
180 # Return the char instance associated with `val'.
181 fun char_instance
(val
: Char): Instance
183 var ic
= self.mainmodule
.get_primitive_class
("Char")
184 return new PrimitiveInstance[Char](ic
.mclass_type
, val
)
187 # Return the float instance associated with `val'.
188 fun float_instance
(val
: Float): Instance
190 var ic
= self.mainmodule
.get_primitive_class
("Float")
191 return new PrimitiveInstance[Float](ic
.mclass_type
, val
)
194 # The unique intance of the `true' value.
195 var true_instance
: Instance
197 # The unique intance of the `false' value.
198 var false_instance
: Instance
200 # The unique intance of the `null' value.
201 var null_instance
: Instance
203 # Return a new array made of `values'.
204 # The dynamic type of the result is Array[elttype].
205 fun array_instance
(values
: Array[Instance], elttype
: MType): Instance
207 assert not elttype
.need_anchor
208 var nat
= new PrimitiveInstance[Array[Instance]](self.mainmodule
.get_primitive_class
("NativeArray").get_mtype
([elttype
]), values
)
209 var mtype
= self.mainmodule
.get_primitive_class
("Array").get_mtype
([elttype
])
210 var res
= new Instance(mtype
)
211 self.init_instance
(res
)
212 self.send
(self.mainmodule
.force_get_primitive_method
("with_native", mtype
), [res
, nat
, self.int_instance
(values
.length
)])
213 self.check_init_instance
(res
)
217 # Return a new native string initialized with `txt'
218 fun native_string_instance
(txt
: String): Instance
220 var val
= new Buffer.from
(txt
)
221 var ic
= self.mainmodule
.get_primitive_class
("NativeString")
222 return new PrimitiveInstance[Buffer](ic
.mclass_type
, val
)
225 # The current frame used to store local variables of the current method executed
226 fun frame
: Frame do return frames
.first
228 # The stack of all frames. The first one is the current one.
229 var frames
: List[Frame] = new List[Frame]
231 # Return a stack stace. One line per function
232 fun stack_trace
: String
235 b
.append
(",---- Stack trace -- - - -\n")
237 b
.append
("| {f.mpropdef} ({f.current_node.location})\n")
239 b
.append
("`------------------- - - -")
243 # Exit the program with a message
244 fun fatal
(message
: String)
246 if frames
.is_empty
then
249 self.frame
.current_node
.fatal
(self, message
)
254 # Execute `mpropdef' for a `args' (where args[0] is the receiver).
255 # Return a falue if `mpropdef' is a function, or null if it is a procedure.
256 # The call is direct/static. There is no message-seding/late-bindng.
257 fun call
(mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
259 var vararg_rank
= mpropdef
.msignature
.vararg_rank
260 if vararg_rank
>= 0 then
261 assert args
.length
>= mpropdef
.msignature
.arity
+ 1 # because of self
263 args
= new Array[Instance]
265 args
.add
(rawargs
.first
) # recv
267 for i
in [0..vararg_rank
[ do
268 args
.add
(rawargs
[i
+1])
271 var vararg_lastrank
= vararg_rank
+ rawargs
.length-1-mpropdef
.msignature
.arity
272 var vararg
= new Array[Instance]
273 for i
in [vararg_rank
..vararg_lastrank
] do
274 vararg
.add
(rawargs
[i
+1])
276 # FIXME: its it to late to determine the vararg type, this should have been done during a previous analysis
277 var elttype
= mpropdef
.msignature
.parameter_mtypes
[vararg_rank
].anchor_to
(self.mainmodule
, args
.first
.mtype
.as(MClassType))
278 args
.add
(self.array_instance
(vararg
, elttype
))
280 for i
in [vararg_lastrank
+1..rawargs
.length-1
[ do
281 args
.add
(rawargs
[i
+1])
284 assert args
.length
== mpropdef
.msignature
.arity
+ 1 # because of self
286 # Look for the AST node that implements the property
287 var mproperty
= mpropdef
.mproperty
288 if self.modelbuilder
.mpropdef2npropdef
.has_key
(mpropdef
) then
289 var npropdef
= self.modelbuilder
.mpropdef2npropdef
[mpropdef
]
290 return npropdef
.call
(self, mpropdef
, args
)
291 else if mproperty
.name
== "init" then
292 var nclassdef
= self.modelbuilder
.mclassdef2nclassdef
[mpropdef
.mclassdef
]
293 return nclassdef
.call
(self, mpropdef
, args
)
295 fatal
("Fatal Error: method {mpropdef} not found in the AST")
300 # Execute `mproperty' for a `args' (where args[0] is the receiver).
301 # Return a falue if `mproperty' is a function, or null if it is a procedure.
302 # The call is polimotphic. There is a message-seding/late-bindng according to te receiver (args[0]).
303 fun send
(mproperty
: MMethod, args
: Array[Instance]): nullable Instance
305 var recv
= args
.first
306 var mtype
= recv
.mtype
307 if mtype
isa MNullType then
308 if mproperty
.name
== "==" then
309 return self.bool_instance
(args
[0] == args
[1])
310 else if mproperty
.name
== "!=" then
311 return self.bool_instance
(args
[0] != args
[1])
313 #fatal("Reciever is null. {mproperty}. {args.join(" ")} {self.frame.current_node.class_name}")
314 fatal
("Reciever is null")
317 var propdefs
= mproperty
.lookup_definitions
(self.mainmodule
, mtype
)
318 if propdefs
.length
> 1 then
319 fatal
("NOT YET IMPLEMETED ERROR: Property conflict: {propdefs.join(", ")}")
322 assert propdefs
.length
== 1 else
323 fatal
("Fatal Error: No property '{mproperty}' for '{recv}'")
326 var propdef
= propdefs
.first
327 return self.call
(propdef
, args
)
330 # Read the attribute `mproperty' of an instance `recv' and return its value.
331 # If the attribute in not yet initialized, then aborts with an error message.
332 fun read_attribute
(mproperty
: MAttribute, recv
: Instance): Instance
334 if not recv
.attributes
.has_key
(mproperty
) then
335 fatal
("Uninitialized attribute {mproperty.name}")
338 return recv
.attributes
[mproperty
]
341 # Fill the initial values of the newly created instance `recv'.
342 # `recv.mtype' is used to know what must be filled.
343 fun init_instance
(recv
: Instance)
345 for cd
in recv
.mtype
.collect_mclassdefs
(self.mainmodule
)
347 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
348 for npropdef
in n
.n_propdefs
do
349 if npropdef
isa AAttrPropdef then
350 npropdef
.init_expr
(self, recv
)
356 # Check that non nullable attributes of `recv' are correctly initialized.
357 # This function is used as the last instruction of a new
358 # FIXME: this will work better once there is nullable types
359 fun check_init_instance
(recv
: Instance)
361 for cd
in recv
.mtype
.collect_mclassdefs
(self.mainmodule
)
363 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
364 for npropdef
in n
.n_propdefs
do
365 if npropdef
isa AAttrPropdef and npropdef
.n_expr
== null then
366 # Force read to check the initialization
367 self.read_attribute
(npropdef
.mpropdef
.mproperty
, recv
)
373 # This function determine the correct type according the reciever of the current definition (self).
374 fun unanchor_type
(mtype
: MType): MType
376 return mtype
.anchor_to
(self.mainmodule
, self.frame
.arguments
.first
.mtype
.as(MClassType))
380 # An instance represents a value of the executed program.
382 # The dynamic type of the instance
383 # ASSERT: not self.mtype.is_anchored
386 # The values of the attributes
387 var attributes
: Map[MAttribute, Instance] = new HashMap[MAttribute, Instance]
389 # return true if the instance is the true value.
390 # return false if the instance is the true value.
392 fun is_true
: Bool do abort
394 # Return true if `self' IS `o' (using the Nit semantic of is)
395 fun eq_is
(o
: Instance): Bool do return self is o
397 # Human readable object identity "Type#number"
398 redef fun to_s
do return "{mtype}#{object_id}"
400 # Return the integer valur is the instance is an integer.
402 fun to_i
: Int do abort
404 # The real value encapsulated if the instance is primitive.
406 fun val
: Object do abort
409 # Special instance to handle primitives values (int, bool, etc.)
410 # The trick it just to encapsulate the <<real>> value
411 class PrimitiveInstance[E
: Object]
414 # The real value encapsulated
417 init(mtype
: MType, val
: E
)
425 if val
== true then return true
426 if val
== false then return false
432 if not o
isa PrimitiveInstance[Object] then return false
433 return self.val
== o
.val
438 if not o
isa PrimitiveInstance[Object] then return false
439 return self.val
is o
.val
442 redef fun to_s
do return "{mtype}#{val.object_id}({val})"
444 redef fun to_i
do return val
.as(Int)
447 # Information about local variables in a running method
449 # The current visited node
450 # The node is stored by frame to keep a stack trace
451 var current_node
: ANode
452 # The executed property.
453 # A Method in case of a call, an attribute in case of a default initialization.
454 var mpropdef
: MPropDef
455 # Arguments of the method (the first is te receiver
456 var arguments
: Array[Instance]
457 # Mapping betwen a variable an the current value
458 var map
: Map[Variable, Instance] = new HashMap[Variable, Instance]
462 # Aborts the program with a message
463 # `v' is used to know if a colored message is displayed or not
464 private fun fatal
(v
: NaiveInterpreter, message
: String)
466 if v
.modelbuilder
.toolcontext
.opt_no_color
.value
== true then
467 print
("{message} ({location.file.filename}:{location.line_start})")
469 print
("{location}: {message}\n{location.colored_line("0;31")}")
477 # Execute a `mpropdef' associated with the current node.
478 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
480 fatal
(v
, "Unimplemented {mpropdef}")
485 redef class AConcreteMethPropdef
486 redef fun call
(v
, mpropdef
, args
)
488 var f
= new Frame(self, self.mpropdef
.as(not null), args
)
489 for i
in [0..mpropdef
.msignature
.arity
[ do
490 var variable
= self.n_signature
.n_params
[i
].variable
491 assert variable
!= null
492 f
.map
[variable
] = args
[i
+1]
497 # Call the implicit super-init
498 var auto_super_inits
= self.auto_super_inits
499 if auto_super_inits
!= null then
500 var selfarg
= [args
.first
]
501 for auto_super_init
in auto_super_inits
do
502 if auto_super_init
.intro
.msignature
.arity
== 0 then
503 v
.send
(auto_super_init
, selfarg
)
505 v
.send
(auto_super_init
, args
)
513 var res
= v
.escapevalue
519 redef class AInternMethPropdef
520 redef fun call
(v
, mpropdef
, args
)
522 var pname
= mpropdef
.mproperty
.name
523 var cname
= mpropdef
.mclassdef
.mclass
.name
524 if pname
== "output" then
525 var recv
= args
.first
528 else if pname
== "object_id" then
529 var recv
= args
.first
530 if recv
isa PrimitiveInstance[Object] then
531 return v
.int_instance
(recv
.val
.object_id
)
533 return v
.int_instance
(recv
.object_id
)
535 else if pname
== "output_class_name" then
536 var recv
= args
.first
537 print recv
.mtype
.as(MClassType).mclass
539 else if pname
== "native_class_name" then
540 var recv
= args
.first
541 var txt
= recv
.mtype
.as(MClassType).mclass
.to_s
542 return v
.native_string_instance
(txt
)
543 else if pname
== "==" then
544 # == is correclt redefined for instances
545 return v
.bool_instance
(args
[0] == args
[1])
546 else if pname
== "!=" then
547 return v
.bool_instance
(args
[0] != args
[1])
548 else if pname
== "is_same_type" then
549 return v
.bool_instance
(args
[0].mtype
== args
[1].mtype
)
550 else if pname
== "exit" then
553 else if pname
== "sys" then
555 else if cname
== "Int" then
556 if pname
== "unary -" then
557 return v
.int_instance
(-args
[0].to_i
)
558 else if pname
== "succ" then
559 return v
.int_instance
(args
[0].to_i
+ 1)
560 else if pname
== "prec" then
561 return v
.int_instance
(args
[0].to_i
- 1)
562 else if pname
== "+" then
563 return v
.int_instance
(args
[0].to_i
+ args
[1].to_i
)
564 else if pname
== "-" then
565 return v
.int_instance
(args
[0].to_i
- args
[1].to_i
)
566 else if pname
== "*" then
567 return v
.int_instance
(args
[0].to_i
* args
[1].to_i
)
568 else if pname
== "%" then
569 return v
.int_instance
(args
[0].to_i
% args
[1].to_i
)
570 else if pname
== "/" then
571 return v
.int_instance
(args
[0].to_i
/ args
[1].to_i
)
572 else if pname
== "<" then
573 return v
.bool_instance
(args
[0].to_i
< args
[1].to_i
)
574 else if pname
== ">" then
575 return v
.bool_instance
(args
[0].to_i
> args
[1].to_i
)
576 else if pname
== "<=" then
577 return v
.bool_instance
(args
[0].to_i
<= args
[1].to_i
)
578 else if pname
== ">=" then
579 return v
.bool_instance
(args
[0].to_i
>= args
[1].to_i
)
580 else if pname
== "<=>" then
581 return v
.int_instance
(args
[0].to_i
<=> args
[1].to_i
)
582 else if pname
== "ascii" then
583 return v
.char_instance
(args
[0].to_i
.ascii
)
584 else if pname
== "to_f" then
585 return v
.float_instance
(args
[0].to_i
.to_f
)
586 else if pname
== "lshift" then
587 return v
.int_instance
(args
[0].to_i
.lshift
(args
[1].to_i
))
588 else if pname
== "rshift" then
589 return v
.int_instance
(args
[0].to_i
.rshift
(args
[1].to_i
))
591 else if cname
== "Char" then
592 var recv
= args
[0].val
.as(Char)
593 if pname
== "ascii" then
594 return v
.int_instance
(recv
.ascii
)
595 else if pname
== "succ" then
596 return v
.char_instance
(recv
.succ
)
597 else if pname
== "prec" then
598 return v
.char_instance
(recv
.prec
)
599 else if pname
== "<" then
600 return v
.bool_instance
(recv
< args
[1].val
.as(Char))
601 else if pname
== ">" then
602 return v
.bool_instance
(recv
> args
[1].val
.as(Char))
603 else if pname
== "<=" then
604 return v
.bool_instance
(recv
<= args
[1].val
.as(Char))
605 else if pname
== ">=" then
606 return v
.bool_instance
(recv
>= args
[1].val
.as(Char))
607 else if pname
== "<=>" then
608 return v
.int_instance
(recv
<=> args
[1].val
.as(Char))
610 else if cname
== "Float" then
612 return v
.float_instance
(args
[0].val
.as(Float) + args
[1].val
.as(Float))
613 else if pname
== "-" then
614 return v
.float_instance
(args
[0].val
.as(Float) - args
[1].val
.as(Float))
615 else if pname
== "*" then
616 return v
.float_instance
(args
[0].val
.as(Float) * args
[1].val
.as(Float))
617 else if pname
== "/" then
618 return v
.float_instance
(args
[0].val
.as(Float) / args
[1].val
.as(Float))
619 else if pname
== "to_i" then
620 return v
.int_instance
(args
[0].val
.as(Float).to_i
)
622 else if cname
== "NativeString" then
623 var recvval
= args
.first
.val
.as(Buffer)
624 if pname
== "[]" then
625 return v
.char_instance
(recvval
[args
[1].to_i
])
626 else if pname
== "[]=" then
627 recvval
[args
[1].to_i
] = args
[2].val
.as(Char)
629 else if pname
== "copy_to" then
630 # sig= copy_to(dest: NativeString, length: Int, from: Int, to: Int)
631 recvval
.copy
(args
[3].to_i
, args
[2].to_i
, args
[1].val
.as(Buffer), args
[4].to_i
)
633 else if pname
== "atoi" then
634 return v
.int_instance
(recvval
.to_i
)
636 else if pname
== "calloc_string" then
637 return v
.native_string_instance
("!" * args
[1].to_i
)
638 else if cname
== "NativeArray" then
639 var recvval
= args
.first
.val
.as(Array[Instance])
640 if pname
== "[]" then
641 if args
[1].to_i
>= recvval
.length
then
642 debug
("Illegal access on {recvval} for element {args[1].to_i}/{recvval.length}")
644 return recvval
[args
[1].to_i
]
645 else if pname
== "[]=" then
646 recvval
[args
[1].to_i
] = args
[2]
648 else if pname
== "copy_to" then
649 recvval
.copy
(0, args
[2].to_i
, args
[1].val
.as(Array[Instance]), 0)
652 else if pname
== "calloc_array" then
653 var recvtype
= args
.first
.mtype
.as(MClassType)
654 var mtype
: MType = recvtype
.supertype_to
(v
.mainmodule
, recvtype
, v
.mainmodule
.get_primitive_class
("ArrayCapable"))
655 mtype
= mtype
.as(MGenericType).arguments
.first
656 var val
= new Array[Instance].filled_with
(v
.null_instance
, args
[1].to_i
)
657 return new PrimitiveInstance[Array[Instance]](v
.mainmodule
.get_primitive_class
("NativeArray").get_mtype
([mtype
]), val
)
659 fatal
(v
, "Unimplemented intern {mpropdef}")
664 redef class AbstractArray[E
]
665 fun copy
(start
: Int, len
: Int, dest
: AbstractArray[E
], new_start
: Int)
667 self.copy_to
(start
, len
, dest
, new_start
)
671 redef class AExternInitPropdef
672 redef fun call
(v
, mpropdef
, args
)
674 var pname
= mpropdef
.mproperty
.name
675 var cname
= mpropdef
.mclassdef
.mclass
.name
676 if pname
== "native_stdout" then
677 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, stdout
)
678 else if pname
== "native_stdin" then
679 return new PrimitiveInstance[IStream](mpropdef
.mclassdef
.mclass
.mclass_type
, stdin
)
680 else if pname
== "native_stderr" then
681 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, stderr
)
682 else if pname
== "io_open_read" then
683 var a1
= args
[1].val
.as(Buffer)
684 return new PrimitiveInstance[IStream](mpropdef
.mclassdef
.mclass
.mclass_type
, new IFStream.open
(a1
.to_s
))
685 else if pname
== "io_open_write" then
686 var a1
= args
[1].val
.as(Buffer)
687 return new PrimitiveInstance[OStream](mpropdef
.mclassdef
.mclass
.mclass_type
, new OFStream.open
(a1
.to_s
))
689 fatal
(v
, "Unimplemented extern init {mpropdef}")
694 redef class AExternMethPropdef
696 redef fun call
(v
, mpropdef
, args
)
698 var pname
= mpropdef
.mproperty
.name
699 var cname
= mpropdef
.mclassdef
.mclass
.name
700 if cname
== "NativeFile" then
701 var recvval
= args
.first
.val
702 if pname
== "io_write" then
703 var a1
= args
[1].val
.as(Buffer)
704 recvval
.as(OStream).write
(a1
.substring
(0, args
[2].to_i
))
706 else if pname
== "io_read" then
707 var str
= recvval
.as(IStream).read
(args
[2].to_i
)
708 var a1
= args
[1].val
.as(Buffer)
709 new Buffer.from
(str
).copy
(0, str
.length
, a1
, 0)
710 return v
.int_instance
(str
.length
)
711 else if pname
== "io_close" then
712 recvval
.as(IOS).close
713 return v
.int_instance
(0)
715 else if cname
== "NativeString" then
716 var recvval
= args
.first
.val
.as(Buffer)
717 if pname
== "file_exists" then
718 return v
.bool_instance
(recvval
.to_s
.file_exists
)
719 else if pname
== "file_mkdir" then
722 else if pname
== "get_environ" then
723 var txt
= args
.first
.val
.as(Buffer).to_s
.environ
724 return v
.native_string_instance
(txt
)
726 else if pname
== "native_argc" then
727 return v
.int_instance
(v
.arguments
.length
)
728 else if pname
== "native_argv" then
729 var txt
= v
.arguments
[args
[1].to_i
]
730 return v
.native_string_instance
(txt
)
731 else if pname
== "get_time" then
732 return v
.int_instance
(get_time
)
733 else if pname
== "lexer_goto" then
734 return v
.int_instance
(lexer_goto
(args
[1].to_i
, args
[2].to_i
))
735 else if pname
== "lexer_accept" then
736 return v
.int_instance
(lexer_accept
(args
[1].to_i
))
737 else if pname
== "parser_goto" then
738 return v
.int_instance
(parser_goto
(args
[1].to_i
, args
[2].to_i
))
739 else if pname
== "parser_action" then
740 return v
.int_instance
(parser_action
(args
[1].to_i
, args
[2].to_i
))
742 fatal
(v
, "Unimplemented extern {mpropdef}")
747 redef class AAttrPropdef
748 redef fun call
(v
, mpropdef
, args
)
750 var attr
= self.mpropdef
.mproperty
751 if args
.length
== 1 then
752 return v
.read_attribute
(attr
, args
.first
)
754 assert args
.length
== 2
755 args
.first
.attributes
[attr
] = args
[1]
760 # Evaluate and set the default value of the attribute in `recv'
761 private fun init_expr
(v
: NaiveInterpreter, recv
: Instance)
763 var nexpr
= self.n_expr
764 if nexpr
!= null then
765 var f
= new Frame(self, self.mpropdef
.as(not null), [recv
])
767 var val
= v
.expr
(nexpr
)
769 assert not v
.is_escaping
770 recv
.attributes
[self.mpropdef
.mproperty
] = val
773 var mtype
= self.mpropdef
.static_mtype
.as(not null)
774 # TODO The needinit info is statically computed, move it to modelbuilder or whatever
775 mtype
= mtype
.resolve_for
(self.mpropdef
.mclassdef
.bound_mtype
, self.mpropdef
.mclassdef
.bound_mtype
, self.mpropdef
.mclassdef
.mmodule
, true)
776 if mtype
isa MNullableType then
777 recv
.attributes
[self.mpropdef
.mproperty
] = v
.null_instance
782 redef class AClassdef
783 # Execute an implicit `mpropdef' associated with the current node.
784 private fun call
(v
: NaiveInterpreter, mpropdef
: MMethodDef, args
: Array[Instance]): nullable Instance
786 var super_inits
= self.super_inits
787 if super_inits
!= null then
788 assert args
.length
== 1
789 for su
in super_inits
do
794 var recv
= args
.first
796 # Collect undefined attributes
797 for npropdef
in self.n_propdefs
do
798 if npropdef
isa AAttrPropdef and npropdef
.n_expr
== null then
799 recv
.attributes
[npropdef
.mpropdef
.mproperty
] = args
[i
]
808 # Evaluate the node as a possible expression.
809 # Return a possible value
810 # NOTE: Do not call this method directly, but use `v.expr'
811 # This method is here to be implemented by subclasses.
812 private fun expr
(v
: NaiveInterpreter): nullable Instance
814 fatal
(v
, "Unimplemented expr {class_name}")
818 # Evaluate the node as a statement.
819 # NOTE: Do not call this method directly, but use `v.stmt'
820 # This method is here to be implemented by subclasses (no need to return something).
821 private fun stmt
(v
: NaiveInterpreter)
828 redef class ABlockExpr
831 for e
in self.n_expr
do
833 if v
.is_escaping
then return
838 redef class AVardeclExpr
844 v
.frame
.map
[self.variable
.as(not null)] = i
852 return v
.frame
.map
[self.variable
.as(not null)]
856 redef class AVarAssignExpr
859 var i
= v
.expr
(self.n_value
)
860 v
.frame
.map
[self.variable
.as(not null)] = i
864 redef class AVarReassignExpr
867 var vari
= v
.frame
.map
[self.variable
.as(not null)]
868 var value
= v
.expr
(self.n_value
)
869 var res
= v
.send
(reassign_property
.mproperty
, [vari
, value
])
871 v
.frame
.map
[self.variable
.as(not null)] = res
875 redef class ASelfExpr
878 return v
.frame
.arguments
.first
882 redef class AContinueExpr
885 v
.continuemark
= self.escapemark
889 redef class ABreakExpr
892 v
.breakmark
= self.escapemark
896 redef class AReturnExpr
908 redef class AAbortExpr
919 var cond
= v
.expr
(self.n_expr
)
928 redef class AIfexprExpr
931 var cond
= v
.expr
(self.n_expr
)
933 return v
.expr
(self.n_then
)
935 return v
.expr
(self.n_else
)
944 v
.is_break
(self.escapemark
) # Clear the break (if any)
948 redef class AWhileExpr
952 var cond
= v
.expr
(self.n_expr
)
953 if not cond
.is_true
then return
955 if v
.is_break
(self.escapemark
) then return
956 v
.is_continue
(self.escapemark
) # Clear the break
957 if v
.is_escaping
then return
962 redef class ALoopExpr
967 if v
.is_break
(self.escapemark
) then return
968 v
.is_continue
(self.escapemark
) # Clear the break
969 if v
.is_escaping
then return
977 var col
= v
.expr
(self.n_expr
)
978 #self.debug("col {col}")
979 var iter
= v
.send
(v
.mainmodule
.force_get_primitive_method
("iterator", col
.mtype
), [col
]).as(not null)
980 #self.debug("iter {iter}")
982 var isok
= v
.send
(v
.mainmodule
.force_get_primitive_method
("is_ok", iter
.mtype
), [iter
]).as(not null)
983 if not isok
.is_true
then return
984 var item
= v
.send
(v
.mainmodule
.force_get_primitive_method
("item", iter
.mtype
), [iter
]).as(not null)
985 #self.debug("item {item}")
986 v
.frame
.map
[self.variables
.first
] = item
988 if v
.is_break
(self.escapemark
) then return
989 v
.is_continue
(self.escapemark
) # Clear the break
990 if v
.is_escaping
then return
991 v
.send
(v
.mainmodule
.force_get_primitive_method
("next", iter
.mtype
), [iter
])
996 redef class AAssertExpr
999 var cond
= v
.expr
(self.n_expr
)
1000 if not cond
.is_true
then
1002 if v
.is_escaping
then return
1005 fatal
(v
, "Assert '{nid.text}' failed")
1007 fatal
(v
, "Assert failed")
1017 var cond
= v
.expr
(self.n_expr
)
1018 if cond
.is_true
then return cond
1019 return v
.expr
(self.n_expr2
)
1023 redef class AAndExpr
1026 var cond
= v
.expr
(self.n_expr
)
1027 if not cond
.is_true
then return cond
1028 return v
.expr
(self.n_expr2
)
1032 redef class ANotExpr
1035 var cond
= v
.expr
(self.n_expr
)
1036 return v
.bool_instance
(not cond
.is_true
)
1040 redef class AOrElseExpr
1043 var i
= v
.expr
(self.n_expr
)
1044 if i
!= v
.null_instance
then return i
1045 return v
.expr
(self.n_expr2
)
1052 var i
= v
.expr
(self.n_expr
)
1053 var i2
= v
.expr
(self.n_expr2
)
1054 return v
.bool_instance
(i
.eq_is
(i2
))
1058 redef class AIntExpr
1061 return v
.int_instance
(self.value
.as(not null))
1065 redef class AFloatExpr
1068 return v
.float_instance
(self.value
.as(not null))
1072 redef class ACharExpr
1075 return v
.char_instance
(self.value
.as(not null))
1079 redef class AArrayExpr
1082 var val
= new Array[Instance]
1083 for nexpr
in self.n_exprs
.n_exprs
do
1084 val
.add
(v
.expr
(nexpr
))
1086 var mtype
= v
.unanchor_type
(self.mtype
.as(not null)).as(MGenericType)
1087 var elttype
= mtype
.arguments
.first
1088 return v
.array_instance
(val
, elttype
)
1092 redef class AStringFormExpr
1095 var txt
= self.value
.as(not null)
1096 var nat
= v
.native_string_instance
(txt
)
1097 var res
= new Instance(v
.mainmodule
.get_primitive_class
("String").mclass_type
)
1098 v
.init_instance
(res
)
1099 v
.send
(v
.mainmodule
.force_get_primitive_method
("from_cstring", res
.mtype
), [res
, nat
])
1100 v
.check_init_instance
(res
)
1105 redef class ASuperstringExpr
1108 var array
= new Array[Instance]
1109 for nexpr
in n_exprs
do
1110 array
.add
(v
.expr
(nexpr
))
1112 var i
= v
.array_instance
(array
, v
.mainmodule
.get_primitive_class
("Object").mclass_type
)
1113 var res
= v
.send
(v
.mainmodule
.force_get_primitive_method
("to_s", i
.mtype
), [i
])
1119 redef class ACrangeExpr
1122 var e1
= v
.expr
(self.n_expr
)
1123 var e2
= v
.expr
(self.n_expr2
)
1124 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1125 var res
= new Instance(mtype
)
1126 v
.init_instance
(res
)
1127 v
.send
(v
.mainmodule
.force_get_primitive_method
("init", mtype
), [res
, e1
, e2
])
1128 v
.check_init_instance
(res
)
1133 redef class AOrangeExpr
1136 var e1
= v
.expr
(self.n_expr
)
1137 var e2
= v
.expr
(self.n_expr2
)
1138 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1139 var res
= new Instance(mtype
)
1140 v
.init_instance
(res
)
1141 v
.send
(v
.mainmodule
.force_get_primitive_method
("without_last", mtype
), [res
, e1
, e2
])
1142 v
.check_init_instance
(res
)
1147 redef class ATrueExpr
1150 return v
.bool_instance
(true)
1154 redef class AFalseExpr
1157 return v
.bool_instance
(false)
1161 redef class ANullExpr
1164 return v
.null_instance
1168 redef class AIsaExpr
1171 var i
= v
.expr
(self.n_expr
)
1172 var mtype
= v
.unanchor_type
(self.cast_type
.as(not null))
1173 return v
.bool_instance
(v
.is_subtype
(i
.mtype
, mtype
))
1177 redef class AAsCastExpr
1180 var i
= v
.expr
(self.n_expr
)
1181 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1182 if not v
.is_subtype
(i
.mtype
, mtype
) then
1183 #fatal(v, "Cast failed expected {mtype}, got {i}")
1184 fatal
(v
, "Cast failed")
1190 redef class AAsNotnullExpr
1193 var i
= v
.expr
(self.n_expr
)
1194 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1195 if i
.mtype
isa MNullType then
1196 fatal
(v
, "Cast failed")
1202 redef class AParExpr
1205 return v
.expr
(self.n_expr
)
1209 redef class AOnceExpr
1212 if v
.onces
.has_key
(self) then
1213 return v
.onces
[self]
1215 var res
= v
.expr
(self.n_expr
)
1222 redef class ASendExpr
1225 var recv
= v
.expr
(self.n_expr
)
1227 for a
in compute_raw_arguments
do
1230 var mproperty
= self.mproperty
.as(not null)
1231 return v
.send
(mproperty
, args
)
1235 redef class ASendReassignFormExpr
1238 var recv
= v
.expr
(self.n_expr
)
1240 for a
in compute_raw_arguments
do
1243 var value
= v
.expr
(self.n_value
)
1245 var mproperty
= self.mproperty
.as(not null)
1246 var read
= v
.send
(mproperty
, args
)
1249 var write
= v
.send
(self.reassign_property
.mproperty
, [read
, value
])
1250 assert write
!= null
1254 v
.send
(self.write_mproperty
.as(not null), args
)
1258 redef class ASuperExpr
1261 var recv
= v
.frame
.arguments
.first
1263 for a
in self.n_args
.n_exprs
do
1266 if args
.length
== 1 then
1267 args
= v
.frame
.arguments
1270 var mproperty
= self.mproperty
1271 if mproperty
!= null then
1272 if mproperty
.intro
.msignature
.arity
== 0 then
1276 var res
= v
.send
(mproperty
, args
)
1280 # stantard call-next-method
1281 var mpropdef
= v
.frame
.mpropdef
1282 # FIXME: we do not want an ugly static call!
1283 var mpropdefs
= mpropdef
.mproperty
.lookup_super_definitions
(mpropdef
.mclassdef
.mmodule
, mpropdef
.mclassdef
.bound_mtype
)
1284 if mpropdefs
.length
!= 1 then
1285 debug
("MPRODFEFS for super {mpropdef} for {recv}: {mpropdefs.join(", ")}")
1287 mpropdef
= mpropdefs
.first
1288 assert mpropdef
isa MMethodDef
1289 var res
= v
.call
(mpropdef
, args
)
1294 redef class ANewExpr
1297 var mtype
= v
.unanchor_type
(self.mtype
.as(not null))
1298 var recv
= new Instance(mtype
)
1299 v
.init_instance
(recv
)
1301 for a
in self.n_args
.n_exprs
do
1304 var mproperty
= self.mproperty
.as(not null)
1305 var res2
= v
.send
(mproperty
, args
)
1306 if res2
!= null then
1307 #self.debug("got {res2} from {mproperty}. drop {recv}")
1310 v
.check_init_instance
(recv
)
1315 redef class AAttrExpr
1318 var recv
= v
.expr
(self.n_expr
)
1319 var mproperty
= self.mproperty
.as(not null)
1320 return v
.read_attribute
(mproperty
, recv
)
1324 redef class AAttrAssignExpr
1327 var recv
= v
.expr
(self.n_expr
)
1328 var i
= v
.expr
(self.n_value
)
1329 var mproperty
= self.mproperty
.as(not null)
1330 recv
.attributes
[mproperty
] = i
1334 redef class AAttrReassignExpr
1337 var recv
= v
.expr
(self.n_expr
)
1338 var value
= v
.expr
(self.n_value
)
1339 var mproperty
= self.mproperty
.as(not null)
1340 var attr
= v
.read_attribute
(mproperty
, recv
)
1341 var res
= v
.send
(reassign_property
.mproperty
, [attr
, value
])
1343 recv
.attributes
[mproperty
] = res
1347 redef class AIssetAttrExpr
1350 var recv
= v
.expr
(self.n_expr
)
1351 var mproperty
= self.mproperty
.as(not null)
1352 return v
.bool_instance
(recv
.attributes
.has_key
(mproperty
))
1356 redef class ADebugTypeExpr