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