Class: Yadriggy::ASTree

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

Overview

Abstract syntax tree (AST).

Instance Attribute Summary collapse

Attributes inherited from ASTnode

#parent, #usertype

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ASTnode

#add_child, #add_children, #const_value, #const_value_in_class, #get_context_class, #get_receiver_object, #is_proc?, #root, #source_location, #source_location_string, #value, #value_in_class

Constructor Details

#initialize(ast_table, proc, file, sexp) ⇒ ASTree

Returns a new instance of ASTree.



1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
# File 'lib/yadriggy/ast.rb', line 1785

def initialize(ast_table, proc, file, sexp)
  unless proc.is_a?(Proc) || proc.is_a?(Method) ||
         proc.is_a?(UnboundMethod)
    raise "unknown context #{proc.class.name}"
  end

  @astrees = ast_table   # ASTreeTable
  @context = proc	# Proc or Method
  @file_name = file
  @tree = ASTree.to_node(sexp)
  add_child(@tree)
  @astrees[proc] = self
end

Instance Attribute Details

#astreesASTreeTable (readonly)

Returns all the reified ASTs.

Returns:

  • (ASTreeTable)

    all the reified ASTs.



1773
1774
1775
# File 'lib/yadriggy/ast.rb', line 1773

def astrees
  @astrees
end

#contextMethod|Proc (readonly)

Returns the Method or Proc object given for the reification.

Returns:

  • (Method|Proc)

    the Method or Proc object given for the reification.



1777
1778
1779
# File 'lib/yadriggy/ast.rb', line 1777

def context
  @context
end

#file_nameString (readonly)

Returns the source file name.

Returns:

  • (String)

    the source file name.



1780
1781
1782
# File 'lib/yadriggy/ast.rb', line 1780

def file_name
  @file_name
end

#treeASTnode (readonly)

Returns the AST.

Returns:



1783
1784
1785
# File 'lib/yadriggy/ast.rb', line 1783

def tree
  @tree
end

Class Method Details

.append_tags(clazz) ⇒ Object



1849
1850
1851
# File 'lib/yadriggy/ast.rb', line 1849

def self.append_tags(clazz)
  clazz.tags.each {|t| @tags[t] = clazz }
end

.to_node(sexp) ⇒ Object



1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
# File 'lib/yadriggy/ast.rb', line 1872

def self.to_node(sexp)
  if sexp.nil?
    nil
  elsif sexp[0] == :var_ref || sexp[0] == :var_field ||
        sexp[0] == :const_ref
    to_node(sexp[1])
  else
    klass = @tags[sexp[0]]
    if klass.nil?
      sexp_name = if sexp.is_a?(Array)
                    sexp[0].to_s
                  else
                    ':' + sexp.to_s
                  end
      raise "unknown s-expression " + sexp_name
    else
      node = klass.new(sexp)
      StringLiteral.normalize(node)
    end
  end
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.



1821
1822
1823
# File 'lib/yadriggy/ast.rb', line 1821

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

#pretty_print(pp) ⇒ Object



1799
1800
1801
# File 'lib/yadriggy/ast.rb', line 1799

def pretty_print(pp)
  Yadriggy::simpler_pretty_print(pp, self, "@astrees")
end

#reify(proc) ⇒ ASTree|nil

Gets the abstract syntax tree of the given procedure.

Parameters:

  • proc (Proc|Method|UnboundMethod)

    the procedure or method.

Returns:

  • (ASTree|nil)

    the reified AST.

See Also:



1808
1809
1810
1811
1812
1813
1814
1815
1816
# File 'lib/yadriggy/ast.rb', line 1808

def reify(proc)
  ast_obj = @astrees[proc]
  unless ast_obj.nil?
    ast_obj
  else
    ast = SourceCode.get_sexp(proc)
    ast && ast[1] && ASTree.new(@astrees, proc, ast[0], ast[1])
  end
end