Class: Purple::SexpVM

Inherits:
Object
  • Object
show all
Defined in:
lib/purple/sexp_vm.rb

Instance Method Summary collapse

Constructor Details

#initializeSexpVM

Returns a new instance of SexpVM.



6
7
8
# File 'lib/purple/sexp_vm.rb', line 6

def initialize
  @vars = {}
end

Instance Method Details

#evaluate(sexp) ⇒ Object



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
# File 'lib/purple/sexp_vm.rb', line 10

def evaluate(sexp)
  f, *args = *sexp
#ap "x"*80
#ap f.inspect
#ap args.inspect
  ret =case f
  when :program
   # TODO: eliminate redundant array surrounding each expr
   ret = nil
   args.each { |stmt| ret = evaluate stmt.first }
   ret
  when :assign
    raise 'huh?' unless args.length == 2
    ident, expr = args.first, args.last
    name = ident.last
    @vars[name] = evaluate expr
  when :int
    raise 'huh?' unless args.length == 1
    args.first
  when :+
    raise 'huh?' unless args.length == 2
    evaluate(args.first) + evaluate(args.last)
  when :-
    raise 'huh?' unless args.length == 2
    evaluate(args.first) - evaluate(args.last)
  when :*
    raise 'huh?' unless args.length == 2
    evaluate(args.first) * evaluate(args.last)
  when :/
    raise 'huh?' unless args.length == 2
    evaluate(args.first) / evaluate(args.last)
  when :ident
    @vars[args.first]
  else
    raise "unknown sexp op: #{f}"
  end
end