Module: Locomotive::AstHelpers::AstNode
- Included in:
- RelationalAlgebra::RelAlgAstNode
- Defined in:
- lib/locomotive/tree_helpers/ast.rb
Overview
Implements the composite behaviour, thus a node with two children.
Instance Attribute Summary collapse
-
#kind ⇒ Object
Returns the value of attribute kind.
-
#left_child ⇒ Object
Returns the value of attribute left_child.
-
#owner ⇒ Object
Returns the value of attribute owner.
-
#right_child ⇒ Object
Returns the value of attribute right_child.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #has_left_child? ⇒ Boolean
- #has_right_child? ⇒ Boolean
-
#initialize(kind, value = nil, left = nil, right = nil) ⇒ Object
Initialize the node with values.</b> An Astract Syntax Tree-Node has a 1.
-
#is_leaf? ⇒ Boolean
checks whether the node is a leaf or not.
-
#traverse(strategy = nil, &block) ⇒ Object
Traverses the ast with a given strategy.
- #traverse_strategy=(strategy) ⇒ Object
Instance Attribute Details
#kind ⇒ Object
Returns the value of attribute kind.
17 18 19 |
# File 'lib/locomotive/tree_helpers/ast.rb', line 17 def kind @kind end |
#left_child ⇒ Object
Returns the value of attribute left_child.
21 22 23 |
# File 'lib/locomotive/tree_helpers/ast.rb', line 21 def left_child @left_child end |
#owner ⇒ Object
Returns the value of attribute owner.
17 18 19 |
# File 'lib/locomotive/tree_helpers/ast.rb', line 17 def owner @owner end |
#right_child ⇒ Object
Returns the value of attribute right_child.
21 22 23 |
# File 'lib/locomotive/tree_helpers/ast.rb', line 21 def right_child @right_child end |
#value ⇒ Object
Returns the value of attribute value.
17 18 19 |
# File 'lib/locomotive/tree_helpers/ast.rb', line 17 def value @value end |
Instance Method Details
#has_left_child? ⇒ Boolean
66 67 68 |
# File 'lib/locomotive/tree_helpers/ast.rb', line 66 def has_left_child? self.left_child != nil end |
#has_right_child? ⇒ Boolean
70 71 72 |
# File 'lib/locomotive/tree_helpers/ast.rb', line 70 def has_right_child? self.right_child != nil end |
#initialize(kind, value = nil, left = nil, right = nil) ⇒ Object
Initialize the node with values.</b> An Astract Syntax Tree-Node has a
1. kind
2. value
3. a left child
4. a right child
5. some node specific annotations
The values 2. - 5. can be omitted because there can be some nodes with a kind only (e.g. separators). A node with neither left- nor right-child is a leaf-node.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/locomotive/tree_helpers/ast.rb', line 39 def initialize(kind, value = nil, left = nil, right = nil) self.owner = nil self.kind, self.value = kind, value self.left_child = left if left != nil self.right_child = right if right != nil self.traverse_strategy = DEFAULT_TRAVERSE_STRATEGY end |
#is_leaf? ⇒ Boolean
checks whether the node is a leaf or not
76 77 78 79 |
# File 'lib/locomotive/tree_helpers/ast.rb', line 76 def is_leaf? self.left_child == nil and self.right_child == nil end |
#traverse(strategy = nil, &block) ⇒ Object
Traverses the ast with a given strategy. If nothing given simple prefix-traversal is used
90 91 92 93 |
# File 'lib/locomotive/tree_helpers/ast.rb', line 90 def traverse(strategy=nil, &block) @strategy ||= strategy || DEFAULT_TRAVERSE_STRATEGY @strategy.traverse(self, &block) end |
#traverse_strategy=(strategy) ⇒ Object
81 82 83 84 85 |
# File 'lib/locomotive/tree_helpers/ast.rb', line 81 def traverse_strategy=(strategy) @strategy = strategy self.left_child.traverse_strategy = strategy if self.has_left_child? self.right_child.traverse_strategy = strategy if self.has_right_child? end |