2015-10-23 14:22:55 +03:00
|
|
|
module Soml
|
2015-09-19 18:56:18 +03:00
|
|
|
Compiler.class_eval do
|
2014-08-13 20:05:32 +03:00
|
|
|
|
2015-10-23 21:27:36 +03: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 18:06:00 +03:00
|
|
|
def on_if_statement statement
|
2015-10-19 15:31:48 +03:00
|
|
|
branch_type , condition , if_true , if_false = *statement
|
2015-09-19 17:57:44 +03:00
|
|
|
condition = condition.first
|
2015-05-04 14:22:22 +03:00
|
|
|
|
2015-10-22 14:50:58 +03:00
|
|
|
reset_regs
|
2015-10-23 21:27:36 +03:00
|
|
|
process(condition)
|
|
|
|
|
2015-10-19 16:22:24 +03:00
|
|
|
branch_class = Object.const_get "Register::Is#{branch_type.capitalize}"
|
2015-10-29 12:45:29 +02:00
|
|
|
true_block = Register::Label.new(if_true, "if_true")
|
2015-10-23 14:08:12 +03:00
|
|
|
add_code branch_class.new( condition , true_block )
|
2014-08-13 20:05:32 +03:00
|
|
|
|
2015-10-23 21:27:36 +03:00
|
|
|
# compile the false block
|
2015-10-22 14:50:58 +03:00
|
|
|
reset_regs
|
2015-10-23 21:27:36 +03:00
|
|
|
process_all(if_false) if if_false
|
|
|
|
merge = Register::Label.new(statement , "if_merge")
|
2015-10-29 12:45:29 +02:00
|
|
|
add_code Register::Branch.new(if_false, merge )
|
2014-08-13 20:05:32 +03:00
|
|
|
|
2015-10-23 21:27:36 +03:00
|
|
|
# compile the true block
|
|
|
|
add_code true_block
|
2015-10-22 14:50:58 +03:00
|
|
|
reset_regs
|
2015-10-23 21:27:36 +03:00
|
|
|
process_all(if_true)
|
2014-08-13 20:05:32 +03:00
|
|
|
|
2015-05-15 21:11:44 +03:00
|
|
|
#puts "compiled if: end"
|
2015-10-23 21:27:36 +03:00
|
|
|
add_code merge
|
2014-08-13 20:05:32 +03:00
|
|
|
|
2015-10-23 21:27:36 +03:00
|
|
|
nil # statements don't return anything
|
2014-07-14 21:28:21 +03:00
|
|
|
end
|
2015-05-08 15:10:30 +03:00
|
|
|
end
|
2015-05-04 14:22:22 +03:00
|
|
|
end
|