Class: Lispcalc::Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/lispcalc/interpreter.rb

Instance Method Summary collapse

Constructor Details

#initialize(ctx = Context.new) ⇒ Interpreter

Returns a new instance of Interpreter.



6
7
8
9
# File 'lib/lispcalc/interpreter.rb', line 6

def initialize(ctx = Context.new)
  @ctx = ctx
  @functions = Functions.new(ctx)
end

Instance Method Details

#eval_list(list) ⇒ Object Also known as: eval

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
# File 'lib/lispcalc/interpreter.rb', line 11

def eval_list(list)
  raise ArgumentError, 'expecting an instance of Array' unless list.instance_of?(Array)
  raise SyntaxError, 'empty list' if list.empty?

  fn, *args = list
  raise SyntaxError, 'the first element of a list must be a symbol' unless fn.instance_of?(Symbol)

  call(fn, eval_args(args))
end

#eval_symbol(symbol) ⇒ Object



23
24
25
# File 'lib/lispcalc/interpreter.rb', line 23

def eval_symbol(symbol)
  @ctx[symbol] || symbol
end