Class: Yadriggy::HashLiteral

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

Overview

Hash table.

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

Returns a new instance of HashLiteral.



720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
# File 'lib/yadriggy/ast.rb', line 720

def initialize(sexp)
  if sexp[0] == :hash && sexp[1]
    list = has_tag?(sexp[1], :assoclist_from_args)[1]
  else
    list = sexp[1]
  end
  if list.nil?
    @pairs = []
  else
    @pairs = list.map do |e|
      has_tag?(e, :assoc_new)
      [to_node(e[1]), to_node(e[2])]
    end
  end
  @pairs.map do |p|
    add_child(p[0])
    add_child(p[1])
  end
end

Instance Attribute Details

#pairsArray<Array<ASTnode>> (readonly)

Returns the elements in the hash table.

Returns:

  • (Array<Array<ASTnode>>)

    the hash elements. Each element is a key-value pair. For example, if the source code is ‘=> value1, key2 => value2`, then `[[key1, value2], [key2, value2]]` is returned.



716
717
718
# File 'lib/yadriggy/ast.rb', line 716

def pairs
  @pairs
end

Class Method Details

.tagsObject



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

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



743
744
745
# File 'lib/yadriggy/ast.rb', line 743

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