class Yadriggy::ASTree

Abstract syntax tree (AST).

Attributes

astrees[R]

@return [ASTreeTable] all the reified ASTs.

context[R]

@return [Method|Proc] the Method or Proc object given

for the reification.
file_name[R]

@return [String] the source file name.

tree[R]

@return [ASTnode] the AST.

Public Class Methods

append_tags(clazz) click to toggle source
# File lib/yadriggy/ast.rb, line 1845
def self.append_tags(clazz)
  clazz.tags.each {|t| @tags[t] = clazz }
end
new(ast_table, proc, file, sexp) click to toggle source
# File lib/yadriggy/ast.rb, line 1781
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
to_node(sexp) click to toggle source
# File lib/yadriggy/ast.rb, line 1868
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

Public Instance Methods

accept(evaluator) click to toggle source

A method for Visitor pattern. @param [Eval] evaluator the visitor of Visitor pattern. @return [void]

# File lib/yadriggy/ast.rb, line 1817
def accept(evaluator)
  evaluator.astree(self)
end
pretty_print(pp) click to toggle source
# File lib/yadriggy/ast.rb, line 1795
def pretty_print(pp)
  Yadriggy::simpler_pretty_print(pp, self, "@astrees")
end
reify(proc) click to toggle source

Gets the abstract syntax tree of the given procedure.

@param [Proc|Method|UnboundMethod] proc the procedure or method. @return [ASTree|nil] the reified AST. @see Yadriggy.reify

# File lib/yadriggy/ast.rb, line 1804
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