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
|
|
|
|
2015-07-24 12:56:31 +02:00
|
|
|
def start bl
|
|
|
|
set_block bl
|
2015-07-23 19:09:19 +02:00
|
|
|
end
|
2015-07-24 12:56:31 +02:00
|
|
|
|
|
|
|
def set_block bl
|
|
|
|
return if @block == bl
|
|
|
|
raise "Error, nil block" unless bl
|
2015-07-24 09:01:01 +02:00
|
|
|
old = @block
|
2015-07-24 12:56:31 +02:00
|
|
|
@block = bl
|
|
|
|
trigger(:block_changed , old , bl)
|
|
|
|
set_instruction bl.codes.first
|
2015-07-23 19:09:19 +02:00
|
|
|
end
|
2015-07-24 12:56:31 +02:00
|
|
|
|
2015-07-23 19:09:19 +02:00
|
|
|
def set_instruction i
|
|
|
|
return if @instruction == i
|
2015-07-24 12:56:31 +02:00
|
|
|
raise "Error, nil instruction" unless i
|
2015-07-24 09:01:01 +02:00
|
|
|
old = @instruction
|
2015-07-23 19:09:19 +02:00
|
|
|
@instruction = i
|
2015-07-24 09:01:01 +02:00
|
|
|
trigger(:instruction_changed, old , i)
|
2015-07-23 19:09:19 +02:00
|
|
|
end
|
2015-07-24 12:56:31 +02:00
|
|
|
|
|
|
|
def get_register( reg )
|
|
|
|
reg = reg.symbol if reg.is_a? Register::RegisterReference
|
|
|
|
raise "Not a register #{reg}" unless Register::RegisterReference.look_like_reg(reg)
|
|
|
|
@registers[reg]
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_register reg , val
|
|
|
|
old = get_register( reg ) # also ensures format
|
|
|
|
return if old === val
|
|
|
|
@registers[reg] = val
|
|
|
|
trigger(:register_changed, reg , old , val)
|
|
|
|
end
|
|
|
|
|
2015-07-23 19:09:19 +02:00
|
|
|
def tick
|
|
|
|
name = @instruction.class.name.split("::").last
|
2015-07-24 09:01:01 +02:00
|
|
|
fetch = send "execute_#{name}"
|
|
|
|
return unless fetch
|
|
|
|
get_next_intruction
|
2015-07-23 19:09:19 +02:00
|
|
|
end
|
|
|
|
|
2015-07-24 09:01:01 +02:00
|
|
|
def execute_Branch
|
2015-07-24 09:15:58 +02:00
|
|
|
target = @instruction.block
|
2015-07-24 09:01:01 +02:00
|
|
|
set_block target
|
2015-07-24 09:15:58 +02:00
|
|
|
false
|
2015-07-24 09:01:01 +02:00
|
|
|
end
|
2015-07-24 12:56:31 +02:00
|
|
|
def execute_LoadConstant
|
|
|
|
to = @instruction.register
|
|
|
|
value = @instruction.constant.object_id
|
|
|
|
set_register( to , value )
|
|
|
|
true
|
|
|
|
end
|
2015-07-23 12:15:44 +02:00
|
|
|
end
|