aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/lua/runtime.jvm.lux
blob: e0b037bf5258a67b6f9f633ff458438cc4155330 (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
(.module:
  lux
  (lux (control ["p" parser "p/" Monad<Parser>]
                [monad #+ do])
       (data text/format
             (coll [list "list/" Monad<List>]))
       [macro]
       (macro [code]
              ["s" syntax #+ syntax:])
       [io #+ Process])
  [//]
  (luxc [lang]
        (lang (host [lua #+ Lua Expression Statement]))))

(def: prefix Text "LuxRuntime")

(def: #export unit Expression (%t //.unit))

(def: (flag value)
  (-> Bool Lua)
  (if value
    (lua.string "")
    lua.nil))

(def: (variant' tag last? value)
  (-> Expression Expression Expression Expression)
  (lua.table (list [//.variant-tag-field tag]
                   [//.variant-flag-field last?]
                   [//.variant-value-field value])))

(def: #export (variant tag last? value)
  (-> Nat Bool Expression Expression)
  (variant' (%i (nat-to-int tag)) (flag last?) value))

(def: none
  Expression
  (variant +0 false unit))

(def: some
  (-> Expression Expression)
  (variant +1 true))

(def: left
  (-> Expression Expression)
  (variant +0 false))

(def: right
  (-> Expression Expression)
  (variant +1 true))

(type: Runtime Lua)

(def: declaration
  (s.Syntax [Text (List Text)])
  (p.either (p.seq s.local-symbol (p/wrap (list)))
            (s.form (p.seq s.local-symbol (p.some s.local-symbol)))))

(syntax: (runtime: {[name args] declaration}
           definition)
  (let [implementation (code.local-symbol (format "@@" name))
        runtime (code.text (format "__" prefix "__" (lang.normalize-name name)))
        argsC+ (list/map code.local-symbol args)
        argsLC+ (list/map (|>> lang.normalize-name code.text) args)
        declaration (` ((~ (code.local-symbol name))
                        (~+ argsC+)))
        type (` (-> (~+ (list.repeat (list.size argsC+) (` lua.Lua)))
                    lua.Lua))]
    (wrap (list (` (def: #export (~ declaration)
                     (~ type)
                     (lua.apply (~ runtime) (list (~+ argsC+)))))
                (` (def: (~ implementation)
                     Lua
                     (~ (case argsC+
                          #.Nil
                          (` (lua.global! (~ runtime) (#.Some (~ definition))))

                          _
                          (` (let [(~' @) (~ runtime)
                                   (~+ (|> (list.zip2 argsC+ argsLC+)
                                           (list/map (function (_ [left right]) (list left right)))
                                           list/join))]
                               (lua.function! (~ runtime) (list (~+ argsLC+))
                                              (~ definition))))))))))))

(runtime: (array//copy array)
  (lua.block! (list (lua.local! "temp" (#.Some (lua.array (list))))
                    (lua.for-step! "idx" (lua.int 1) (lua.length array) (lua.int 1)
                                   (lua.apply "table.insert" (list "temp" (lua.nth "idx" array))))
                    (lua.return! "temp"))))

(runtime: (array//sub array from to)
  (lua.block! (list (lua.local! "temp" (#.Some (lua.array (list))))
                    (lua.for-step! "idx" from to (lua.int 1)
                                   (lua.apply "table.insert" (list "temp" (lua.nth "idx" array))))
                    (lua.return! "temp"))))

(runtime: (array//concat left right)
  (let [copy! (function (_ input output)
                (lua.for-step! "idx" (lua.int 1) (format input ".n") (lua.int 1)
                               (lua.apply "table.insert" (list output (lua.nth "idx" input)))))]
    (lua.block! (list (lua.local! "temp" (#.Some (lua.array (list))))
                      (copy! left "temp")
                      (copy! right "temp")
                      (lua.return! "temp")))))

(runtime: (lux//try op)
  (lua.block! (list (format "local success, value = " (lua.apply "pcall" (list (lua.function (list) (lua.return! (lua.apply op (list unit)))))) ";")
                    (lua.if! "success"
                             (lua.return! (right "value"))
                             (lua.return! (left "value"))))))

(runtime: (lux//program-args program-args)
  (lua.block! (list (lua.local! "inputs" (#.Some none))
                    (lua.for-step! "idx" (lua.length program-args) (lua.int 1) (lua.int -1)
                                   (lua.set! "inputs" (some (lua.array (list (lua.nth "idx" program-args)
                                                                             "inputs")))))
                    (lua.return! "inputs"))))

(def: runtime//lux
  Runtime
  (format @@lux//try
          @@lux//program-args))

(runtime: (product//left product index)
  (lua.block! (list (lua.local! "index_min_length" (#.Some (lua.+ (lua.int 1) index)))
                    (lua.if! (lua.>= "index_min_length" (lua.length product))
                             ## No need for recursion
                             (lua.return! (lua.nth "index_min_length" product))
                             ## Needs recursion
                             (lua.return! (product//left (lua.nth (lua.length product)
                                                                  product)
                                                         (lua.- (lua.length product)
                                                                "index_min_length")))))))

(runtime: (product//right product index)
  (lua.block! (list (lua.local! "index_min_length" (#.Some (lua.+ (lua.int 1) index)))
                    (lua.cond! (list [(lua.= "index_min_length" (lua.length product))
                                      ## Last element.
                                      (lua.return! (lua.nth "index_min_length" product))]
                                     [(lua.< "index_min_length" (lua.length product))
                                      ## Needs recursion
                                      (lua.return! (product//right (lua.nth (lua.length product)
                                                                            product)
                                                                   (lua.- (lua.length product)
                                                                          "index_min_length")))])
                               ## Must slice
                               (lua.return! (array//sub product "index_min_length" (lua.length product)))))))

(runtime: (sum//get sum wantedTag wantsLast)
  (let [no-match! (lua.return! lua.nil)
        sum-tag (format "sum." //.variant-tag-field)
        sum-flag (format "sum." //.variant-flag-field)
        sum-value (format "sum." //.variant-value-field)
        is-last? (lua.= (lua.string "") sum-flag)
        test-recursion! (lua.if! is-last?
                                 ## Must recurse.
                                 (lua.return! (sum//get sum-value (lua.- sum-tag wantedTag) wantsLast))
                                 no-match!)]
    (lua.cond! (list [(lua.= sum-tag wantedTag)
                      (lua.if! (lua.= wantsLast sum-flag)
                               (lua.return! sum-value)
                               test-recursion!)]

                     [(lua.> sum-tag wantedTag)
                      test-recursion!]

                     [(lua.and (lua.< sum-tag wantedTag)
                               (lua.= (lua.string "") wantsLast))
                      (lua.return! (variant' (lua.- wantedTag sum-tag) sum-flag sum-value))])

               no-match!)))

(def: runtime//adt
  Runtime
  (format @@product//left
          @@product//right
          @@sum//get))

(runtime: (bit//shift-right param subject)
  (let [mask (|> (lua.int 1)
                 (lua.bit-shl (lua.- param (lua.int 64)))
                 (lua.- (lua.int 1)))]
    (lua.return! (|> subject
                     (lua.bit-shr param)
                     (lua.bit-and mask)))))

(runtime: (bit//count subject)
  (lua.block! (list (lua.local! "count" (#.Some (lua.int 0)))
                    (lua.while! (lua.> (lua.int 0) subject)
                                (lua.block! (list (lua.set! "count" (lua.+ (lua.% (lua.int 2) subject)
                                                                           "count"))
                                                  (lua.set! subject (lua.// (lua.int 2) subject)))))
                    (lua.return! "count"))))

(def: runtime//bit
  Runtime
  (format @@bit//count
          @@bit//shift-right))

(runtime: (nat//< param subject)
  (lua.return! (lua.apply "math.ult" (list subject param))))

(runtime: (nat/// param subject)
  (lua.if! (lua.< (lua.int 0) param)
           (lua.if! (nat//< param subject)
                    (lua.return! (lua.int 0))
                    (lua.return! (lua.int 1)))
           (lua.block! (list (lua.local! "quotient" (#.Some (|> subject
                                                                (lua.bit-shr (lua.int 1))
                                                                (lua.// param)
                                                                (lua.bit-shl (lua.int 1)))))
                             (lua.local! "remainder" (#.Some (lua.- (lua.* param "quotient")
                                                                    subject)))
                             (lua.if! (lua.not (nat//< param "remainder"))
                                      (lua.return! (lua.+ (lua.int 1) "quotient"))
                                      (lua.return! "quotient"))))))

(runtime: (nat//% param subject)
  (let [flat (|> subject
                 (nat/// param)
                 (lua.* param))]
    (lua.return! (lua.- flat subject))))

(def: runtime//nat
  Runtime
  (format @@nat//<
          @@nat///
          @@nat//%))

(runtime: deg//low-mask
  (|> (lua.int 1)
      (lua.bit-shl (lua.int 32))
      (lua.- (lua.int 1))))

(runtime: (deg//* param subject)
  (lua.block! (list (lua.local! "sL" (#.Some (lua.bit-and deg//low-mask subject)))
                    (lua.local! "sH" (#.Some (bit//shift-right (lua.int 32) subject)))
                    (lua.local! "pL" (#.Some (lua.bit-and deg//low-mask param)))
                    (lua.local! "pH" (#.Some (bit//shift-right (lua.int 32) param)))
                    (lua.local! "bottom" (#.Some (bit//shift-right (lua.int 32)
                                                                   (lua.* "pL" "sL"))))
                    (lua.local! "middle" (#.Some (lua.+ (lua.* "pL" "sH")
                                                        (lua.* "pH" "sL"))))
                    (lua.local! "top" (#.Some (lua.* "pH" "sH")))
                    (lua.return! (|> "bottom"
                                     (lua.+ "middle")
                                     (bit//shift-right (lua.int 32))
                                     (lua.+ "top"))))))

(runtime: (deg//leading-zeroes input)
  (lua.block! (list (lua.local! "zeroes" (#.Some (lua.int 64)))
                    (lua.while! (lua.not (lua.= (lua.int 0) input))
                                (lua.block! (list (lua.set! "zeroes" (lua.- (lua.int 1) "zeroes"))
                                                  (lua.set! input (bit//shift-right (lua.int 1) input)))))
                    (lua.return! "zeroes"))))

(runtime: (deg/// param subject)
  (lua.if! (lua.= param subject)
           (lua.return! (lua.int -1))
           (lua.block! (list (lua.local! "min_shift" (#.Some (lua.apply "math.min" (list (deg//leading-zeroes param)
                                                                                         (deg//leading-zeroes subject)))))
                             (lua.return! (|> (lua.bit-shl "min_shift" subject)
                                              (lua.// (|> (lua.bit-shl "min_shift" param)
                                                          (lua.bit-and deg//low-mask)))
                                              (lua.bit-shl (lua.int 32))))))))

(runtime: (deg//from-frac input)
  (let [->int (|>> (list) (lua.apply "math.floor"))]
    (lua.block! (list (lua.local! "two32" (#.Some (lua.apply "math.pow" (list (lua.float 2.0) (lua.float 32.0)))))
                      (lua.local! "shifted" (#.Some (|> input
                                                        (lua.% (lua.float 1.0))
                                                        (lua.* "two32"))))
                      (lua.local! "low" (#.Some (|> "shifted"
                                                    (lua.% (lua.float 1.0))
                                                    (lua.* "two32")
                                                    ->int)))
                      (lua.local! "high" (#.Some (|> "shifted" ->int)))
                      (lua.return! (lua.+ (lua.bit-shl (lua.int 32) "high")
                                          "low"))))))

(def: runtime//deg
  Runtime
  (format @@deg//low-mask
          @@deg//*
          @@deg//leading-zeroes
          @@deg///
          @@deg//from-frac))

(runtime: (text//index subject param start)
  (lua.block! (list (lua.local! "idx" (#.Some (lua.apply "string.find" (list subject param start (lua.bool true)))))
                    (lua.if! (lua.= lua.nil "idx")
                             (lua.return! none)
                             (lua.return! (some "idx"))))))

(runtime: (text//clip text from to)
  (lua.block! (list (lua.local! "size" (#.Some (lua.apply "string.len" (list text))))
                    (lua.if! (lua.or (lua.> "size" from)
                                     (lua.> "size" to))
                             (lua.return! none)
                             (lua.return! (some (lua.apply "string.sub" (list text from to))))))))

(runtime: (text//replace-once text to-find replacement)
  (let [find-index (lua.apply "string.find" (list text to-find (lua.int 1) (lua.bool true)))]
    (lua.block! (list (lua.local! "findSize" (#.Some (lua.apply "string.len" (list to-find))))
                      (lua.local! "parts" (#.Some (lua.array (list))))
                      (lua.local! "idx" (#.Some find-index))
                      (lua.when! (lua.not (lua.= lua.nil "idx"))
                                 (let [find-pre (lua.apply "string.sub" (list text (lua.int 1) "idx"))
                                       find-post (lua.apply "string.sub" (list text "idx" (lua.+ "idx" "findSize")))]
                                   (lua.block! (list (lua.apply "table.insert" (list "parts" find-pre))
                                                     (lua.apply "table.insert" (list "parts" replacement))
                                                     (lua.set! text find-post)))))
                      (lua.apply "table.insert" (list "parts" text))
                      (lua.return! (lua.apply "table.concat" (list "parts")))))))

(runtime: (text//replace-all text to-find replacement)
  (let [find-index (lua.apply "string.find" (list text to-find (lua.int 1) (lua.bool true)))]
    (lua.block! (list (lua.local! "findSize" (#.Some (lua.apply "string.len" (list to-find))))
                      (lua.local! "parts" (#.Some (lua.array (list))))
                      (lua.local! "idx" (#.Some find-index))
                      (lua.while! (lua.not (lua.= lua.nil "idx"))
                                  (let [find-pre (lua.apply "string.sub" (list text (lua.int 1) (lua.- (lua.int 1) "idx")))
                                        find-post (lua.apply "string.sub" (list text (lua.+ "findSize" "idx")))]
                                    (lua.block! (list (lua.apply "table.insert" (list "parts" find-pre))
                                                      (lua.apply "table.insert" (list "parts" replacement))
                                                      (lua.set! text find-post)
                                                      (lua.set! "idx" find-index)))))
                      (lua.apply "table.insert" (list "parts" text))
                      (lua.return! (lua.apply "table.concat" (list "parts")))))))

(runtime: (text//char text idx)
  (lua.block! (list (lua.local! "char" (#.Some (lua.apply "string.byte" (list text idx))))
                    (lua.if! (lua.= lua.nil "char")
                             (lua.return! none)
                             (lua.return! (some "char"))))))

(runtime: (text//hash input)
  (lua.block! (list (lua.local! "hash" (#.Some (lua.int 0)))
                    (lua.for-step! "idx" (lua.int 1) (lua.apply "string.len" (list input)) (lua.int 1)
                                   (lua.set! "hash" (|> "hash"
                                                        (lua.bit-shl (lua.int 5))
                                                        (lua.- "hash")
                                                        (lua.+ (lua.apply "string.byte" (list input "idx"))))))
                    (lua.return! "hash"))))

(def: runtime//text
  Runtime
  (format @@text//index
          @@text//clip
          @@text//replace-once
          @@text//replace-all
          @@text//char
          @@text//hash))

(def: (check-index-out-of-bounds array idx body!)
  (-> Expression Expression Statement Statement)
  (lua.if! (lua.<= (lua.length array)
                   idx)
           body!
           (lua.error (lua.string "Array index out of bounds!"))))

(runtime: (array//new size)
  (lua.block! (list (lua.local! "output" (#.Some (lua.array (list))))
                    (lua.for-step! "idx" (lua.int 1) size (lua.int 1)
                                   (lua.apply "table.insert" (list "output" unit)))
                    (lua.return! "output"))))

(runtime: (array//get array idx)
  (<| (check-index-out-of-bounds array idx)
      (lua.block! (list (lua.local! "temp" (#.Some (lua.nth idx array)))
                        (lua.if! (lua.or (lua.= lua.nil "temp")
                                         (lua.= unit "temp"))
                                 (lua.return! none)
                                 (lua.return! (some "temp")))))))

(runtime: (array//put array idx value)
  (<| (check-index-out-of-bounds array idx)
      (lua.block! (list (lua.set! (lua.nth idx array) value)
                        (lua.return! array)))))

(def: runtime//array
  Runtime
  (format @@array//sub
          @@array//concat
          @@array//copy
          @@array//new
          @@array//get
          @@array//put
          ))

(def: #export atom//field Text "_lux_atom")

(runtime: (atom//compare-and-swap atom old new)
  (let [atom//field (lua.string atom//field)]
    (lua.if! (lua.= old (lua.nth atom//field atom))
             (lua.block! (list (lua.set! (lua.nth atom//field atom) new)
                               (lua.return! (lua.bool true))))
             (lua.return! (lua.bool false)))))

(def: runtime//atom
  Runtime
  (format @@atom//compare-and-swap))

(runtime: (box//write value box)
  (lua.block! (list (lua.set! (lua.nth (lua.int 0) box)
                              value)
                    (lua.return! unit))))

(def: runtime//box
  Runtime
  (format @@box//write))

(def: process//incoming
  Text
  (lang.normalize-name "process//incoming"))

(runtime: (process//loop _)
  (let [migrate-incoming! (lua.block! (list (lua.for-step! "idx" (lua.int 1) (lua.length process//incoming) (lua.int 1)
                                                           (lua.apply "table.insert" (list "queue" (lua.nth "idx" process//incoming))))
                                            (lua.set! process//incoming (lua.array (list)))))
        consume-queue! (lua.block! (list (lua.local! "survivors" (#.Some (lua.array (list))))
                                         (lua.local! "active_processes" (#.Some (lua.length "queue")))
                                         (lua.for-step! "idx" (lua.int 1) "active_processes" (lua.int 1)
                                                        (lua.block! (list (lua.local! "process" (#.Some (lua.nth "idx" "queue")))
                                                                          (lua.when! (lua.apply "coroutine.resume" (list "process"))
                                                                                     (lua.apply "table.insert" (list "survivors" "process"))))))
                                         (lua.set! "queue" "survivors")))
        loop-body! (lua.block! (list migrate-incoming!
                                     consume-queue!))]
    (lua.block! (list (lua.local! "queue" (#.Some (lua.array (list))))
                      loop-body!
                      (lua.while! (|> (lua.length "queue") (lua.> (lua.int 0)))
                                  loop-body!)))))

(runtime: (process//future procedure)
  (lua.block! (list (lua.apply "table.insert" (list process//incoming
                                                    (lua.function (list)
                                                      (lua.return! (lua.apply procedure (list unit))))))
                    (lua.return! unit))))

(runtime: (process//schedule milli-seconds procedure)
  (let [now (lua.apply "os.time" (list))]
    (lua.block! (list (lua.local! "start" (#.Some now))
                      (lua.local! "seconds" (#.Some (lua.// (lua.int 1_000)
                                                            milli-seconds)))
                      (lua.apply "table.insert" (list process//incoming
                                                      (lua.function (list)
                                                        (lua.block! (list (lua.while! (lua.< "seconds" (lua.- "start" now))
                                                                                      (lua.apply "coroutine.yield" (list)))
                                                                          (lua.return! (lua.apply procedure (list unit))))))))
                      (lua.return! unit)))))

(def: runtime//process
  Runtime
  (format (lua.global! process//incoming (#.Some (lua.array (list))))
          @@process//loop
          @@process//future
          @@process//schedule))

(runtime: (lua//get object field)
  (lua.block! (list (lua.local! "value" (#.Some (lua.nth field object)))
                    (lua.if! (lua.= lua.nil "value")
                             (lua.return! none)
                             (lua.return! (some "value"))))))

(runtime: (lua//set object field value)
  (lua.block! (list (lua.set! (lua.nth field object) value)
                    (lua.return! object))))

(def: runtime//lua
  Runtime
  (format @@lua//get
          @@lua//set))

(def: runtime
  Runtime
  (format runtime//lux
          runtime//adt
          runtime//bit
          runtime//nat
          runtime//deg
          runtime//text
          runtime//array
          runtime//atom
          runtime//box
          runtime//process
          runtime//lua))

(def: #export artifact Text (format prefix ".lua"))

(def: #export translate
  (Meta (Process Top))
  (do macro.Monad<Meta>
    [_ //.init-module-buffer
     _ (//.save runtime)]
    (//.save-module! artifact)))