rubyx/lib/soml/compiler/while_statement.rb

29 lines
967 B
Ruby
Raw Normal View History

2015-10-23 13:22:55 +02:00
module Soml
Compiler.class_eval do
def on_WhileStatement statement
#puts statement.inspect
#branch_type , condition , statements = *statement
2015-09-20 15:52:26 +02:00
condition_label = Register::Label.new(statement.condition , "condition_label")
# unconditionally branch to the condition upon entering the loop
add_code Register::Branch.new(statement.condition,condition_label)
add_code start = Register::Label.new(statement , "while_start" )
reset_regs
process(statement.statements)
2015-07-18 18:02:54 +02:00
# This is where the loop starts, though in subsequent iterations it's in the middle
add_code condition_label
reset_regs
process(statement.condition)
2015-09-20 15:52:26 +02:00
branch_class = Object.const_get "Register::Is#{statement.branch_type.capitalize}"
# this is where the while ends and both branches meet
add_code branch_class.new( statement.condition , start )
2015-07-18 18:02:54 +02:00
nil # statements don't return anything
2014-07-14 15:19:47 +02:00
end
end
end