2015-07-23 12:15:44 +02:00
|
|
|
|
2015-07-23 19:09:19 +02:00
|
|
|
require "eventable"
|
|
|
|
|
2015-07-23 12:15:44 +02:00
|
|
|
class Interpreter
|
2015-07-23 19:09:19 +02:00
|
|
|
include Eventable
|
2015-07-23 12:15:44 +02:00
|
|
|
|
|
|
|
attr_accessor :instruction
|
2015-07-23 19:09:19 +02:00
|
|
|
attr_accessor :block
|
2015-07-23 12:15:44 +02:00
|
|
|
attr_accessor :registers
|
|
|
|
|
|
|
|
def initialize
|
2015-07-23 12:20:53 +02:00
|
|
|
@registers = Hash[(0...12).collect{|i| ["r#{i}" , "undefined"]}]
|
2015-07-23 12:15:44 +02:00
|
|
|
end
|
2015-07-23 19:09:19 +02:00
|
|
|
|
|
|
|
def start block
|
|
|
|
set_block block
|
|
|
|
end
|
|
|
|
def set_block block
|
|
|
|
return if @block == block
|
|
|
|
@block = block
|
|
|
|
trigger(:block, block)
|
|
|
|
set_instruction block.codes.first
|
|
|
|
end
|
|
|
|
def set_instruction i
|
|
|
|
return if @instruction == i
|
|
|
|
@instruction = i
|
|
|
|
trigger(:instruction, i)
|
|
|
|
end
|
|
|
|
def tick
|
|
|
|
name = @instruction.class.name.split("::").last
|
|
|
|
send "execute_#{name}"
|
|
|
|
end
|
|
|
|
|
2015-07-23 12:15:44 +02:00
|
|
|
end
|