aboutsummaryrefslogtreecommitdiff
path: root/lux-r/source/luxc/lang/translation/r/runtime.jvm.lux
blob: d641041d2e442d5ce235e1c5fbd8db21862de4cf (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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
(.module:
  lux
  (lux (control ["p" parser "p/" Monad<Parser>]
                [monad #+ do])
       (data [bit]
             [number (#+ hex) ("int/" Interval<Int>)]
             text/format
             (coll [list "list/" Monad<List>]))
       [macro]
       (macro [code]
              ["s" syntax #+ syntax:])
       [io #+ Process])
  [//]
  (luxc [lang]
        (lang (host [r #+ SVar Expression @@]))))

(def: prefix Text "LuxRuntime")

(def: #export unit Expression (r.string //.unit))

(def: full-32 (hex "+FFFFFFFF"))
(def: half-32 (hex "+7FFFFFFF"))
(def: post-32 (hex "+100000000"))

(def: (cap-32 input)
  (-> Nat Int)
  (cond (n/> full-32 input)
        (|> input (bit.and full-32) cap-32)
        
        (n/> half-32 input)
        (|> post-32 (n/- input) .int (i/* -1))
        
        ## else
        (.int input)))

(def: high-32 (bit.logical-right-shift +32))
(def: low-32 (|>> (bit.and (hex "+FFFFFFFF"))))

(def: #export (int value)
  (-> Int Expression)
  (let [value (.nat value)
        high (|> value ..high-32 cap-32)
        low (|> value ..low-32 cap-32)]
    (r.named-list (list [//.int-high-field (r.int high)]
                        [//.int-low-field (r.int low)]))))

(def: (flag value)
  (-> Bit Expression)
  (if value
    (r.string "")
    r.null))

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

(def: #export (variant tag last? value)
  (-> Nat Bit Expression Expression)
  (variant' (r.int (.int tag))
            (flag last?)
            value))

(def: #export none
  Expression
  (variant +0 #0 unit))

(def: #export some
  (-> Expression Expression)
  (variant +1 #1))

(def: #export left
  (-> Expression Expression)
  (variant +0 #0))

(def: #export right
  (-> Expression Expression)
  (variant +1 #1))

(type: Runtime Expression)

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

(syntax: (runtime: {[name args] declaration}
           definition)
  (let [implementation (code.local-identifier (format "@@" name))
        runtime (format prefix "__" (lang.normalize-name name))
        $runtime (` (r.var (~ (code.text runtime))))
        @runtime (` (@@ (~ $runtime)))
        argsC+ (list/map code.local-identifier args)
        argsLC+ (list/map (|>> lang.normalize-name (format "LRV__") code.text (~) (r.var) (`))
                          args)
        declaration (` ((~ (code.local-identifier name))
                        (~+ argsC+)))
        type (` (-> (~+ (list.repeat (list.size argsC+) (` r.Expression)))
                    r.Expression))]
    (wrap (list (` (def: (~' #export) (~ declaration)
                     (~ type)
                     (~ (case argsC+
                          #.Nil
                          @runtime

                          _
                          (` (r.apply (list (~+ argsC+)) (~ @runtime)))))))
                (` (def: (~ implementation)
                     r.Expression
                     (~ (case argsC+
                          #.Nil
                          (` (r.set! (~ $runtime) (~ definition)))

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

(syntax: #export (with-vars {vars (s.tuple (p.many s.local-identifier))}
                   body)
  (wrap (list (` (let [(~+ (|> vars
                               (list/map (function (_ var)
                                           (list (code.local-identifier var)
                                                 (` (r.var (~ (code.text (format "LRV__" (lang.normalize-name var)))))))))
                               list/join))]
                   (~ body))))))

(def: high-shift (r.bit-shl (r.int 32)))

(runtime: f2^32 (|> (r.int 2) (r.** (r.int 32))))
(runtime: f2^63 (|> (r.int 2) (r.** (r.int 63))))

(def: (as-double value)
  (-> Expression Expression)
  (r.apply (list value) (r.global "as.double")))

(def: (as-integer value)
  (-> Expression Expression)
  (r.apply (list value) (r.global "as.integer")))

(runtime: (int//unsigned-low input)
  (with-vars [low]
    ($_ r.then
        (r.set! low (|> (@@ input) (r.nth (r.string //.int-low-field))))
        (r.if (|> (@@ low) (r.>= (r.int 0)))
          (@@ low)
          (|> (@@ low) (r.+ f2^32))))))

(runtime: (int//to-float input)
  (let [high (|> (@@ input)
                 (r.nth (r.string //.int-high-field))
                 high-shift)
        low (|> (@@ input)
                int//unsigned-low)]
    (|> high (r.+ low) as-double)))

(runtime: (int//new high low)
  (r.named-list (list [//.int-high-field (as-integer (@@ high))]
                      [//.int-low-field (as-integer (@@ low))])))

(template [<name> <value>]
  [(runtime: <name>
     (..int <value>))]

  [int//zero 0]
  [int//one 1]
  [int//min int/bottom]
  [int//max int/top]
  )

(def: #export int64-high (r.nth (r.string //.int-high-field)))
(def: #export int64-low (r.nth (r.string //.int-low-field)))

(runtime: (bit//not input)
  (int//new (|> (@@ input) int64-high r.bit-not)
            (|> (@@ input) int64-low r.bit-not)))

(runtime: (int//+ param subject)
  (with-vars [sH sL pH pL
              x00 x16 x32 x48]
    ($_ r.then
        (r.set! sH (|> (@@ subject) int64-high))
        (r.set! sL (|> (@@ subject) int64-low))
        (r.set! pH (|> (@@ param) int64-high))
        (r.set! pL (|> (@@ param) int64-low))
        (let [bits16 (r.code "0xFFFF")
              move-top-16 (r.bit-shl (r.int 16))
              top-16 (r.bit-ushr (r.int 16))
              bottom-16 (r.bit-and bits16)
              split-16 (function (_ source)
                         [(|> source top-16)
                          (|> source bottom-16)])
              split-int (function (_ high low)
                          [(split-16 high)
                           (split-16 low)])
              
              [[s48 s32] [s16 s00]] (split-int (@@ sH) (@@ sL))
              [[p48 p32] [p16 p00]] (split-int (@@ pH) (@@ pL))
              new-half (function (_ top bottom)
                         (|> top bottom-16 move-top-16
                             (r.bit-or (bottom-16 bottom))))]
          ($_ r.then
              (r.set! x00 (|> s00 (r.+ p00)))
              (r.set! x16 (|> (@@ x00) top-16 (r.+ s16) (r.+ p16)))
              (r.set! x32 (|> (@@ x16) top-16 (r.+ s32) (r.+ p32)))
              (r.set! x48 (|> (@@ x32) top-16 (r.+ s48) (r.+ p48)))
              (int//new (new-half (@@ x48) (@@ x32))
                        (new-half (@@ x16) (@@ x00))))))))

(runtime: (int//= reference sample)
  (let [n/a? (function (_ value)
               (r.apply (list value) (r.global "is.na")))
        isTRUE? (function (_ value)
                  (r.apply (list value) (r.global "isTRUE")))
        comparison (: (-> (-> Expression Expression) Expression)
                      (function (_ field)
                        (|> (|> (field (@@ sample)) (r.= (field (@@ reference))))
                            (r.or (|> (n/a? (field (@@ sample)))
                                      (r.and (n/a? (field (@@ reference)))))))))]
    (|> (comparison int64-high)
        (r.and (comparison int64-low))
        isTRUE?)))

(runtime: (int//negate input)
  (r.if (|> (@@ input) (int//= int//min))
    int//min
    (|> (@@ input) bit//not (int//+ int//one))))

(runtime: int//-one
  (int//negate int//one))

(runtime: (int//- param subject)
  (int//+ (int//negate (@@ param)) (@@ subject)))

(runtime: (int//< reference sample)
  (with-vars [r-? s-?]
    ($_ r.then
        (r.set! s-? (|> (@@ sample) int64-high (r.< (r.int 0))))
        (r.set! r-? (|> (@@ reference) int64-high (r.< (r.int 0))))
        (|> (|> (@@ s-?) (r.and (r.not (@@ r-?))))
            (r.or (|> (r.not (@@ s-?)) (r.and (@@ r-?)) r.not))
            (r.or (|> (@@ sample)
                      (int//- (@@ reference))
                      int64-high
                      (r.< (r.int 0))))))))

(runtime: (int//from-float input)
  (r.cond (list [(r.apply (list (@@ input)) (r.global "is.nan"))
                 int//zero]
                [(|> (@@ input) (r.<= (r.negate f2^63)))
                 int//min]
                [(|> (@@ input) (r.+ (r.float 1.0)) (r.>= f2^63))
                 int//max]
                [(|> (@@ input) (r.< (r.float 0.0)))
                 (|> (@@ input) r.negate int//from-float int//negate)])
          (int//new (|> (@@ input) (r./ f2^32))
                    (|> (@@ input) (r.%% f2^32)))))

(runtime: (int//* param subject)
  (with-vars [sH sL pH pL
              x00 x16 x32 x48]
    ($_ r.then
        (r.set! sH (|> (@@ subject) int64-high))
        (r.set! pH (|> (@@ param) int64-high))
        (let [negative-subject? (|> (@@ sH) (r.< (r.int 0)))
              negative-param? (|> (@@ pH) (r.< (r.int 0)))]
          (r.cond (list [negative-subject?
                         (r.if negative-param?
                           (int//* (int//negate (@@ param))
                                   (int//negate (@@ subject)))
                           (int//negate (int//* (@@ param)
                                                (int//negate (@@ subject)))))]

                        [negative-param?
                         (int//negate (int//* (int//negate (@@ param))
                                              (@@ subject)))])
                  ($_ r.then
                      (r.set! sL (|> (@@ subject) int64-low))
                      (r.set! pL (|> (@@ param) int64-low))
                      (let [bits16 (r.code "0xFFFF")
                            move-top-16 (r.bit-shl (r.int 16))
                            top-16 (r.bit-ushr (r.int 16))
                            bottom-16 (r.bit-and bits16)
                            split-16 (function (_ source)
                                       [(|> source top-16)
                                        (|> source bottom-16)])
                            split-int (function (_ high low)
                                        [(split-16 high)
                                         (split-16 low)])
                            new-half (function (_ top bottom)
                                       (|> top bottom-16 move-top-16
                                           (r.bit-or (bottom-16 bottom))))
                            x16-top (|> (@@ x16) top-16)
                            x32-top (|> (@@ x32) top-16)]
                        (with-vars [s48 s32 s16 s00
                                    p48 p32 p16 p00]
                          (let [[[_s48 _s32] [_s16 _s00]] (split-int (@@ sH) (@@ sL))
                                [[_p48 _p32] [_p16 _p00]] (split-int (@@ pH) (@@ pL))
                                set-subject-chunks! ($_ r.then (r.set! s48 _s48) (r.set! s32 _s32) (r.set! s16 _s16) (r.set! s00 _s00))
                                set-param-chunks! ($_ r.then (r.set! p48 _p48) (r.set! p32 _p32) (r.set! p16 _p16) (r.set! p00 _p00))]
                            ($_ r.then
                                set-subject-chunks!
                                set-param-chunks!
                                (r.set! x00 (|> (@@ s00) (r.* (@@ p00))))
                                (r.set! x16 (|> (@@ x00) top-16 (r.+ (|> (@@ s16) (r.* (@@ p00))))))
                                (r.set! x32 x16-top)
                                (r.set! x16 (|> (@@ x16) bottom-16 (r.+ (|> (@@ s00) (r.* (@@ p16))))))
                                (r.set! x32 (|> (@@ x32) (r.+ x16-top) (r.+ (|> (@@ s32) (r.* (@@ p00))))))
                                (r.set! x48 x32-top)
                                (r.set! x32 (|> (@@ x32) bottom-16 (r.+ (|> (@@ s16) (r.* (@@ p16))))))
                                (r.set! x48 (|> (@@ x48) (r.+ x32-top)))
                                (r.set! x32 (|> (@@ x32) bottom-16 (r.+ (|> (@@ s00) (r.* (@@ p32))))))
                                (r.set! x48 (|> (@@ x48) (r.+ x32-top)
                                                (r.+ (|> (@@ s48) (r.* (@@ p00))))
                                                (r.+ (|> (@@ s32) (r.* (@@ p16))))
                                                (r.+ (|> (@@ s16) (r.* (@@ p32))))
                                                (r.+ (|> (@@ s00) (r.* (@@ p48))))))
                                (int//new (new-half (@@ x48) (@@ x32))
                                          (new-half (@@ x16) (@@ x00))))))
                        )))))))

(def: (limit-shift! shift)
  (-> SVar Expression)
  (r.set! shift (|> (@@ shift) (r.bit-and (r.int 63)))))

(def: (no-shift-clause shift input)
  (-> SVar SVar [Expression Expression])
  [(|> (@@ shift) (r.= (r.int 0)))
   (@@ input)])

(runtime: (bit//left-shift shift input)
  ($_ r.then
      (limit-shift! shift)
      (r.cond (list (no-shift-clause shift input)
                    [(|> (@@ shift) (r.< (r.int 32)))
                     (let [mid (|> (int64-low (@@ input)) (r.bit-ushr (|> (r.int 32) (r.- (@@ shift)))))
                           high (|> (int64-high (@@ input))
                                    (r.bit-shl (@@ shift))
                                    (r.bit-or mid))
                           low (|> (int64-low (@@ input))
                                   (r.bit-shl (@@ shift)))]
                       (int//new high low))])
              (let [high (|> (int64-high (@@ input))
                             (r.bit-shl (|> (@@ shift) (r.- (r.int 32)))))]
                (int//new high (r.int 0))))))

(runtime: (bit//arithmetic-right-shift-32 shift input)
  (let [top-bit (|> (@@ input) (r.bit-and (r.int (hex "80000000"))))]
    (|> (@@ input)
        (r.bit-ushr (@@ shift))
        (r.bit-or top-bit))))

(runtime: (bit//arithmetic-right-shift shift input)
  ($_ r.then
      (limit-shift! shift)
      (r.cond (list (no-shift-clause shift input)
                    [(|> (@@ shift) (r.< (r.int 32)))
                     (let [mid (|> (int64-high (@@ input)) (r.bit-shl (|> (r.int 32) (r.- (@@ shift)))))
                           high (|> (int64-high (@@ input))
                                    (bit//arithmetic-right-shift-32 (@@ shift)))
                           low (|> (int64-low (@@ input))
                                   (r.bit-ushr (@@ shift))
                                   (r.bit-or mid))]
                       (int//new high low))])
              (let [low (|> (int64-high (@@ input))
                            (bit//arithmetic-right-shift-32 (|> (@@ shift) (r.- (r.int 32)))))
                    high (r.if (|> (int64-high (@@ input)) (r.>= (r.int 0)))
                           (r.int 0)
                           (r.int -1))]
                (int//new high low)))))

(runtime: (int/// param subject)
  (let [negative? (|>> (int//< int//zero))
        valid-division-check [(|> (@@ param) (int//= int//zero))
                              (r.stop (r.string "Cannot divide by zero!"))]
        short-circuit-check [(|> (@@ subject) (int//= int//zero))
                             int//zero]]
    (r.cond (list valid-division-check
                  short-circuit-check

                  [(|> (@@ subject) (int//= int//min))
                   (r.cond (list [(|> (|> (@@ param) (int//= int//one))
                                      (r.or (|> (@@ param) (int//= int//-one))))
                                  int//min]
                                 [(|> (@@ param) (int//= int//min))
                                  int//one])
                           (with-vars [approximation]
                             ($_ r.then
                                 (r.set! approximation
                                         (|> (@@ subject)
                                             (bit//arithmetic-right-shift (r.int 1))
                                             (int/// (@@ param))
                                             (bit//left-shift (r.int 1))))
                                 (r.if (|> (@@ approximation) (int//= int//zero))
                                   (r.if (negative? (@@ param))
                                     int//one
                                     int//-one)
                                   (let [remainder (int//- (int//* (@@ param) (@@ approximation))
                                                           (@@ subject))]
                                     (|> remainder
                                         (int/// (@@ param))
                                         (int//+ (@@ approximation))))))))]
                  [(|> (@@ param) (int//= int//min))
                   int//zero]

                  [(negative? (@@ subject))
                   (r.if (negative? (@@ param))
                     (|> (int//negate (@@ subject))
                         (int/// (int//negate (@@ param))))
                     (|> (int//negate (@@ subject))
                         (int/// (@@ param))
                         int//negate))]

                  [(negative? (@@ param))
                   (|> (@@ param)
                       int//negate
                       (int/// (@@ subject))
                       int//negate)])
            (with-vars [result remainder approximate approximate-result log2 approximate-remainder]
              ($_ r.then
                  (r.set! result int//zero)
                  (r.set! remainder (@@ subject))
                  (r.while (|> (|> (@@ remainder) (int//< (@@ param)))
                               (r.or (|> (@@ remainder) (int//= (@@ param)))))
                           (let [calc-rough-estimate (r.apply (list (|> (int//to-float (@@ remainder)) (r./ (int//to-float (@@ param)))))
                                                              (r.global "floor"))
                                 calc-approximate-result (int//from-float (@@ approximate))
                                 calc-approximate-remainder (|> (@@ approximate-result) (int//* (@@ param)))
                                 delta (r.if (|> (r.float 48.0) (r.<= (@@ log2)))
                                         (r.float 1.0)
                                         (r.** (|> (@@ log2) (r.- (r.float 48.0)))
                                               (r.float 2.0)))]
                             ($_ r.then
                                 (r.set! approximate (r.apply (list (r.float 1.0) calc-rough-estimate)
                                                              (r.global "max")))
                                 (r.set! log2 (let [log (function (_ input)
                                                          (r.apply (list input) (r.global "log")))]
                                                (r.apply (list (|> (log (r.int 2))
                                                                   (r./ (log (@@ approximate)))))
                                                         (r.global "ceil"))))
                                 (r.set! approximate-result calc-approximate-result)
                                 (r.set! approximate-remainder calc-approximate-remainder)
                                 (r.while (|> (negative? (@@ approximate-remainder))
                                              (r.or (|> (@@ approximate-remainder) (int//< (@@ remainder)))))
                                          ($_ r.then
                                              (r.set! approximate (|> delta (r.- (@@ approximate))))
                                              (r.set! approximate-result calc-approximate-result)
                                              (r.set! approximate-remainder calc-approximate-remainder)))
                                 (r.set! result (|> (r.if (|> (@@ approximate-result) (int//= int//zero))
                                                      int//one
                                                      (@@ approximate-result))
                                                    (int//+ (@@ result))))
                                 (r.set! remainder (|> (@@ remainder) (int//- (@@ approximate-remainder)))))))
                  (@@ result)))
            )))

(runtime: (int//% param subject)
  (let [flat (|> (@@ subject) (int/// (@@ param)) (int//* (@@ param)))]
    (|> (@@ subject) (int//- flat))))

(def: runtime//int
  Runtime
  ($_ r.then
      @@int//zero
      @@int//one
      @@int//min
      @@int//max
      @@int//=
      @@int//<
      @@int//+
      @@int//-
      @@int//negate
      @@int//-one
      @@int//unsigned-low
      @@int//to-float
      @@int//*
      @@int///
      @@int//%))

(runtime: (lux//try op)
  (with-vars [error value]
    (r.try ($_ r.then
               (r.set! value (r.apply (list ..unit) (@@ op)))
               (..right (@@ value)))
           #.None
           (#.Some (r.function (list error)
                     (..left (r.nth (r.string "message")
                                    (@@ error)))))
           #.None)))

(runtime: (lux//program-args program-args)
  (with-vars [inputs value]
    ($_ r.then
        (r.set! inputs ..none)
        (<| (r.for-in value (@@ program-args))
            (r.set! inputs (..some (r.list (list (@@ value) (@@ inputs))))))
        (@@ inputs))))

(def: runtime//lux
  Runtime
  ($_ r.then
      @@lux//try
      @@lux//program-args))

(def: current-time-float
  Expression
  (let [raw-time (r.apply (list) (r.global "Sys.time"))]
    (r.apply (list raw-time) (r.global "as.numeric"))))

(runtime: (io//current-time! _)
  (|> current-time-float
      (r.* (r.float 1,000.0))
      int//from-float))

(def: runtime//io
  Runtime
  ($_ r.then
      @@io//current-time!))

(def: minimum-index-length
  (-> SVar Expression)
  (|>> @@ (r.+ (r.int 1))))

(def: (product-element product index)
  (-> Expression Expression Expression)
  (|> product (r.nth (|> index (r.+ (r.int 1))))))

(def: (product-tail product)
  (-> SVar Expression)
  (|> (@@ product) (r.nth (r.length (@@ product)))))

(def: (updated-index min-length product)
  (-> Expression Expression Expression)
  (|> min-length (r.- (r.length product))))

(runtime: (product//left product index)
  (let [$index_min_length (r.var "index_min_length")]
    ($_ r.then
        (r.set! $index_min_length (minimum-index-length index))
        (r.if (|> (r.length (@@ product)) (r.> (@@ $index_min_length)))
          ## No need for recursion
          (product-element (@@ product) (@@ index))
          ## Needs recursion
          (product//left (product-tail product)
                         (updated-index (@@ $index_min_length) (@@ product)))))))

(runtime: (product//right product index)
  (let [$index_min_length (r.var "index_min_length")]
    ($_ r.then
        (r.set! $index_min_length (minimum-index-length index))
        (r.cond (list [## Last element.
                       (|> (r.length (@@ product)) (r.= (@@ $index_min_length)))
                       (product-element (@@ product) (@@ index))]
                      [## Needs recursion
                       (|> (r.length (@@ product)) (r.< (@@ $index_min_length)))
                       (product//right (product-tail product)
                                       (updated-index (@@ $index_min_length) (@@ product)))])
                ## Must slice
                (|> (@@ product) (r.slice-from (@@ index)))))))

(runtime: (sum//get sum wanted_tag wants_last)
  (let [no-match r.null
        sum-tag (|> (@@ sum) (r.nth (r.string //.variant-tag-field)))
        sum-flag (|> (@@ sum) (r.nth (r.string //.variant-flag-field)))
        sum-value (|> (@@ sum) (r.nth (r.string //.variant-value-field)))
        is-last? (|> sum-flag (r.= (r.string "")))
        test-recursion (r.if is-last?
                         ## Must recurse.
                         (sum//get sum-value
                                   (|> (@@ wanted_tag) (r.- sum-tag))
                                   (@@ wants_last))
                         no-match)]
    (r.cond (list [(r.= sum-tag (@@ wanted_tag))
                   (r.if (r.= (@@ wants_last) sum-flag)
                     sum-value
                     test-recursion)]

                  [(|> (@@ wanted_tag) (r.> sum-tag))
                   test-recursion]

                  [(|> (|> (@@ wants_last) (r.= (r.string "")))
                       (r.and (|> (@@ wanted_tag) (r.< sum-tag))))
                   (variant' (|> sum-tag (r.- (@@ wanted_tag))) sum-flag sum-value)])

            no-match)))

(def: runtime//adt
  Runtime
  ($_ r.then
      @@product//left
      @@product//right
      @@sum//get
      ))

(template [<name> <op>]
  [(runtime: (<name> mask input)
     (int//new (<op> (int64-high (@@ mask))
                     (int64-high (@@ input)))
               (<op> (int64-low (@@ mask))
                     (int64-low (@@ input)))))]

  [bit//and r.bit-and]
  [bit//or  r.bit-or]
  [bit//xor r.bit-xor]
  )

(runtime: (bit//logical-right-shift shift input)
  ($_ r.then
      (limit-shift! shift)
      (r.cond (list (no-shift-clause shift input)
                    [(|> (@@ shift) (r.< (r.int 32)))
                     (with-vars [$mid]
                       (let [mid (|> (int64-high (@@ input)) (r.bit-shl (|> (r.int 32) (r.- (@@ shift)))))
                             high (|> (int64-high (@@ input)) (r.bit-ushr (@@ shift)))
                             low (|> (int64-low (@@ input))
                                     (r.bit-ushr (@@ shift))
                                     (r.bit-or (r.if (r.apply (list (@@ $mid)) (r.global "is.na"))
                                                 (r.int 0)
                                                 (@@ $mid))))]
                         ($_ r.then
                             (r.set! $mid mid)
                             (int//new high low))))]
                    [(|> (@@ shift) (r.= (r.int 32)))
                     (let [high (int64-high (@@ input))]
                       (int//new (r.int 0) high))])
              (let [low (|> (int64-high (@@ input)) (r.bit-ushr (|> (@@ shift) (r.- (r.int 32)))))]
                (int//new (r.int 0) low)))))

(def: runtime//bit
  Runtime
  ($_ r.then
      @@bit//and
      @@bit//or
      @@bit//xor
      @@bit//not
      @@bit//left-shift
      @@bit//arithmetic-right-shift-32
      @@bit//arithmetic-right-shift
      @@bit//logical-right-shift
      ))

(runtime: (frac//decode input)
  (with-vars [output]
    ($_ r.then
        (r.set! output (r.apply (list (@@ input)) (r.global "as.numeric")))
        (r.if (|> (@@ output) (r.= r.n/a))
          ..none
          (..some (@@ output))))))

(def: runtime//frac
  Runtime
  ($_ r.then
      @@frac//decode))

(def: inc (-> Expression Expression) (|>> (r.+ (r.int 1))))

(template [<name> <top-cmp>]
  [(def: (<name> top value)
     (-> Expression Expression Expression)
     (|> (|> value (r.>= (r.int 0)))
         (r.and (|> value (<top-cmp> top)))))]

  [within? r.<]
  [up-to?  r.<=]
  )

(def: (text-clip start end text)
  (-> Expression Expression Expression Expression)
  (r.apply (list text start end)
           (r.global "substr")))

(def: (text-length text)
  (-> Expression Expression)
  (r.apply (list text) (r.global "nchar")))

(runtime: (text//index subject param start)
  (with-vars [idx startF subjectL]
    ($_ r.then
        (r.set! startF (int//to-float (@@ start)))
        (r.set! subjectL (text-length (@@ subject)))
        (r.if (|> (@@ startF) (within? (@@ subjectL)))
          ($_ r.then
              (r.set! idx (|> (r.apply-kw (list (@@ param) (r.if (|> (@@ startF) (r.= (r.int 0)))
                                                             (@@ subject)
                                                             (text-clip (inc (@@ startF))
                                                                        (inc (@@ subjectL))
                                                                        (@@ subject))))
                                          (list ["fixed" (r.bool #1)])
                                          (r.global "regexpr"))
                              (r.nth (r.int 1))))
              (r.if (|> (@@ idx) (r.= (r.int -1)))
                ..none
                (..some (int//from-float (|> (@@ idx) (r.+ (@@ startF)))))))
          ..none))))

(runtime: (text//clip text from to)
  (with-vars [length]
    ($_ r.then
        (r.set! length (r.length (@@ text)))
        (r.if ($_ r.and
                  (|> (@@ to) (within? (@@ length)))
                  (|> (@@ from) (up-to? (@@ to))))
          (..some (text-clip (inc (@@ from)) (inc (@@ to)) (@@ text)))
          ..none))))

(def: (char-at idx text)
  (-> Expression Expression Expression)
  (r.apply (list (text-clip idx idx text))
           (r.global "utf8ToInt")))

(runtime: (text//char text idx)
  (r.if (|> (@@ idx) (within? (r.length (@@ text))))
    ($_ r.then
        (r.set! idx (inc (@@ idx)))
        (..some (int//from-float (char-at (@@ idx) (@@ text)))))
    ..none))

(def: runtime//text
  Runtime
  ($_ r.then
      @@text//index
      @@text//clip
      @@text//char))

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

(runtime: (array//new size)
  (with-vars [output]
    ($_ r.then
        (r.set! output (r.list (list)))
        (r.set-nth! (|> (@@ size) (r.+ (r.int 1)))
                    r.null
                    output)
        (@@ output))))

(runtime: (array//get array idx)
  (with-vars [temp]
    (<| (check-index-out-of-bounds (@@ array) (@@ idx))
        ($_ r.then
            (r.set! temp (|> (@@ array) (r.nth (@@ idx))))
            (r.if (|> (@@ temp) (r.= r.null))
              ..none
              (..some (@@ temp)))))))

(runtime: (array//put array idx value)
  (<| (check-index-out-of-bounds (@@ array) (@@ idx))
      ($_ r.then
          (r.set-nth! (@@ idx) (@@ value) array)
          (@@ array))))

(def: runtime//array
  Runtime
  ($_ r.then
      @@array//new
      @@array//get
      @@array//put))

(runtime: (box//write value box)
  ($_ r.then
      (r.set-nth! (r.int 1) (@@ value) box)
      ..unit))

(def: runtime//box
  Runtime
  ($_ r.then
      @@box//write))

(def: runtime
  Runtime
  ($_ r.then
      runtime//lux
      @@f2^32
      @@f2^63
      @@int//new
      @@int//from-float
      runtime//bit
      runtime//int
      runtime//adt
      runtime//frac
      runtime//text
      runtime//array
      runtime//box
      runtime//io
      ))

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

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