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.



1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
# File 'lib/yadriggy/ast.rb', line 1081

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.



1074
1075
1076
# File 'lib/yadriggy/ast.rb', line 1074

def all_elsif
  @all_elsif
end

#condASTnode (readonly)

Returns the condition expression.

Returns:

  • (ASTnode)

    the condition expression.



1066
1067
1068
# File 'lib/yadriggy/ast.rb', line 1066

def cond
  @cond
end

#elseASTnode|nil (readonly)

Returns the else-expression.

Returns:

  • (ASTnode|nil)

    the else-expression.



1077
1078
1079
# File 'lib/yadriggy/ast.rb', line 1077

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.



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

def op
  @op
end

#thenASTnode (readonly)

Returns the then-expressin.

Returns:

  • (ASTnode)

    the then-expressin.



1069
1070
1071
# File 'lib/yadriggy/ast.rb', line 1069

def then
  @then
end

Class Method Details

.tagsObject



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

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.



1107
1108
1109
# File 'lib/yadriggy/ast.rb', line 1107

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