forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.lg
51 lines (45 loc) · 1.03 KB
/
env.lg
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
load "../logo/printer.lg
load "../logo/types.lg
to env_new :outer :binds :exprs
localmake "data []
if not emptyp :binds [
localmake "i 0
while [:i < _count :binds] [
ifelse (nth :binds :i) = [symbol &] [
localmake "val drop :exprs :i
make "i (:i + 1)
localmake "key nth :binds :i
] [
localmake "val nth :exprs :i
localmake "key nth :binds :i
]
make "data hashmap_put :data :key :val
make "i (:i + 1)
]
]
output listtoarray list :outer :data
end
to env_outer :env
output item 1 :env
end
to env_data :env
output item 2 :env
end
to env_find :env :key
if emptyp :env [output []]
localmake "val hashmap_get env_data :env :key
ifelse emptyp :val [
output env_find env_outer :env :key
] [
output :env
]
end
to env_get :env :key
localmake "foundenv env_find :env :key
if emptyp :foundenv [(throw "error sentence (word "' pr_str :key "true "' ) [not found])]
output hashmap_get env_data :foundenv :key
end
to env_set :env :key :val
.setitem 2 :env hashmap_put env_data :env :key :val
output :val
end