fix the if test by naming branch and label and stitching them in constructor
This commit is contained in:
parent
ba15f352db
commit
7534626d87
@ -1,28 +1,31 @@
|
|||||||
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 = Virtual::ImplicitBranch.new
|
branch_name = "if_merge_#{@@counter}".to_sym
|
||||||
|
@@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
|
||||||
label = Virtual::Label.new
|
merge = Virtual::Label.new(branch_name)
|
||||||
method.add label
|
method.add merge
|
||||||
branch.swap
|
branch.swap
|
||||||
method.current = branch
|
method.current = branch
|
||||||
if_false.each do |part|
|
if_false.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
|
||||||
method.add label
|
method.add merge
|
||||||
branch.swap
|
branch.swap
|
||||||
method.current = label
|
method.current = merge
|
||||||
#TODO should return the union of the true and false types
|
#TODO should return the union of the true and false types
|
||||||
last
|
last
|
||||||
end
|
end
|
||||||
|
@ -20,6 +20,17 @@ module Virtual
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
module Named
|
||||||
|
def initialize name , nex = nil
|
||||||
|
super(nex)
|
||||||
|
@name = name
|
||||||
|
end
|
||||||
|
attr_reader :name
|
||||||
|
def attributes
|
||||||
|
[:name ] + super
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# the first instruction we need is to stop. Off course in a real machine this would be a syscall, but that is just
|
# the first instruction we need is to stop. Off course in a real machine this would be a syscall, but that is just
|
||||||
# an implementation (in a programm it would be a function). But in a virtual machine, not only do we need this instruction,
|
# an implementation (in a programm it would be a function). But in a virtual machine, not only do we need this instruction,
|
||||||
# it is indeed the first instruction as just this instruction is the smallest possible programm for the machine.
|
# it is indeed the first instruction as just this instruction is the smallest possible programm for the machine.
|
||||||
@ -33,6 +44,7 @@ module Virtual
|
|||||||
|
|
||||||
#resolves to nothing, but allows forward definition
|
#resolves to nothing, but allows forward definition
|
||||||
class Label < Instruction
|
class Label < Instruction
|
||||||
|
include Named
|
||||||
end
|
end
|
||||||
|
|
||||||
# the next instruction represents the true branch and the other is the .... other
|
# the next instruction represents the true branch and the other is the .... other
|
||||||
@ -40,13 +52,27 @@ 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
|
||||||
def initialize nex = nil , other = nil
|
def initialize name , nex = nil , other = nil
|
||||||
super(nex)
|
super(nex)
|
||||||
|
@next = nex
|
||||||
@other = other
|
@other = other
|
||||||
|
if other
|
||||||
|
label = self.next
|
||||||
|
while label
|
||||||
|
break if label.is_a?(Label) and label.name == name
|
||||||
|
label = label.next
|
||||||
|
end
|
||||||
|
before_label = self.other
|
||||||
|
while before_label.next
|
||||||
|
break if before_label.next.is_a?(Label) and before_label.next.name == name
|
||||||
|
before_label = before_label.next
|
||||||
|
end
|
||||||
|
before_label.next = label
|
||||||
|
end
|
||||||
end
|
end
|
||||||
attr_reader :other
|
attr_reader :other , :name
|
||||||
def attributes
|
def attributes
|
||||||
[:other] + super
|
[:name , :next , :other]
|
||||||
end
|
end
|
||||||
# so code can be "poured in" in the same way as normal, we swap the braches around in after the true condition
|
# so code can be "poured in" in the same way as normal, we swap the braches around in after the true condition
|
||||||
# and swap them back after
|
# and swap them back after
|
||||||
@ -64,14 +90,7 @@ module Virtual
|
|||||||
|
|
||||||
# A note: future branch conditions include OverflowBranch and other non-c
|
# A note: future branch conditions include OverflowBranch and other non-c
|
||||||
class FrameGet < Instruction
|
class FrameGet < Instruction
|
||||||
def initialize name , nex = nil
|
include Named
|
||||||
super(nex)
|
|
||||||
@name = name
|
|
||||||
end
|
|
||||||
attr_reader :name
|
|
||||||
def attributes
|
|
||||||
[:name ] + super
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class FrameSend < Instruction
|
class FrameSend < Instruction
|
||||||
@ -110,13 +129,6 @@ module Virtual
|
|||||||
end
|
end
|
||||||
|
|
||||||
class ObjectGet < Instruction
|
class ObjectGet < Instruction
|
||||||
def initialize name , nex = nil
|
include Named
|
||||||
super(nex)
|
|
||||||
@name = name
|
|
||||||
end
|
|
||||||
attr_reader :name
|
|
||||||
def attributes
|
|
||||||
[:name] + super
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -54,7 +54,7 @@ def ofthen(n)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@output = 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(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)))))]
|
||||||
check
|
check
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user