blob: 0b84f4741e45f2717fc8daefa3a5a6ea40a69c87 (
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
(.module:
[lux (#- inc)
[abstract
[monad (#+ do)]]
[control
["." function]
["p" parser]]
[data
[number (#+ hex)
["." i64]]
["." text
format]
[collection
["." list ("#@." functor)]]]
["." macro
["." code]
["s" syntax (#+ syntax:)]]
[host
["_" python (#+ Expression SVar Computation Literal Statement)]]]
["." ///
["//." //
[//
["/////." name]
["." synthesis]]]]
)
(template [<name> <base>]
[(type: #export <name>
(<base> SVar (Expression Any) (Statement Any)))]
[Operation ///.Operation]
[Phase ///.Phase]
[Handler ///.Handler]
[Bundle ///.Bundle]
)
(def: prefix Text "LuxRuntime")
(def: #export unit (_.string synthesis.unit))
(def: (flag value)
(-> Bit (Computation Any))
(if value
(_.string "")
_.none))
(def: (variant' tag last? value)
(-> (Expression Any) (Expression Any) (Expression Any) (Computation Any))
(_.tuple (list tag last? value)))
(def: #export (variant tag last? value)
(-> Nat Bit (Expression Any) (Computation Any))
(variant' (_.int (.int tag))
(flag last?)
value))
(def: #export none
(Computation Any)
(..variant 0 #0 unit))
(def: #export some
(-> (Expression Any) (Computation Any))
(..variant 1 #1))
(def: #export left
(-> (Expression Any) (Computation Any))
(..variant 0 #0))
(def: #export right
(-> (Expression Any) (Computation Any))
(..variant 1 #1))
(def: runtime-name
(-> Text SVar)
(|>> /////name.normalize
(format ..prefix "_")
_.var))
(def: (feature name definition)
(-> SVar (-> SVar (Statement Any)) (Statement Any))
(definition name))
(syntax: #export (with-vars {vars (s.tuple (p.some s.local-identifier))}
body)
(wrap (list (` (let [(~+ (|> vars
(list@map (function (_ var)
(list (code.local-identifier var)
(` (_.var (~ (code.text (/////name.normalize var))))))))
list.concat))]
(~ body))))))
(syntax: (runtime: {declaration (p.or s.local-identifier
(s.form (p.and s.local-identifier
(p.some s.local-identifier))))}
code)
(case declaration
(#.Left name)
(macro.with-gensyms [g!_]
(let [nameC (code.local-identifier name)
code-nameC (code.local-identifier (format "@" name))
runtime-nameC (` (runtime-name (~ (code.text name))))]
(wrap (list (` (def: #export (~ nameC) SVar (~ runtime-nameC)))
(` (def: (~ code-nameC)
(Statement Any)
(..feature (~ runtime-nameC)
(function ((~ g!_) (~ nameC))
(~ code)))))))))
(#.Right [name inputs])
(macro.with-gensyms [g!_]
(let [nameC (code.local-identifier name)
code-nameC (code.local-identifier (format "@" name))
runtime-nameC (` (runtime-name (~ (code.text name))))
inputsC (list@map code.local-identifier inputs)
inputs-typesC (list@map (function.constant (` (_.Expression Any)))
inputs)]
(wrap (list (` (def: #export ((~ nameC) (~+ inputsC))
(-> (~+ inputs-typesC) (Computation Any))
(_.apply/* (~ runtime-nameC) (list (~+ inputsC)))))
(` (def: (~ code-nameC)
(Statement Any)
(..feature (~ runtime-nameC)
(function ((~ g!_) (~ g!_))
(..with-vars [(~+ inputsC)]
(_.def (~ g!_) (list (~+ inputsC))
(~ code)))))))))))))
(runtime: (lux//try op)
(with-vars [error value]
(_.try ($_ _.then
(_.set (list value) (_.apply/* op (list unit)))
(_.return (right value)))
(list [(list (_.var "Exception")) error
(_.return (left (_.str/1 error)))]))))
(runtime: (lux//program-args program-args)
(with-vars [inputs value]
($_ _.then
(_.set (list inputs) none)
(<| (_.for-in value program-args)
(_.set (list inputs)
(some (_.tuple (list value inputs)))))
(_.return inputs))))
(def: runtime//lux
(Statement Any)
($_ _.then
@lux//try
@lux//program-args))
(runtime: (io//log! message)
($_ _.then
(_.print message)
(_.return ..unit)))
(runtime: (io//throw! message)
($_ _.then
(_.raise (_.Exception/1 message))
(_.return ..unit)))
(runtime: (io//exit! code)
($_ _.then
(_.import "sys")
(_.statement (|> (_.var "sys") (_.do "exit" (list code))))
(_.return ..unit)))
(runtime: (io//current-time! _)
($_ _.then
(_.import "time")
(_.return (|> (_.var "time")
(_.do "time" (list))
(_.* (_.int +1,000))
_.int/1))))
(def: runtime//io
(Statement Any)
($_ _.then
@io//log!
@io//throw!
@io//exit!
@io//current-time!))
(def: last-index
(|>> _.len/1 (_.- (_.int +1))))
(runtime: (tuple//left lefts tuple)
(with-vars [last-index-right]
($_ _.then
(_.set (list last-index-right) (..last-index tuple))
(_.if (_.> lefts last-index-right)
## No need for recursion
(_.return (_.nth lefts tuple))
## Needs recursion
(_.return (tuple//left (_.- last-index-right lefts)
(_.nth last-index-right tuple)))))))
(runtime: (tuple//right lefts tuple)
(with-vars [last-index-right right-index]
($_ _.then
(_.set (list last-index-right) (..last-index tuple))
(_.set (list right-index) (_.+ (_.int +1) lefts))
(_.cond (list [(_.= right-index last-index-right)
(_.return (_.nth right-index tuple))]
[(_.> right-index last-index-right)
## Needs recursion.
(_.return (tuple//right (_.- last-index-right lefts)
(_.nth last-index-right tuple)))])
(_.return (_.slice-from right-index tuple)))
)))
(runtime: (sum//get sum wantsLast wantedTag)
(let [no-match! (_.return _.none)
sum-tag (_.nth (_.int +0) sum)
sum-flag (_.nth (_.int +1) sum)
sum-value (_.nth (_.int +2) sum)
is-last? (_.= (_.string "") sum-flag)
test-recursion! (_.if is-last?
## Must recurse.
(_.return (sum//get sum-value wantsLast (_.- sum-tag wantedTag)))
no-match!)]
(_.cond (list [(_.= sum-tag wantedTag)
(_.if (_.= wantsLast sum-flag)
(_.return sum-value)
test-recursion!)]
[(_.> sum-tag wantedTag)
test-recursion!]
[(_.and (_.< sum-tag wantedTag)
(_.= (_.string "") wantsLast))
(_.return (variant' (_.- wantedTag sum-tag) sum-flag sum-value))])
no-match!)))
(def: runtime//adt
(Statement Any)
($_ _.then
@tuple//left
@tuple//right
@sum//get))
(def: full-64-bits
Literal
(_.manual "0xFFFFFFFFFFFFFFFF"))
(runtime: (i64//64 input)
(with-vars [capped]
(_.cond (list [(|> input (_.> full-64-bits))
(_.return (|> input (_.bit-and full-64-bits) i64//64))]
[(|> input (_.> (: Literal (_.manual "0x7FFFFFFFFFFFFFFF"))))
($_ _.then
(_.set (list capped)
(_.int/1 (|> (: Literal (_.manual "0x10000000000000000"))
(_.- input))))
(_.if (|> capped (_.<= (: Literal (_.manual "9223372036854775807L"))))
(_.return (|> capped (_.* (_.int -1))))
(_.return (: Literal (_.manual "-9223372036854775808L")))))])
(_.return input))))
(runtime: (i64//logic-right-shift param subject)
(let [mask (|> (_.int +1)
(_.bit-shl (_.- param (_.int +64)))
(_.- (_.int +1)))]
(_.return (|> subject
(_.bit-shr param)
(_.bit-and mask)))))
(def: runtime//i64
(Statement Any)
($_ _.then
@i64//64
@i64//logic-right-shift))
(runtime: (frac//decode input)
(with-vars [ex]
(_.try
(_.return (..some (_.float/1 input)))
(list [(list (_.var "Exception")) ex
(_.return ..none)]))))
(def: runtime//frac
(Statement Any)
($_ _.then
@frac//decode))
(runtime: (text//index subject param start)
(with-vars [idx]
($_ _.then
(_.set (list idx) (|> subject (_.do "find" (list param start))))
(_.if (_.= (_.int -1) idx)
(_.return ..none)
(_.return (..some idx))))))
(def: inc (|>> (_.+ (_.int +1))))
(def: (within? top value)
(-> (Expression Any) (Expression Any) (Computation Any))
(_.and (|> value (_.>= (_.int +0)))
(|> value (_.< top))))
(runtime: (text//clip @text @from @to)
(_.return (|> @text (_.slice @from (inc @to)))))
(runtime: (text//char idx text)
(_.if (|> idx (within? (_.len/1 text)))
(_.return (..some (_.ord/1 (|> text (_.slice idx (inc idx))))))
(_.return ..none)))
(def: runtime//text
(Statement Any)
($_ _.then
@text//index
@text//clip
@text//char))
(runtime: (array//get array idx)
(with-vars [temp]
($_ _.then
(_.set (list temp) (_.nth idx array))
(_.if (_.= _.none temp)
(_.return ..none)
(_.return (..some temp))))))
(runtime: (array//put array idx value)
($_ _.then
(_.set (list (_.nth idx array)) value)
(_.return array)))
(def: runtime//array
(Statement Any)
($_ _.then
@array//get
@array//put))
(runtime: (box//write value box)
($_ _.then
(_.set (list (_.nth (_.int +0) box)) value)
(_.return ..unit)))
(def: runtime//box
(Statement Any)
@box//write)
(def: runtime
(Statement Any)
($_ _.then
runtime//lux
runtime//adt
runtime//i64
runtime//frac
runtime//text
runtime//array
runtime//box
runtime//io
))
(def: #export artifact ..prefix)
(def: #export generate
(Operation Any)
(///.with-buffer
(do ////.monad
[_ (///.save! ["" ..prefix] (<| (_.comment "-*- coding: utf-8 -*-")
..runtime))]
(///.save-buffer! ..artifact))))
|