diff --git a/lib/mom/basic_values.rb b/lib/mom/basic_values.rb new file mode 100644 index 00000000..3b143896 --- /dev/null +++ b/lib/mom/basic_values.rb @@ -0,0 +1,55 @@ +module Mom + # just name scoping the same stuff to mom + # so we know we are on the way down, keeping our layers seperated + # and we can put constant adding into the to_risc methods (instead of on vool classes) + class Constant + end + + class IntegerConstant < Constant + attr_reader :value + def initialize(value) + @value = value + end + def ct_type + Parfait.object_space.get_class_by_name(:Integer).instance_type + end + end + class FloatConstant < Constant + attr_reader :value + def initialize(value) + @value = value + end + def ct_type + true + end + end + class TrueConstant < Constant + def ct_type + Parfait.object_space.get_class_by_name(:True).instance_type + end + end + class FalseConstant < Constant + def ct_type + Parfait.object_space.get_class_by_name(:False).instance_type + end + end + class NilConstant < Constant + def ct_type + Parfait.object_space.get_class_by_name(:Nil).instance_type + end + end + class StringConstant < Constant + attr_reader :value + def initialize(value) + @value = value + end + def ct_type + Parfait.object_space.get_class_by_name(:Word).instance_type + end + end + class SymbolConstant < String + def ct_type + Parfait.object_space.get_class_by_name(:Word).instance_type + end + end +end diff --git a/lib/mom/instruction.rb b/lib/mom/instruction.rb index c4b6ac3f..8608e1bb 100644 --- a/lib/mom/instruction.rb +++ b/lib/mom/instruction.rb @@ -19,6 +19,7 @@ module Mom end end +require_relative "basic_values" require_relative "simple_call" require_relative "dynamic_call" require_relative "truth_check" diff --git a/lib/mom/slot_load.rb b/lib/mom/slot_load.rb index 089d66fe..1bcdd9db 100644 --- a/lib/mom/slot_load.rb +++ b/lib/mom/slot_load.rb @@ -30,7 +30,8 @@ module Mom def initialize(left , right) left = SlotDefinition.new(left.shift , left) if left.is_a? Array @left , @right = left , right - raise "right not SlotDefinition, #{left}" unless left.is_a? SlotDefinition + raise "left not SlotDefinition, #{left}" unless left.is_a? SlotDefinition +# raise "right not Mom, #{right.to_rxf}" unless right.class.name.include?("Mom") end end @@ -44,7 +45,7 @@ module Mom def initialize(left , right) super - raise "right not constant, #{right}" unless right.is_a? Vool::ConstantStatement + raise "right not constant, #{right}" unless right.is_a? Mom::Constant end end diff --git a/lib/parfait/integer.rb b/lib/parfait/integer.rb index b3dcbd5b..868125df 100644 --- a/lib/parfait/integer.rb +++ b/lib/parfait/integer.rb @@ -1,11 +1,10 @@ # Integer class for representing maths on Integers -# Integers are Values (not Objects), +# Integers are Objects, spcifically DataObjects # - they have fixed value # - they are immutable -# you can *not* assign instance variables or methods - -# TODO how this idea works with Numeric ? +# (both by implementation, not design. +# Ie it would be possible to change the value, we just don't support that) module Parfait class Integer diff --git a/lib/vool/statements/basic_values.rb b/lib/vool/statements/basic_values.rb index c61eb3f1..8535cd90 100644 --- a/lib/vool/statements/basic_values.rb +++ b/lib/vool/statements/basic_values.rb @@ -15,6 +15,9 @@ module Vool def initialize(value) @value = value end + def to_mom(method) + return Mom::IntegerConstant.new(@value) + end def ct_type Parfait.object_space.get_class_by_name(:Integer).instance_type end @@ -45,10 +48,12 @@ module Vool end class SelfStatement < Statement attr_reader :clazz - def set_class(clazz) @clazz = clazz end + def to_mom(in_method) + Mom::SlotDefinition.new(:message , [:self]) + end def ct_type @clazz.instance_type end @@ -60,6 +65,9 @@ module Vool def initialize(value) @value = value end + def to_mom(method) + return Mom::StringConstant.new(@value) + end def ct_type Parfait.object_space.get_class_by_name(:Word).instance_type end diff --git a/lib/vool/statements/ivar_statement.rb b/lib/vool/statements/ivar_statement.rb index 66039240..fc5bd80b 100644 --- a/lib/vool/statements/ivar_statement.rb +++ b/lib/vool/statements/ivar_statement.rb @@ -18,7 +18,7 @@ module Vool end def to_mom( method ) - @value.slot_class.new([:message , :self , @name] , @value) + @value.slot_class.new([:message , :self , @name] , @value.to_mom(method)) end end diff --git a/lib/vool/statements/local_statement.rb b/lib/vool/statements/local_statement.rb index 6e765279..5d447f7c 100644 --- a/lib/vool/statements/local_statement.rb +++ b/lib/vool/statements/local_statement.rb @@ -12,7 +12,7 @@ module Vool else type = :frame end - @value.slot_class.new(Mom::SlotDefinition.new(:message , [type , @name]) , @value) + @value.slot_class.new(Mom::SlotDefinition.new(:message , [type , @name]) , @value.to_mom(method)) end end diff --git a/lib/vool/statements/return_statement.rb b/lib/vool/statements/return_statement.rb index 6b5ce28e..06415b24 100644 --- a/lib/vool/statements/return_statement.rb +++ b/lib/vool/statements/return_statement.rb @@ -11,11 +11,11 @@ module Vool super end - # To return form a method in mom instructions we need to do three things: + # To return form a method in mom instructions we need to do two things: # - store the given return value, this is a SlotMove / SlotConstant # - activate return sequence (reinstantiate old message and jump to return address) def to_mom( method ) - move = @return_value.slot_class.new( [:message , :return_value] , @return_value) + move = @return_value.slot_class.new( [:message , :return_value] , @return_value.to_mom(method)) Mom::Statements.new [move , Mom::ReturnSequence.new] end diff --git a/lib/vool/statements/send_statement.rb b/lib/vool/statements/send_statement.rb index b9a8d24e..2ac2195f 100644 --- a/lib/vool/statements/send_statement.rb +++ b/lib/vool/statements/send_statement.rb @@ -43,11 +43,11 @@ module Vool def message_setup(in_method) setup = [Mom::MessageSetup.new(in_method)] - receiver = @receiver.slot_class.new([:message , :next_message , :receiver] , @receiver) + receiver = @receiver.slot_class.new([:message , :next_message , :receiver] , @receiver.to_mom(in_method)) arg_target = [:message , :next_message , :arguments] args = [] @arguments.each_with_index do |arg , index| - args << arg.slot_class.new( arg_target + [index] , arg) + args << arg.slot_class.new( arg_target + [index] , arg.to_mom(in_method)) end setup << Mom::ArgumentTransfer.new( receiver , args ) end diff --git a/lib/vool/vool_compiler.rb b/lib/vool/vool_compiler.rb index 4beefca5..e72ff867 100644 --- a/lib/vool/vool_compiler.rb +++ b/lib/vool/vool_compiler.rb @@ -9,8 +9,8 @@ module Vool statements end def self.ruby_to_mom(source) - statements = elf.ruby_to_vool(source) - statements.to_mom + statements = self.ruby_to_vool(source) + statements.to_mom(nil) end end end diff --git a/test/mom/test_assignment.rb b/test/mom/test_assignment.rb index f7b2e0d7..a0ca4387 100644 --- a/test/mom/test_assignment.rb +++ b/test/mom/test_assignment.rb @@ -16,7 +16,7 @@ module Mom assert_equal 1 , @stats.length end def test_assigns_class - assert_equal Vool::IntegerStatement , @stats.right.class , @stats.inspect + assert_equal Mom::IntegerConstant , @stats.right.class , @stats.inspect end def test_assigns_value assert_equal 5 , @stats.right.value , @stats.inspect @@ -49,7 +49,7 @@ module Mom assert_equal 1 , @stats.length end def test_assigns_class - assert_equal Vool::InstanceVariable , @stats.right.class , @stats.inspect + assert_equal Mom::SlotDefinition , @stats.right.class , @stats.inspect end end end diff --git a/test/mom/test_simple_call_statement.rb b/test/mom/test_simple_call_statement.rb index 3a9bf830..a6e6ce29 100644 --- a/test/mom/test_simple_call_statement.rb +++ b/test/mom/test_simple_call_statement.rb @@ -13,8 +13,8 @@ module Mom def test_if_compiles assert_equal @stats.last.class , SimpleCall , @stats end - def test_length - assert_equal 3 , @stats.length + def test_method_name + assert_equal @stats.last.method.name , :mod4 , @stats.last.to_rxf end def test_array check_array [MessageSetup,ArgumentTransfer,SimpleCall] , @stats diff --git a/test/vool/to_mom/send/test_send_self.rb b/test/vool/to_mom/send/test_send_self.rb index 30bc7763..480f93c7 100644 --- a/test/vool/to_mom/send/test_send_self.rb +++ b/test/vool/to_mom/send/test_send_self.rb @@ -16,7 +16,7 @@ module Vool end def test_receiver - assert_equal SelfStatement, @stats[1].receiver.right.class + assert_equal Mom::SlotDefinition, @stats[1].receiver.right.class end def test_arg_one diff --git a/test/vool/to_mom/send/test_send_simple.rb b/test/vool/to_mom/send/test_send_simple.rb index 9b888394..284c9a63 100644 --- a/test/vool/to_mom/send/test_send_simple.rb +++ b/test/vool/to_mom/send/test_send_simple.rb @@ -12,7 +12,7 @@ module Vool @first = @stats.first end def receiver - [IntegerStatement , 5] + [Mom::IntegerConstant , 5] end def test_call_has_right_method assert_equal :mod4, @stats[2].method.name diff --git a/test/vool/to_mom/send/test_send_simple_args.rb b/test/vool/to_mom/send/test_send_simple_args.rb index 8eb6e629..a8963d7d 100644 --- a/test/vool/to_mom/send/test_send_simple_args.rb +++ b/test/vool/to_mom/send/test_send_simple_args.rb @@ -12,14 +12,14 @@ module Vool end def receiver - [IntegerStatement , 5] + [Mom::IntegerConstant , 5] end def test_args_two_move assert_equal :next_message, @stats[1].arguments[1].left.slots[0] assert_equal :arguments, @stats[1].arguments[1].left.slots[1] end def test_args_two_str - assert_equal IntegerStatement, @stats[1].arguments[1].right.class + assert_equal Mom::IntegerConstant, @stats[1].arguments[1].right.class assert_equal 2, @stats[1].arguments[1].right.value end end diff --git a/test/vool/to_mom/send/test_send_simple_string.rb b/test/vool/to_mom/send/test_send_simple_string.rb index a08c977f..04bb6dad 100644 --- a/test/vool/to_mom/send/test_send_simple_string.rb +++ b/test/vool/to_mom/send/test_send_simple_string.rb @@ -12,7 +12,7 @@ module Vool @first = @stats.first end def receiver - [StringStatement , "5"] + [Mom::StringConstant , "5"] end def test_args_one_move @@ -20,7 +20,7 @@ module Vool assert_equal :arguments, @stats[1].arguments[0].left.slots[1] end def test_args_one_str - assert_equal IntegerStatement, @stats[1].arguments[0].right.class + assert_equal Mom::IntegerConstant, @stats[1].arguments[0].right.class assert_equal 1, @stats[1].arguments[0].right.value end end diff --git a/test/vool/to_mom/test_assign.rb b/test/vool/to_mom/test_assign.rb index f8a7e550..d37ef6d0 100644 --- a/test/vool/to_mom/test_assign.rb +++ b/test/vool/to_mom/test_assign.rb @@ -29,7 +29,7 @@ module Vool assert @stats.first.right end def test_slot_assigns_int - assert_equal IntegerStatement , @first.right.class + assert_equal Mom::IntegerConstant , @first.right.class end end diff --git a/test/vool/to_mom/test_ivar.rb b/test/vool/to_mom/test_ivar.rb index 61bd285e..ef817b45 100644 --- a/test/vool/to_mom/test_ivar.rb +++ b/test/vool/to_mom/test_ivar.rb @@ -31,7 +31,7 @@ module Vool assert @method.first.right end def test_slot_assigns_int - assert_equal IntegerStatement , @method.first.right.class + assert_equal Mom::IntegerConstant , @method.first.right.class end end end diff --git a/test/vool/to_mom/test_local.rb b/test/vool/to_mom/test_local.rb index 8e4da0d5..047dc37f 100644 --- a/test/vool/to_mom/test_local.rb +++ b/test/vool/to_mom/test_local.rb @@ -32,7 +32,7 @@ module Vool assert @first.right end def test_slot_assigns_int - assert_equal IntegerStatement , @first.right.class + assert_equal Mom::IntegerConstant , @first.right.class end end end diff --git a/test/vool/to_mom/test_return.rb b/test/vool/to_mom/test_return.rb index 4f0883a9..95eb315b 100644 --- a/test/vool/to_mom/test_return.rb +++ b/test/vool/to_mom/test_return.rb @@ -34,7 +34,7 @@ module Vool assert @stats.first.right end def test_slot_assigns_int - assert_equal IntegerStatement , @stats.first.right.class + assert_equal Mom::IntegerConstant , @stats.first.right.class end end class TestReturnSendMom < MiniTest::Test @@ -42,7 +42,7 @@ module Vool def setup Risc.machine.boot - @stats = compile_first_method( "return foo").first + @stats = compile_first_method( "return 5.mod4").first end def test_two_instructions_are_returned