2014-05-05 09:02:02 +02:00
|
|
|
module Ast
|
2014-05-24 09:18:54 +02:00
|
|
|
class IfExpression < Expression
|
2014-05-05 09:02:02 +02:00
|
|
|
attr_reader :cond, :if_true, :if_false
|
|
|
|
def initialize cond, if_true, if_false
|
|
|
|
@cond, @if_true, @if_false = cond, if_true, if_false
|
|
|
|
end
|
2014-05-10 18:02:51 +02:00
|
|
|
def inspect
|
|
|
|
self.class.name + ".new(" + cond.inspect + ", "+
|
|
|
|
if_true.inspect + "," + if_false.inspect + " )"
|
|
|
|
end
|
2014-05-10 11:54:10 +02:00
|
|
|
def attributes
|
|
|
|
[:cond, :if_true, :if_false]
|
2014-05-05 09:02:02 +02:00
|
|
|
end
|
2014-05-24 09:41:57 +02:00
|
|
|
def compile context , into
|
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
|
|
|
|
false_block = into.new_block "#{into.name}_if_false"
|
|
|
|
true_block = false_block.new_block "#{into.name}_if_true"
|
|
|
|
merge_block = true_block.new_block "#{into.name}_if_merge"
|
2014-05-24 09:41:57 +02:00
|
|
|
|
|
|
|
cond_val = cond.compile(context , into)
|
|
|
|
puts "compiled if condition #{cond_val.inspect}"
|
2014-05-25 10:35:45 +02:00
|
|
|
into.b true_block , condition_code: cond_val.operator
|
2014-05-24 09:41:57 +02:00
|
|
|
|
2014-05-25 10:35:45 +02:00
|
|
|
if_false.each do |part|
|
|
|
|
last = part.compile(context , false_block )
|
|
|
|
puts "compiled in if false #{last.inspect}"
|
|
|
|
end
|
|
|
|
false_block.b merge_block
|
|
|
|
|
2014-05-24 09:41:57 +02:00
|
|
|
last = nil
|
|
|
|
if_true.each do |part|
|
2014-05-25 10:35:45 +02:00
|
|
|
last = part.compile(context , true_block )
|
2014-05-24 09:41:57 +02:00
|
|
|
puts "compiled in if true #{last.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "compile if end"
|
|
|
|
into.insert_at merge_block
|
|
|
|
|
|
|
|
return last
|
|
|
|
end
|
2014-05-05 09:02:02 +02:00
|
|
|
end
|
|
|
|
end
|