rubyx/lib/vool/while_statement.rb

39 lines
1.1 KiB
Ruby
Raw Normal View History

2017-04-01 20:28:57 +02:00
module Vool
class WhileStatement < Statement
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 to_mom( compiler )
merge_label = Mom::Label.new(self, "merge_label_#{object_id.to_s(16)}")
cond_label = Mom::Label.new(self, "cond_label_#{object_id.to_s(16)}")
codes = cond_label
codes << @hoisted.to_mom(compiler) if @hoisted
codes << @condition.to_mom(compiler) if @condition.is_a?(SendStatement)
codes << Mom::TruthCheck.new(condition.to_slot(compiler) , merge_label)
codes << @body.to_mom(compiler)
codes << Mom::Jump.new(cond_label)
codes << merge_label
end
def each(&block)
block.call(self)
@condition.each(&block)
@hoisted.each(&block) if @hoisted
@body.each(&block)
2017-04-08 11:10:42 +02:00
end
2018-07-03 21:18:19 +02:00
def to_s(depth = 0)
lines =[ "while (#{@condition})" , @body.to_s(1) , "end"]
lines.unshift( @hoisted.to_s) if @hoisted
at_depth(depth , *lines )
2018-07-03 21:18:19 +02:00
end
2017-04-01 20:28:57 +02:00
end
end