2014-06-25 01:47:59 +02:00
|
|
|
module Ast
|
|
|
|
class IfExpression < Expression
|
|
|
|
# attr_reader :cond, :if_true, :if_false
|
2014-07-14 15:19:47 +02:00
|
|
|
def compile frame , method
|
2014-07-16 12:20:47 +02:00
|
|
|
is = cond.compile(frame , method)
|
|
|
|
# is.is_false(frame,method)
|
|
|
|
last = is
|
|
|
|
if_true.each do |part|
|
|
|
|
last = part.compile(frame,method )
|
|
|
|
raise part.inspect if last.nil?
|
|
|
|
end
|
|
|
|
last
|
2014-07-14 20:28:21 +02:00
|
|
|
end
|
|
|
|
def old
|
2014-06-25 01:47:59 +02:00
|
|
|
f = context.function
|
|
|
|
# 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 = f.new_block "if_merge"
|
|
|
|
true_block = f.new_block "if_true"
|
|
|
|
false_block = f.new_block "if_false"
|
|
|
|
|
|
|
|
puts "compiling if condition #{cond}"
|
|
|
|
cond_val = cond.compile(context)
|
2014-06-26 16:52:15 +02:00
|
|
|
unless cond_val.is_a? Virtual::BranchCondition
|
2014-06-25 01:47:59 +02:00
|
|
|
cond_val = cond_val.is_true? f
|
|
|
|
end
|
|
|
|
f.b true_block , condition_code: cond_val.operator
|
|
|
|
f.insertion_point.branch = true_block
|
|
|
|
|
|
|
|
f.insert_at false_block
|
|
|
|
if_false.each do |part|
|
|
|
|
puts "compiling in if false #{part}"
|
|
|
|
last = part.compile(context )
|
|
|
|
end
|
|
|
|
f.b merge_block
|
|
|
|
f.insertion_point.branch = false_block
|
|
|
|
|
|
|
|
f.insert_at true_block
|
|
|
|
last = nil
|
|
|
|
if_true.each do |part|
|
|
|
|
puts "compiling in if true #{part}"
|
|
|
|
last = part.compile(context )
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "compiled if: end"
|
|
|
|
f.insert_at merge_block
|
|
|
|
|
|
|
|
return last
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|