From 308670b90ceae1ae879a6c1d58560cc86f8f95d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Mon, 7 Oct 2019 20:14:40 +0300 Subject: [PATCH] Start to transform SL into SM --- lib/slot_language/load_maker.rb | 6 ++++++ lib/slot_language/macro_maker.rb | 14 +++++++++++++- lib/slot_language/slot_compiler.rb | 8 ++++---- lib/slot_language/slot_maker.rb | 24 +++++++++++++----------- test/slot_language/test_load_maker.rb | 16 ++++++++++++++++ test/slot_language/test_macro_maker.rb | 13 ++++++++++--- test/slot_language/test_slot_maker.rb | 13 +++++++------ 7 files changed, 69 insertions(+), 25 deletions(-) create mode 100644 test/slot_language/test_load_maker.rb diff --git a/lib/slot_language/load_maker.rb b/lib/slot_language/load_maker.rb index e0bf2961..67ac5158 100644 --- a/lib/slot_language/load_maker.rb +++ b/lib/slot_language/load_maker.rb @@ -8,5 +8,11 @@ module SlotLanguage raise "No Slot #{left}" unless left.is_a?(SlotMaker) raise "No Slot #{right}" unless right.is_a?(SlotMaker) end + + def to_slot(compiler) + left_d = @left.slot_def(compiler) + right_d = @right.slot_def(compiler) + SlotMachine::SlotLoad.new("source" , left_d , right_d) + end end end diff --git a/lib/slot_language/macro_maker.rb b/lib/slot_language/macro_maker.rb index dc75dc96..89a51b26 100644 --- a/lib/slot_language/macro_maker.rb +++ b/lib/slot_language/macro_maker.rb @@ -17,7 +17,19 @@ module SlotLanguage end def to_slot(compiler) - @source.first + chain = do_link( @source.first , compiler) + rest = @source.dup + rest.shift + rest.each do |link| + chain << do_link(link , compiler) + end + chain + end + + private + def do_link(link,compiler) + return link if link.is_a?(SlotMachine::Instruction) + link.to_slot(compiler) end end end diff --git a/lib/slot_language/slot_compiler.rb b/lib/slot_language/slot_compiler.rb index 8e5748d6..e2b633df 100644 --- a/lib/slot_language/slot_compiler.rb +++ b/lib/slot_language/slot_compiler.rb @@ -24,15 +24,15 @@ module SlotLanguage return goto(name,kids) if(name == :goto) return check(name,receiver, kids) if(name == :==) return assign(receiver, name , kids) if(name.to_s.end_with?("=")) - SlotMaker.new( name , receiver ) + SlotMaker.new( name ) end def on_lvar(lvar) - SlotMaker.new(lvar.children.first , nil) + SlotMaker.new(lvar.children.first ) end def on_lvasgn( expression) name = expression.children[0] value = process(expression.children[1]) - LoadMaker.new(SlotMaker.new(name,nil),value) + LoadMaker.new(SlotMaker.new(name),value) end alias :on_ivasgn :on_lvasgn @@ -49,7 +49,7 @@ module SlotLanguage end end def on_ivar expression - SlotMaker.new(expression.children.first,nil) + SlotMaker.new(expression.children.first) end private diff --git a/lib/slot_language/slot_maker.rb b/lib/slot_language/slot_maker.rb index fa0dbc37..e7b722a3 100644 --- a/lib/slot_language/slot_maker.rb +++ b/lib/slot_language/slot_maker.rb @@ -1,22 +1,24 @@ module SlotLanguage class SlotMaker - attr_reader :name , :leaps + attr_reader :leaps - def initialize(name , more) - @name = name - if(more.is_a?(Array)) - @leaps = more + def initialize(leaps) + case leaps + when Array + @leaps = leaps + when nil + raise "No leaps given" else - @leaps = [more] if more + @leaps = [leaps] end end def add_slot_name(name) - if(@leaps) - @leaps << name - else - @leaps = [name] - end + @leaps << name + end + + def slot_def(compiler) + SlotMachine::SlotDefinition.new(:message , leaps) end end end diff --git a/test/slot_language/test_load_maker.rb b/test/slot_language/test_load_maker.rb new file mode 100644 index 00000000..c54f6da0 --- /dev/null +++ b/test/slot_language/test_load_maker.rb @@ -0,0 +1,16 @@ +require_relative "helper" + +module SlotLanguage + class TestLoadMaker < MiniTest::Test + include SlotToHelper + def setup + super + left = SlotMaker.new(:hi ) + right = SlotMaker.new(:hi ) + @slot = LoadMaker.new( left,right ).to_slot(@compiler) + end + def test_to_slot + assert_equal SlotMachine::SlotLoad , @slot.class + end + end +end diff --git a/test/slot_language/test_macro_maker.rb b/test/slot_language/test_macro_maker.rb index acb4db11..bf43fbb8 100644 --- a/test/slot_language/test_macro_maker.rb +++ b/test/slot_language/test_macro_maker.rb @@ -7,11 +7,18 @@ module SlotLanguage super @slot = MacroMaker.load_string( mini_file ).to_slot(@compiler) end - def test_to_slot - assert @slot.is_a?(SlotMachine::Instruction) , @slot.class + def test_label + assert_equal SlotMachine::Label , @slot.class + end + def test_assign + assert_equal SlotMachine::SlotLoad , @slot.next.class + assert_equal :message , @slot.next.left.known_object + assert_equal [:a] , @slot.next.left.slots + assert_equal :message , @slot.next.right.known_object + assert_equal [:b] , @slot.next.right.slots end def test_length - assert_equal 1 , @slot.length + assert_equal 2 , @slot.length end end diff --git a/test/slot_language/test_slot_maker.rb b/test/slot_language/test_slot_maker.rb index ad8b4064..58b18b77 100644 --- a/test/slot_language/test_slot_maker.rb +++ b/test/slot_language/test_slot_maker.rb @@ -2,12 +2,13 @@ require_relative "helper" module SlotLanguage class TestSlotMaker < MiniTest::Test - include SlotHelper - - def test_label - label = compile("while_label") - assert_equal SlotMachine::Label , label.class - assert_equal :while_label , label.name + include SlotToHelper + def setup + super + @maker = SlotMaker.new(:hi ) + end + def test_slot + @maker.slot_def(@compiler) end end end