move branch counter to branch instruction (from if) in preperation of while

This commit is contained in:
Torsten Ruger 2014-07-16 22:28:28 +03:00
parent 7534626d87
commit efeb910843
3 changed files with 11 additions and 7 deletions

View File

@ -1,21 +1,18 @@
module Ast module Ast
class IfExpression < Expression class IfExpression < Expression
# attr_reader :cond, :if_true, :if_false # 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 def compile frame , method
is = cond.compile(frame , method) is = cond.compile(frame , method)
# is.is_false(frame,method) # is.is_false(frame,method)
# TODO should/will use different branches for different conditions. # TODO should/will use different branches for different conditions.
branch_name = "if_merge_#{@@counter}".to_sym branch = Virtual::ImplicitBranch.new "if_merge"
@@counter += 1
branch = Virtual::ImplicitBranch.new branch_name
method.add branch method.add branch
last = is last = is
if_true.each do |part| if_true.each do |part|
last = part.compile(frame,method ) last = part.compile(frame,method )
raise part.inspect if last.nil? raise part.inspect if last.nil?
end end
merge = Virtual::Label.new(branch_name) merge = Virtual::Label.new(branch.name)
method.add merge method.add merge
branch.swap branch.swap
method.current = branch method.current = branch

View File

@ -52,9 +52,16 @@ module Virtual
# this is an abstract base class (though no measures are taken to prevent instantiation) and derived # this is an abstract base class (though no measures are taken to prevent instantiation) and derived
# class names indicate the actual test # class names indicate the actual test
class Branch < Instruction 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 def initialize name , nex = nil , other = nil
super(nex) 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 @other = other
if other if other
label = self.next label = self.next

View File

@ -54,7 +54,7 @@ def ofthen(n)
end end
end end
HERE 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 check
end end