2017-01-14 18:28:44 +01:00
|
|
|
module Vm
|
2016-12-09 13:29:06 +01:00
|
|
|
module WhileStatement
|
2015-05-04 13:22:22 +02:00
|
|
|
|
2016-03-07 10:55:28 +01:00
|
|
|
def on_WhileStatement statement
|
|
|
|
#branch_type , condition , statements = *statement
|
2015-09-20 15:52:26 +02:00
|
|
|
|
2016-12-09 13:29:06 +01:00
|
|
|
condition_label = compile_while_preamble( statement ) #jump there
|
2015-11-04 19:23:26 +01:00
|
|
|
|
2016-12-09 13:29:06 +01:00
|
|
|
start = compile_while_body( statement )
|
2015-07-18 18:02:54 +02:00
|
|
|
|
2015-11-04 19:23:26 +01:00
|
|
|
# This is where the loop starts, though in subsequent iterations it's in the middle
|
|
|
|
add_code condition_label
|
2016-12-09 13:29:06 +01:00
|
|
|
|
|
|
|
compile_while_condition( statement )
|
2015-09-20 15:52:26 +02:00
|
|
|
|
2016-03-07 10:55:28 +01:00
|
|
|
branch_class = Object.const_get "Register::Is#{statement.branch_type.capitalize}"
|
2015-10-23 20:27:36 +02:00
|
|
|
# this is where the while ends and both branches meet
|
2016-03-07 10:55:28 +01:00
|
|
|
add_code branch_class.new( statement.condition , start )
|
2015-07-18 18:02:54 +02:00
|
|
|
|
2015-10-23 20:27:36 +02:00
|
|
|
nil # statements don't return anything
|
2014-07-14 15:19:47 +02:00
|
|
|
end
|
2016-12-09 13:29:06 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def compile_while_preamble( statement )
|
2016-12-28 18:01:58 +01:00
|
|
|
condition_label = Register.label(statement.condition , "condition_label")
|
2016-12-09 13:29:06 +01:00
|
|
|
# unconditionally branch to the condition upon entering the loop
|
|
|
|
add_code Register::Branch.new(statement.condition , condition_label)
|
|
|
|
condition_label
|
|
|
|
end
|
|
|
|
def compile_while_body( statement )
|
2016-12-28 18:01:58 +01:00
|
|
|
start = Register.label(statement , "while_start" )
|
2016-12-09 13:29:06 +01:00
|
|
|
add_code start
|
|
|
|
reset_regs
|
|
|
|
process(statement.statements)
|
|
|
|
start
|
|
|
|
end
|
|
|
|
def compile_while_condition( statement )
|
|
|
|
reset_regs
|
|
|
|
process(statement.condition)
|
|
|
|
end
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
2015-05-04 13:22:22 +02:00
|
|
|
end
|