From 1dabe0fda130408984516cfe4b9a9021602a03cd Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 15 Aug 2018 19:59:17 +0300 Subject: [PATCH] finish the idea behind #8, conditionally creating variables by using space? , the ? makes for conditional creation of the variable This is especially useful for constants (ie space) --- lib/mom/instruction/message_setup.rb | 12 ++---------- lib/risc/builder.rb | 13 ++++++++++--- test/risc/test_builder2.rb | 11 +++++++++++ 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/lib/mom/instruction/message_setup.rb b/lib/mom/instruction/message_setup.rb index 7632147d..a3407922 100644 --- a/lib/mom/instruction/message_setup.rb +++ b/lib/mom/instruction/message_setup.rb @@ -64,8 +64,8 @@ module Mom # set the method into the message def build_message_data( builder ) builder.build do - space! << Parfait.object_space - next_message! << space[:next_message] + space? << Parfait.object_space + next_message? << space[:next_message] #FIXME in a multithreaded future this should be done using lock free compare and swap. next_message_reg! << next_message[:next_message] @@ -75,14 +75,6 @@ module Mom next_message[:caller] << message next_message[:method] << callable - # type << callable[:arguments_type] - # named_list << next_message[:arguments] - # named_list[:type] << type - # - # type << callable[:frame_type] - # named_list << next_message[:frame] - # named_list[:type] << type - end end end diff --git a/lib/risc/builder.rb b/lib/risc/builder.rb index 377d1593..0e1b3d5a 100644 --- a/lib/risc/builder.rb +++ b/lib/risc/builder.rb @@ -39,9 +39,16 @@ module Risc reg = Risc.label( @source , "#{name}_#{object_id}") @source_used = true else - raise "Must create (with !) before using #{name}" unless name[-1] == "!" + last_char = name[-1] name = name[0 ... -1] - #raise "name exists before being created #{name}" if @names.has_key?(name) + if last_char == "!" or last_char == "?" + if @names.has_key?(name) + return @names[name] if last_char == "?" + raise "Name exists before creating it #{name}#{last_char}" + end + else + raise "Must create (with ! or ?) before using #{name}#{last_char}" + end type = infer_type(name ) reg = @compiler.use_reg( type.object_class.name ).set_builder(self) end @@ -130,7 +137,7 @@ module Risc to.set_builder( self ) # esecially div10 comes in without having used builder from.set_builder( self ) # not named regs, different regs ==> silent errors build do - space! << Parfait.object_space + space? << Parfait.object_space to << space[:next_integer] integer_2! << to[:next_integer] space[:next_integer] << integer_2 diff --git a/test/risc/test_builder2.rb b/test/risc/test_builder2.rb index 6a919eeb..a1636c1b 100644 --- a/test/risc/test_builder2.rb +++ b/test/risc/test_builder2.rb @@ -25,6 +25,17 @@ module Risc def test_caller_reg assert_equal :Message , @builder.infer_type(:caller_reg).class_name end + def test_define_twice + @builder.caller_reg! + assert_raises{ @builder.caller_reg! } + end + def test_define_conditionally_first + assert_equal :r1 , @builder.caller_reg?.symbol + end + def test_define_conditionally_again + first = @builder.caller_reg! + assert_equal first , @builder.caller_reg? + end def test_caller_tmp assert_equal :Message , @builder.infer_type(:caller_tmp).class_name end