diff --git a/lib/ast/if_expression.rb b/lib/ast/if_expression.rb index d58b397c..3d1a677a 100644 --- a/lib/ast/if_expression.rb +++ b/lib/ast/if_expression.rb @@ -1,21 +1,18 @@ module Ast class IfExpression < Expression # attr_reader :cond, :if_true, :if_false - @@counter = 0 #naming the braches by counting mainly to get them back together in testing def compile frame , method is = cond.compile(frame , method) # is.is_false(frame,method) # TODO should/will use different branches for different conditions. - branch_name = "if_merge_#{@@counter}".to_sym - @@counter += 1 - branch = Virtual::ImplicitBranch.new branch_name + branch = Virtual::ImplicitBranch.new "if_merge" method.add branch last = is if_true.each do |part| last = part.compile(frame,method ) raise part.inspect if last.nil? end - merge = Virtual::Label.new(branch_name) + merge = Virtual::Label.new(branch.name) method.add merge branch.swap method.current = branch diff --git a/lib/virtual/instruction.rb b/lib/virtual/instruction.rb index bd9c4615..d89a35af 100644 --- a/lib/virtual/instruction.rb +++ b/lib/virtual/instruction.rb @@ -52,9 +52,16 @@ module Virtual # this is an abstract base class (though no measures are taken to prevent instantiation) and derived # class names indicate the actual test class Branch < Instruction + @@counter = 1 #naming the braches by counting mainly to get them back together in testing + # TODO, while above sounds ok at first, it messes up with different test order fails etc, as the counter will + # be different. Need a better way to create a unique but repeatable name def initialize name , nex = nil , other = nil super(nex) - @next = nex + unless(name.to_s.split("_").last.to_i > 0) + name = "#{name}_#{@@counter}".to_sym + @@counter += 1 + end + @name = name @other = other if other label = self.next diff --git a/test/virtual/test_methods.rb b/test/virtual/test_methods.rb index be13db31..2016cd8d 100644 --- a/test/virtual/test_methods.rb +++ b/test/virtual/test_methods.rb @@ -54,7 +54,7 @@ def ofthen(n) end end HERE - @output = [Virtual::MethodDefinition.new(:ofthen,[Virtual::Argument.new(:n,Virtual::Mystery.new())],Virtual::SelfReference.new(nil),Virtual::Local.new(:maybenot,Virtual::IntegerConstant.new(667)),Virtual::MethodEnter.new(Virtual::ImplicitBranch.new(nil,Virtual::FrameSet.new(:isit,Virtual::IntegerConstant.new(42),Virtual::Label.new(:if_merge_0,nil)),Virtual::FrameSet.new(:maybenot,Virtual::IntegerConstant.new(667),Virtual::Label.new(:if_merge_0,nil)))))] + @output = [Virtual::MethodDefinition.new(:ofthen,[Virtual::Argument.new(:n,Virtual::Mystery.new())],Virtual::SelfReference.new(nil),Virtual::Local.new(:maybenot,Virtual::IntegerConstant.new(667)),Virtual::MethodEnter.new(Virtual::ImplicitBranch.new(:if_merge_1,Virtual::FrameSet.new(:isit,Virtual::IntegerConstant.new(42),Virtual::Label.new(:if_merge_1,nil)),Virtual::FrameSet.new(:maybenot,Virtual::IntegerConstant.new(667),Virtual::Label.new(:if_merge_1,nil)))))] check end