now object slot , almost done
This commit is contained in:
parent
1da5cd16c3
commit
8d02d82ff2
51
lib/slot_machine/instruction/object_definition.rb
Normal file
51
lib/slot_machine/instruction/object_definition.rb
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
module SlotMachine
|
||||||
|
class ObjectDefinition < SlotDefinition
|
||||||
|
|
||||||
|
# get the right definition, depending on the object
|
||||||
|
def self.for(object , slots)
|
||||||
|
case object
|
||||||
|
when :message
|
||||||
|
MessageDefinition.new(slots)
|
||||||
|
when Constant
|
||||||
|
ConstantDefinition.new(object , slots)
|
||||||
|
when Parfait::Object
|
||||||
|
ObjectDefinition.new(object , slots)
|
||||||
|
else
|
||||||
|
SlotDefinition.new(object,slots)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize( object , slots)
|
||||||
|
super(object , slots )
|
||||||
|
end
|
||||||
|
|
||||||
|
def known_name
|
||||||
|
known_object.class.short_name
|
||||||
|
end
|
||||||
|
|
||||||
|
# load the slots into a register
|
||||||
|
# the code is added to compiler
|
||||||
|
# the register returned
|
||||||
|
def to_register(compiler, source)
|
||||||
|
type = known_object.get_type
|
||||||
|
right = compiler.use_reg( type )
|
||||||
|
const = Risc.load_constant(source, known_object , right)
|
||||||
|
compiler.add_code const
|
||||||
|
if slots.length > 0
|
||||||
|
# desctructively replace the existing value to be loaded if more slots
|
||||||
|
compiler.add_code Risc.slot_to_reg( source , right ,slots[0], right)
|
||||||
|
end
|
||||||
|
if slots.length > 1
|
||||||
|
# desctructively replace the existing value to be loaded if more slots
|
||||||
|
index = Risc.resolve_to_index(slots[0] , slots[1] ,compiler)
|
||||||
|
compiler.add_code Risc::SlotToReg.new( source , right ,index, right)
|
||||||
|
if slots.length > 2
|
||||||
|
raise "3 slots only for type #{slots}" unless slots[2] == :type
|
||||||
|
compiler.add_code Risc::SlotToReg.new( source , right , Parfait::TYPE_INDEX, right)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return const.register
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
@ -22,6 +22,8 @@ module SlotMachine
|
|||||||
MessageDefinition.new(slots)
|
MessageDefinition.new(slots)
|
||||||
when Constant
|
when Constant
|
||||||
ConstantDefinition.new(object , slots)
|
ConstantDefinition.new(object , slots)
|
||||||
|
when Parfait::Object
|
||||||
|
ObjectDefinition.new(object , slots)
|
||||||
else
|
else
|
||||||
SlotDefinition.new(object,slots)
|
SlotDefinition.new(object,slots)
|
||||||
end
|
end
|
||||||
@ -47,8 +49,6 @@ module SlotMachine
|
|||||||
|
|
||||||
def known_name
|
def known_name
|
||||||
case known_object
|
case known_object
|
||||||
when Parfait::Object
|
|
||||||
known_object.class.short_name
|
|
||||||
when Risc::Label
|
when Risc::Label
|
||||||
known_object.to_s
|
known_object.to_s
|
||||||
else
|
else
|
||||||
@ -67,7 +67,7 @@ module SlotMachine
|
|||||||
end
|
end
|
||||||
right = compiler.use_reg( type )
|
right = compiler.use_reg( type )
|
||||||
case known_object
|
case known_object
|
||||||
when Parfait::Object , Risc::Label
|
when Risc::Label
|
||||||
const = Risc.load_constant(source, known_object , right)
|
const = Risc.load_constant(source, known_object , right)
|
||||||
compiler.add_code const
|
compiler.add_code const
|
||||||
if slots.length > 0
|
if slots.length > 0
|
||||||
|
@ -83,3 +83,4 @@ end
|
|||||||
require_relative "slot_definition"
|
require_relative "slot_definition"
|
||||||
require_relative "message_definition"
|
require_relative "message_definition"
|
||||||
require_relative "constant_definition"
|
require_relative "constant_definition"
|
||||||
|
require_relative "object_definition"
|
||||||
|
@ -31,7 +31,7 @@ module Sol
|
|||||||
def test_receiver
|
def test_receiver
|
||||||
assert_equal SlotMachine::ArgumentTransfer, @ins.next(1).class
|
assert_equal SlotMachine::ArgumentTransfer, @ins.next(1).class
|
||||||
assert_equal 0, @ins.next(1).arguments.length
|
assert_equal 0, @ins.next(1).arguments.length
|
||||||
assert_equal SlotDefinition, @ins.next(1).receiver.class
|
assert_equal ObjectDefinition, @ins.next(1).receiver.class
|
||||||
assert_equal Parfait::Class, @ins.next(1).receiver.known_object.class
|
assert_equal Parfait::Class, @ins.next(1).receiver.known_object.class
|
||||||
assert_equal :Space, @ins.next(1).receiver.known_object.name
|
assert_equal :Space, @ins.next(1).receiver.known_object.name
|
||||||
end
|
end
|
||||||
|
@ -39,7 +39,7 @@ module Sol
|
|||||||
assert_equal :Object , @ins.next.receiver.known_object.name
|
assert_equal :Object , @ins.next.receiver.known_object.name
|
||||||
end
|
end
|
||||||
def test_receiver_move
|
def test_receiver_move
|
||||||
assert_equal SlotDefinition, @ins.next.receiver.class
|
assert_equal ObjectDefinition, @ins.next.receiver.class
|
||||||
end
|
end
|
||||||
def test_receiver
|
def test_receiver
|
||||||
assert_equal Parfait::Class, @ins.next.receiver.known_object.class
|
assert_equal Parfait::Class, @ins.next.receiver.known_object.class
|
||||||
|
@ -28,7 +28,7 @@ module Sol
|
|||||||
assert_equal [:next_message, :arg1], left.slots
|
assert_equal [:next_message, :arg1], left.slots
|
||||||
end
|
end
|
||||||
def test_check_left
|
def test_check_left
|
||||||
assert_equal SlotDefinition, @ins.left.class
|
assert_equal ObjectDefinition, @ins.left.class
|
||||||
assert_equal Parfait::CallableMethod, @ins.left.known_object.class
|
assert_equal Parfait::CallableMethod, @ins.left.known_object.class
|
||||||
assert_equal :main, @ins.left.known_object.name
|
assert_equal :main, @ins.left.known_object.name
|
||||||
assert @ins.left.slots.empty?
|
assert @ins.left.slots.empty?
|
||||||
|
Loading…
x
Reference in New Issue
Block a user