1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2013 Lucas Bajolet <lucas.bajolet@gmail.com>
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 # Debugging of a nit program using the NaiveInterpreter
20 intrude import naive_interpreter
22 redef class ToolContext
24 var opt_debugger_mode
: OptionBool = new OptionBool("Launches the target program with the debugger attached to it", "-d")
29 self.option_context
.add_option
(self.opt_debugger_mode
)
33 redef class ModelBuilder
34 # Execute the program from the entry point (Sys::main) of the `mainmodule'
35 # `arguments' are the command-line arguments in order
37 # 1. the AST is fully loaded.
38 # 2. the model is fully built.
39 # 3. the instructions are fully analysed.
40 fun run_debugger
(mainmodule
: MModule, arguments
: Array[String])
43 self.toolcontext
.info
("*** START INTERPRETING ***", 1)
45 var interpreter
= new Debugger(self, mainmodule
, arguments
)
47 init_naive_interpreter
(interpreter
, mainmodule
)
50 self.toolcontext
.info
("*** END INTERPRETING: {time1-time0} ***", 2)
54 # The class extending NaiveInterpreter by adding debugging methods
56 super NaiveInterpreter
58 # Gets an attribute 'attribute_name' contained in variable 'variable'
59 fun get_attribute_in_mutable_instance
(variable
: MutableInstance, attribute_name
: String): nullable MAttribute
61 var map_of_attributes
= variable
.attributes
63 for key
in map_of_attributes
.keys
do
64 if key
.to_s
.substring_from
(1) == attribute_name
then
72 # Recursive function, returns the variable described by 'total_chain'
73 fun get_variable_in_mutable_instance
(variable
: MutableInstance, iterator
: Iterator[String]): nullable Instance
75 var attribute
= get_attribute_in_mutable_instance
(variable
, iterator
.item
)
77 if attribute
== null then return null
81 if iterator
.is_ok
then
82 var new_variable
= variable
.attributes
[attribute
]
83 if new_variable
isa MutableInstance then
84 return get_variable_in_mutable_instance
(new_variable
, iterator
)
89 return variable
.attributes
[attribute
]