2018-03-15 06:54:14 +01:00
|
|
|
require_relative "normalizer"
|
2017-09-05 11:04:52 +02:00
|
|
|
|
2017-04-01 20:28:57 +02:00
|
|
|
module Vool
|
|
|
|
class WhileStatement < Statement
|
2018-03-15 06:54:14 +01:00
|
|
|
include Normalizer
|
2017-04-06 15:06:51 +02:00
|
|
|
attr_reader :condition , :statements
|
2017-04-03 10:49:21 +02:00
|
|
|
|
2017-04-06 15:06:51 +02:00
|
|
|
def initialize( condition , statements )
|
2017-04-03 10:49:21 +02:00
|
|
|
@condition = condition
|
2017-04-06 15:06:51 +02:00
|
|
|
@statements = statements
|
|
|
|
simplify_condition
|
2017-04-03 10:49:21 +02:00
|
|
|
end
|
2017-04-06 15:06:51 +02:00
|
|
|
|
2018-03-15 06:54:14 +01:00
|
|
|
def normalize(method)
|
|
|
|
cond , rest = *normalize_name(@condition)
|
|
|
|
me = WhileStatement.new(cond , @statements.normalize(method))
|
|
|
|
return me unless rest
|
|
|
|
rest << me
|
|
|
|
end
|
|
|
|
|
2017-09-05 11:04:52 +02:00
|
|
|
def to_mom( method )
|
|
|
|
statements = @statements.to_mom( method )
|
|
|
|
condition , hoisted = hoist_condition( method )
|
|
|
|
cond = Mom::TruthCheck.new(condition.to_mom(method))
|
|
|
|
check = Mom::WhileStatement.new( cond , statements )
|
|
|
|
check.hoisted = hoisted.to_mom(method) if hoisted
|
|
|
|
check
|
|
|
|
end
|
|
|
|
|
2018-03-15 16:03:38 +01:00
|
|
|
def flatten(options = {})
|
|
|
|
merge_label = Label.new( "merge_label_#{object_id}")
|
|
|
|
cond_label = Label.new( "cond_label_#{object_id}")
|
|
|
|
@nekst = cond_label
|
|
|
|
@nekst.append(hoisted.flatten) if hoisted
|
|
|
|
@nekst.append condition.flatten( true_label: cond_label , false_label: merge_label)
|
|
|
|
@nekst.append merge_label
|
|
|
|
@nekst
|
|
|
|
end
|
|
|
|
|
2017-04-06 15:06:51 +02:00
|
|
|
def simplify_condition
|
|
|
|
return unless @condition.is_a?(ScopeStatement)
|
|
|
|
@condition = @condition.first if @condition.single?
|
|
|
|
end
|
|
|
|
|
2017-04-08 11:10:42 +02:00
|
|
|
def collect(arr)
|
|
|
|
@condition.collect(arr)
|
|
|
|
@statements.collect(arr)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2017-04-01 20:28:57 +02:00
|
|
|
end
|
|
|
|
end
|