aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/jvm/procedure/common.jvm.lux
blob: abd2d49c8b128f3370115d764556a5c4f7073f14 (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
(.module:
  lux
  (lux (control [monad #+ do]
                ["p" parser]
                ["ex" exception #+ exception:])
       (data ["e" error]
             [text]
             text/format
             (coll [list "list/" Functor<List>]
                   [dict #+ Dict]))
       [macro #+ with-gensyms]
       (macro [code]
              ["s" syntax #+ syntax:])
       [host])
  (luxc ["&" lang]
        (lang [".L" host]
              (host ["$" jvm]
                    (jvm ["$t" type]
                         ["$d" def]
                         ["$i" inst]))
              ["la" analysis]
              ["ls" synthesis]))
  (/// [".T" runtime]
       [".T" case]
       [".T" function]
       [".T" loop]))

(host.import java/lang/Long
  (#static MIN_VALUE Long)
  (#static MAX_VALUE Long))

(host.import java/lang/Double
  (#static MIN_VALUE Double)
  (#static MAX_VALUE Double)
  (#static NaN Double)
  (#static POSITIVE_INFINITY Double)
  (#static NEGATIVE_INFINITY Double))

## [Types]
(type: #export Translator
  (-> ls.Synthesis (Meta $.Inst)))

(type: #export Proc
  (-> Translator (List ls.Synthesis) (Meta $.Inst)))

(type: #export Bundle
  (Dict Text Proc))

(syntax: (Vector [size s.nat] elemT)
  (wrap (list (` [(~+ (list.repeat size elemT))]))))

(type: #export Nullary (-> (Vector +0 $.Inst) $.Inst))
(type: #export Unary   (-> (Vector +1 $.Inst) $.Inst))
(type: #export Binary  (-> (Vector +2 $.Inst) $.Inst))
(type: #export Trinary (-> (Vector +3 $.Inst) $.Inst))
(type: #export Variadic (-> (List $.Inst) $.Inst))

## [Utils]
(def: $Object $.Type ($t.class "java.lang.Object" (list)))
(def: $Object-Array $.Type ($t.array +1 $Object))
(def: $Variant $.Type ($t.array +1 $Object))
(def: $String $.Type ($t.class "java.lang.String" (list)))
(def: $CharSequence $.Type ($t.class "java.lang.CharSequence" (list)))
(def: $Function $.Type ($t.class hostL.function-class (list)))

(def: #export (install name unnamed)
  (-> Text (-> Text Proc)
      (-> Bundle Bundle))
  (dict.put name (unnamed name)))

(def: #export (prefix prefix bundle)
  (-> Text Bundle Bundle)
  (|> bundle
      dict.entries
      (list/map (function [[key val]] [(format prefix " " key) val]))
      (dict.from-list text.Hash<Text>)))

(def: (wrong-arity proc expected actual)
  (-> Text Nat Nat Text)
  (format "Wrong number of arguments for " (%t proc) "\n"
          "Expected: " (|> expected nat-to-int %i) "\n"
          "  Actual: " (|> actual nat-to-int %i)))

(syntax: (arity: [name s.local-symbol] [arity s.nat])
  (with-gensyms [g!proc g!name g!translate g!inputs]
    (do @
      [g!input+ (monad.seq @ (list.repeat arity (macro.gensym "input")))]
      (wrap (list (` (def: #export ((~ (code.local-symbol name)) (~ g!proc))
                       (-> (-> (..Vector (~ (code.nat arity)) $.Inst) $.Inst)
                           (-> Text ..Proc))
                       (function [(~ g!name)]
                         (function [(~ g!translate) (~ g!inputs)]
                           (case (~ g!inputs)
                             (^ (list (~+ g!input+)))
                             (do macro.Monad<Meta>
                               [(~+ (|> g!input+
                                        (list/map (function [g!input]
                                                    (list g!input (` ((~ g!translate) (~ g!input))))))
                                        list.concat))]
                               ((~' wrap) ((~ g!proc) [(~+ g!input+)])))

                             (~' _)
                             (macro.fail (wrong-arity (~ g!name) +1 (list.size (~ g!inputs))))))))))))))

(arity: nullary +0)
(arity: unary +1)
(arity: binary +2)
(arity: trinary +3)

(def: #export (variadic proc)
  (-> Variadic (-> Text Proc))
  (function [proc-name]
    (function [translate inputsS]
      (do macro.Monad<Meta>
        [inputsI (monad.map @ translate inputsS)]
        (wrap (proc inputsI))))))

## [Instructions]
(def: lux-intI $.Inst (|>> $i.I2L ($i.wrap #$.Long)))
(def: jvm-intI $.Inst (|>> ($i.unwrap #$.Long) $i.L2I))

(def: (array-writeI arrayI idxI elemI)
  (-> $.Inst $.Inst $.Inst
      $.Inst)
  (|>> arrayI ($i.CHECKCAST ($t.descriptor $Object-Array))
       $i.DUP
       idxI jvm-intI
       elemI
       $i.AASTORE))

(def: (predicateI tester)
  (-> (-> $.Label $.Inst)
      $.Inst)
  (<| $i.with-label (function [@then])
      $i.with-label (function [@end])
      (|>> (tester @then)
           ($i.GETSTATIC "java.lang.Boolean" "FALSE" ($t.class "java.lang.Boolean" (list)))
           ($i.GOTO @end)
           ($i.label @then)
           ($i.GETSTATIC "java.lang.Boolean" "TRUE" ($t.class "java.lang.Boolean" (list)))
           ($i.label @end)
           )))

(def: unitI $.Inst ($i.string hostL.unit))

## [Procedures]
## [[Lux]]
(def: (lux//is [leftI rightI])
  Binary
  (|>> leftI
       rightI
       (predicateI $i.IF_ACMPEQ)))

(def: (lux//if [testI thenI elseI])
  Trinary
  (caseT.translate-if testI thenI elseI))

(def: (lux//try riskyI)
  Unary
  (|>> riskyI
       ($i.CHECKCAST hostL.function-class)
       ($i.INVOKESTATIC hostL.runtime-class "try"
                        ($t.method (list $Function) (#.Some $Object-Array) (list))
                        false)))

(def: (lux//noop valueI)
  Unary
  valueI)

(exception: #export Wrong-Syntax)
(def: #export (wrong-syntax procedure args)
  (-> Text (List ls.Synthesis) Text)
  (format "Procedure: " procedure "\n"
          "Arguments: " (%code (code.tuple args))))

(def: lux//loop
  (-> Text Proc)
  (function [proc-name]
    (function [translate inputsS]
      (case (s.run inputsS ($_ p.seq s.nat (s.tuple (p.many s.any)) s.any))
        (#e.Success [offset initsS+ bodyS])
        (loopT.translate-loop translate offset initsS+ bodyS)

        (#e.Error error)
        (&.throw Wrong-Syntax (wrong-syntax proc-name inputsS)))
      )))

(def: lux//recur
  (-> Text Proc)
  (function [proc-name]
    (function [translate inputsS]
      (loopT.translate-recur translate inputsS))))

## [[Bits]]
(do-template [<name> <op>]
  [(def: (<name> [inputI maskI])
     Binary
     (|>> inputI ($i.unwrap #$.Long)
          maskI ($i.unwrap #$.Long)
          <op> ($i.wrap #$.Long)))]

  [bit//and $i.LAND]
  [bit//or  $i.LOR]
  [bit//xor $i.LXOR]
  )

(def: (bit//count inputI)
  Unary
  (|>> inputI ($i.unwrap #$.Long)
       ($i.INVOKESTATIC "java.lang.Long" "bitCount" ($t.method (list $t.long) (#.Some $t.int) (list)) false)
       lux-intI))

(do-template [<name> <op>]
  [(def: (<name> [inputI shiftI])
     Binary
     (|>> inputI ($i.unwrap #$.Long)
          shiftI jvm-intI
          <op>
          ($i.wrap #$.Long)))]

  [bit//shift-left           $i.LSHL]
  [bit//shift-right          $i.LSHR]
  [bit//unsigned-shift-right $i.LUSHR]
  )

## [[Arrays]]
(def: (array//new lengthI)
  Unary
  (|>> lengthI jvm-intI ($i.ANEWARRAY ($t.binary-name "java.lang.Object"))))

(def: (array//get [arrayI idxI])
  Binary
  (<| $i.with-label (function [@is-null])
      $i.with-label (function [@end])
      (|>> arrayI ($i.CHECKCAST ($t.descriptor $Object-Array))
           idxI jvm-intI
           $i.AALOAD
           $i.DUP
           ($i.IFNULL @is-null)
           runtimeT.someI
           ($i.GOTO @end)
           ($i.label @is-null)
           $i.POP
           runtimeT.noneI
           ($i.label @end))))

(def: (array//put [arrayI idxI elemI])
  Trinary
  (array-writeI arrayI idxI elemI))

(def: (array//remove [arrayI idxI])
  Binary
  (array-writeI arrayI idxI $i.NULL))

(def: (array//size arrayI)
  Unary
  (|>> arrayI ($i.CHECKCAST ($t.descriptor $Object-Array))
       $i.ARRAYLENGTH
       lux-intI))

## [[Numbers]]
(def: nat-method
  $.Method
  ($t.method (list $t.long $t.long) (#.Some $t.long) (list)))

(def: deg-method $.Method nat-method)

(def: compare-nat-method
  $.Method
  ($t.method (list $t.long $t.long) (#.Some $t.int) (list)))

(do-template [<name> <const> <type>]
  [(def: (<name> _)
     Nullary
     (|>> <const> ($i.wrap <type>)))]

  [nat//min                ($i.long 0)                              #$.Long]
  [nat//max                ($i.long -1)                             #$.Long]

  [int//min                ($i.long Long::MIN_VALUE)                #$.Long]
  [int//max                ($i.long Long::MAX_VALUE)                #$.Long]
  
  [frac//smallest          ($i.double Double::MIN_VALUE)            #$.Double]
  [frac//min               ($i.double (f/* -1.0 Double::MAX_VALUE)) #$.Double]
  [frac//max               ($i.double Double::MAX_VALUE)            #$.Double]
  [frac//not-a-number      ($i.double Double::NaN)                  #$.Double]
  [frac//positive-infinity ($i.double Double::POSITIVE_INFINITY)    #$.Double]
  [frac//negative-infinity ($i.double Double::NEGATIVE_INFINITY)    #$.Double]

  [deg//min                ($i.long 0)                              #$.Long]
  [deg//max                ($i.long -1)                             #$.Long]
  )

(do-template [<name> <type> <op>]
  [(def: (<name> [subjectI paramI])
     Binary
     (|>> subjectI ($i.unwrap <type>)
          paramI ($i.unwrap <type>)
          <op>
          ($i.wrap <type>)))]

  [int//add        #$.Long   $i.LADD]
  [int//sub        #$.Long   $i.LSUB]
  [int//mul        #$.Long   $i.LMUL]
  [int//div        #$.Long   $i.LDIV]
  [int//rem        #$.Long   $i.LREM]
  
  [nat//add        #$.Long   $i.LADD]
  [nat//sub        #$.Long   $i.LSUB]
  [nat//mul        #$.Long   $i.LMUL]
  [nat//div        #$.Long   ($i.INVOKESTATIC hostL.runtime-class "div_nat" nat-method false)]
  [nat//rem        #$.Long   ($i.INVOKESTATIC hostL.runtime-class "rem_nat" nat-method false)]

  [frac//add       #$.Double $i.DADD]
  [frac//sub       #$.Double $i.DSUB]
  [frac//mul       #$.Double $i.DMUL]
  [frac//div       #$.Double $i.DDIV]
  [frac//rem       #$.Double $i.DREM]

  [deg//add        #$.Long   $i.LADD]
  [deg//sub        #$.Long   $i.LSUB]
  [deg//mul        #$.Long   ($i.INVOKESTATIC hostL.runtime-class "mul_deg" deg-method false)]
  [deg//div        #$.Long   ($i.INVOKESTATIC hostL.runtime-class "div_deg" deg-method false)]
  [deg//rem        #$.Long   $i.LSUB]
  [deg//scale      #$.Long   $i.LMUL]
  [deg//reciprocal #$.Long   $i.LDIV]
  )

(do-template [<eq> <lt> <unwrap> <cmp>]
  [(do-template [<name> <reference>]
     [(def: (<name> [subjectI paramI])
        Binary
        (|>> subjectI <unwrap>
             paramI <unwrap>
             <cmp>
             ($i.int <reference>)
             (predicateI $i.IF_ICMPEQ)))]
     [<eq>  0]
     [<lt> -1])]

  [nat//eq  nat//lt  ($i.unwrap #$.Long)   ($i.INVOKESTATIC hostL.runtime-class "compare_nat" compare-nat-method false)]
  [int//eq  int//lt  ($i.unwrap #$.Long)   $i.LCMP]
  [frac//eq frac//lt ($i.unwrap #$.Double) $i.DCMPG]
  [deg//eq  deg//lt  ($i.unwrap #$.Long)   ($i.INVOKESTATIC hostL.runtime-class "compare_nat" compare-nat-method false)]
  )

(do-template [<name> <prepare> <transform>]
  [(def: (<name> inputI)
     Unary
     (|>> inputI <prepare> <transform>))]

  [nat//to-int id id]
  [nat//char ($i.unwrap #$.Long)
   ((|>> $i.L2I $i.I2C ($i.INVOKESTATIC "java.lang.Character" "toString" ($t.method (list $t.char) (#.Some $String) (list)) false)))]
  
  [int//to-nat id id]
  [int//to-frac ($i.unwrap #$.Long) (<| ($i.wrap #$.Double) $i.L2D)]

  [frac//to-int ($i.unwrap #$.Double) (<| ($i.wrap #$.Long) $i.D2L)]
  [frac//to-deg ($i.unwrap #$.Double)
   (<| ($i.wrap #$.Long) ($i.INVOKESTATIC hostL.runtime-class "frac_to_deg"
                                          ($t.method (list $t.double) (#.Some $t.long) (list)) false))]
  [frac//encode ($i.unwrap #$.Double)
   ($i.INVOKESTATIC "java.lang.Double" "toString" ($t.method (list $t.double) (#.Some $String) (list)) false)]
  [frac//decode ($i.CHECKCAST "java.lang.String")
   ($i.INVOKESTATIC hostL.runtime-class "decode_frac" ($t.method (list $String) (#.Some $Object-Array) (list)) false)]

  [deg//to-frac ($i.unwrap #$.Long)
   (<| ($i.wrap #$.Double) ($i.INVOKESTATIC hostL.runtime-class "deg_to_frac"
                                            ($t.method (list $t.long) (#.Some $t.double) (list)) false))]
  )

## [[Text]]
(do-template [<name> <class> <method> <post> <outputT>]
  [(def: (<name> inputI)
     Unary
     (|>> inputI
          ($i.CHECKCAST "java.lang.String")
          ($i.INVOKEVIRTUAL <class> <method> ($t.method (list) (#.Some <outputT>) (list)) false)
          <post>))]

  [text//size  "java.lang.String" "length"      lux-intI $t.int]
  [text//hash  "java.lang.Object" "hashCode"    lux-intI $t.int]
  [text//upper "java.lang.String" "toUpperCase" id       $String]
  [text//lower "java.lang.String" "toLowerCase" id       $String]
  )

(do-template [<name> <pre-subject> <pre-param> <op> <post>]
  [(def: (<name> [subjectI paramI])
     Binary
     (|>> subjectI <pre-subject>
          paramI <pre-param>
          <op> <post>))]

  [text//eq id id
   ($i.INVOKEVIRTUAL "java.lang.Object" "equals" ($t.method (list $Object) (#.Some $t.boolean) (list)) false)
   ($i.wrap #$.Boolean)]
  [text//lt ($i.CHECKCAST "java.lang.String") ($i.CHECKCAST "java.lang.String")
   ($i.INVOKEVIRTUAL "java.lang.String" "compareTo" ($t.method (list $String) (#.Some $t.int) (list)) false)
   (<| (predicateI $i.IF_ICMPEQ) ($i.int -1))]
  [text//concat ($i.CHECKCAST "java.lang.String") ($i.CHECKCAST "java.lang.String")
   ($i.INVOKEVIRTUAL "java.lang.String" "concat" ($t.method (list $String) (#.Some $String) (list)) false)
   id]
  [text//contains? ($i.CHECKCAST "java.lang.String") ($i.CHECKCAST "java.lang.String")
   ($i.INVOKEVIRTUAL "java.lang.String" "contains" ($t.method (list $CharSequence) (#.Some $t.boolean) (list)) false)
   ($i.wrap #$.Boolean)]
  [text//char ($i.CHECKCAST "java.lang.String") jvm-intI
   ($i.INVOKESTATIC hostL.runtime-class "text_char" ($t.method (list $String $t.int) (#.Some $Variant) (list)) false)
   id]
  )

(do-template [<name> <pre-subject> <pre-param> <pre-extra> <op>]
  [(def: (<name> [subjectI paramI extraI])
     Trinary
     (|>> subjectI <pre-subject>
          paramI <pre-param>
          extraI <pre-extra>
          <op>))]

  [text//clip ($i.CHECKCAST "java.lang.String") jvm-intI jvm-intI
   ($i.INVOKESTATIC hostL.runtime-class "text_clip"
                    ($t.method (list $String $t.int $t.int) (#.Some $Variant) (list)) false)]
  [text//replace-once ($i.CHECKCAST "java.lang.String")
   (<| ($i.INVOKESTATIC "java.util.regex.Pattern" "quote" ($t.method (list $String) (#.Some $String) (list)) false)
       ($i.CHECKCAST "java.lang.String"))
   ($i.CHECKCAST "java.lang.String")
   ($i.INVOKEVIRTUAL "java.lang.String" "replaceFirst" ($t.method (list $String $String) (#.Some $String) (list)) false)]
  [text//replace-all ($i.CHECKCAST "java.lang.String")
   (<| ($i.INVOKESTATIC "java.util.regex.Pattern" "quote" ($t.method (list $String) (#.Some $String) (list)) false)
       ($i.CHECKCAST "java.lang.String"))
   ($i.CHECKCAST "java.lang.String")
   ($i.INVOKEVIRTUAL "java.lang.String" "replaceAll" ($t.method (list $String $String) (#.Some $String) (list)) false)]
  )

(def: index-method $.Method ($t.method (list $String $t.int) (#.Some $t.int) (list)))
(def: (text//index [textI partI startI])
  Trinary
  (<| $i.with-label (function [@not-found])
      $i.with-label (function [@end])
      (|>> textI ($i.CHECKCAST "java.lang.String")
           partI ($i.CHECKCAST "java.lang.String")
           startI jvm-intI
           ($i.INVOKEVIRTUAL "java.lang.String" "indexOf" index-method false)
           $i.DUP
           ($i.int -1)
           ($i.IF_ICMPEQ @not-found)
           lux-intI
           runtimeT.someI
           ($i.GOTO @end)
           ($i.label @not-found)
           $i.POP
           runtimeT.noneI
           ($i.label @end))))

## [[Math]]
(def: math-unary-method ($t.method (list $t.double) (#.Some $t.double) (list)))
(def: math-binary-method ($t.method (list $t.double $t.double) (#.Some $t.double) (list)))

(do-template [<name> <method>]
  [(def: (<name> inputI)
     Unary
     (|>> inputI
          ($i.unwrap #$.Double)
          ($i.INVOKESTATIC "java.lang.Math" <method> math-unary-method false)
          ($i.wrap #$.Double)))]

  [math//cos "cos"]
  [math//sin "sin"]
  [math//tan "tan"]
  [math//acos "acos"]
  [math//asin "asin"]
  [math//atan "atan"]
  [math//cosh "cosh"]
  [math//sinh "sinh"]
  [math//tanh "tanh"]
  [math//exp "exp"]
  [math//log "log"]
  [math//ceil "ceil"]
  [math//floor "floor"]
  )

(do-template [<name> <method>]
  [(def: (<name> [inputI paramI])
     Binary
     (|>> inputI ($i.unwrap #$.Double)
          paramI ($i.unwrap #$.Double)
          ($i.INVOKESTATIC "java.lang.Math" <method> math-binary-method false)
          ($i.wrap #$.Double)))]

  [math//atan2 "atan2"]
  [math//pow "pow"]
  )

(def: (math//round inputI)
  Unary
  (|>> inputI
       ($i.unwrap #$.Double)
       ($i.INVOKESTATIC "java.lang.Math" "round" ($t.method (list $t.double) (#.Some $t.long) (list)) false)
       $i.L2D
       ($i.wrap #$.Double)))

## [[IO]]
(def: string-method $.Method ($t.method (list $String) #.None (list)))
(def: (io//log messageI)
  Unary
  (|>> ($i.GETSTATIC "java.lang.System" "out" ($t.class "java.io.PrintStream" (list)))
       messageI
       ($i.CHECKCAST "java.lang.String")
       ($i.INVOKEVIRTUAL "java.io.PrintStream" "println" string-method false)
       unitI))

(def: (io//error messageI)
  Unary
  (|>> ($i.NEW "java.lang.Error")
       $i.DUP
       messageI
       ($i.CHECKCAST "java.lang.String")
       ($i.INVOKESPECIAL "java.lang.Error" "<init>" string-method false)
       $i.ATHROW))

(def: (io//exit codeI)
  Unary
  (|>> codeI jvm-intI
       ($i.INVOKESTATIC "java.lang.System" "exit" ($t.method (list $t.int) #.None (list)) false)
       $i.NULL))

(def: (io//current-time [])
  Nullary
  (|>> ($i.INVOKESTATIC "java.lang.System" "currentTimeMillis" ($t.method (list) (#.Some $t.long) (list)) false)
       ($i.wrap #$.Long)))

## [[Atoms]]
(def: atom-class Text "java.util.concurrent.atomic.AtomicReference")
(def: (atom//new initI)
  Unary
  (|>> ($i.NEW atom-class)
       $i.DUP
       initI
       ($i.INVOKESPECIAL atom-class "<init>" ($t.method (list $Object) #.None (list)) false)))

(def: (atom//read atomI)
  Unary
  (|>> atomI
       ($i.CHECKCAST atom-class)
       ($i.INVOKEVIRTUAL atom-class "get" ($t.method (list) (#.Some $Object) (list)) false)))

(def: (atom//compare-and-swap [atomI oldI newI])
  Trinary
  (|>> atomI
       ($i.CHECKCAST atom-class)
       oldI
       newI
       ($i.INVOKEVIRTUAL atom-class "compareAndSet" ($t.method (list $Object $Object) (#.Some $t.boolean) (list)) false)
       ($i.wrap #$.Boolean)))

## [[Box]]
(def: empty-boxI
  $.Inst
  (|>> ($i.int 1) ($i.ANEWARRAY ($t.binary-name "java.lang.Object"))))

(def: check-boxI
  $.Inst
  ($i.CHECKCAST ($t.descriptor $Object-Array)))

(def: (box//new initI)
  Unary
  (|>> empty-boxI
       $i.DUP ($i.int 0) initI $i.AASTORE))

(def: (box//read boxI)
  Unary
  (|>> boxI check-boxI
       ($i.int 0) $i.AALOAD))

(def: (box//write [valueI boxI])
  Binary
  (|>> boxI check-boxI
       ($i.int 0) valueI $i.AASTORE
       unitI))

## [[Processes]]
(def: (process//concurrency-level [])
  Nullary
  (|>> ($i.INVOKESTATIC "java.lang.Runtime" "getRuntime" ($t.method (list) (#.Some ($t.class "java.lang.Runtime" (list))) (list)) false)
       ($i.INVOKEVIRTUAL "java.lang.Runtime" "availableProcessors" ($t.method (list) (#.Some $t.int) (list)) false)
       lux-intI))

(def: (process//future procedureI)
  Unary
  (|>> procedureI ($i.CHECKCAST hostL.function-class)
       ($i.INVOKESTATIC hostL.runtime-class "future"
                        ($t.method (list $Function) (#.Some $Object) (list)) false)))

(def: (process//schedule [millisecondsI procedureI])
  Binary
  (|>> millisecondsI ($i.unwrap #$.Long)
       procedureI ($i.CHECKCAST hostL.function-class)
       ($i.INVOKESTATIC hostL.runtime-class "schedule"
                        ($t.method (list $t.long $Function) (#.Some $Object) (list)) false)))

## [Bundles]
(def: lux-procs
  Bundle
  (|> (dict.new text.Hash<Text>)
      (install "noop" (unary lux//noop))
      (install "is" (binary lux//is))
      (install "try" (unary lux//try))
      (install "if" (trinary lux//if))
      (install "loop" lux//loop)
      (install "recur" lux//recur)
      ))

(def: bit-procs
  Bundle
  (<| (prefix "bit")
      (|> (dict.new text.Hash<Text>)
          (install "count" (unary bit//count))
          (install "and" (binary bit//and))
          (install "or" (binary bit//or))
          (install "xor" (binary bit//xor))
          (install "shift-left" (binary bit//shift-left))
          (install "unsigned-shift-right" (binary bit//unsigned-shift-right))
          (install "shift-right" (binary bit//shift-right))
          )))

(def: nat-procs
  Bundle
  (<| (prefix "nat")
      (|> (dict.new text.Hash<Text>)
          (install "+" (binary nat//add))
          (install "-" (binary nat//sub))
          (install "*" (binary nat//mul))
          (install "/" (binary nat//div))
          (install "%" (binary nat//rem))
          (install "=" (binary nat//eq))
          (install "<" (binary nat//lt))
          (install "min" (nullary nat//min))
          (install "max" (nullary nat//max))
          (install "to-int" (unary nat//to-int))
          (install "char" (unary nat//char)))))

(def: int-procs
  Bundle
  (<| (prefix "int")
      (|> (dict.new text.Hash<Text>)
          (install "+" (binary int//add))
          (install "-" (binary int//sub))
          (install "*" (binary int//mul))
          (install "/" (binary int//div))
          (install "%" (binary int//rem))
          (install "=" (binary int//eq))
          (install "<" (binary int//lt))
          (install "min" (nullary int//min))
          (install "max" (nullary int//max))
          (install "to-nat" (unary int//to-nat))
          (install "to-frac" (unary int//to-frac)))))

(def: deg-procs
  Bundle
  (<| (prefix "deg")
      (|> (dict.new text.Hash<Text>)
          (install "+" (binary deg//add))
          (install "-" (binary deg//sub))
          (install "*" (binary deg//mul))
          (install "/" (binary deg//div))
          (install "%" (binary deg//rem))
          (install "=" (binary deg//eq))
          (install "<" (binary deg//lt))
          (install "scale" (binary deg//scale))
          (install "reciprocal" (binary deg//reciprocal))
          (install "min" (nullary deg//min))
          (install "max" (nullary deg//max))
          (install "to-frac" (unary deg//to-frac)))))

(def: frac-procs
  Bundle
  (<| (prefix "frac")
      (|> (dict.new text.Hash<Text>)
          (install "+" (binary frac//add))
          (install "-" (binary frac//sub))
          (install "*" (binary frac//mul))
          (install "/" (binary frac//div))
          (install "%" (binary frac//rem))
          (install "=" (binary frac//eq))
          (install "<" (binary frac//lt))
          (install "smallest" (nullary frac//smallest))
          (install "min" (nullary frac//min))
          (install "max" (nullary frac//max))
          (install "not-a-number" (nullary frac//not-a-number))
          (install "positive-infinity" (nullary frac//positive-infinity))
          (install "negative-infinity" (nullary frac//negative-infinity))
          (install "to-deg" (unary frac//to-deg))
          (install "to-int" (unary frac//to-int))
          (install "encode" (unary frac//encode))
          (install "decode" (unary frac//decode)))))

(def: text-procs
  Bundle
  (<| (prefix "text")
      (|> (dict.new text.Hash<Text>)
          (install "=" (binary text//eq))
          (install "<" (binary text//lt))
          (install "concat" (binary text//concat))
          (install "index" (trinary text//index))
          (install "size" (unary text//size))
          (install "hash" (unary text//hash))
          (install "replace-once" (trinary text//replace-once))
          (install "replace-all" (trinary text//replace-all))
          (install "char" (binary text//char))
          (install "clip" (trinary text//clip))
          (install "upper" (unary text//upper))
          (install "lower" (unary text//lower))
          )))

(def: array-procs
  Bundle
  (<| (prefix "array")
      (|> (dict.new text.Hash<Text>)
          (install "new" (unary array//new))
          (install "get" (binary array//get))
          (install "put" (trinary array//put))
          (install "remove" (binary array//remove))
          (install "size" (unary array//size))
          )))

(def: math-procs
  Bundle
  (<| (prefix "math")
      (|> (dict.new text.Hash<Text>)
          (install "cos" (unary math//cos))
          (install "sin" (unary math//sin))
          (install "tan" (unary math//tan))
          (install "acos" (unary math//acos))
          (install "asin" (unary math//asin))
          (install "atan" (unary math//atan))
          (install "cosh" (unary math//cosh))
          (install "sinh" (unary math//sinh))
          (install "tanh" (unary math//tanh))
          (install "exp" (unary math//exp))
          (install "log" (unary math//log))
          (install "ceil" (unary math//ceil))
          (install "floor" (unary math//floor))
          (install "round" (unary math//round))
          (install "atan2" (binary math//atan2))
          (install "pow" (binary math//pow))
          )))

(def: io-procs
  Bundle
  (<| (prefix "io")
      (|> (dict.new text.Hash<Text>)
          (install "log" (unary io//log))
          (install "error" (unary io//error))
          (install "exit" (unary io//exit))
          (install "current-time" (nullary io//current-time)))))

(def: atom-procs
  Bundle
  (<| (prefix "atom")
      (|> (dict.new text.Hash<Text>)
          (install "new" (unary atom//new))
          (install "read" (unary atom//read))
          (install "compare-and-swap" (trinary atom//compare-and-swap)))))

(def: box-procs
  Bundle
  (<| (prefix "box")
      (|> (dict.new text.Hash<Text>)
          (install "new" (unary box//new))
          (install "read" (unary box//read))
          (install "write" (binary box//write)))))

(def: process-procs
  Bundle
  (<| (prefix "process")
      (|> (dict.new text.Hash<Text>)
          (install "concurrency-level" (nullary process//concurrency-level))
          (install "future" (unary process//future))
          (install "schedule" (binary process//schedule))
          )))

(def: #export procedures
  Bundle
  (<| (prefix "lux")
      (|> lux-procs
          (dict.merge bit-procs)
          (dict.merge nat-procs)
          (dict.merge int-procs)
          (dict.merge deg-procs)
          (dict.merge frac-procs)
          (dict.merge text-procs)
          (dict.merge array-procs)
          (dict.merge math-procs)
          (dict.merge io-procs)
          (dict.merge atom-procs)
          (dict.merge box-procs)
          (dict.merge process-procs)
          )))