2015-10-23 13:22:55 +02:00
|
|
|
module Soml
|
2015-09-19 17:56:18 +02:00
|
|
|
Compiler.class_eval do
|
2014-08-13 19:05:32 +02:00
|
|
|
|
2015-10-23 20:27:36 +02:00
|
|
|
# an if evaluates the condition and jumps to the true block if true
|
|
|
|
# so the else block is automatically after that.
|
|
|
|
# But then the else needs to jump over the true block unconditionally.
|
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
|
2015-05-04 13:22:22 +02:00
|
|
|
|
2015-10-22 13:50:58 +02:00
|
|
|
reset_regs
|
2015-10-23 20:27:36 +02:00
|
|
|
process(condition)
|
|
|
|
|
2015-10-19 15:22:24 +02:00
|
|
|
branch_class = Object.const_get "Register::Is#{branch_type.capitalize}"
|
2015-10-29 11:45:29 +01:00
|
|
|
true_block = Register::Label.new(if_true, "if_true")
|
2015-10-23 13:08:12 +02:00
|
|
|
add_code branch_class.new( condition , true_block )
|
2014-08-13 19:05:32 +02:00
|
|
|
|
2015-10-23 20:27:36 +02:00
|
|
|
# compile the false block
|
2015-10-22 13:50:58 +02:00
|
|
|
reset_regs
|
2015-10-23 20:27:36 +02:00
|
|
|
process_all(if_false) if if_false
|
|
|
|
merge = Register::Label.new(statement , "if_merge")
|
2015-10-29 11:45:29 +01:00
|
|
|
add_code Register::Branch.new(if_false, merge )
|
2014-08-13 19:05:32 +02:00
|
|
|
|
2015-10-23 20:27:36 +02:00
|
|
|
# compile the true block
|
|
|
|
add_code true_block
|
2015-10-22 13:50:58 +02:00
|
|
|
reset_regs
|
2015-10-23 20:27:36 +02:00
|
|
|
process_all(if_true)
|
2014-08-13 19:05:32 +02:00
|
|
|
|
2015-05-15 20:11:44 +02:00
|
|
|
#puts "compiled if: end"
|
2015-10-23 20:27:36 +02:00
|
|
|
add_code merge
|
2014-08-13 19:05:32 +02:00
|
|
|
|
2015-10-23 20:27:36 +02:00
|
|
|
nil # statements don't return anything
|
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
|