implement put string and fix set
This commit is contained in:
parent
228b1b5832
commit
1ae4baf3f7
@ -7,9 +7,11 @@ class Interpreter
|
||||
attr_accessor :instruction
|
||||
attr_accessor :link
|
||||
attr_accessor :block
|
||||
attr_accessor :registers
|
||||
attr_reader :registers
|
||||
attr_reader :stdout
|
||||
|
||||
def initialize
|
||||
@stdout = ""
|
||||
@registers = {}
|
||||
(0...12).each do |r|
|
||||
set_register "r#{r}".to_sym , "undefined"
|
||||
@ -32,7 +34,6 @@ class Interpreter
|
||||
def set_instruction i
|
||||
return if @instruction == i
|
||||
raise "Error, nil instruction" unless i
|
||||
puts "next up #{i.class}"
|
||||
old = @instruction
|
||||
@instruction = i
|
||||
trigger(:instruction_changed, old , i)
|
||||
@ -70,8 +71,9 @@ class Interpreter
|
||||
|
||||
def object_for reg
|
||||
id = get_register(reg)
|
||||
object = Virtual.machine.objects[id]
|
||||
Virtual.machine.objects[id]
|
||||
end
|
||||
|
||||
# Instruction interpretation starts here
|
||||
def execute_Branch
|
||||
target = @instruction.block
|
||||
@ -95,8 +97,8 @@ class Interpreter
|
||||
end
|
||||
|
||||
def execute_SetSlot
|
||||
object = object_for( @instruction.register )
|
||||
value = object_for( @instruction.array )
|
||||
value = object_for( @instruction.register )
|
||||
object = object_for( @instruction.array )
|
||||
object.internal_object_set( @instruction.index , value )
|
||||
true
|
||||
end
|
||||
@ -116,8 +118,21 @@ class Interpreter
|
||||
|
||||
def execute_SaveReturn
|
||||
object = object_for @instruction.register
|
||||
raise "save return has nothing to save" unless @link
|
||||
object.internal_object_set @instruction.index , @link
|
||||
true
|
||||
end
|
||||
|
||||
def execute_Syscall
|
||||
name = @instruction.name
|
||||
case name
|
||||
when :putstring
|
||||
str = object_for( :r1 ) # should test length, ie r2
|
||||
raise "NO string for putstring #{str}" unless str.is_a? Symbol
|
||||
@stdout += str.to_s
|
||||
else
|
||||
raise "un-implemented syscall #{name}"
|
||||
end
|
||||
true
|
||||
end
|
||||
end
|
||||
|
@ -19,40 +19,51 @@ class InterpreterTest < MiniTest::Test
|
||||
end
|
||||
return last
|
||||
end
|
||||
def test_first_branch
|
||||
def test_branch
|
||||
was = @interpreter.block
|
||||
assert_equal Register::Branch , ticks(1).class
|
||||
assert was != @interpreter.block
|
||||
end
|
||||
def test_second
|
||||
def test_load
|
||||
assert_equal Register::LoadConstant , ticks(2).class
|
||||
assert_equal Parfait::Space , Virtual.machine.objects[ @interpreter.get_register(:r1)].class
|
||||
assert_equal :r1, @interpreter.instruction.array.symbol
|
||||
end
|
||||
def test_third
|
||||
def test_get
|
||||
assert_equal Register::GetSlot , ticks(3).class
|
||||
assert @interpreter.get_register( :r3 )
|
||||
assert @interpreter.get_register( :r3 ).is_a? Integer
|
||||
end
|
||||
def test_fourth
|
||||
assert_equal Register::SetSlot , ticks(4).class
|
||||
end
|
||||
def test_fifth
|
||||
def test_transfer
|
||||
transfer = ticks 5
|
||||
assert_equal Register::RegisterTransfer , transfer.class
|
||||
assert_equal @interpreter.get_register(transfer.to) , @interpreter.get_register(transfer.from)
|
||||
end
|
||||
def test_sixth
|
||||
assert_equal Register::GetSlot , ticks(6).class
|
||||
end
|
||||
def test_seventh
|
||||
def test_call
|
||||
assert_equal Register::FunctionCall , ticks(7).class
|
||||
assert @interpreter.link
|
||||
end
|
||||
def test_eighth
|
||||
assert_equal Register::SaveReturn , ticks(8).class
|
||||
def test_save
|
||||
done = ticks(8)
|
||||
assert_equal Register::SaveReturn , done.class
|
||||
assert @interpreter.get_register done.register.symbol
|
||||
end
|
||||
def test_ninth
|
||||
assert_equal Register::LoadConstant , ticks(9).class
|
||||
|
||||
def test_chain
|
||||
["Branch" , "LoadConstant" , "GetSlot" , "SetSlot" , "RegisterTransfer" ,
|
||||
"GetSlot" , "FunctionCall" , "SaveReturn" , "LoadConstant" , "SetSlot" ,
|
||||
"GetSlot" , "GetSlot" , "SetSlot" , "LoadConstant" , "SetSlot" ,
|
||||
"RegisterTransfer" , "GetSlot" , "FunctionCall" , "SaveReturn" ,
|
||||
"RegisterTransfer" , "Syscall"].each_with_index do |name , index|
|
||||
got = ticks(1)
|
||||
assert got.class.name.index(name) , "Wrong class for #{index}, expect #{name} , got #{got}"
|
||||
end
|
||||
end
|
||||
|
||||
def test_putstring
|
||||
done = ticks(21)
|
||||
assert_equal Register::Syscall , done.class
|
||||
assert_equal "Hello again" , @interpreter.stdout
|
||||
end
|
||||
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user