aa79e41d1c
seems to fit the layer much better as we really have a very reduced instruction set
42 lines
1.2 KiB
Ruby
42 lines
1.2 KiB
Ruby
module Vm
|
|
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 )
|
|
# branch_type , condition , if_true , if_false = *statement
|
|
|
|
true_block = compile_if_condition( statement )
|
|
merge = compile_if_false( statement )
|
|
add_code true_block
|
|
compile_if_true(statement)
|
|
add_code merge
|
|
nil # statements don't return anything
|
|
end
|
|
|
|
private
|
|
|
|
def compile_if_condition( statement )
|
|
reset_regs
|
|
process(statement.condition)
|
|
branch_class = Object.const_get "Risc::Is#{statement.branch_type.capitalize}"
|
|
true_block = Risc.label(statement, "if_true")
|
|
add_code branch_class.new( statement.condition , true_block )
|
|
return true_block
|
|
end
|
|
def compile_if_true( statement )
|
|
reset_regs
|
|
process(statement.if_true)
|
|
end
|
|
|
|
def compile_if_false( statement )
|
|
reset_regs
|
|
process(statement.if_false) if statement.if_false.statements
|
|
merge = Risc.label(statement , "if_merge")
|
|
add_code Risc::Branch.new(statement.if_false, merge )
|
|
merge
|
|
end
|
|
end
|
|
end
|