Class: Yadriggy::Conditional

Inherits:
ASTnode
  • Object
show all
Includes:
AstHelper
Defined in:
lib/yadriggy/ast.rb

Overview

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

Instance Attribute Summary collapse

Attributes inherited from ASTnode

#parent, #usertype

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AstHelper

#has_tag?, #to_node, #to_nodes

Methods inherited from ASTnode

#add_child, #add_children, #const_value, #const_value_in_class, #get_context_class, #get_receiver_object, #is_proc?, #pretty_print, #root, #source_location, #source_location_string, #value, #value_in_class

Constructor Details

#initialize(sexp) ⇒ Conditional

Returns a new instance of Conditional.



1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
# File 'lib/yadriggy/ast.rb', line 1067

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

Instance Attribute Details

#all_elsifArray<ASTnode> (readonly)

Returns the elsif-expressions.

Returns:

  • (Array<ASTnode>)

    an array of the elsif-expressions. It may be an empty array.



1060
1061
1062
# File 'lib/yadriggy/ast.rb', line 1060

def all_elsif
  @all_elsif
end

#condASTnode (readonly)

Returns the condition expression.

Returns:

  • (ASTnode)

    the condition expression.



1052
1053
1054
# File 'lib/yadriggy/ast.rb', line 1052

def cond
  @cond
end

#elseASTnode|nil (readonly)

Returns the else-expression.

Returns:

  • (ASTnode|nil)

    the else-expression.



1063
1064
1065
# File 'lib/yadriggy/ast.rb', line 1063

def else
  @else
end

#opSymbol (readonly)

Returns :if, :unless, :if_mod, :unless_mod, or :ifop.

Returns:

  • (Symbol)

    :if, :unless, :if_mod, :unless_mod, or :ifop.



1049
1050
1051
# File 'lib/yadriggy/ast.rb', line 1049

def op
  @op
end

#thenASTnode (readonly)

Returns the then-expressin.

Returns:

  • (ASTnode)

    the then-expressin.



1055
1056
1057
# File 'lib/yadriggy/ast.rb', line 1055

def then
  @then
end

Class Method Details

.tagsObject



1065
# File 'lib/yadriggy/ast.rb', line 1065

def self.tags() [:if, :unless, :ifop, :if_mod, :unless_mod] end

Instance Method Details

#accept(evaluator) ⇒ void

This method returns an undefined value.

A method for Visitor pattern.

Parameters:

  • evaluator (Eval)

    the visitor of Visitor pattern.



1093
1094
1095
# File 'lib/yadriggy/ast.rb', line 1093

def accept(evaluator)
  evaluator.conditional(self)
end