2014-06-25 01:47:59 +02:00
|
|
|
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)
|
2014-07-16 19:16:40 +02:00
|
|
|
# is.is_false(frame,method)
|
|
|
|
# TODO should/will use different branches for different conditions.
|
2014-07-16 21:28:28 +02:00
|
|
|
branch = Virtual::ImplicitBranch.new "if_merge"
|
2014-07-16 19:16:40 +02:00
|
|
|
method.add 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
|
2014-07-16 21:28:28 +02:00
|
|
|
merge = Virtual::Label.new(branch.name)
|
2014-07-16 20:16:08 +02:00
|
|
|
method.add merge
|
2014-07-16 19:16:40 +02:00
|
|
|
branch.swap
|
|
|
|
method.current = branch
|
|
|
|
if_false.each do |part|
|
2014-07-25 09:49:34 +02:00
|
|
|
last = part.compile(method,message )
|
2014-07-16 19:16:40 +02:00
|
|
|
raise part.inspect if last.nil?
|
|
|
|
end
|
2014-07-16 20:16:08 +02:00
|
|
|
method.add merge
|
2014-07-16 19:16:40 +02:00
|
|
|
branch.swap
|
2014-07-16 20:16:08 +02:00
|
|
|
method.current = merge
|
2014-07-16 19:16:40 +02:00
|
|
|
#TODO should return the union of the true and false types
|
2014-07-16 12:20:47 +02:00
|
|
|
last
|
2014-07-14 20:28:21 +02:00
|
|
|
end
|
2014-06-25 01:47:59 +02:00
|
|
|
end
|
|
|
|
end
|