Class: Ruspea::Interpreter::Evaler

Inherits:
Object
  • Object
show all
Includes:
Error, Runtime
Defined in:
lib/ruspea/interpreter/evaler.rb

Instance Method Summary collapse

Instance Method Details

#call(value, context: Env::Empty.instance) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ruspea/interpreter/evaler.rb', line 6

def call(value, context: Env::Empty.instance)
  return call(value.value, context: context) if value.is_a?(Form)

  case value
  when Sym
    context.lookup value
  when Numeric
    value
  when String
    value
  when NilClass
    nil
  when TrueClass
    true
  when FalseClass
    false
  when Array
    value.map { |f|
      call(f, context: context)
    }
  when List
    fn = fn_from_invocation(value, context)
    # TODO: raise if ! respond_to?(:call)

    arguments =
      if fn.respond_to?(:arity)
        if fn.arity == 1 && value.tail.count > 1
          [value.tail]
        else
          value.tail.to_a
        end
      else
        if fn.is_a? Fn
          value.tail.to_a
        else
          [value.tail.to_a]
        end
      end

    fn.call(*arguments, context: context, evaler: self)
  else
    raise "Unrecognized expression"
  end
end