forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnv.io
45 lines (40 loc) · 1011 Bytes
/
Env.io
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
Env := Object clone do(
outer ::= nil
data ::= nil
with := method(aOuter, aBinds, aExprs,
self clone setOuter(aOuter) setData(Map clone) initBinds(aBinds, aExprs)
)
initBinds := method(aBinds, aExprs,
if(aBinds isNil not,
aBinds foreach(i, b,
if(b val == "&",
set(aBinds at(i + 1), aExprs slice(i)) break,
set(b, aExprs at(i))
)
)
)
self
)
set := method(key, val,
data atPut(key val, val)
val
)
find := method(key,
keyStr := key val
if(data hasKey(keyStr),
self,
if(outer isNil,
nil,
outer find(key)
)
)
)
get := method(key,
keyStr := key val
foundEnv := find(key)
if(foundEnv isNil,
Exception raise("'" .. keyStr .. "' not found"),
(foundEnv data) at(keyStr)
)
)
)