This repository has been archived by the owner on Aug 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.fnl
72 lines (59 loc) · 1.91 KB
/
env.fnl
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
69
70
71
72
(local list (require :list))
(local env {})
(tset env :__index env)
(local expr (require :expr))
(fn env.new [?parent]
"Create a empty environment with ?PARENT."
(setmetatable {:table {} :rules {} :parent ?parent} env))
(fn env._put [self key value]
"Put KEY VALUE pair into environment."
(tset self.table key value))
(fn env._get [self key]
"Retreive value for KEY."
(let [value (. self.table key)
parent (. self :parent)]
(if value
value
(when parent
(parent:_get key)))))
(fn env.put-rules [self name list]
"Put LIST of rules into ruleset with NAME."
(if (not (. self.rules name))
(tset self.rules name (or list []))
(let [rs (. self.rules name)]
(each [_ r (ipairs list)]
(table.insert rs r))))
self)
(fn env.get-rules [self name]
"Get active list of rules for NAME."
(if self.parent
(let [l (self.parent:get-rules name)]
(each [_ r (ipairs (. self.rules name))]
(table.insert l r)))
(. self.rules name)))
(fn env.get-rulesets [self]
"Get a list of ruleset names."
(let [l (if self.parent (self.parent:get-rulesets) [])]
(list.append-list l (collect [k _ (pairs (or self.rules {}))] (values k)))
l))
(fn env.get-attribute [self symbol attrib ?default]
"Retreive attribute of SYMBOL for key ATTRIB.
Attributes are just fancy variables whose values are lua values."
(let [value (self:_get (.. symbol "/" attrib))]
(if (not= value nil)
value
?default)))
(fn env.set-attribute [self symbol attrib value]
"Set attribute of SYMBOL for key ATTRIB to VALUE."
(self:_put (.. symbol "/" attrib) value)
self)
(fn env.print [self]
"Print SELF to stdout."
(when self.parent
(self.parent:print)
(print "---"))
(each [k v (pairs self.table)]
(print (string.format "%s: %s" k (expr.to-string v)))))
;; Instantiate global environment
(tset env :global (env.new))
env