correct flattening for moms if

This commit is contained in:
Torsten Ruger 2017-09-08 13:12:24 +03:00
parent 985dc9904d
commit 0f83b89805
3 changed files with 46 additions and 4 deletions

View File

@ -11,7 +11,17 @@ module Mom
end end
def flatten def flatten
self head = hoisted.flatten
true_label = Label.new( "true_label_#{object_id}")
false_label = Label.new( "false_label_#{object_id}")
merge_label = Label.new( "merge_label_#{object_id}")
head.append condition.flatten( true_label: true_label , false_label: false_label)
head.append true_label
head.append if_true.flatten( merge_label: merge_label)
head.append false_label
head.append if_false.flatten( merge_label: merge_label)
head.append merge_label
head
end end
end end

View File

@ -1,7 +1,7 @@
module Mom module Mom
class Statement class Statement
# flattening will change the structure from a tree to a linked list (and use # flattening will change the structure from a tree to a linked list (and use
# next_instruction to do so) # nekst to do so)
def flatten def flatten
raise "not implemented for #{self}" raise "not implemented for #{self}"
end end
@ -10,9 +10,14 @@ module Mom
class Statements < Statement class Statements < Statement
include Common::Statements include Common::Statements
def flatten def flatten( options = {} )
@statements.each{ |s| s.flatten } flat = @statements.pop.flatten
while( nekst = @statements.pop )
flat.append nekst.flatten()
end
flat
end end
end end
end end

View File

@ -0,0 +1,27 @@
require_relative "helper"
module Mom
class TestConditionIfMom < MiniTest::Test
include MomCompile
def setup
Risc.machine.boot
@stats = compile_first_method_flat( "if(@a == 5) ; 5.mod4 ; else; 4.mod4 ; end")
@first = @stats.next
end
def test_if_compiles
assert IfStatement != @stats.class , @stats
end
def test_check
assert_equal TruthCheck , @first.class
end
def test_condition_is_slot
assert_equal SlotDefinition , @first.condition.class , @first
end
def test_length
assert_equal 9 , @stats.length
end
end
end