2015-10-23 13:22:55 +02:00
|
|
|
module Soml
|
2015-09-19 17:56:18 +02:00
|
|
|
Compiler.class_eval do
|
2015-05-04 13:22:22 +02:00
|
|
|
|
2015-10-09 17:06:00 +02:00
|
|
|
def on_while_statement statement
|
2015-10-09 16:51:14 +02:00
|
|
|
#puts statement.inspect
|
2015-10-19 14:31:48 +02:00
|
|
|
branch_type , condition , statements = *statement
|
2015-09-20 15:52:26 +02:00
|
|
|
condition = condition.first
|
|
|
|
|
2015-10-23 20:27:36 +02:00
|
|
|
add_code start = Register::Label.new(statement , "while_start" )
|
2015-07-18 18:02:54 +02:00
|
|
|
|
2015-10-23 20:27:36 +02:00
|
|
|
reset_regs
|
|
|
|
process(condition)
|
2015-09-20 15:52:26 +02:00
|
|
|
|
2015-10-19 15:37:12 +02:00
|
|
|
branch_class = Object.const_get "Register::Is#{branch_type.capitalize}"
|
2015-10-23 20:27:36 +02:00
|
|
|
# this is where the while ends and both branches meet
|
|
|
|
merge = Register::Label.new(statement , "while_merge")
|
2015-10-23 13:08:12 +02:00
|
|
|
add_code branch_class.new( condition , merge )
|
2015-07-18 18:02:54 +02:00
|
|
|
|
2015-10-23 20:27:36 +02:00
|
|
|
reset_regs
|
|
|
|
process_all(statements)
|
2015-07-18 18:02:54 +02:00
|
|
|
|
|
|
|
# unconditionally branch to the start
|
2015-10-23 13:08:12 +02:00
|
|
|
add_code Register::Branch.new(statement,start)
|
2015-07-18 18:02:54 +02:00
|
|
|
|
|
|
|
# continue execution / compiling at the merge block
|
2015-10-23 20:27:36 +02:00
|
|
|
add_code merge
|
|
|
|
nil # statements don't return anything
|
2014-07-14 15:19:47 +02:00
|
|
|
end
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
2015-05-04 13:22:22 +02:00
|
|
|
end
|