Class: Grammar::Ruby::Code::Conditional

Inherits:
List show all
Defined in:
lib/grammar/ruby/code.rb

Defined Under Namespace

Classes: Question

Constant Summary

Constants inherited from Grammar::Ruby::Code

False, FalseTerminator, Implicit, Nil, Self, True

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from List

#_codes, #_lines, #_operator0, #_paren

Methods inherited from Grammar::Ruby::Code

[], #_and, #_assign, #_classname, #_code_self, #_codes, #_data, #_def, #_dot, #_else, #_ensure, #_equal, #_immediate, #_inspect, #_lines, #_locals, #_not, #_question, #_rand, #_rescue, #_ror, #_rstep, #_splat, #_step, #_to_block, #_until, #_while, #method_missing

Constructor Details

#initialize(operands) ⇒ Conditional

Returns a new instance of Conditional.



686
687
688
689
690
691
692
693
694
695
696
# File 'lib/grammar/ruby/code.rb', line 686

def initialize(operands)
    @operands = operands
    always = true
    never = true
    @operands.each { |operand|
         always &&= operand._always
         never &&= operand._never
         break if !always && !never
    }
    @always = always ? 1 : never ? -1 : 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Grammar::Ruby::Code

Class Method Details

.new(cond, yes, no) ⇒ Object



683
684
685
# File 'lib/grammar/ruby/code.rb', line 683

def self.new(cond, yes, no)
    super([Question.new([cond, yes]), no])
end

Instance Method Details

#_alwaysObject



700
701
702
# File 'lib/grammar/ruby/code.rb', line 700

def _always
    @always>0
end

#_mid(before) ⇒ Object



716
717
718
719
720
721
722
723
724
725
726
727
728
729
# File 'lib/grammar/ruby/code.rb', line 716

def _mid(before)
    operands = []
    last = @operands.pop
    @operands.map { |operand|
        operand._mid(operands)
    }
    @operands = operands
    n = @operands.size
    last._mid(@operands)
    # TODO : remove final else in this case (translate prev to And)
    @operands << last if @operands.size==n
    @always = 0
    before << self
end

#_neverObject



703
704
705
# File 'lib/grammar/ruby/code.rb', line 703

def _never
    @always<0
end

#_operatorObject



697
698
699
# File 'lib/grammar/ruby/code.rb', line 697

def _operator
    " : "
end

#_orObject

:yield:



730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
# File 'lib/grammar/ruby/code.rb', line 730

def _or # :yield:
    last = @operands.pop
    always = true
    @operands.each { |operand|
        always &&= operand._always or break
    }
    unless always
        @operands << last
        return(super)
    end
    last = last._or { yield }
    always &&= last._always
    @always = 1 if always
    last._rcond(@operands)
    self
end

#_rcond(before) ⇒ Object



746
747
748
# File 'lib/grammar/ruby/code.rb', line 746

def _rcond(before)
    before.concat(@operands)
end

#_unless(cond, yes) ⇒ Object



706
707
708
709
710
711
712
713
714
715
# File 'lib/grammar/ruby/code.rb', line 706

def _unless(cond, yes)
    if @operands.first[1]._equal(yes)
        @operands.first[0] = cond._or{@operands.first[0]}
    else
        @operands.insert(0, operand=Question.new([cond, yes]))
        @always = 0 if @always>0 && !operand._always
        @always = 0 if @always<0 && !operand._never
    end
    self
end