2015-10-07 14:22:47 +02:00
|
|
|
module Phisol
|
2015-09-19 17:56:18 +02:00
|
|
|
Compiler.class_eval do
|
2015-05-04 13:22:22 +02:00
|
|
|
# if - attr_reader :cond, :if_true, :if_false
|
2014-08-13 19:05:32 +02:00
|
|
|
|
2015-10-09 17:06:00 +02:00
|
|
|
def on_if_statement statement
|
2015-10-19 14:31:48 +02:00
|
|
|
branch_type , condition , if_true , if_false = *statement
|
2015-09-19 16:57:44 +02:00
|
|
|
condition = condition.first
|
2014-08-13 19:05:32 +02:00
|
|
|
# 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-10-05 23:27:13 +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
|
2015-05-04 13:22:22 +02:00
|
|
|
|
2015-10-22 13:50:58 +02:00
|
|
|
reset_regs
|
2015-10-07 09:02:51 +02:00
|
|
|
is = process(condition)
|
2015-10-19 15:22:24 +02:00
|
|
|
branch_class = Object.const_get "Register::Is#{branch_type.capitalize}"
|
|
|
|
@method.source.add_code branch_class.new( condition , true_block )
|
2014-08-13 19:05:32 +02:00
|
|
|
|
|
|
|
# compile the true block (as we think of it first, even it is second in sequential order)
|
2015-10-05 23:27:13 +02:00
|
|
|
@method.source.current true_block
|
2015-09-20 16:33:05 +02:00
|
|
|
|
2015-10-22 13:50:58 +02:00
|
|
|
reset_regs
|
2015-09-19 17:56:18 +02:00
|
|
|
last = process_all(if_true).last
|
2014-08-13 19:05:32 +02:00
|
|
|
|
|
|
|
# compile the false block
|
2015-10-05 23:27:13 +02:00
|
|
|
@method.source.current false_block
|
2015-10-22 13:50:58 +02:00
|
|
|
reset_regs
|
2015-09-20 16:33:05 +02:00
|
|
|
last = process_all(if_false).last if if_false
|
2015-10-19 15:08:00 +02:00
|
|
|
@method.source.add_code Register::Branch.new(statement, merge_block )
|
2014-08-13 19:05:32 +02:00
|
|
|
|
2015-05-15 20:11:44 +02:00
|
|
|
#puts "compiled if: end"
|
2015-10-05 23:27:13 +02:00
|
|
|
@method.source.current merge_block
|
2014-08-13 19:05:32 +02:00
|
|
|
|
2014-07-16 19:16:40 +02:00
|
|
|
#TODO should return the union of the true and false types
|
2014-07-16 12:20:47 +02:00
|
|
|
last
|
2014-07-14 20:28:21 +02:00
|
|
|
end
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
2015-05-04 13:22:22 +02:00
|
|
|
end
|