forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.jl
55 lines (45 loc) · 877 Bytes
/
env.jl
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
module env
export Env, env_set, env_find, env_get
type Env
outer::Any
data::Dict{Symbol,Any}
end
function Env()
Env(nothing, Dict())
end
function Env(outer)
Env(outer, Dict())
end
function Env(outer, binds, exprs)
e = Env(outer, Dict())
for i=1:length(binds)
if binds[i] == :&
e.data[binds[i+1]] = exprs[i:end]
break
else
e.data[binds[i]] = exprs[i]
end
end
e
end
function env_set(env::Env, k::Symbol, v)
env.data[k] = v
end
function env_find(env::Env, k::Symbol)
if haskey(env.data, k)
env
elseif env.outer != nothing
env_find(env.outer, k)
else
nothing
end
end
function env_get(env::Env, k::Symbol)
e = env_find(env, k)
if e != nothing
e.data[k]
else
error("'$(string(k))' not found")
end
end
end