ifs working
This commit is contained in:
parent
da0e1cdc5f
commit
259b248588
@ -9,7 +9,6 @@ module Vool
|
|||||||
@condition = cond
|
@condition = cond
|
||||||
@if_true = if_true
|
@if_true = if_true
|
||||||
@if_false = if_false
|
@if_false = if_false
|
||||||
simplify_condition
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize
|
def normalize
|
||||||
@ -18,36 +17,23 @@ module Vool
|
|||||||
me = IfStatement.new(cond , @if_true.normalize, fals)
|
me = IfStatement.new(cond , @if_true.normalize, fals)
|
||||||
return me unless rest
|
return me unless rest
|
||||||
rest << me
|
rest << me
|
||||||
|
rest
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_mom( method )
|
def to_mom( method )
|
||||||
if_true = @if_true.to_mom( method )
|
true_label = Mom::Label.new( "true_label_#{object_id}")
|
||||||
if_false = @if_false ? @if_false.to_mom( method ) : nil
|
false_label = Mom::Label.new( "false_label_#{object_id}")
|
||||||
condition , hoisted = hoist_condition( method )
|
merge_label = Mom::Label.new( "merge_label_#{object_id}")
|
||||||
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
|
|
||||||
|
|
||||||
def flatten(options = {})
|
head = Mom::TruthCheck.new(condition.slot_definition(method) , false_label)
|
||||||
true_label = Label.new( "true_label_#{object_id}")
|
head << true_label
|
||||||
false_label = Label.new( "false_label_#{object_id}")
|
head << if_true.to_mom(method)
|
||||||
merge_label = Label.new( "merge_label_#{object_id}")
|
head << Mom::Jump.new(merge_label) if if_false
|
||||||
first = condition.flatten( true_label: true_label , false_label: false_label)
|
head << false_label
|
||||||
if hoisted
|
if if_false
|
||||||
head = hoisted.flatten
|
head << if_false.to_mom(method)
|
||||||
head.append first
|
head << merge_label
|
||||||
else
|
|
||||||
head = first
|
|
||||||
end
|
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
|
head
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -57,11 +43,6 @@ module Vool
|
|||||||
@if_false.each(&block) if @if_false
|
@if_false.each(&block) if @if_false
|
||||||
end
|
end
|
||||||
|
|
||||||
def simplify_condition
|
|
||||||
return unless @condition.is_a?(ScopeStatement)
|
|
||||||
@condition = @condition.first if @condition.single?
|
|
||||||
end
|
|
||||||
|
|
||||||
def has_false?
|
def has_false?
|
||||||
@if_false != nil
|
@if_false != nil
|
||||||
end
|
end
|
||||||
|
@ -4,24 +4,25 @@ require_relative "helper"
|
|||||||
module Vool
|
module Vool
|
||||||
class TestConditionIfMom < MiniTest::Test
|
class TestConditionIfMom < MiniTest::Test
|
||||||
include MomCompile
|
include MomCompile
|
||||||
|
include Mom
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
Risc.machine.boot
|
Risc.machine.boot
|
||||||
@stats = compile_first_method( "if(@a == 5) ; 5.mod4 ; else; 4.mod4 ; end")
|
@ins = compile_first_method( "if(@a == 5) ; 5.mod4 ; else; 4.mod4 ; end")
|
||||||
@first = @stats.first
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_if_compiles_as_array
|
def test_condition
|
||||||
assert_equal Mom::IfStatement , @first.class , @stats
|
assert_equal TruthCheck , @ins.next(12).class
|
||||||
end
|
|
||||||
def test_condition_compiles_to_slot
|
|
||||||
assert_equal Mom::TruthCheck , @first.condition.class
|
|
||||||
end
|
end
|
||||||
def test_condition_is_slot
|
def test_condition_is_slot
|
||||||
assert_equal Mom::SlotDefinition , @first.condition.condition.class , @stats
|
assert_equal SlotDefinition , @ins.next(12).condition.class , @ins
|
||||||
end
|
end
|
||||||
def test_hoisetd
|
def test_hoisted_dynamic_call
|
||||||
assert_equal Mom::SlotLoad , @first.hoisted.class
|
assert_equal DynamicCall , @ins.next(10).class
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
30
test/vool/to_mom/test_if_no_else.rb
Normal file
30
test/vool/to_mom/test_if_no_else.rb
Normal 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
|
@ -4,29 +4,27 @@ require_relative "helper"
|
|||||||
module Vool
|
module Vool
|
||||||
class TestSimpleIfMom < MiniTest::Test
|
class TestSimpleIfMom < MiniTest::Test
|
||||||
include MomCompile
|
include MomCompile
|
||||||
|
include Mom
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
Risc.machine.boot
|
Risc.machine.boot
|
||||||
@ins = compile_first_method( "if(@a) ; 5.mod4 ; else; 4.mod4 ; end")
|
@ins = compile_first_method( "if(@a) ; 5.mod4 ; else; 4.mod4 ; end")
|
||||||
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
|
def test_condition_compiles_to_check
|
||||||
assert_equal Mom::TruthCheck , @ins.condition.class , @ins
|
assert_equal TruthCheck , @ins.class , @ins
|
||||||
end
|
end
|
||||||
def test_condition_is_slot
|
def test_condition_is_slot
|
||||||
assert_equal Mom::SlotDefinition , @ins.condition.condition.class , @ins
|
assert_equal SlotDefinition , @ins.condition.class , @ins
|
||||||
end
|
end
|
||||||
def test_nothing_hoisted
|
def test_label_after_check
|
||||||
assert_nil @ins.hoisted , @ins
|
assert_equal Label , @ins.next.class , @ins
|
||||||
|
end
|
||||||
|
def test_label_last
|
||||||
|
assert_equal Label , @ins.last.class , @ins
|
||||||
end
|
end
|
||||||
def test_array
|
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
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user