Class: Nay::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/nay/evaluator.rb

Overview

Knows how to take a stream of lexically analyzed tokens along with a parameters hash and evaluate interpolated key paths.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lexer) ⇒ Evaluator

Returns a new instance of Evaluator.



9
10
11
# File 'lib/nay/evaluator.rb', line 9

def initialize(lexer)
  @lexer = lexer
end

Instance Attribute Details

#lexerObject (readonly)

Returns the value of attribute lexer.



7
8
9
# File 'lib/nay/evaluator.rb', line 7

def lexer
  @lexer
end

Instance Method Details

#evaluate(parameters = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nay/evaluator.rb', line 13

def evaluate(parameters = {})
  io      = StringIO.new
  pointer = parameters

  while (token = lexer.next_token)
    case token.type
    when Lexer::CONTENT
      io << token.literal
    when Lexer::OPEN_EXPRESSION
      pointer = parameters
    when Lexer::IDENTIFIER
      pointer = traverse(pointer, token.literal)
    when Lexer::CLOSE_EXPRESSION
      io << pointer.to_s
    end
  end

  io.string
end