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.



762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'lib/yadriggy/ast.rb', line 762

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 {key1 => value1, key2 => value2}, then [[key1, value2], [key2, value2]] is returned.



758
759
760
# File 'lib/yadriggy/ast.rb', line 758

def pairs
  @pairs
end

Class Method Details

.tagsObject



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

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.



785
786
787
# File 'lib/yadriggy/ast.rb', line 785

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