2014-06-25 01:47:59 +02:00
|
|
|
module Ast
|
|
|
|
class IfExpression < Expression
|
|
|
|
# attr_reader :cond, :if_true, :if_false
|
2014-08-13 19:05:32 +02:00
|
|
|
|
2014-07-25 09:49:34 +02:00
|
|
|
def compile method , message
|
2014-08-13 19:05:32 +02:00
|
|
|
# to execute the logic as the if states it, the blocks are the other way around
|
|
|
|
# so we can the jump over the else if true ,and the else joins unconditionally after the true_block
|
|
|
|
merge_block = method.new_block "if_merge" # last one, created first
|
|
|
|
true_block = method.new_block "if_true" # second, linked in after current, before merge
|
|
|
|
false_block = method.new_block "if_false" # directly next in order, ie if we don't jump we land here
|
|
|
|
|
|
|
|
|
2014-07-25 09:49:34 +02:00
|
|
|
is = cond.compile(method,message)
|
2014-07-16 19:16:40 +02:00
|
|
|
# TODO should/will use different branches for different conditions.
|
2014-08-13 19:05:32 +02:00
|
|
|
# just a scetch : cond_val = cond_val.is_true?(method) unless cond_val.is_a? Virtual::BranchCondition
|
2014-10-18 12:53:01 +02:00
|
|
|
method.add_code Virtual::IsTrueBranch.new( true_block )
|
2014-08-13 19:05:32 +02:00
|
|
|
|
|
|
|
# compile the true block (as we think of it first, even it is second in sequential order)
|
|
|
|
method.current true_block
|
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-08-13 19:05:32 +02:00
|
|
|
|
|
|
|
# compile the false block
|
|
|
|
method.current false_block
|
2014-07-16 19:16:40 +02:00
|
|
|
if_false.each do |part|
|
2014-08-13 19:05:32 +02:00
|
|
|
puts "compiling in if false #{part}"
|
|
|
|
last = part.compile(method,message)
|
2014-07-16 19:16:40 +02:00
|
|
|
raise part.inspect if last.nil?
|
|
|
|
end
|
2014-08-13 19:05:32 +02:00
|
|
|
method.add_code Virtual::UnconditionalBranch.new( merge_block )
|
|
|
|
|
|
|
|
puts "compiled if: end"
|
|
|
|
method.current merge_block
|
|
|
|
|
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
|