def dump_state(ip_offset)
return unless debug?
puts "\nVM STATE -------------------------------------------- "
old_pos = source.pos
debug_pos = old_pos - 10
source.pos = debug_pos < 0 ? 0 : debug_pos
puts "Source: #{source.read(20)}"
puts (" "*"Source: ".size) << (" "*(10+(debug_pos<0 ? debug_pos : 0))) << '^'
source.pos = old_pos
if @error
puts "Error register: #{@error}"
else
puts "Error register: EMPTY"
end
puts "Program: "
for adr in (@ip-5)..(@ip+5)
printf("%s%5d: %s\n",
adr == @ip+ip_offset ? '->' : ' ',
adr,
@program.at(adr)) if adr >= 0 && @program.at(adr)
end
puts "\nStack(#{@values.size}): (last 5, top is top of stack)"
@values.last(5).reverse.each_with_index do |v,i|
printf(" %5d: %s\n", i, v.inspect)
end
puts "\nStack Frames(#{@frames.size}): (last 5, top is top of stack)"
@frames.last(5).reverse.each_with_index do |v,i|
printf(" %5d: trunc stack at %s\n", i, v)
end
puts "\nCall Stack(#{@calls.size}): (last 5, top is top of stack)"
@calls.last(5).reverse.each_with_index do |v,i|
printf(" %5d: return to @%s\n", i, v)
end
puts "---------------------- -------------------------------- "
end