Class: Base::Debugger

Inherits:
Object
  • Object
show all
Defined in:
lib/base/debugger.rb

Constant Summary collapse

PUSH =
VM::COMMANDS.index("push")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vm) ⇒ Debugger

Returns a new instance of Debugger.



7
8
9
# File 'lib/base/debugger.rb', line 7

def initialize(vm)
  @vm = vm
end

Instance Attribute Details

#vmObject (readonly)

Returns the value of attribute vm.



3
4
5
# File 'lib/base/debugger.rb', line 3

def vm
  @vm
end

Instance Method Details

#debugObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/base/debugger.rb', line 11

def debug
  loop do
    next_instruction = memory[ip] == PUSH ? "push #{memory[ip + 1]}" : VM::COMMANDS[memory[ip]] || "???"
    puts "ip=#{ip} stack=#{stack.inner} #{next_instruction}"
    print "> "

    command = $stdin.gets&.strip
    case command
    when ""
      nil
    when "n"
      return true
    when "c", nil
      return false
    when "q"
      exit
    when /\Am(?:\s+([0-9]+))?(?:\s+([0-9]+))?/
      memory_dump($1 && $1.to_i || 0, $2 && $2.to_i)
    when /\Ad(?:\s+([0-9]+))?(?:\s+([0-9]+))?/
      disassemble($1 && $1.to_i || ip, $2 && $2.to_i)
    else
      puts "n - next instruction"
      puts "c - stop debugging and continue"
      puts "m [start [length]] - dump memory"
      puts "d [start [length]] - disassemble"
      puts "q - quit"
    end
  end
end