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.
|
2016-03-07 10:55:28 +01:00
|
|
|
def on_IfStatement statement
|
|
|
|
# branch_type , condition , if_true , if_false = *statement
|
|
|
|
# condition = condition.first
|
2015-05-04 13:22:22 +02:00
|
|
|
|
2015-10-22 13:50:58 +02:00
|
|
|
reset_regs
|
2016-03-07 10:55:28 +01:00
|
|
|
process(statement.condition)
|
2015-10-23 20:27:36 +02:00
|
|
|
|
2016-03-07 10:55:28 +01: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 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
|
2016-03-07 10:55:28 +01:00
|
|
|
process(statement.if_false) if statement.if_false.statements
|
2015-10-23 20:27:36 +02:00
|
|
|
merge = Register::Label.new(statement , "if_merge")
|
2016-03-07 10:55:28 +01:00
|
|
|
add_code Register::Branch.new(statement.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
|
2016-03-07 10:55:28 +01:00
|
|
|
process(statement.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
|