-
Notifications
You must be signed in to change notification settings - Fork 54
/
beta.cljc
254 lines (197 loc) · 6.87 KB
/
beta.cljc
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
(ns meander.substitute.beta
#?(:cljs (:require-macros [meander.substitute.beta]))
(:require [clojure.spec.alpha :as s]
[meander.syntax.beta :as r.syntax]
[meander.util.beta :as r.util]))
(defn make-env
"Derives a map of {mvr ref-sym} from the pattern node where
mvr is a memory variable node [:mvr !xs], and ref-sym is a memory
varable symbol with the suffix _ref__<digits> e.g. !xs_ref__234."
{:private true}
[pat]
(into {} (map (fn [[_ sym :as mvr]]
[mvr (gensym (str sym "_ref__"))]))
(r.syntax/memory-variables pat)))
(defn get-mvr-ref-sym
"INTERNAL Gets the reference symbol for mvr-sym from env. Throws if
the symbol is not found (sanity check)."
{:private true}
[env [_ mvr-sym :as mvr]]
(if-some [[_ mvr-ref-sym] (find env mvr)]
mvr-ref-sym
(throw
(ex-info (str "No ref symbol found for memory variable " mvr-sym)
{:env env
:mvr mvr}))))
(defn compile-ground
{:private true}
[x]
(cond
(symbol? x)
`(quote ~x)
(seq? x)
(if (= (first x) 'quote)
x
(if (= (first x) `list)
(cons (first x) (map compile-ground (rest x)))
(if (seq x)
(cons `list (map compile-ground x))
())))
(map? x)
(into {}
(map
(fn [[k v]]
[(compile-ground k) (compile-ground v)]))
x)
(coll? x)
(into (empty x) (map compile-ground) x)
:else
x))
(defn compile-substitute-dispatch
[node env]
(r.syntax/tag node))
(defmulti compile-substitute
{:arglists '([node env])}
#'compile-substitute-dispatch)
(defmethod compile-substitute :app
[[_ {form :form, terms :terms}] env]
`(list '~'app '~form ~@(map compile-substitute terms (repeat env))))
(defmethod compile-substitute :cat
[[_ nodes] env]
(if (some #{:uns} (map r.syntax/tag nodes))
`(concat ~@(sequence (map
(fn [[tag :as node] env]
(let [form (compile-substitute node env)]
(if (= :uns tag)
form
`(list ~form)))))
nodes
(repeat env)))
`(list ~@(sequence (map compile-substitute) nodes (repeat env)))))
(defmethod compile-substitute :cnj
[[_ {terms :terms}] env]
`(list '~'and ~@(map compile-substitute terms (repeat env))))
(defmethod compile-substitute :drp
[_ _]
`(list))
(defmethod compile-substitute :dsj
[[_ {terms :terms}] env]
`(list '~'or ~@(map compile-substitute terms (repeat env))))
(defmethod compile-substitute :lit
[[_ x] env]
(compile-ground x))
(defmethod compile-substitute :lvr
[[_ sym] env]
sym)
(defmethod compile-substitute :map [[_ m] env]
`(hash-map ~@(sequence
(comp (mapcat identity)
(map (fn [x]
(compile-substitute x env))))
m)))
(defmethod compile-substitute :mvr [mvr env]
(let [mvr-ref-sym (get-mvr-ref-sym env mvr)]
(let [item (gensym "item__")]
`(when-some [[_# ~item] (find (deref ~mvr-ref-sym) 0)]
(vswap! ~mvr-ref-sym subvec 1)
~item))))
(defmethod compile-substitute :not
[[_ {term :term}] env]
`(list '~'not ~(compile-substitute term env)))
(defmethod compile-substitute :prd
[[_ {form :form, terms :terms}] env]
`(list '~'pred '~form ~@(map compile-substitute terms (repeat env))))
(defmethod compile-substitute :prt
[[_ {l :left, r :right}] env]
(if (and (= (r.syntax/tag l) :cat)
(zero? (count (second l))))
(when r
(compile-substitute r env))
`(concat ~(compile-substitute l env)
~(when r
(compile-substitute r env)))))
(defmethod compile-substitute :quo
[[_ {form :form}] env]
`(quote ~form))
(defmethod compile-substitute :rp* [node env]
(let [[_ {items :items}] node
cat-node [:cat items]
mvrs (r.syntax/memory-variables node)]
(if (seq mvrs)
;; If there are mem-vars, loop until one of them is
;; exhausted.
`(let [ret# (transient [])]
(loop []
(if (and ~@(map
(fn [mvr]
`(seq (deref ~(get-mvr-ref-sym env mvr))))
mvrs))
(do
(run! (fn [x#] (conj! ret# x#)) ~(compile-substitute cat-node env))
(recur))
(persistent! ret#))))
;; If there are no mem-vars, loop forever. This case should
;; either warn or throw!
`(sequence (mapcat identity)
(repeatedly (fn [] ~(compile-substitute cat-node env)))))))
(defmethod compile-substitute :rp+ [node env]
(let [[_ {items :items, dots :dots}] node
n (r.util/parse-int (aget (.split (name dots) "\\.+" 2) 1))
cat-node [:cat items]
mvrs (r.syntax/memory-variables node)]
(if (seq mvrs)
;; If there are mem-vars, loop until one or all of them is
;; exhausted and we've looped at least twice.
`(let [ret# (transient [])]
(loop [n# 0]
(if (and ~@(map
(fn [mvr]
`(seq (deref ~(get-mvr-ref-sym env mvr))))
mvrs))
(do
(run! (fn [x#] (conj! ret# x#)) ~(compile-substitute cat-node env))
(recur (inc n#)))
(if (< n# ~n)
(do
(run! (fn [x#] (conj! ret# x#)) ~(compile-substitute cat-node env))
(recur (inc n#)))
(persistent! ret#)))))
;; If there are no mem-vars, loop forever. This case should
;; either warn or throw!
`(sequence (mapcat identity)
(repeatedly (fn [] ~(compile-substitute cat-node env)))))))
(defmethod compile-substitute :rst [[_ {mvr-sym :mvr}] env]
(let [mvr-ref-sym (get-mvr-ref-sym env mvr-sym)]
;; Memory variable substitution is stateful so we must consume
;; all remaining elements in the memory variable reference to
;; avoid nondeterministic behavior.
`(let [res# (deref ~mvr-ref-sym)]
(vreset! ~mvr-ref-sym [])
res#)))
(defmethod compile-substitute :set [[_ s] env]
`(hash-set ~@(map (fn [x] (compile-substitute x env)) s)))
(defmethod compile-substitute :seq
[node env]
(let [[_ prt] node]
`(seq ~(compile-substitute prt env))))
(defmethod compile-substitute :unq
[[_ {expr :expr}] env]
expr)
(defmethod compile-substitute :uns
[[_ {form :form}] env]
form)
(defmethod compile-substitute :vec
[node env]
(let [[_ prt] node]
`(vec ~(compile-substitute prt env))))
(defmacro substitute [term]
(let [node (r.syntax/parse term)
env (make-env node)]
`(let [~@(mapcat
(fn [[[_ mvr-sym] ref-sym]]
[ref-sym `(volatile! ~mvr-sym)])
env)]
~(compile-substitute node env))))
(s/fdef substitute
:args (s/cat :term any?)
:ret any?)