ifs working

This commit is contained in:
Torsten Ruger 2018-03-16 19:05:22 +05:30
parent da0e1cdc5f
commit 259b248588
4 changed files with 62 additions and 52 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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