class Yadriggy::Conditional

if, unless, modifier if/unless, and ternary if (`?:`).

Attributes

all_elsif[R]

Returns the elsif-expressions. @return [Array<ASTnode>] an array of the elsif-expressions.

It may be an empty array.
cond[R]

@return [ASTnode] the condition expression.

else[R]

@return [ASTnode|nil] the else-expression.

op[R]

@return [Symbol] `:if`, `:unless`, `:if_mod`, `:unless_mod`,

or `:ifop`.
then[R]

@return [ASTnode] the then-expressin.

Public Class Methods

new(sexp) click to toggle source
# File lib/yadriggy/ast.rb, line 1077
def initialize(sexp)
  @op = sexp[0]
  @cond = to_node(sexp[1])
  @all_elsif = []
  case @op
  when :ifop                   # ternary if
    @then = to_node(sexp[2])
    @else = to_node(sexp[3])
  when :if_mod, :unless_mod    # modifier if/unless
    @then = to_node(sexp[2])
    @else = nil
  else                         # if/unless
    @then = Exprs.make(sexp[2])
    initialize_else(sexp[3])
  end
  add_child(@cond)
  add_child(@then)
  add_child(@else)
  @all_elsif.each do |pair|
    pair.each { |e| add_child(e) }
  end
end
tags() click to toggle source
# File lib/yadriggy/ast.rb, line 1075
def self.tags() [:if, :unless, :ifop, :if_mod, :unless_mod] end

Public Instance Methods

accept(evaluator) click to toggle source

A method for Visitor pattern. @param [Eval] evaluator the visitor of Visitor pattern. @return [void]

# File lib/yadriggy/ast.rb, line 1103
def accept(evaluator)
  evaluator.conditional(self)
end

Private Instance Methods

initialize_else(else_part) click to toggle source
# File lib/yadriggy/ast.rb, line 1108
def initialize_else(else_part)
  if else_part.nil?
    @else = nil
  elsif else_part[0] == :elsif
    @all_elsif << [to_node(else_part[1]), Exprs.make(else_part[2])]
    initialize_else(else_part[3])
  else
    @else = Exprs.make(has_tag?(else_part, :else)[1])
  end
end