Class: Electr::AST
- Inherits:
-
Object
- Object
- Electr::AST
- Defined in:
- lib/electr/ast/ast.rb
Overview
Base class for the abstract syntax tree.
Direct Known Subclasses
ConstantAST, FuncAST, FuncArgsAST, FuncNameAST, NumericAST, OperatorAST, RootAST, ValueAST, VariableAST
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#add_child(child) ⇒ Object
Public: Add a child node to the end of the children’s list.
- #display(indent = 0) ⇒ Object
-
#initialize(name) ⇒ AST
constructor
A new instance of AST.
-
#leaf? ⇒ Boolean
Public: Returns True if this node is a leaf.
- #size ⇒ Object
- #to_pn ⇒ Object
Constructor Details
#initialize(name) ⇒ AST
Returns a new instance of AST.
6 7 8 9 10 11 12 13 14 |
# File 'lib/electr/ast/ast.rb', line 6 def initialize(name) if name @name = name else @name = self.class.name.split('::').last end @children = [] @value = nil end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
16 17 18 |
# File 'lib/electr/ast/ast.rb', line 16 def children @children end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/electr/ast/ast.rb', line 16 def name @name end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
16 17 18 |
# File 'lib/electr/ast/ast.rb', line 16 def value @value end |
Instance Method Details
#add_child(child) ⇒ Object
Public: Add a child node to the end of the children’s list.
child - An AST node to add to the list of children.
Returns self.
23 24 25 26 |
# File 'lib/electr/ast/ast.rb', line 23 def add_child child @children << child self end |
#display(indent = 0) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/electr/ast/ast.rb', line 37 def display(indent = 0) print name_for_display(indent) print value_for_display puts @children.each {|child| child.display(indent + 2) } end |
#leaf? ⇒ Boolean
Public: Returns True if this node is a leaf.
29 30 31 |
# File 'lib/electr/ast/ast.rb', line 29 def leaf? @children.empty? end |
#size ⇒ Object
33 34 35 |
# File 'lib/electr/ast/ast.rb', line 33 def size @children.size end |