rubyx/lib/vool/statements/if_statement.rb
Torsten Ruger e0f6ba7bcf simple if test without else
unveils many a bug that is now fixed
2018-03-12 17:56:44 +05:30

47 lines
1.0 KiB
Ruby

require_relative "hoister"
module Vool
class IfStatement < Statement
include Hoister
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
simplify_condition
end
def to_mom( method )
if_true = @if_true.to_mom( method )
if_false = @if_false ? @if_false.to_mom( method ) : nil
condition , hoisted = hoist_condition( method )
cond = Mom::TruthCheck.new(condition.to_mom(method))
check = Mom::IfStatement.new( cond , if_true , if_false )
check.hoisted = hoisted.to_mom(method) if hoisted
check
end
def collect(arr)
@if_true.collect(arr)
@if_false.collect(arr) if @if_false
super
end
def simplify_condition
return unless @condition.is_a?(ScopeStatement)
@condition = @condition.first if @condition.single?
end
def has_false?
@if_false != nil
end
def has_true?
@if_true != nil
end
end
end