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.
|
2016-03-07 11:55:28 +02:00
|
|
|
def on_IfStatement statement
|
|
|
|
# branch_type , condition , if_true , if_false = *statement
|
|
|
|
# condition = condition.first
|
2015-05-04 14:22:22 +03:00
|
|
|
|
2015-10-22 14:50:58 +03:00
|
|
|
reset_regs
|
2016-03-07 11:55:28 +02:00
|
|
|
process(statement.condition)
|
2015-10-23 21:27:36 +03:00
|
|
|
|
2016-03-07 11:55:28 +02:00
|
|
|
branch_class = Object.const_get "Register::Is#{statement.branch_type.capitalize}"
|
|
|
|
true_block = Register::Label.new(statement, "if_true")
|
|
|
|
add_code branch_class.new( statement.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
|
2016-03-07 11:55:28 +02:00
|
|
|
process(statement.if_false) if statement.if_false.statements
|
2015-10-23 21:27:36 +03:00
|
|
|
merge = Register::Label.new(statement , "if_merge")
|
2016-03-07 11:55:28 +02:00
|
|
|
add_code Register::Branch.new(statement.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
|
2016-03-07 11:55:28 +02:00
|
|
|
process(statement.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
|