add a way to bend the type for register_value
specifically for factories, where we know the type of next_object even it is not specified
This commit is contained in:
parent
8119030ed2
commit
c9fedec230
@ -21,12 +21,10 @@ module Risc
|
|||||||
# for an XX instance
|
# for an XX instance
|
||||||
def initialize( reg , type , extra = {})
|
def initialize( reg , type , extra = {})
|
||||||
extra = {} unless extra
|
extra = {} unless extra
|
||||||
raise "Not Hash #{extra}" unless extra.is_a?(Hash)
|
|
||||||
type = Parfait.object_space.get_type_by_class_name(type) if type.is_a?(Symbol)
|
|
||||||
raise "No type #{reg}" unless type
|
|
||||||
@type = type
|
|
||||||
@symbol = reg
|
|
||||||
@extra = extra
|
@extra = extra
|
||||||
|
@symbol = reg
|
||||||
|
raise "Not Hash #{extra}" unless extra.is_a?(Hash)
|
||||||
|
known_type(type)
|
||||||
end
|
end
|
||||||
|
|
||||||
def class_name
|
def class_name
|
||||||
@ -42,6 +40,16 @@ module Risc
|
|||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# basically set the type with the given symbol. Symbol is resolved to type
|
||||||
|
# just like in constructor
|
||||||
|
# return self for chaining
|
||||||
|
def known_type( type )
|
||||||
|
type = Parfait.object_space.get_type_by_class_name(type) if type.is_a?(Symbol)
|
||||||
|
raise "No type #{type} for #{self}" unless type
|
||||||
|
@type = type
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
# using the registers type, resolve the slot to an index
|
# using the registers type, resolve the slot to an index
|
||||||
# Using the index and the register, add a SlotToReg to the instruction
|
# Using the index and the register, add a SlotToReg to the instruction
|
||||||
def resolve_and_add(slot , compiler)
|
def resolve_and_add(slot , compiler)
|
||||||
|
@ -73,26 +73,4 @@ module Risc
|
|||||||
assert_equal :message , inst.array.symbol
|
assert_equal :message , inst.array.symbol
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
class TestRegisterSlot4 < MiniTest::Test
|
|
||||||
include HasCompiler
|
|
||||||
def setup
|
|
||||||
Parfait.boot!(Parfait.default_test_options)
|
|
||||||
@compiler = Risc.test_compiler
|
|
||||||
@r0 = RegisterValue.new(:message , :Message).set_compiler(@compiler)
|
|
||||||
@r0[:next_message][:return_address] << @r0
|
|
||||||
end
|
|
||||||
def test_instructions
|
|
||||||
assert_equal SlotToReg , @compiler.risc_instructions.next(1).class
|
|
||||||
assert_equal RegToSlot , @compiler.risc_instructions.next(2).class
|
|
||||||
assert_equal NilClass , @compiler.risc_instructions.next(3).class
|
|
||||||
end
|
|
||||||
def test_slot_to
|
|
||||||
assert_slot_to_reg 1 , :message, 1, :"message.next_message"
|
|
||||||
assert risc(1).register.compiler
|
|
||||||
end
|
|
||||||
def test_reg_to
|
|
||||||
assert_reg_to_slot 2 , :message, :"message.next_message" , 4
|
|
||||||
assert risc(2).register.compiler
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
26
test/risc/test_register_slot1.rb
Normal file
26
test/risc/test_register_slot1.rb
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
require_relative "../helper"
|
||||||
|
|
||||||
|
module Risc
|
||||||
|
class TestRegisterSlot4 < MiniTest::Test
|
||||||
|
include HasCompiler
|
||||||
|
def setup
|
||||||
|
Parfait.boot!(Parfait.default_test_options)
|
||||||
|
@compiler = Risc.test_compiler
|
||||||
|
@r0 = RegisterValue.new(:message , :Message).set_compiler(@compiler)
|
||||||
|
@r0[:next_message][:return_address] << @r0
|
||||||
|
end
|
||||||
|
def test_instructions
|
||||||
|
assert_equal SlotToReg , @compiler.risc_instructions.next(1).class
|
||||||
|
assert_equal RegToSlot , @compiler.risc_instructions.next(2).class
|
||||||
|
assert_equal NilClass , @compiler.risc_instructions.next(3).class
|
||||||
|
end
|
||||||
|
def test_slot_to
|
||||||
|
assert_slot_to_reg 1 , :message, 1, :"message.next_message"
|
||||||
|
assert risc(1).register.compiler
|
||||||
|
end
|
||||||
|
def test_reg_to
|
||||||
|
assert_reg_to_slot 2 , :message, :"message.next_message" , 4
|
||||||
|
assert risc(2).register.compiler
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -45,5 +45,10 @@ module Risc
|
|||||||
# works with nil as compiler, because extra is used
|
# works with nil as compiler, because extra is used
|
||||||
assert_equal :Message , @r1.get_new_left(:arguments , nil).type.class_name
|
assert_equal :Message , @r1.get_new_left(:arguments , nil).type.class_name
|
||||||
end
|
end
|
||||||
|
def test_type_setting
|
||||||
|
reg = @r0.known_type(:Integer)
|
||||||
|
assert_equal Parfait::Type , reg.type.class
|
||||||
|
assert_equal "Integer_Type" , reg.type.name
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user