blob: 334be533182dd81ef431b64187b47d905a444e4a (
plain)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
(.module:
[lux (#- Module)
[abstract
[monad (#+ do)]]
[control
["." try (#+ Try)]
["." exception (#+ exception:)]]
[data
["." product]
["." name ("#@." equivalence)]
["." text
["%" format (#+ format)]]
[collection
["." row (#+ Row)]
["." dictionary (#+ Dictionary)]
["." list ("#@." functor)]]]]
[//
[synthesis (#+ Synthesis)]
[phase
["." extension]]
[///
["." phase]
[meta
[archive
[descriptor (#+ Module)]]]]])
(type: #export Registry
(Dictionary Name Text))
(exception: #export (cannot-interpret {error Text})
(exception.report
["Error" error]))
(exception: #export (unknown-lux-name {name Name} {registry Registry})
(exception.report
["Name" (%.name name)]
["Registry" (|> registry
dictionary.keys
(list.sort (:: name.order <))
(list@map %.name)
(text.join-with text.new-line))]))
(exception: #export (cannot-overwrite-lux-name {lux-name Name}
{old-host-name Text}
{new-host-name Text})
(exception.report
["Lux Name" (%.name lux-name)]
["Old Host Name" old-host-name]
["New Host Name" new-host-name]))
(template [<name>]
[(exception: #export (<name> {name Name})
(exception.report
["Output" (%.name name)]))]
[cannot-overwrite-output]
[no-buffer-for-saving-code]
)
(type: #export Context
{#scope-name Text
#inner-functions Nat})
(signature: #export (Host expression directive)
(: (-> Text expression (Try Any))
evaluate!)
(: (-> Text directive (Try Any))
execute!)
(: (-> Name expression (Try [Text Any directive]))
define!))
(type: #export (Buffer directive) (Row [Name directive]))
(type: #export (Output directive) (Row [Module (Buffer directive)]))
(type: #export (State anchor expression directive)
{#context Context
#anchor (Maybe anchor)
#host (Host expression directive)
#buffer (Maybe (Buffer directive))
#output (Output directive)
#counter Nat
#name-cache Registry})
(template [<special> <general>]
[(type: #export (<special> anchor expression directive)
(<general> (State anchor expression directive) Synthesis expression))]
[State+ extension.State]
[Operation extension.Operation]
[Phase extension.Phase]
[Handler extension.Handler]
[Bundle extension.Bundle]
)
(def: #export (fresh-context scope-name)
(-> Text Context)
{#scope-name scope-name
#inner-functions 0})
(def: #export (state host)
(All [anchor expression directive]
(-> (Host expression directive)
(..State anchor expression directive)))
{#context (..fresh-context "")
#anchor #.None
#host host
#buffer #.None
#output row.empty
#counter 0
#name-cache (dictionary.new name.hash)})
(def: #export (with-specific-context specific-scope expr)
(All [anchor expression directive output]
(-> Text
(Operation anchor expression directive output)
(Operation anchor expression directive output)))
(function (_ [bundle state])
(let [old (get@ #context state)]
(case (expr [bundle (set@ #context (..fresh-context specific-scope) state)])
(#try.Success [[bundle' state']
output])
(#try.Success [[bundle' (set@ #context old state')]
output])
(#try.Failure error)
(#try.Failure error)))))
(def: #export (with-context expr)
(All [anchor expression directive output]
(-> (Operation anchor expression directive output)
(Operation anchor expression directive [Text output])))
(function (_ [bundle state])
(let [[old-scope old-inner] (get@ #context state)
new-scope (format old-scope "$c" (%.nat old-inner))]
(case (expr [bundle (set@ #context (..fresh-context new-scope) state)])
(#try.Success [[bundle' state'] output])
(#try.Success [[bundle' (set@ #context {#scope-name old-scope
#inner-functions (inc old-inner)}
state')]
[new-scope output]])
(#try.Failure error)
(#try.Failure error)))))
(def: #export context
(All [anchor expression directive]
(Operation anchor expression directive Text))
(extension.read (|>> (get@ #context)
(get@ #scope-name))))
(def: #export empty-buffer Buffer row.empty)
(template [<tag>
<with-declaration> <with-type> <with-value>
<set> <get> <get-type> <exception>]
[(exception: #export <exception>)
(def: #export <with-declaration>
(All [anchor expression directive output] <with-type>)
(function (_ body)
(function (_ [bundle state])
(case (body [bundle (set@ <tag> (#.Some <with-value>) state)])
(#try.Success [[bundle' state'] output])
(#try.Success [[bundle' (set@ <tag> (get@ <tag> state) state')]
output])
(#try.Failure error)
(#try.Failure error)))))
(def: #export <get>
(All [anchor expression directive]
(Operation anchor expression directive <get-type>))
(function (_ (^@ stateE [bundle state]))
(case (get@ <tag> state)
(#.Some output)
(#try.Success [stateE output])
#.None
(exception.throw <exception> []))))
(def: #export (<set> value)
(All [anchor expression directive]
(-> <get-type> (Operation anchor expression directive Any)))
(function (_ [bundle state])
(#try.Success [[bundle (set@ <tag> (#.Some value) state)]
[]])))]
[#anchor
(with-anchor anchor)
(-> anchor (Operation anchor expression directive output)
(Operation anchor expression directive output))
anchor
set-anchor anchor anchor no-anchor]
[#buffer
with-buffer
(-> (Operation anchor expression directive output)
(Operation anchor expression directive output))
..empty-buffer
set-buffer buffer (Buffer directive) no-active-buffer]
)
(def: #export output
(All [anchor expression directive]
(Operation anchor expression directive (Output directive)))
(extension.read (get@ #output)))
(def: #export next
(All [anchor expression directive]
(Operation anchor expression directive Nat))
(do phase.monad
[count (extension.read (get@ #counter))
_ (extension.update (update@ #counter inc))]
(wrap count)))
(def: #export (gensym prefix)
(All [anchor expression directive]
(-> Text (Operation anchor expression directive Text)))
(:: phase.monad map (|>> %.nat (format prefix)) ..next))
(template [<name> <inputT>]
[(def: #export (<name> label code)
(All [anchor expression directive]
(-> Text <inputT> (Operation anchor expression directive Any)))
(function (_ (^@ state+ [bundle state]))
(case (:: (get@ #host state) <name> label code)
(#try.Success output)
(#try.Success [state+ output])
(#try.Failure error)
(exception.throw cannot-interpret error))))]
[evaluate! expression]
[execute! directive]
)
(def: #export (define! name code)
(All [anchor expression directive]
(-> Name expression (Operation anchor expression directive [Text Any directive])))
(function (_ (^@ stateE [bundle state]))
(case (:: (get@ #host state) define! name code)
(#try.Success output)
(#try.Success [stateE output])
(#try.Failure error)
(exception.throw cannot-interpret error))))
(def: #export (save! execute? name code)
(All [anchor expression directive]
(-> Bit Name directive (Operation anchor expression directive Any)))
(do phase.monad
[label (..gensym "save")
_ (if execute?
(execute! label code)
(wrap []))
?buffer (extension.read (get@ #buffer))]
(case ?buffer
(#.Some buffer)
(if (row.any? (|>> product.left (name@= name)) buffer)
(phase.throw ..cannot-overwrite-output name)
(extension.update (set@ #buffer (#.Some (row.add [name code] buffer)))))
#.None
(phase.throw no-buffer-for-saving-code name))))
(def: #export (save-buffer! target)
(All [anchor expression directive]
(-> Module (Operation anchor expression directive (Buffer directive))))
(do phase.monad
[buffer ..buffer
_ (extension.update (update@ #output (row.add [target buffer])))]
(wrap buffer)))
(def: #export (remember lux-name)
(All [anchor expression directive]
(-> Name (Operation anchor expression directive Text)))
(function (_ (^@ stateE [_ state]))
(let [cache (get@ #name-cache state)]
(case (dictionary.get lux-name cache)
(#.Some host-name)
(#try.Success [stateE host-name])
#.None
(exception.throw unknown-lux-name [lux-name cache])))))
(def: #export (learn lux-name host-name)
(All [anchor expression directive]
(-> Name Text (Operation anchor expression directive Any)))
(function (_ [bundle state])
(let [cache (get@ #name-cache state)]
(case (dictionary.get lux-name cache)
#.None
(#try.Success [[bundle
(update@ #name-cache
(dictionary.put lux-name host-name)
state)]
[]])
(#.Some old-host-name)
(exception.throw cannot-overwrite-lux-name [lux-name old-host-name host-name])))))
|