2017-09-05 11:04:52 +02:00
|
|
|
|
2017-04-01 20:28:57 +02:00
|
|
|
module Vool
|
|
|
|
class WhileStatement < Statement
|
2018-04-20 08:56:06 +02:00
|
|
|
attr_reader :condition , :body , :hoisted
|
2017-04-03 10:49:21 +02:00
|
|
|
|
2018-04-20 08:56:06 +02:00
|
|
|
def initialize( condition , body , hoisted = nil)
|
|
|
|
@hoisted = hoisted
|
2017-04-03 10:49:21 +02:00
|
|
|
@condition = condition
|
2018-03-16 08:03:11 +01:00
|
|
|
@body = body
|
2017-04-03 10:49:21 +02:00
|
|
|
end
|
2017-04-06 15:06:51 +02:00
|
|
|
|
2018-07-05 13:02:38 +02:00
|
|
|
def to_mom( compiler )
|
2019-08-07 14:08:45 +02:00
|
|
|
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)}")
|
2018-04-20 08:56:06 +02:00
|
|
|
codes = cond_label
|
2018-07-05 13:02:38 +02:00
|
|
|
codes << @hoisted.to_mom(compiler) if @hoisted
|
2019-08-16 13:09:56 +02:00
|
|
|
codes << @condition.to_mom(compiler) if @condition.is_a?(SendStatement)
|
2019-08-19 10:33:12 +02:00
|
|
|
codes << Mom::TruthCheck.new(condition.to_slot(compiler) , merge_label)
|
2018-07-05 13:02:38 +02:00
|
|
|
codes << @body.to_mom(compiler)
|
2018-04-20 08:56:06 +02:00
|
|
|
codes << Mom::Jump.new(cond_label)
|
|
|
|
codes << merge_label
|
2017-09-05 11:04:52 +02:00
|
|
|
end
|
|
|
|
|
2018-03-16 08:03:11 +01:00
|
|
|
def each(&block)
|
|
|
|
block.call(self)
|
2019-08-17 14:58:27 +02:00
|
|
|
@condition.each(&block)
|
2018-04-20 08:56:06 +02:00
|
|
|
@hoisted.each(&block) if @hoisted
|
2018-03-16 08:03:11 +01:00
|
|
|
@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)
|
2019-08-17 22:27:55 +02:00
|
|
|
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
|