From 99d2868400649365a6b2f077344bfc238bb46b53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Thu, 13 Feb 2020 12:53:41 +0700 Subject: [PATCH] Rename check_maker to equal_goto will have different classes for different checks, started that --- lib/slot_language/check_maker.rb | 14 ------ lib/slot_language/equal_goto.rb | 13 +++++ lib/slot_language/slot_compiler.rb | 20 ++++++-- test/slot_language/test_equal_goto.rb | 60 ++++++++++++++++++++++++ test/slot_language/test_slot_compiler.rb | 14 ++---- 5 files changed, 91 insertions(+), 30 deletions(-) delete mode 100644 lib/slot_language/check_maker.rb create mode 100644 lib/slot_language/equal_goto.rb create mode 100644 test/slot_language/test_equal_goto.rb diff --git a/lib/slot_language/check_maker.rb b/lib/slot_language/check_maker.rb deleted file mode 100644 index 734a1774..00000000 --- a/lib/slot_language/check_maker.rb +++ /dev/null @@ -1,14 +0,0 @@ -module SlotLanguage - class CheckMaker - attr_reader :check , :left , :right, :goto - - def initialize(check , left , right) - @check = check - @left = left - @right = right - end - def set_goto(go) - @goto = go - end - end -end diff --git a/lib/slot_language/equal_goto.rb b/lib/slot_language/equal_goto.rb new file mode 100644 index 00000000..26b5c8c3 --- /dev/null +++ b/lib/slot_language/equal_goto.rb @@ -0,0 +1,13 @@ +module SlotLanguage + class EqualGoto + attr_reader :left , :right, :goto + + def initialize( left , right) + @left = left + @right = right + end + def set_goto(go) + @goto = go + end + end +end diff --git a/lib/slot_language/slot_compiler.rb b/lib/slot_language/slot_compiler.rb index 759cad33..d688b932 100644 --- a/lib/slot_language/slot_compiler.rb +++ b/lib/slot_language/slot_compiler.rb @@ -5,11 +5,16 @@ module SlotLanguage class SlotCompiler < AST::Processor DEBUG = false + # conditionals supported, currently only equal + def self.checks + [:==] + end + def self.compile(input) ast = Parser::CurrentRuby.parse( input ) self.new.process(ast) end - + attr_reader :labels def initialize @labels = {} end @@ -27,7 +32,7 @@ module SlotLanguage name = kids.shift return label(name) if(name.to_s.end_with?("_label")) return goto(name,kids) if(name == :goto) - return check(name,receiver, kids) if(name == :==) + return check(name,receiver, kids) if SlotCompiler.checks.include?(name) return assign(receiver, name , kids) if(name.to_s.end_with?("=")) puts "Send #{name} , #{receiver} kids=#{kids}" if DEBUG SlotMaker.new( name ) @@ -78,12 +83,17 @@ module SlotLanguage Goto.new( label ) end def check(name , receiver , kids) - raise "Only ==, not #{name}" unless name == :== raise "Familiy too large #{kids}" if kids.length > 1 puts "Kids " + kids.to_s if DEBUG right = process(kids.first) - CheckMaker.new(name , receiver , right) + case name + when :== + return EqualGoto.new(receiver , right) + else + raise "Only ==, not #{name}" unless name == :== + end end + def assign(receiver , name , kids) name = name.to_s[0...-1].to_sym receiver.add_slot_name(name) @@ -97,6 +107,6 @@ require_relative "named_slot" require_relative "message_slot" require_relative "slot_maker" require_relative "load_maker" -require_relative "check_maker" require_relative "macro_maker" require_relative "goto" +require_relative "equal_goto" diff --git a/test/slot_language/test_equal_goto.rb b/test/slot_language/test_equal_goto.rb new file mode 100644 index 00000000..ef088f2e --- /dev/null +++ b/test/slot_language/test_equal_goto.rb @@ -0,0 +1,60 @@ +require_relative "helper" + +module SlotLanguage + class TestEqualGoto < MiniTest::Test + include SlotHelper + + def do_check(check) + assert_equal EqualGoto , check.class + assert_equal Goto , check.goto.class + assert_equal SlotMaker , check.left.class + assert_equal SlotMaker , check.right.class + end + def test_equal_local + check = compile("goto(exit_label) if(a == b)") + do_check(check) + assert_equal :a , check.left.names[0] + assert_equal :b , check.right.names[0] + end + def test_equal_inst_left + check = compile("goto(exit_label) if(@a == b)") + do_check(check) + assert_equal :@a , check.left.names[0] + end + def test_equal_inst_right + check = compile("goto(exit_label) if(a == @b)") + do_check(check) + assert_equal :@b , check.right.names[0] + end + end + + class TestEqualGotoFull < MiniTest::Test + include SlotHelper + def setup + @expr = compile("start_label;goto(start_label) if( b == c)") + end + def test_2 + assert_equal Array , @expr.class + assert_equal 2 , @expr.length + end + def test_label + assert_equal SlotMachine::Label , @expr.first.class + assert_equal :start_label , @expr.first.name + end + def test_conditional + assert_equal EqualGoto , @expr.last.class + assert_equal :start_label , @expr.last.goto.label.name + end + def test_same_label + assert_equal @expr.first.object_id , @expr.last.goto.label.object_id + end + def test_expression_left + assert_equal SlotMaker , @expr.last.left.class + assert_equal [:b] , @expr.last.left.names + end + def test_expression_right + assert_equal SlotMaker , @expr.last.right.class + assert_equal [:c] , @expr.last.right.names + end + end +end diff --git a/test/slot_language/test_slot_compiler.rb b/test/slot_language/test_slot_compiler.rb index e6eec83e..2d8f4528 100644 --- a/test/slot_language/test_slot_compiler.rb +++ b/test/slot_language/test_slot_compiler.rb @@ -7,6 +7,9 @@ module SlotLanguage def test_init assert SlotCompiler.new end + def test_labels + assert SlotCompiler.new.labels.empty? + end def test_compile assert_equal SlotMaker , compile("a").class end @@ -25,11 +28,6 @@ module SlotLanguage def test_slot_load_linst_trav2 assert_equal LoadMaker , compile_class("@a.c = b.c") end - def test_if - check = compile("goto(exit_label) if(a == b)") - assert_equal CheckMaker , check.class - assert_equal Goto , check.goto.class - end def test_assign assign = compile("c = d") assert_equal LoadMaker , assign.class @@ -42,12 +40,6 @@ 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 Goto , multi.last.class - end def test_shift load = compile("word = name.member") assert_equal LoadMaker , load.class