From 8cac5c064dbaa8dd3d89f0adcdb85e7ecaf9fffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Sun, 6 Oct 2019 19:49:53 +0300 Subject: [PATCH] Start nexr level p i SlotLanguage, calling it MacroMaker Since we already have Macros. Macros are nothing more than a list of SlotMachine Instructions. This is what we are aiming to create (which is also what is created in Sol .to_slot) So the idea came to slot the MacroMaker in there after its done --- lib/slot_language/macro_maker.rb | 23 +++++++++++++ lib/slot_language/slot_compiler.rb | 16 +++++---- test/slot_language/helper.rb | 6 ++++ test/slot_language/mini.slot | 2 ++ test/slot_language/test_macro_maker.rb | 40 +++++++++++++++++++++++ test/slot_language/test_slot_compiler.rb | 20 +++++++++++- test/slot_language/test_slot_compiler2.rb | 8 +++++ 7 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 lib/slot_language/macro_maker.rb create mode 100644 test/slot_language/mini.slot create mode 100644 test/slot_language/test_macro_maker.rb create mode 100644 test/slot_language/test_slot_compiler2.rb diff --git a/lib/slot_language/macro_maker.rb b/lib/slot_language/macro_maker.rb new file mode 100644 index 00000000..dc75dc96 --- /dev/null +++ b/lib/slot_language/macro_maker.rb @@ -0,0 +1,23 @@ + +module SlotLanguage + class MacroMaker + attr_reader :source + + def self.load_file(relative_name) + path = File.expand_path( "../code/#{relative_name}" , __FILE__) + load_string( File.read(path)) + end + def self.load_string(source_code) + MacroMaker.new( SlotCompiler.compile(source_code) ) + end + + def initialize( source ) + @source = source + raise "undefined source #{source}" unless source.is_a?(Array) + end + + def to_slot(compiler) + @source.first + end + end +end diff --git a/lib/slot_language/slot_compiler.rb b/lib/slot_language/slot_compiler.rb index 0eaf2945..8e5748d6 100644 --- a/lib/slot_language/slot_compiler.rb +++ b/lib/slot_language/slot_compiler.rb @@ -29,27 +29,30 @@ module SlotLanguage def on_lvar(lvar) SlotMaker.new(lvar.children.first , nil) end - def on_lvasgn expression + def on_lvasgn( expression) name = expression.children[0] value = process(expression.children[1]) LoadMaker.new(SlotMaker.new(name,nil),value) end + alias :on_ivasgn :on_lvasgn + def on_if(expression) condition = process(expression.children[0]) condition.set_goto( process(expression.children[1]) ) condition end def on_begin(exp) - process(exp.first) + if( exp.children.length == 1) + process(exp.first) + else + process_all(exp) + end end def on_ivar expression - SlotMaker.new(instance_name(expression.children.first),nil) + SlotMaker.new(expression.children.first,nil) end private - def instance_name(sym) - sym.to_s[1 .. -1].to_sym - end def label(name) SlotMachine::Label.new(name.to_s , name) end @@ -78,3 +81,4 @@ require_relative "message_slot" require_relative "slot_maker" require_relative "load_maker" require_relative "check_maker" +require_relative "macro_maker" diff --git a/test/slot_language/helper.rb b/test/slot_language/helper.rb index 82ebe3b9..1abb8a0c 100644 --- a/test/slot_language/helper.rb +++ b/test/slot_language/helper.rb @@ -9,4 +9,10 @@ module SlotLanguage compile(input).class end end + module SlotToHelper + def setup + Parfait.boot!({}) + @compiler = SlotMachine::SlotCollection.compiler_for( :Space , :main,{},{}) + end + end end diff --git a/test/slot_language/mini.slot b/test/slot_language/mini.slot new file mode 100644 index 00000000..e1e25e32 --- /dev/null +++ b/test/slot_language/mini.slot @@ -0,0 +1,2 @@ +start_label +a = b diff --git a/test/slot_language/test_macro_maker.rb b/test/slot_language/test_macro_maker.rb new file mode 100644 index 00000000..acb4db11 --- /dev/null +++ b/test/slot_language/test_macro_maker.rb @@ -0,0 +1,40 @@ +require_relative "helper" + +module SlotLanguage + class TestMacroMakerLoad < MiniTest::Test + include SlotToHelper + def setup + super + @slot = MacroMaker.load_string( mini_file ).to_slot(@compiler) + end + def test_to_slot + assert @slot.is_a?(SlotMachine::Instruction) , @slot.class + end + def test_length + assert_equal 1 , @slot.length + end + end + + class TestMacroMakerLoad < MiniTest::Test + include SlotHelper + + def check_mini(maker) + assert_equal MacroMaker , maker.class + assert_equal Array , maker.source.class + assert_equal SlotMachine::Label , maker.source.first.class + assert_equal 2 , maker.source.length + end + def mini_file + File.read(File.expand_path( "../mini.slot" , __FILE__)) + end + def test_mini_file + check_mini MacroMaker.load_file("../../../test/slot_language/mini.slot") + end + def test_mini_string + check_mini MacroMaker.load_string( mini_file ) + end + def test_mini_source + check_mini MacroMaker.new( SlotCompiler.compile(mini_file)) + end + end +end diff --git a/test/slot_language/test_slot_compiler.rb b/test/slot_language/test_slot_compiler.rb index cff68650..ca7d898d 100644 --- a/test/slot_language/test_slot_compiler.rb +++ b/test/slot_language/test_slot_compiler.rb @@ -15,9 +15,21 @@ module SlotLanguage assert_equal SlotMachine::Label , label.class assert_equal :while_label , label.name end - def test_slot_load + def test_slot_load_rinst assert_equal LoadMaker , compile_class("a = @b") end + def test_slot_load_linst + assert_equal LoadMaker , compile_class("@a = b") + end + def test_slot_load_lrinst + assert_equal LoadMaker , compile_class("@a = @b") + end + def test_slot_load_linst_trav + assert_equal LoadMaker , compile_class("@a = b.c") + end + def test_slot_load_linst_trav2 + assert_equal LoadMaker , compile_class("@a.c = b.c") + end def test_goto assert_equal SlotMachine::Jump , compile_class("goto(exit_label)") end @@ -38,5 +50,11 @@ module SlotLanguage assign = compile("c.next = d") assert_equal LoadMaker , assign.class end + def test_multiline + multi = compile("start_label;c = c.next;goto(start_label)") + assert_equal Array , multi.class + assert_equal SlotMachine::Label , multi.first.class + assert_equal SlotMachine::Jump , multi.last.class + end end end diff --git a/test/slot_language/test_slot_compiler2.rb b/test/slot_language/test_slot_compiler2.rb new file mode 100644 index 00000000..9c5d7242 --- /dev/null +++ b/test/slot_language/test_slot_compiler2.rb @@ -0,0 +1,8 @@ +require_relative "helper" + +module SlotLanguage + class TestSlotCompiler < MiniTest::Test + include SlotHelper + + end +end