forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.cr
68 lines (54 loc) · 1.5 KB
/
env.cr
1
2
3
4
5
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require "./types"
require "./error"
module Mal
class Env
property data
def initialize(@outer)
@data = {} of String => Mal::Type
end
def initialize(@outer, binds, exprs : Array(Mal::Type))
@data = {} of String => Mal::Type
eval_error "binds must be list or vector" unless binds.is_a? Array
# Note:
# Array#zip() can't be used because overload resolution failed
(0...binds.size).each do |idx|
sym = binds[idx].unwrap
eval_error "bind name must be symbol" unless sym.is_a? Mal::Symbol
if sym.str == "&"
eval_error "missing variable parameter name" if binds.size == idx
next_param = binds[idx+1].unwrap
eval_error "bind name must be symbol" unless next_param.is_a? Mal::Symbol
var_args = Mal::List.new
exprs[idx..-1].each{|e| var_args << e} if idx < exprs.size
@data[next_param.str] = Mal::Type.new var_args
break
end
@data[sym.str] = exprs[idx]
end
end
def dump
puts "ENV BEGIN".colorize.red
@data.each do |k, v|
puts " #{k} -> #{print(v)}".colorize.red
end
puts "ENV END".colorize.red
end
def set(key, value)
@data[key] = value
end
def find(key)
return self if @data.has_key? key
o = @outer
if o
o.find key
else
nil
end
end
def get(key)
e = find key
eval_error "'#{key}' not found" unless e
e.data[key]
end
end
end