Class: BracketNotation::Identifier

Inherits:
Expression show all
Defined in:
lib/bracket_notation/expressions/identifier.rb

Overview

This class represents an identifier expression. Identifers are expressions that can have children – branch nodes, in other words.

Instance Attribute Summary collapse

Attributes inherited from Expression

#parent, #value

Instance Method Summary collapse

Constructor Details

#initialize(value, depth = 0) ⇒ Identifier

Saves the identifier value and optional tree depth.



36
37
38
39
# File 'lib/bracket_notation/expressions/identifier.rb', line 36

def initialize(value, depth = 0)
  super
  @children = []
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



33
34
35
# File 'lib/bracket_notation/expressions/identifier.rb', line 33

def children
  @children
end

#depthObject

Returns the value of attribute depth.



33
34
35
# File 'lib/bracket_notation/expressions/identifier.rb', line 33

def depth
  @depth
end

Instance Method Details

#add_child(expression) ⇒ Object

Adds the given expression to the children array and updates the new child’s parent and depth attributes.



43
44
45
46
47
# File 'lib/bracket_notation/expressions/identifier.rb', line 43

def add_child(expression)
  expression.parent = self
  expression.depth = @depth + 1
  @children << expression
end

#pretty_printObject

Prints a visual representation of the syntax tree.



56
57
58
59
60
# File 'lib/bracket_notation/expressions/identifier.rb', line 56

def pretty_print
  out = super
  @children.each {|child| out << child.pretty_print}
  out
end