redoing the ifs with new (old) block structure

This commit is contained in:
Torsten Ruger
2014-08-13 20:05:32 +03:00
parent 90cbeddc0a
commit 7e60827dd3
7 changed files with 37 additions and 29 deletions

View File

@ -48,7 +48,7 @@ module Ast
call.do_call into
after = into.new_block("#{name}#{@@counter+=1}")
into.insert_at after
into.current after
into.pop([]) unless current_function.nil?
function.return_type
end

View File

@ -1,28 +1,40 @@
module Ast
class IfExpression < Expression
# attr_reader :cond, :if_true, :if_false
def compile method , message
# 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
is = cond.compile(method,message)
# is.is_false(frame,method)
# TODO should/will use different branches for different conditions.
branch = Virtual::ImplicitBranch.new "if_merge"
method.add_code branch
# just a scetch : cond_val = cond_val.is_true?(method) unless cond_val.is_a? Virtual::BranchCondition
method.add_code Virtual::ImplicitBranch.new( true_block )
# compile the true block (as we think of it first, even it is second in sequential order)
method.current true_block
last = is
if_true.each do |part|
last = part.compile(method,message )
raise part.inspect if last.nil?
end
merge = Virtual::Label.new(branch.name)
method.add_code merge
branch.swap
method.current = branch
# compile the false block
method.current false_block
if_false.each do |part|
last = part.compile(method,message )
puts "compiling in if false #{part}"
last = part.compile(method,message)
raise part.inspect if last.nil?
end
method.add_code merge
branch.swap
method.current = merge
method.add_code Virtual::UnconditionalBranch.new( merge_block )
puts "compiled if: end"
method.current merge_block
#TODO should return the union of the true and false types
last
end