From bf6e0853ce54f9107eb7755621dd8c4985d773b0 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 20 Apr 2018 09:56:06 +0300 Subject: [PATCH] fix while back jump while normalising the condition had gone before the jump target accidentally --- lib/vool/statements/while_statement.rb | 21 +++++++++++---------- test/support/compiling.rb | 2 +- test/vool/to_mom/test_while_condition.rb | 13 +++++++++++-- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/lib/vool/statements/while_statement.rb b/lib/vool/statements/while_statement.rb index b36be95b..bfe9b1c5 100644 --- a/lib/vool/statements/while_statement.rb +++ b/lib/vool/statements/while_statement.rb @@ -3,33 +3,34 @@ require_relative "normalizer" module Vool class WhileStatement < Statement 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 @body = body end def normalize cond , rest = *normalize_name(@condition) - me = WhileStatement.new(cond , @body.normalize) - return me unless rest - rest << me - rest + WhileStatement.new(cond , @body.normalize , rest) end def to_mom( method ) merge_label = Mom::Label.new( "merge_label_#{object_id}") cond_label = Mom::Label.new( "cond_label_#{object_id}") - cond_label << Mom::TruthCheck.new(condition.slot_definition(method) , merge_label) - cond_label << @body.to_mom(method) - cond_label << Mom::Jump.new(cond_label) - cond_label << merge_label + codes = cond_label + codes << @hoisted.to_mom(method) if @hoisted + codes << Mom::TruthCheck.new(condition.slot_definition(method) , merge_label) + codes << @body.to_mom(method) + codes << Mom::Jump.new(cond_label) + codes << merge_label end def each(&block) block.call(self) block.call(@condition) + @hoisted.each(&block) if @hoisted @body.each(&block) end diff --git a/test/support/compiling.rb b/test/support/compiling.rb index 7b0b1cf6..10237b5e 100644 --- a/test/support/compiling.rb +++ b/test/support/compiling.rb @@ -52,7 +52,7 @@ module MomCompile is = is.next end 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" end ret.gsub('"' , '') diff --git a/test/vool/to_mom/test_while_condition.rb b/test/vool/to_mom/test_while_condition.rb index 6536baf2..94670423 100644 --- a/test/vool/to_mom/test_while_condition.rb +++ b/test/vool/to_mom/test_while_condition.rb @@ -18,10 +18,19 @@ module Vool assert_equal SlotDefinition , @ins.next(5).condition.class , @ins end 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 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