fix while back jump

while normalising the condition had gone before the
jump target accidentally
This commit is contained in:
Torsten Ruger 2018-04-20 09:56:06 +03:00
parent 04359546b7
commit bf6e0853ce
3 changed files with 23 additions and 13 deletions

View File

@ -3,33 +3,34 @@ require_relative "normalizer"
module Vool module Vool
class WhileStatement < Statement class WhileStatement < Statement
include Normalizer include Normalizer
attr_reader :condition , :body attr_reader :condition , :body , :hoisted
def initialize( condition , body ) def initialize( condition , body , hoisted = nil)
@hoisted = hoisted
@condition = condition @condition = condition
@body = body @body = body
end end
def normalize def normalize
cond , rest = *normalize_name(@condition) cond , rest = *normalize_name(@condition)
me = WhileStatement.new(cond , @body.normalize) WhileStatement.new(cond , @body.normalize , rest)
return me unless rest
rest << me
rest
end end
def to_mom( method ) def to_mom( method )
merge_label = Mom::Label.new( "merge_label_#{object_id}") merge_label = Mom::Label.new( "merge_label_#{object_id}")
cond_label = Mom::Label.new( "cond_label_#{object_id}") cond_label = Mom::Label.new( "cond_label_#{object_id}")
cond_label << Mom::TruthCheck.new(condition.slot_definition(method) , merge_label) codes = cond_label
cond_label << @body.to_mom(method) codes << @hoisted.to_mom(method) if @hoisted
cond_label << Mom::Jump.new(cond_label) codes << Mom::TruthCheck.new(condition.slot_definition(method) , merge_label)
cond_label << merge_label codes << @body.to_mom(method)
codes << Mom::Jump.new(cond_label)
codes << merge_label
end end
def each(&block) def each(&block)
block.call(self) block.call(self)
block.call(@condition) block.call(@condition)
@hoisted.each(&block) if @hoisted
@body.each(&block) @body.each(&block)
end end

View File

@ -52,7 +52,7 @@ module MomCompile
is = is.next is = is.next
end end
ret = "" ret = ""
res.to_s.split(",").each_slice(6).each do |line| res.to_s.split(",").each_slice(5).each do |line|
ret += " " + line.join(",") + " ,\n" ret += " " + line.join(",") + " ,\n"
end end
ret.gsub('"' , '') ret.gsub('"' , '')

View File

@ -18,10 +18,19 @@ module Vool
assert_equal SlotDefinition , @ins.next(5).condition.class , @ins assert_equal SlotDefinition , @ins.next(5).condition.class , @ins
end end
def test_hoisetd def test_hoisetd
assert_equal MessageSetup , @ins.class jump = @ins.next(9)
assert_kind_of Jump , jump
assert jump.label.name.start_with?("cond_label") , jump.label.name
end
def test_label
label = @ins
assert_equal Label , label.class
assert label.name.start_with?("cond_label") , label.name
end end
def test_array def test_array
check_array [MessageSetup,ArgumentTransfer,SimpleCall,SlotLoad,Label,TruthCheck,MessageSetup,ArgumentTransfer,SimpleCall,Jump,Label] , @ins check_array [Label, MessageSetup, ArgumentTransfer, SimpleCall, SlotLoad ,
TruthCheck, MessageSetup, ArgumentTransfer, SimpleCall, Jump ,
Label] , @ins
end end
end end