Class: Campa::Evaler

Inherits:
Object
  • Object
show all
Defined in:
lib/campa/evaler.rb

Instance Method Summary collapse

Constructor Details

#initializeEvaler

Returns a new instance of Evaler.



3
4
5
# File 'lib/campa/evaler.rb', line 3

def initialize
  @printer = Printer.new
end

Instance Method Details

#call(expression, env = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/campa/evaler.rb', line 7

def call(expression, env = {})
  context = self.context(env)

  case expression
  when Numeric, TrueClass, FalseClass, NilClass, String, ::Symbol, List::EMPTY
    expression
  when Symbol
    resolve(expression, context)
  when List
    invoke(expression, context)
  end
end

#eval(reader, env = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/campa/evaler.rb', line 20

def eval(reader, env = {})
  context = self.context(env)

  result = nil
  while (token = reader.next)
    result = call(token, context)
  end
  result
end