rubyx/lib/bosl/compiler/if_expression.rb

39 lines
1.5 KiB
Ruby
Raw Normal View History

module Bosl
Compiler.class_eval do
# if - attr_reader :cond, :if_true, :if_false
def on_if expression
2015-09-19 16:57:44 +02:00
condition , if_true , if_false = *expression
condition = condition.first
# to execute the logic as the if states it, the blocks are the other way around
2015-05-30 11:20:39 +02:00
# so we can the jump over the else if true ,
# and the else joins unconditionally after the true_block
2015-07-03 19:13:03 +02:00
merge_block = method.source.new_block "if_merge" # last one, created first
true_block = method.source.new_block "if_true" # second, linked in after current, before merge
false_block = method.source.new_block "if_false" # directly next in order, ie if we don't jump we land here
is = process(condition )
# TODO should/will use different branches for different conditions.
# just a scetch : cond_val = cond_val.is_true?(method) unless cond_val.is_a? BranchCondition
2015-09-19 16:57:44 +02:00
method.source.add_code Virtual::IsTrueBranch.new( true_block )
# compile the true block (as we think of it first, even it is second in sequential order)
2015-07-03 19:13:03 +02:00
method.source.current true_block
2015-09-20 16:33:05 +02:00
last = process_all(if_true).last
# compile the false block
2015-07-03 19:13:03 +02:00
method.source.current false_block
2015-09-20 16:33:05 +02:00
last = process_all(if_false).last if if_false
2015-09-19 16:57:44 +02:00
method.source.add_code Virtual::UnconditionalBranch.new( merge_block )
2015-05-15 20:11:44 +02:00
#puts "compiled if: end"
2015-07-03 19:13:03 +02:00
method.source.current merge_block
#TODO should return the union of the true and false types
2014-07-16 12:20:47 +02:00
last
end
end
end