rubyx/lib/phisol/compiler/while_statement.rb

30 lines
829 B
Ruby
Raw Normal View History

2015-10-07 14:22:47 +02:00
module Phisol
Compiler.class_eval do
def on_while statement
#puts statement.inspect
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)
@method.source.add_code Register::IsZeroBranch.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
@method.source.add_code Register::AlwaysBranch.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