2017-01-14 18:28:44 +01:00
|
|
|
module Vm
|
2016-12-09 12:38:07 +01:00
|
|
|
module IfStatement
|
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-12-09 12:38:07 +01:00
|
|
|
def on_IfStatement( statement )
|
2016-03-07 10:55:28 +01:00
|
|
|
# branch_type , condition , if_true , if_false = *statement
|
2015-05-04 13:22:22 +02:00
|
|
|
|
2016-12-09 13:29:06 +01:00
|
|
|
true_block = compile_if_condition( statement )
|
|
|
|
merge = compile_if_false( statement )
|
2016-12-09 12:38:07 +01:00
|
|
|
add_code true_block
|
2016-12-09 13:29:06 +01:00
|
|
|
compile_if_true(statement)
|
2016-12-09 12:38:07 +01:00
|
|
|
add_code merge
|
|
|
|
nil # statements don't return anything
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-12-09 13:29:06 +01:00
|
|
|
def compile_if_condition( statement )
|
2015-10-22 13:50:58 +02:00
|
|
|
reset_regs
|
2016-03-07 10:55:28 +01:00
|
|
|
process(statement.condition)
|
|
|
|
branch_class = Object.const_get "Register::Is#{statement.branch_type.capitalize}"
|
2016-12-28 18:01:58 +01:00
|
|
|
true_block = Register.label(statement, "if_true")
|
2016-03-07 10:55:28 +01:00
|
|
|
add_code branch_class.new( statement.condition , true_block )
|
2016-12-09 12:38:07 +01:00
|
|
|
return true_block
|
|
|
|
end
|
2016-12-09 13:29:06 +01:00
|
|
|
def compile_if_true( statement )
|
2016-12-09 12:38:07 +01:00
|
|
|
reset_regs
|
|
|
|
process(statement.if_true)
|
|
|
|
end
|
2014-08-13 19:05:32 +02:00
|
|
|
|
2016-12-09 13:29:06 +01:00
|
|
|
def compile_if_false( statement )
|
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
|
2016-12-28 18:01:58 +01:00
|
|
|
merge = Register.label(statement , "if_merge")
|
2016-03-07 10:55:28 +01:00
|
|
|
add_code Register::Branch.new(statement.if_false, merge )
|
2016-12-09 12:38:07 +01:00
|
|
|
merge
|
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
|