Class: BracketNotation::Expression

Inherits:
Object
  • Object
show all
Defined in:
lib/bracket_notation/expressions/expression.rb

Overview

This class represents the result of evaluating a token. It is an abstract class that should have a subclass for every possible kind of expression.

Direct Known Subclasses

Identifier, Terminal

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, depth = 0) ⇒ Expression

Saves the token value and optional tree depth.



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

def initialize(value, depth = 0)
  @value = value
  @depth = depth
  @parent = nil
end

Instance Attribute Details

#depthObject

Returns the value of attribute depth.



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

def depth
  @depth
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#pretty_printObject

Prints a visual representation of the syntax tree.



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

def pretty_print
  out = ""
  @depth.times {out << "--"}
  out << " " if @depth > 0
  out << "#{@value}\n"
end