blob: 3cca0c06077f17ffae41b92440cc06723306fc1d (
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
|
(.module:
[lux #*
[control
["ex" exception (#+ exception:)]
[monad (#+ do)]
pipe]
[data
["." product]
["." error (#+ Error)]
["." name ("name/." Equivalence<Name>)]
["." text
format]
[collection
["." row (#+ Row)]
["." dictionary (#+ Dictionary)]]]
[world
[file (#+ File)]]]
["." //
["." extension]]
[//synthesis (#+ Synthesis)])
(do-template [<name>]
[(exception: #export (<name>)
"")]
[no-active-buffer]
[no-anchor]
)
(exception: #export (cannot-interpret {error Text})
(ex.report ["Error" error]))
(exception: #export (unknown-lux-name {name Name})
(ex.report ["Name" (%name name)]))
(exception: #export (cannot-overwrite-lux-name {lux-name Name}
{old-host-name Text}
{new-host-name Text})
(ex.report ["Lux Name" (%name lux-name)]
["Old Host Name" old-host-name]
["New Host Name" new-host-name]))
(do-template [<name>]
[(exception: #export (<name> {name Name})
(ex.report ["Artifact" (%name name)]))]
[cannot-overwrite-artifact]
[no-buffer-for-saving-code]
)
(type: #export Context
{#scope-name Text
#inner-functions Nat})
(signature: #export (Host expression statement)
(: (-> Text expression (Error Any))
evaluate!)
(: (-> Text statement (Error Any))
execute!)
(: (-> Name expression (Error [Text Any]))
define!))
(type: #export (Buffer statement) (Row [Name statement]))
(type: #export (Artifacts statement) (Dictionary File (Buffer statement)))
(type: #export (State anchor expression statement)
{#context Context
#anchor (Maybe anchor)
#host (Host expression statement)
#buffer (Maybe (Buffer statement))
#artifacts (Artifacts statement)
#counter Nat
#name-cache (Dictionary Name Text)})
(do-template [<special> <general>]
[(type: #export (<special> anchor expression statement)
(<general> (State anchor expression statement) Synthesis expression))]
[State+ extension.State]
[Operation extension.Operation]
[Phase extension.Phase]
[Handler extension.Handler]
[Bundle extension.Bundle]
)
(def: #export (state host)
(All [anchor expression statement]
(-> (Host expression statement)
(..State anchor expression statement)))
{#context {#scope-name ""
#inner-functions 0}
#anchor #.None
#host host
#buffer #.None
#artifacts (dictionary.new text.Hash<Text>)
#counter 0
#name-cache (dictionary.new name.Hash<Name>)})
(def: #export (with-context expr)
(All [anchor expression statement output]
(-> (Operation anchor expression statement output)
(Operation anchor expression statement [Text output])))
(function (_ [bundle state])
(let [[old-scope old-inner] (get@ #context state)
new-scope (format old-scope "c___" (%i (.int old-inner)))]
(case (expr [bundle (set@ #context [new-scope 0] state)])
(#error.Success [[bundle' state'] output])
(#error.Success [[bundle' (set@ #context [old-scope (inc old-inner)] state')]
[new-scope output]])
(#error.Error error)
(#error.Error error)))))
(def: #export context
(All [anchor expression statement]
(Operation anchor expression statement Text))
(extension.read (|>> (get@ #context)
(get@ #scope-name))))
(do-template [<tag>
<with-declaration> <with-type> <with-value>
<get> <get-type> <exception>]
[(def: #export <with-declaration>
(All [anchor expression statement output] <with-type>)
(function (_ body)
(function (_ [bundle state])
(case (body [bundle (set@ <tag> (#.Some <with-value>) state)])
(#error.Success [[bundle' state'] output])
(#error.Success [[bundle' (set@ <tag> (get@ <tag> state) state')]
output])
(#error.Error error)
(#error.Error error)))))
(def: #export <get>
(All [anchor expression statement]
(Operation anchor expression statement <get-type>))
(function (_ (^@ stateE [bundle state]))
(case (get@ <tag> state)
(#.Some output)
(#error.Success [stateE output])
#.None
(ex.throw <exception> []))))]
[#anchor
(with-anchor anchor)
(-> anchor (Operation anchor expression statement output)
(Operation anchor expression statement output))
anchor
anchor anchor no-anchor]
[#buffer
with-buffer
(-> (Operation anchor expression statement output)
(Operation anchor expression statement output))
row.empty
buffer (Buffer statement) no-active-buffer]
)
(def: #export artifacts
(All [anchor expression statement]
(Operation anchor expression statement (Artifacts statement)))
(extension.read (get@ #artifacts)))
(def: #export next
(All [anchor expression statement]
(Operation anchor expression statement Nat))
(do //.Monad<Operation>
[_ (extension.update (update@ #counter inc))]
(extension.read (get@ #counter))))
(def: (temp-label state)
(All [anchor expression statement]
(-> (State anchor expression statement) Text))
(format (get@ [#context #scope-name] state) " " (%n (get@ #counter state))))
(do-template [<name> <inputT>]
[(def: #export (<name> code)
(All [anchor expression statement]
(-> <inputT> (Operation anchor expression statement Any)))
(function (_ [bundle state])
(case (:: (get@ #host state) <name> (temp-label state) code)
(#error.Error error)
(ex.throw cannot-interpret error)
(#error.Success output)
(#error.Success [[bundle (update@ #counter inc state)]
output]))))]
[evaluate! expression]
[execute! statement]
)
(def: #export (define! name code)
(All [anchor expression statement]
(-> Name expression (Operation anchor expression statement [Text Any])))
(function (_ (^@ stateE [bundle state]))
(case (:: (get@ #host state) define! name code)
(#error.Error error)
(ex.throw cannot-interpret error)
(#error.Success output)
(#error.Success [stateE output]))))
(def: #export (save! name code)
(All [anchor expression statement]
(-> Name statement (Operation anchor expression statement Any)))
(do //.Monad<Operation>
[_ (execute! code)
?buffer (extension.read (get@ #buffer))]
(case ?buffer
(#.Some buffer)
(if (row.any? (|>> product.left (name/= name)) buffer)
(//.throw cannot-overwrite-artifact name)
(extension.update (set@ #buffer (#.Some (row.add [name code] buffer)))))
#.None
(//.throw no-buffer-for-saving-code name))))
(def: #export (save-buffer! target)
(All [anchor expression statement]
(-> File (Operation anchor expression statement Any)))
(do //.Monad<Operation>
[buffer ..buffer]
(extension.update (update@ #artifacts (dictionary.put target buffer)))))
(def: #export (remember lux-name)
(All [anchor expression statement]
(-> Name (Operation anchor expression statement Text)))
(function (_ (^@ stateE [_ state]))
(|> state
(get@ #name-cache)
(dictionary.get lux-name)
(case> (#.Some host-name)
(#error.Success [stateE host-name])
#.None
(ex.throw unknown-lux-name lux-name)))))
(def: #export (learn lux-name host-name)
(All [anchor expression statement]
(-> Name Text (Operation anchor expression statement Any)))
(function (_ [bundle state])
(let [cache (get@ #name-cache state)]
(case (dictionary.get lux-name cache)
(#.Some old-host-name)
(ex.throw cannot-overwrite-lux-name [lux-name old-host-name host-name])
#.None
(#error.Success [[bundle
(update@ #name-cache
(dictionary.put lux-name host-name)
state)]
[]])))))
|