rubyx/lib/ast/if_expression.rb

30 lines
914 B
Ruby
Raw Normal View History

module Ast
class IfExpression < Expression
# attr_reader :cond, :if_true, :if_false
2014-07-25 09:49:34 +02:00
def compile method , message
is = cond.compile(method,message)
# is.is_false(frame,method)
# TODO should/will use different branches for different conditions.
branch = Virtual::ImplicitBranch.new "if_merge"
method.add_code branch
2014-07-16 12:20:47 +02:00
last = is
if_true.each do |part|
2014-07-25 09:49:34 +02:00
last = part.compile(method,message )
2014-07-16 12:20:47 +02:00
raise part.inspect if last.nil?
end
merge = Virtual::Label.new(branch.name)
method.add_code merge
branch.swap
method.current = branch
if_false.each do |part|
2014-07-25 09:49:34 +02:00
last = part.compile(method,message )
raise part.inspect if last.nil?
end
method.add_code merge
branch.swap
method.current = merge
#TODO should return the union of the true and false types
2014-07-16 12:20:47 +02:00
last
end
end
end