2015-10-23 14:22:55 +03:00
|
|
|
module Soml
|
2015-09-19 18:56:18 +03:00
|
|
|
Compiler.class_eval do
|
2015-05-04 14:22:22 +03:00
|
|
|
|
2015-10-09 18:06:00 +03:00
|
|
|
def on_while_statement statement
|
2015-10-09 17:51:14 +03:00
|
|
|
#puts statement.inspect
|
2015-10-19 15:31:48 +03:00
|
|
|
branch_type , condition , statements = *statement
|
2015-09-20 16:52:26 +03:00
|
|
|
condition = condition.first
|
|
|
|
|
2015-11-04 20:23:26 +02:00
|
|
|
condition_label = Register::Label.new(statement , "condition_label")
|
|
|
|
# unconditionally branch to the condition upon entering the loop
|
|
|
|
add_code Register::Branch.new(statement,condition_label)
|
|
|
|
|
2015-10-23 21:27:36 +03:00
|
|
|
add_code start = Register::Label.new(statement , "while_start" )
|
2015-11-04 20:23:26 +02:00
|
|
|
reset_regs
|
|
|
|
process_all(statements)
|
2015-07-18 19:02:54 +03:00
|
|
|
|
2015-11-04 20:23:26 +02:00
|
|
|
# This is where the loop starts, though in subsequent iterations it's in the middle
|
|
|
|
add_code condition_label
|
2015-10-23 21:27:36 +03:00
|
|
|
reset_regs
|
|
|
|
process(condition)
|
2015-09-20 16:52:26 +03:00
|
|
|
|
2015-10-19 16:37:12 +03:00
|
|
|
branch_class = Object.const_get "Register::Is#{branch_type.capitalize}"
|
2015-10-23 21:27:36 +03:00
|
|
|
# this is where the while ends and both branches meet
|
2015-11-04 20:23:26 +02:00
|
|
|
add_code branch_class.new( condition , start )
|
2015-07-18 19:02:54 +03:00
|
|
|
|
2015-10-23 21:27:36 +03:00
|
|
|
nil # statements don't return anything
|
2014-07-14 16:19:47 +03:00
|
|
|
end
|
2015-05-08 15:10:30 +03:00
|
|
|
end
|
2015-05-04 14:22:22 +03:00
|
|
|
end
|