finishes the yellow world instructions
This commit is contained in:
parent
87cec0f4bc
commit
e460e0041b
@ -2,12 +2,22 @@
|
|||||||
require "eventable"
|
require "eventable"
|
||||||
|
|
||||||
class Interpreter
|
class Interpreter
|
||||||
|
# fire events for changed pc and register contents
|
||||||
include Eventable
|
include Eventable
|
||||||
|
|
||||||
attr_accessor :instruction
|
# current instruction or pc
|
||||||
attr_accessor :link
|
attr_reader :instruction
|
||||||
attr_accessor :block
|
|
||||||
|
# an (arm style) link register. store the return address to return to
|
||||||
|
attr_reader :link
|
||||||
|
|
||||||
|
# current executing block. since this is not a hardware simulator this is luxury
|
||||||
|
attr_reader :block
|
||||||
|
|
||||||
|
# the registers, 12
|
||||||
attr_reader :registers
|
attr_reader :registers
|
||||||
|
|
||||||
|
# collect the output
|
||||||
attr_reader :stdout
|
attr_reader :stdout
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@ -54,6 +64,7 @@ class Interpreter
|
|||||||
end
|
end
|
||||||
|
|
||||||
def tick
|
def tick
|
||||||
|
return unless @instruction
|
||||||
name = @instruction.class.name.split("::").last
|
name = @instruction.class.name.split("::").last
|
||||||
fetch = send "execute_#{name}"
|
fetch = send "execute_#{name}"
|
||||||
return unless fetch
|
return unless fetch
|
||||||
@ -130,9 +141,21 @@ class Interpreter
|
|||||||
str = object_for( :r1 ) # should test length, ie r2
|
str = object_for( :r1 ) # should test length, ie r2
|
||||||
raise "NO string for putstring #{str}" unless str.is_a? Symbol
|
raise "NO string for putstring #{str}" unless str.is_a? Symbol
|
||||||
@stdout += str.to_s
|
@stdout += str.to_s
|
||||||
|
when :exit
|
||||||
|
@instruction = nil
|
||||||
|
return false
|
||||||
else
|
else
|
||||||
raise "un-implemented syscall #{name}"
|
raise "un-implemented syscall #{name}"
|
||||||
end
|
end
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def execute_FunctionReturn
|
||||||
|
object = object_for( @instruction.register )
|
||||||
|
#wouldn't need to assign to link, but makes tsting easier
|
||||||
|
@link = object.internal_object_get( @instruction.index )
|
||||||
|
@block , @instruction = @link
|
||||||
|
# we jump back to the call instruction. so it is as if the call never happened and we continue
|
||||||
|
true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -53,17 +53,30 @@ class InterpreterTest < MiniTest::Test
|
|||||||
["Branch" , "LoadConstant" , "GetSlot" , "SetSlot" , "RegisterTransfer" ,
|
["Branch" , "LoadConstant" , "GetSlot" , "SetSlot" , "RegisterTransfer" ,
|
||||||
"GetSlot" , "FunctionCall" , "SaveReturn" , "LoadConstant" , "SetSlot" ,
|
"GetSlot" , "FunctionCall" , "SaveReturn" , "LoadConstant" , "SetSlot" ,
|
||||||
"GetSlot" , "GetSlot" , "SetSlot" , "LoadConstant" , "SetSlot" ,
|
"GetSlot" , "GetSlot" , "SetSlot" , "LoadConstant" , "SetSlot" ,
|
||||||
"RegisterTransfer" , "GetSlot" , "FunctionCall" , "SaveReturn" ,
|
"RegisterTransfer" , "GetSlot" , "FunctionCall" , "SaveReturn" , "RegisterTransfer" ,
|
||||||
"RegisterTransfer" , "Syscall"].each_with_index do |name , index|
|
"Syscall" , "RegisterTransfer" , "RegisterTransfer" , "SetSlot" , "GetSlot" ,
|
||||||
|
"GetSlot" , "RegisterTransfer" ,"GetSlot" , "GetSlot","GetSlot",
|
||||||
|
"FunctionReturn" , "RegisterTransfer" , "Syscall" , "NilClass"].each_with_index do |name , index|
|
||||||
got = ticks(1)
|
got = ticks(1)
|
||||||
assert got.class.name.index(name) , "Wrong class for #{index}, expect #{name} , got #{got}"
|
assert got.class.name.index(name) , "Wrong class for #{index+1}, expect #{name} , got #{got}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_putstring
|
def test_putstring
|
||||||
done = ticks(21)
|
done = ticks(21)
|
||||||
assert_equal Register::Syscall , done.class
|
assert_equal Register::Syscall , done.class
|
||||||
assert_equal "Hello again" , @interpreter.stdout
|
assert_equal "Hello again" , @interpreter.stdout
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_return
|
||||||
|
done = ticks(31)
|
||||||
|
assert_equal Register::FunctionReturn , done.class
|
||||||
|
assert @interpreter.block.is_a?(Virtual::Block)
|
||||||
|
assert @interpreter.instruction.is_a?(Register::Instruction) , "not instruction #{@interpreter.instruction}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_exit
|
||||||
|
done = ticks(34)
|
||||||
|
assert_equal NilClass , done.class
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user