Class: Yadriggy::Def

Inherits:
Parameters show all
Defined in:
lib/yadriggy/ast.rb

Overview

Method definition or a singular method definition.

Instance Attribute Summary collapse

Attributes inherited from Parameters

#block_param, #keywords, #optionals, #params, #params_after_rest, #rest_of_keywords, #rest_of_params

Attributes inherited from ASTnode

#parent, #usertype

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Parameters

#initialize_params

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) ⇒ Def

Returns a new instance of Def.



1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
# File 'lib/yadriggy/ast.rb', line 1569

def initialize(sexp)
  if sexp[0] == :def
    @singular = nil
    offset = 0
  else
    @singular = to_node(sexp[1])
    add_child(@singular)
    offset = 2
  end

  def_name = sexp[1 + offset]
  @name = if def_name[0] == :@op
            to_node(def_name)
          else
            to_node(has_tag?(def_name, :@ident))
          end
  add_child(@name)

  params = sexp[2 + offset]
  if !params.nil? && params[0] == :paren
    super(params[1])
  else
    super(params)
  end

  bodystmt = has_tag?(sexp[3 + offset], :bodystmt)
  @body = Exprs.make(bodystmt[1])
  @rescue = Rescue.make(bodystmt[2], bodystmt[3], bodystmt[4])
  add_child(@body)
  add_child(@rescue)
end

Instance Attribute Details

#bodyExprs|ASTnode (readonly)

Returns the body.

Returns:



1563
1564
1565
# File 'lib/yadriggy/ast.rb', line 1563

def body
  @body
end

#nameIdentifier (readonly)

Returns the method name.

Returns:



1561
1562
1563
# File 'lib/yadriggy/ast.rb', line 1561

def name
  @name
end

#rescueRescue|nil (readonly)

Returns the rescue clause.

Returns:

  • (Rescue|nil)

    the rescue clause.



1565
1566
1567
# File 'lib/yadriggy/ast.rb', line 1565

def rescue
  @rescue
end

#singularASTnode|nil (readonly)

Returns the object if the definition is a singular method.

Returns:

  • (ASTnode|nil)

    the object if the definition is a singular method.



1559
1560
1561
# File 'lib/yadriggy/ast.rb', line 1559

def singular
  @singular
end

Class Method Details

.tagsObject



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

def self.tags() [:def, :defs] 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.



1604
1605
1606
# File 'lib/yadriggy/ast.rb', line 1604

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