2015-10-07 15:22:47 +03:00
|
|
|
module Phisol
|
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
|
|
|
|
condition , statements = *statement
|
2015-09-20 16:52:26 +03:00
|
|
|
condition = condition.first
|
|
|
|
|
2015-07-18 19:02:54 +03:00
|
|
|
# this is where the while ends and both branches meet
|
2015-10-06 00:27:13 +03:00
|
|
|
merge = @method.source.new_block("while merge")
|
2015-07-18 19:02:54 +03:00
|
|
|
# this comes after the current and beofre the merge
|
2015-10-06 00:27:13 +03:00
|
|
|
start = @method.source.new_block("while_start" )
|
|
|
|
@method.source.current start
|
2015-07-18 19:02:54 +03:00
|
|
|
|
2015-09-20 16:52:26 +03:00
|
|
|
cond = process(condition)
|
|
|
|
|
2015-10-07 10:02:51 +03:00
|
|
|
@method.source.add_code Register::IsZeroBranch.new(condition,merge)
|
2015-07-18 19:02:54 +03:00
|
|
|
|
2015-10-09 17:51:14 +03:00
|
|
|
last = process_all(statements).last
|
2015-07-18 19:02:54 +03:00
|
|
|
|
|
|
|
# unconditionally branch to the start
|
2015-10-09 17:51:14 +03:00
|
|
|
@method.source.add_code Register::AlwaysBranch.new(statement,start)
|
2015-07-18 19:02:54 +03:00
|
|
|
|
|
|
|
# continue execution / compiling at the merge block
|
2015-10-06 00:27:13 +03:00
|
|
|
@method.source.current merge
|
2014-07-17 16:46:17 +03:00
|
|
|
last
|
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
|