rubyx/lib/vool/statements/if_statement.rb
2018-03-16 19:05:22 +05:30

56 lines
1.3 KiB
Ruby

require_relative "normalizer"
module Vool
class IfStatement < Statement
include Normalizer
attr_reader :condition , :if_true , :if_false
def initialize( cond , if_true , if_false = nil)
@condition = cond
@if_true = if_true
@if_false = if_false
end
def normalize
cond , rest = *normalize_name(@condition)
fals = @if_false ? @if_false.normalize : nil
me = IfStatement.new(cond , @if_true.normalize, fals)
return me unless rest
rest << me
rest
end
def to_mom( method )
true_label = Mom::Label.new( "true_label_#{object_id}")
false_label = Mom::Label.new( "false_label_#{object_id}")
merge_label = Mom::Label.new( "merge_label_#{object_id}")
head = Mom::TruthCheck.new(condition.slot_definition(method) , false_label)
head << true_label
head << if_true.to_mom(method)
head << Mom::Jump.new(merge_label) if if_false
head << false_label
if if_false
head << if_false.to_mom(method)
head << merge_label
end
head
end
def each(&block)
block.call(condition)
@if_true.each(&block)
@if_false.each(&block) if @if_false
end
def has_false?
@if_false != nil
end
def has_true?
@if_true != nil
end
end
end