From 8d02d82ff2e3abf72362bd97bc4f041d27bcc6ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Mon, 10 Feb 2020 19:28:48 +0700 Subject: [PATCH] now object slot , almost done --- .../instruction/object_definition.rb | 51 +++++++++++++++++++ .../instruction/slot_definition.rb | 6 +-- lib/slot_machine/instruction/slot_load.rb | 1 + .../class_send/test_class_send_inherited.rb | 2 +- test/sol/class_send/test_send_class.rb | 2 +- test/sol/test_yield_statement.rb | 2 +- 6 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 lib/slot_machine/instruction/object_definition.rb diff --git a/lib/slot_machine/instruction/object_definition.rb b/lib/slot_machine/instruction/object_definition.rb new file mode 100644 index 00000000..335a0664 --- /dev/null +++ b/lib/slot_machine/instruction/object_definition.rb @@ -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 diff --git a/lib/slot_machine/instruction/slot_definition.rb b/lib/slot_machine/instruction/slot_definition.rb index 71fd2490..0951fa9b 100644 --- a/lib/slot_machine/instruction/slot_definition.rb +++ b/lib/slot_machine/instruction/slot_definition.rb @@ -22,6 +22,8 @@ module SlotMachine MessageDefinition.new(slots) when Constant ConstantDefinition.new(object , slots) + when Parfait::Object + ObjectDefinition.new(object , slots) else SlotDefinition.new(object,slots) end @@ -47,8 +49,6 @@ module SlotMachine def known_name case known_object - when Parfait::Object - known_object.class.short_name when Risc::Label known_object.to_s else @@ -67,7 +67,7 @@ module SlotMachine end right = compiler.use_reg( type ) case known_object - when Parfait::Object , Risc::Label + when Risc::Label const = Risc.load_constant(source, known_object , right) compiler.add_code const if slots.length > 0 diff --git a/lib/slot_machine/instruction/slot_load.rb b/lib/slot_machine/instruction/slot_load.rb index 9a23a103..40443415 100644 --- a/lib/slot_machine/instruction/slot_load.rb +++ b/lib/slot_machine/instruction/slot_load.rb @@ -83,3 +83,4 @@ end require_relative "slot_definition" require_relative "message_definition" require_relative "constant_definition" +require_relative "object_definition" diff --git a/test/sol/class_send/test_class_send_inherited.rb b/test/sol/class_send/test_class_send_inherited.rb index db4b0643..7ba92e83 100644 --- a/test/sol/class_send/test_class_send_inherited.rb +++ b/test/sol/class_send/test_class_send_inherited.rb @@ -31,7 +31,7 @@ module Sol def test_receiver assert_equal SlotMachine::ArgumentTransfer, @ins.next(1).class 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 :Space, @ins.next(1).receiver.known_object.name end diff --git a/test/sol/class_send/test_send_class.rb b/test/sol/class_send/test_send_class.rb index 92d57bdd..954e4a3a 100644 --- a/test/sol/class_send/test_send_class.rb +++ b/test/sol/class_send/test_send_class.rb @@ -39,7 +39,7 @@ module Sol assert_equal :Object , @ins.next.receiver.known_object.name end def test_receiver_move - assert_equal SlotDefinition, @ins.next.receiver.class + assert_equal ObjectDefinition, @ins.next.receiver.class end def test_receiver assert_equal Parfait::Class, @ins.next.receiver.known_object.class diff --git a/test/sol/test_yield_statement.rb b/test/sol/test_yield_statement.rb index a12f710d..c6ab8245 100644 --- a/test/sol/test_yield_statement.rb +++ b/test/sol/test_yield_statement.rb @@ -28,7 +28,7 @@ module Sol assert_equal [:next_message, :arg1], left.slots end 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 :main, @ins.left.known_object.name assert @ins.left.slots.empty?