rubyx/lib/soml/compiler/while_statement.rb

31 lines
887 B
Ruby
Raw Normal View History

2015-10-23 13:22:55 +02:00
module Soml
Compiler.class_eval do
def on_while_statement statement
#puts statement.inspect
branch_type , condition , statements = *statement
2015-09-20 15:52:26 +02:00
condition = condition.first
2015-07-18 18:02:54 +02:00
# this is where the while ends and both branches meet
merge = @method.source.new_block("while merge")
2015-07-18 18:02:54 +02:00
# this comes after the current and beofre the merge
start = @method.source.new_block("while_start" )
@method.source.current start
2015-07-18 18:02:54 +02:00
2015-09-20 15:52:26 +02:00
cond = process(condition)
2015-10-19 15:37:12 +02:00
branch_class = Object.const_get "Register::Is#{branch_type.capitalize}"
add_code branch_class.new( condition , merge )
2015-07-18 18:02:54 +02:00
last = process_all(statements).last
2015-07-18 18:02:54 +02:00
# unconditionally branch to the start
add_code Register::Branch.new(statement,start)
2015-07-18 18:02:54 +02:00
# continue execution / compiling at the merge block
@method.source.current merge
last
2014-07-14 15:19:47 +02:00
end
end
end