rubyx/lib/soml/compiler/if_statement.rb

36 lines
1.0 KiB
Ruby
Raw Normal View History

2015-10-23 13:22:55 +02:00
module Soml
Compiler.class_eval do
# 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.
def on_if_statement statement
branch_type , condition , if_true , if_false = *statement
2015-09-19 16:57:44 +02:00
condition = condition.first
reset_regs
process(condition)
2015-10-19 15:22:24 +02:00
branch_class = Object.const_get "Register::Is#{branch_type.capitalize}"
true_block = Register::Label.new(statement, "if_true")
add_code branch_class.new( condition , true_block )
# compile the false block
reset_regs
process_all(if_false) if if_false
merge = Register::Label.new(statement , "if_merge")
add_code Register::Branch.new(statement, merge )
# compile the true block
add_code true_block
reset_regs
process_all(if_true)
2015-05-15 20:11:44 +02:00
#puts "compiled if: end"
add_code merge
nil # statements don't return anything
end
end
end