Class: Yadriggy::ASTree
Overview
Abstract syntax tree (AST).
Instance Attribute Summary collapse
-
#astrees ⇒ ASTreeTable
readonly
All the reified ASTs.
-
#context ⇒ Method|Proc
readonly
The Method or Proc object given for the reification.
-
#file_name ⇒ String
readonly
The source file name.
-
#tree ⇒ ASTnode
readonly
The AST.
Attributes inherited from ASTnode
Class Method Summary collapse
Instance Method Summary collapse
-
#accept(evaluator) ⇒ void
A method for Visitor pattern.
-
#initialize(ast_table, proc, file, sexp) ⇒ ASTree
constructor
A new instance of ASTree.
- #pretty_print(pp) ⇒ Object
-
#reify(proc) ⇒ ASTree|nil
Gets the abstract syntax tree of the given procedure.
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.
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 |
# File 'lib/yadriggy/ast.rb', line 1709 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
#astrees ⇒ ASTreeTable (readonly)
Returns all the reified ASTs.
1697 1698 1699 |
# File 'lib/yadriggy/ast.rb', line 1697 def astrees @astrees end |
#context ⇒ Method|Proc (readonly)
Returns the Method or Proc object given for the reification.
1701 1702 1703 |
# File 'lib/yadriggy/ast.rb', line 1701 def context @context end |
#file_name ⇒ String (readonly)
Returns the source file name.
1704 1705 1706 |
# File 'lib/yadriggy/ast.rb', line 1704 def file_name @file_name end |
#tree ⇒ ASTnode (readonly)
Returns the AST.
1707 1708 1709 |
# File 'lib/yadriggy/ast.rb', line 1707 def tree @tree end |
Class Method Details
.append_tags(clazz) ⇒ Object
1773 1774 1775 |
# File 'lib/yadriggy/ast.rb', line 1773 def self.(clazz) clazz..each {|t| [t] = clazz } end |
.to_node(sexp) ⇒ Object
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 |
# File 'lib/yadriggy/ast.rb', line 1796 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 = [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.
1745 1746 1747 |
# File 'lib/yadriggy/ast.rb', line 1745 def accept(evaluator) evaluator.astree(self) end |
#pretty_print(pp) ⇒ Object
1723 1724 1725 |
# File 'lib/yadriggy/ast.rb', line 1723 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.
1732 1733 1734 1735 1736 1737 1738 1739 1740 |
# File 'lib/yadriggy/ast.rb', line 1732 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 |