+ # Processes the modification function to modify a variable dynamically
+ #
+ # Command of type variable = value
+ fun process_mod_function(parts_of_command: Array[String])
+ do
+ parts_of_command[0] = get_real_variable_name(parts_of_command[0])
+ var parts_of_variable = parts_of_command[0].split_with(".")
+
+ if parts_of_variable.length > 1 then
+ var last_part = parts_of_variable.pop
+ var first_part = parts_of_command[0].substring(0,parts_of_command[0].length - last_part.length - 1)
+ var papa = seek_variable(first_part, frame)
+
+ if papa != null and papa isa MutableInstance then
+ var attribute = get_attribute_in_mutable_instance(papa, last_part)
+
+ if attribute != null then
+ modify_argument_of_complex_type(papa, attribute, parts_of_command[2])
+ end
+ end
+ else
+ var target = seek_variable(parts_of_variable[0], frame)
+ if target != null then
+ modify_in_frame(target, parts_of_command[2])
+ end
+ end
+ end
+
+ # Processes the untrace variable command
+ #
+ # Command pattern : "untrace variable"
+ fun process_untrace_command(parts_of_command: Array[String])
+ do
+ var variable_name = get_real_variable_name(parts_of_command[1])
+ if untrace_variable(variable_name) then
+ print "Untraced variable {parts_of_command[1]}"
+ else
+ print "{parts_of_command[1]} is not traced"
+ end
+ end
+
+ # Processes the trace variable command
+ #
+ # Command pattern : "trace variable [break/print]"
+ fun process_trace_command(parts_of_command: Array[String])
+ do
+ var variable_name = get_real_variable_name(parts_of_command[1])
+ var breaker:Bool
+
+ if seek_variable(variable_name, frame) == null then
+ print "Cannot find a variable called {parts_of_command[1]}"
+ return
+ end
+
+ if parts_of_command.length == 3 then
+ if parts_of_command[2] == "break" then
+ breaker = true
+ else
+ breaker = false
+ end
+ else
+ breaker = false
+ end
+
+ trace_variable(variable_name, breaker)
+
+ print "Successfully tracing {parts_of_command[1]}"
+ end
+
+ #######################################################################
+ ## Trace Management functions ##
+ #######################################################################
+
+ # Effectively untraces the variable called *variable_name*
+ #
+ # Returns true if the variable exists, false otherwise
+ private fun untrace_variable(variable_name: String): Bool
+ do
+ var to_remove: nullable TraceObject = null
+ for i in self.traces do
+ if i.is_variable_traced_in_frame(variable_name, frame) then
+ to_remove = i
+ end
+ end
+
+ if to_remove != null then
+ self.traces.remove(to_remove)
+ return true
+ else
+ return false
+ end
+ end
+
+ # Effectively traces the variable *variable_name* either in print or break mode depending on the value of breaker (break if true, print if false)
+ #
+ private fun trace_variable(variable_name: String, breaker: Bool)
+ do
+ for i in self.traces do
+ if i.is_variable_traced_in_frame(variable_name, frame) then
+ print "This variable is already traced"
+ return
+ end
+ end
+
+ var trace_object: TraceObject
+
+ if breaker then
+ trace_object = new TraceObject(true)
+ else
+ trace_object = new TraceObject(false)
+ end
+
+ # We trace the current variable found for the current frame
+ trace_object.add_frame_variable(self.frame, variable_name)
+
+ var position_of_variable_in_arguments = get_position_of_variable_in_arguments(frame, variable_name)
+
+ # Start parsing the frames starting with the parent of the current one, until the highest
+ # When the variable traced is declared locally, the loop stops
+ for i in [1 .. frames.length-1] do
+
+ # If the variable was reported to be an argument of the previous frame
+ if position_of_variable_in_arguments != -1 then
+
+ var local_name = get_identifiers_in_current_instruction(get_function_arguments(frames[i].current_node.location.text))[position_of_variable_in_arguments]
+
+ position_of_variable_in_arguments = get_position_of_variable_in_arguments(frames[i], local_name)
+
+ trace_object.add_frame_variable(frames[i], local_name)
+ else
+ break
+ end
+ end
+
+ self.traces.add(trace_object)
+ end
+
+ # If the variable *variable_name* is an argument of the function being executed in the frame *frame*
+ # The function returns its position in the arguments
+ # Else, it returns -1
+ private fun get_position_of_variable_in_arguments(frame: Frame, variable_name: String): Int
+ do
+ var identifiers = get_identifiers_in_current_instruction(get_function_arguments(frame.mpropdef.location.text))
+ for i in [0 .. identifiers.length-1] do
+ # If the current traced variable is an argument of the current function, we trace its parent (at least)
+ if identifiers[i] == variable_name then return i
+ end
+ return -1
+ end
+
+ # Gets all the identifiers of an instruction (uses the rules of Nit as of Mar 05 2013)
+ #
+ fun get_identifiers_in_current_instruction(instruction: Text): Array[String]
+ do
+ var result_array = new Array[String]
+ var instruction_buffer = new FlatBuffer
+
+ var trigger_char_escape = false
+ var trigger_string_escape = false
+ var trigger_concat_in_string = false
+
+ for i in instruction.chars do
+ if trigger_char_escape then
+ if i == '\'' then trigger_char_escape = false
+ else if trigger_string_escape then
+ if i == '{' then
+ trigger_concat_in_string = true
+ trigger_string_escape = false
+ else if i == '\"' then trigger_string_escape = false
+ else
+ if i.is_alphanumeric or i == '_' then
+ instruction_buffer.add(i)
+ else if i == '.' then
+ if instruction_buffer.is_numeric or (instruction_buffer.chars[0] >= 'A' and instruction_buffer.chars[0] <= 'Z') then
+ instruction_buffer.clear
+ else
+ result_array.push(instruction_buffer.to_s)
+ instruction_buffer.add(i)
+ end
+ else if i == '\'' then
+ trigger_char_escape = true
+ else if i == '\"' then
+ trigger_string_escape = true
+ else if i == '}' then
+ trigger_concat_in_string = false
+ trigger_string_escape = true
+ else
+ if instruction_buffer.length > 0 and not instruction_buffer.is_numeric and not (instruction_buffer.chars[0] >= 'A' and instruction_buffer.chars[0] <= 'Z') then result_array.push(instruction_buffer.to_s)
+ instruction_buffer.clear
+ end
+ end
+ end
+
+ if instruction_buffer.length > 0 and not instruction_buffer.is_numeric and not (instruction_buffer.chars[0] >= 'A' and instruction_buffer.chars[0] <= 'Z') then result_array.push(instruction_buffer.to_s)
+
+ return result_array
+ end
+
+ # Takes a function call or declaration and strips all but the arguments
+ #
+ fun get_function_arguments(function: Text): String
+ do
+ var buf = new FlatBuffer
+ var trigger_copy = false
+
+ for i in function.chars do
+ if i == ')' then break
+ if trigger_copy then buf.add(i)
+ if i == '(' then trigger_copy = true
+ end
+
+ return buf.to_s
+ end
+