2014-05-05 09:02:02 +02:00
|
|
|
module Ast
|
2014-05-24 09:18:54 +02:00
|
|
|
class IfExpression < Expression
|
2014-06-04 21:03:45 +02:00
|
|
|
# attr_reader :cond, :if_true, :if_false
|
2014-06-10 22:57:56 +02:00
|
|
|
def compile context
|
|
|
|
f = context.function
|
2014-05-25 10:35:45 +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
|
2014-06-10 22:57:56 +02:00
|
|
|
merge_block = f.new_block "if_merge"
|
2014-06-12 07:34:46 +02:00
|
|
|
true_block = f.new_block "if_true"
|
|
|
|
false_block = f.new_block "if_false"
|
2014-05-24 09:41:57 +02:00
|
|
|
|
2014-06-02 14:11:48 +02:00
|
|
|
puts "compiling if condition #{cond}"
|
2014-06-10 22:57:56 +02:00
|
|
|
cond_val = cond.compile(context)
|
2014-06-24 11:22:32 +02:00
|
|
|
unless cond_val.is_a? Vm::BranchCondition
|
|
|
|
cond_val = cond_val.is_true? f
|
|
|
|
end
|
2014-06-10 22:57:56 +02:00
|
|
|
f.b true_block , condition_code: cond_val.operator
|
|
|
|
f.insertion_point.branch = true_block
|
2014-06-09 18:24:09 +02:00
|
|
|
|
2014-06-10 22:57:56 +02:00
|
|
|
f.insert_at false_block
|
2014-05-25 10:35:45 +02:00
|
|
|
if_false.each do |part|
|
2014-06-02 14:11:48 +02:00
|
|
|
puts "compiling in if false #{part}"
|
2014-06-10 22:57:56 +02:00
|
|
|
last = part.compile(context )
|
2014-05-25 10:35:45 +02:00
|
|
|
end
|
2014-06-10 22:57:56 +02:00
|
|
|
f.b merge_block
|
|
|
|
f.insertion_point.branch = false_block
|
2014-05-25 10:35:45 +02:00
|
|
|
|
2014-06-10 22:57:56 +02:00
|
|
|
f.insert_at true_block
|
2014-05-24 09:41:57 +02:00
|
|
|
last = nil
|
|
|
|
if_true.each do |part|
|
2014-06-02 14:11:48 +02:00
|
|
|
puts "compiling in if true #{part}"
|
2014-06-10 22:57:56 +02:00
|
|
|
last = part.compile(context )
|
2014-05-24 09:41:57 +02:00
|
|
|
end
|
|
|
|
|
2014-06-02 14:11:48 +02:00
|
|
|
puts "compiled if: end"
|
2014-06-10 22:57:56 +02:00
|
|
|
f.insert_at merge_block
|
2014-05-24 09:41:57 +02:00
|
|
|
|
|
|
|
return last
|
|
|
|
end
|
2014-05-05 09:02:02 +02:00
|
|
|
end
|
|
|
|
end
|