refactor if_statement

This commit is contained in:
Torsten Ruger
2016-12-09 13:38:07 +02:00
parent 1e7093c63f
commit fa14e35a50
4 changed files with 27 additions and 20 deletions

View File

@@ -1,35 +1,41 @@
module Typed
Compiler.class_eval do
module IfStatement
# 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_IfStatement statement
def on_IfStatement( statement )
# branch_type , condition , if_true , if_false = *statement
# condition = condition.first
true_block = compile_condition( statement )
merge = compile_false( statement )
add_code true_block
compile_true(statement)
add_code merge
nil # statements don't return anything
end
private
def compile_condition( statement )
reset_regs
process(statement.condition)
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 )
return true_block
end
def compile_true( statement )
reset_regs
process(statement.if_true)
end
# compile the false block
def compile_false( statement )
reset_regs
process(statement.if_false) if statement.if_false.statements
merge = Register::Label.new(statement , "if_merge")
add_code Register::Branch.new(statement.if_false, merge )
# compile the true block
add_code true_block
reset_regs
process(statement.if_true)
#puts "compiled if: end"
add_code merge
nil # statements don't return anything
merge
end
end
end