move the code insertion functionality up to function. makes more sense. block still carries code though
This commit is contained in:
@ -1,32 +1,36 @@
|
||||
module Ast
|
||||
class IfExpression < Expression
|
||||
# attr_reader :cond, :if_true, :if_false
|
||||
def compile context , into
|
||||
def compile context
|
||||
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
|
||||
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"
|
||||
false_block = f.new_block "if_false"
|
||||
true_block = f.new_block "if_true"
|
||||
merge_block = f.new_block "if_merge"
|
||||
|
||||
puts "compiling if condition #{cond}"
|
||||
cond_val = cond.compile(context , into)
|
||||
into.b true_block , condition_code: cond_val.operator
|
||||
into.branch = true_block
|
||||
cond_val = cond.compile(context)
|
||||
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 , false_block )
|
||||
last = part.compile(context )
|
||||
end
|
||||
false_block.b merge_block
|
||||
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 , true_block )
|
||||
last = part.compile(context )
|
||||
end
|
||||
|
||||
puts "compiled if: end"
|
||||
into.insert_at merge_block
|
||||
f.insert_at merge_block
|
||||
|
||||
return last
|
||||
end
|
||||
|
Reference in New Issue
Block a user