rubyx/lib/vool/statements/while_statement.rb

39 lines
1014 B
Ruby
Raw Normal View History

require_relative "normalizer"
2017-04-01 20:28:57 +02:00
module Vool
class WhileStatement < Statement
include Normalizer
attr_reader :condition , :body , :hoisted
2017-04-03 10:49:21 +02:00
def initialize( condition , body , hoisted = nil)
@hoisted = hoisted
2017-04-03 10:49:21 +02:00
@condition = condition
@body = body
2017-04-03 10:49:21 +02:00
end
def normalize
2018-03-16 14:11:17 +01:00
cond , rest = *normalize_name(@condition)
WhileStatement.new(cond , @body.normalize , rest)
end
def to_mom( method )
merge_label = Mom::Label.new( "merge_label_#{object_id.to_s(16)}")
cond_label = Mom::Label.new( "cond_label_#{object_id.to_s(16)}")
codes = cond_label
codes << @hoisted.to_mom(method) if @hoisted
codes << Mom::TruthCheck.new(condition.slot_definition(method) , merge_label)
codes << @body.to_mom(method)
codes << Mom::Jump.new(cond_label)
codes << merge_label
end
def each(&block)
block.call(self)
block.call(@condition)
@hoisted.each(&block) if @hoisted
@body.each(&block)
2017-04-08 11:10:42 +02:00
end
2017-04-01 20:28:57 +02:00
end
end