-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmacros-env.mal
More file actions
36 lines (30 loc) · 862 Bytes
/
macros-env.mal
File metadata and controls
36 lines (30 loc) · 862 Bytes
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
;; Copied from mal/env.mal from the Mal project
(def! bind-env (fn* [env b e]
(if (empty? b)
env
(let* [b0 (first b)]
(if (= '& b0)
(assoc env (str (nth b 1)) e)
(bind-env (assoc env (str b0) (first e)) (rest b) (rest e)))))))
(def! new-env (fn* [& args]
(if (<= (count args) 1)
(atom {:outer (first args)})
(atom (apply bind-env {:outer (first args)} (rest args))))))
(def! env-find (fn* [env k]
(env-find-str env (str k))))
(def! env-find-str (fn* [env ks]
(if env
(let* [data @env]
(if (contains? data ks)
env
(env-find-str (get data :outer) ks))))))
(def! env-get (fn* [env k]
(let* [ks (str k)
e (env-find-str env ks)]
(if e
(get @e ks)
(throw (str "'" ks "' not found"))))))
(def! env-set (fn* [env k v]
(do
(swap! env assoc (str k) v)
v)))