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.



1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
# File 'lib/yadriggy/ast.rb', line 1589

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, :@const))
          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:



1583
1584
1585
# File 'lib/yadriggy/ast.rb', line 1583

def body
  @body
end

#nameIdentifier (readonly)

Returns the method name.

Returns:



1581
1582
1583
# File 'lib/yadriggy/ast.rb', line 1581

def name
  @name
end

#rescueRescue|nil (readonly)

Returns the rescue clause.

Returns:

  • (Rescue|nil)

    the rescue clause.



1585
1586
1587
# File 'lib/yadriggy/ast.rb', line 1585

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.



1579
1580
1581
# File 'lib/yadriggy/ast.rb', line 1579

def singular
  @singular
end

Class Method Details

.tagsObject



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

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.



1624
1625
1626
# File 'lib/yadriggy/ast.rb', line 1624

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