From 259b24858868241c86cd9b044be036281f99f68c Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 16 Mar 2018 19:05:22 +0530 Subject: [PATCH] ifs working --- lib/vool/statements/if_statement.rb | 43 ++++++++------------------- test/vool/to_mom/test_if_condition.rb | 21 ++++++------- test/vool/to_mom/test_if_no_else.rb | 30 +++++++++++++++++++ test/vool/to_mom/test_if_simple.rb | 20 ++++++------- 4 files changed, 62 insertions(+), 52 deletions(-) create mode 100644 test/vool/to_mom/test_if_no_else.rb diff --git a/lib/vool/statements/if_statement.rb b/lib/vool/statements/if_statement.rb index fd7cca95..e8806968 100644 --- a/lib/vool/statements/if_statement.rb +++ b/lib/vool/statements/if_statement.rb @@ -9,7 +9,6 @@ module Vool @condition = cond @if_true = if_true @if_false = if_false - simplify_condition end def normalize @@ -18,36 +17,23 @@ module Vool me = IfStatement.new(cond , @if_true.normalize, fals) return me unless rest rest << me + rest end def to_mom( method ) - if_true = @if_true.to_mom( method ) - if_false = @if_false ? @if_false.to_mom( method ) : nil - condition , hoisted = hoist_condition( method ) - cond = Mom::TruthCheck.new(condition.to_mom(method)) - check = Mom::IfStatement.new( cond , if_true , if_false ) - check.hoisted = hoisted.to_mom(method) if hoisted - check - end + true_label = Mom::Label.new( "true_label_#{object_id}") + false_label = Mom::Label.new( "false_label_#{object_id}") + merge_label = Mom::Label.new( "merge_label_#{object_id}") - def flatten(options = {}) - true_label = Label.new( "true_label_#{object_id}") - false_label = Label.new( "false_label_#{object_id}") - merge_label = Label.new( "merge_label_#{object_id}") - first = condition.flatten( true_label: true_label , false_label: false_label) - if hoisted - head = hoisted.flatten - head.append first - else - head = first + head = Mom::TruthCheck.new(condition.slot_definition(method) , false_label) + head << true_label + head << if_true.to_mom(method) + head << Mom::Jump.new(merge_label) if if_false + head << false_label + if if_false + head << if_false.to_mom(method) + head << merge_label end - head.append true_label - head.append if_true.flatten( merge_label: merge_label) - if( if_false) - head.append false_label - head.append if_false.flatten( merge_label: merge_label) - end - head.append merge_label head end @@ -57,11 +43,6 @@ module Vool @if_false.each(&block) if @if_false end - def simplify_condition - return unless @condition.is_a?(ScopeStatement) - @condition = @condition.first if @condition.single? - end - def has_false? @if_false != nil end diff --git a/test/vool/to_mom/test_if_condition.rb b/test/vool/to_mom/test_if_condition.rb index e91bd460..e3ed295b 100644 --- a/test/vool/to_mom/test_if_condition.rb +++ b/test/vool/to_mom/test_if_condition.rb @@ -4,24 +4,25 @@ require_relative "helper" module Vool class TestConditionIfMom < MiniTest::Test include MomCompile + include Mom def setup Risc.machine.boot - @stats = compile_first_method( "if(@a == 5) ; 5.mod4 ; else; 4.mod4 ; end") - @first = @stats.first + @ins = compile_first_method( "if(@a == 5) ; 5.mod4 ; else; 4.mod4 ; end") end - def test_if_compiles_as_array - assert_equal Mom::IfStatement , @first.class , @stats - end - def test_condition_compiles_to_slot - assert_equal Mom::TruthCheck , @first.condition.class + def test_condition + assert_equal TruthCheck , @ins.next(12).class end def test_condition_is_slot - assert_equal Mom::SlotDefinition , @first.condition.condition.class , @stats + assert_equal SlotDefinition , @ins.next(12).condition.class , @ins end - def test_hoisetd - assert_equal Mom::SlotLoad , @first.hoisted.class + def test_hoisted_dynamic_call + assert_equal DynamicCall , @ins.next(10).class end + def test_array + check_array [NotSameCheck,SlotLoad,MessageSetup,SlotLoad,ArgumentTransfer,SimpleCall,SlotLoad,MessageSetup,SlotLoad,ArgumentTransfer,DynamicCall,SlotLoad,TruthCheck,Label,MessageSetup,SlotLoad,ArgumentTransfer,SimpleCall,Jump,Label,MessageSetup,SlotLoad,ArgumentTransfer,SimpleCall,Label], @ins + end + end end diff --git a/test/vool/to_mom/test_if_no_else.rb b/test/vool/to_mom/test_if_no_else.rb new file mode 100644 index 00000000..36c0f094 --- /dev/null +++ b/test/vool/to_mom/test_if_no_else.rb @@ -0,0 +1,30 @@ + +require_relative "helper" + +module Vool + class TestIfNoElse < MiniTest::Test + include MomCompile + include Mom + + def setup + Risc.machine.boot + @ins = compile_first_method( "if(@a) ; 5.mod4 ; end") + end + + def test_condition_compiles_to_check + assert_equal TruthCheck , @ins.class , @ins + end + def test_condition_is_slot + assert_equal SlotDefinition , @ins.condition.class , @ins + end + def test_label_after_check + assert_equal Label , @ins.next.class , @ins + end + def test_label_last + assert_equal Label , @ins.last.class , @ins + end + def test_array + check_array [TruthCheck,Label,MessageSetup,SlotLoad,ArgumentTransfer,SimpleCall,Label], @ins + end + end +end diff --git a/test/vool/to_mom/test_if_simple.rb b/test/vool/to_mom/test_if_simple.rb index ca364445..87273139 100644 --- a/test/vool/to_mom/test_if_simple.rb +++ b/test/vool/to_mom/test_if_simple.rb @@ -4,29 +4,27 @@ require_relative "helper" module Vool class TestSimpleIfMom < MiniTest::Test include MomCompile + include Mom def setup Risc.machine.boot @ins = compile_first_method( "if(@a) ; 5.mod4 ; else; 4.mod4 ; end") end - def test_compiles_not_array - assert Array != @ins.class , @ins - end - def test_if_compiles_as_array - assert_equal Mom::IfStatement , @ins.class , @ins - end def test_condition_compiles_to_check - assert_equal Mom::TruthCheck , @ins.condition.class , @ins + assert_equal TruthCheck , @ins.class , @ins end def test_condition_is_slot - assert_equal Mom::SlotDefinition , @ins.condition.condition.class , @ins + assert_equal SlotDefinition , @ins.condition.class , @ins end - def test_nothing_hoisted - assert_nil @ins.hoisted , @ins + def test_label_after_check + assert_equal Label , @ins.next.class , @ins + end + def test_label_last + assert_equal Label , @ins.last.class , @ins end def test_array - check_array [SlotLoad,TruthCheck,Label,MessageSetup,ArgumentTransfer,SimpleCall,Label], @ins + check_array [TruthCheck,Label,MessageSetup,SlotLoad,ArgumentTransfer,SimpleCall,Jump,Label,MessageSetup,SlotLoad,ArgumentTransfer,SimpleCall,Label], @ins end end end