aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/jvm/procedure/host.lux
blob: d5a7bd3f5a6a25537dba49b94c269873873265c2 (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
(.module:
  [lux (#- int char)
   [abstract
    ["." monad (#+ do)]]
   [control
    ["p" parser ("#@." monad)]
    ["ex" exception (#+ exception:)]]
   [data
    ["." product]
    ["." error]
    ["." text
     format
     ["l" lexer]]
    [collection
     ["." list ("#@." functor)]
     ["." dictionary (#+ Dictionary)]]]
   [tool
    [compiler
     ["." synthesis (#+ Synthesis %synthesis)]
     ["." phase ("#@." monad)
      [generation
       [extension (#+ Nullary Unary Binary
                      nullary unary binary)]]
      ["." extension
       ["." bundle]]]]]
   [host (#+ import:)]]
  [luxc
   [lang
    [host
     ["$" jvm (#+ Primitive Label Inst Method Handler Bundle Operation)
      ["_t" type]
      ["_" inst]]]]])

(template [<name>]
  [(exception: #export (<name> {message Text})
     message)]

  [invalid-syntax-for-jvm-type]
  [invalid-syntax-for-argument-generation]
  )

(template [<name> <inst>]
  [(def: <name>
     Inst
     <inst>)]

  [L2S (|>> _.L2I _.I2S)]
  [L2B (|>> _.L2I _.I2B)]
  [L2C (|>> _.L2I _.I2C)]
  )

(template [<name> <unwrap> <conversion> <wrap>]
  [(def: (<name> inputI)
     (Unary Inst)
     (if (is? _.NOP <conversion>)
       inputI
       (|>> inputI
            <conversion>)))]
  
  [conversion::double-to-float #$.Double _.D2F #$.Float]
  [conversion::double-to-int #$.Double _.D2I #$.Int]
  [conversion::double-to-long #$.Double _.D2L #$.Long]
  [conversion::float-to-double #$.Float _.F2D #$.Double]
  [conversion::float-to-int #$.Float _.F2I #$.Int]
  [conversion::float-to-long #$.Float _.F2L #$.Long]
  [conversion::int-to-byte #$.Int _.I2B #$.Byte]
  [conversion::int-to-char #$.Int _.I2C #$.Char]
  [conversion::int-to-double #$.Int _.I2D #$.Double]
  [conversion::int-to-float #$.Int _.I2F #$.Float]
  [conversion::int-to-long #$.Int _.I2L #$.Long]
  [conversion::int-to-short #$.Int _.I2S #$.Short]
  [conversion::long-to-double #$.Long _.L2D #$.Double]
  [conversion::long-to-float #$.Long _.L2F #$.Float]
  [conversion::long-to-int #$.Long _.L2I #$.Int]
  [conversion::long-to-short #$.Long L2S #$.Short]
  [conversion::long-to-byte #$.Long L2B #$.Byte]
  [conversion::long-to-char #$.Long L2C #$.Char]
  [conversion::char-to-byte #$.Char _.I2B #$.Byte]
  [conversion::char-to-short #$.Char _.I2S #$.Short]
  [conversion::char-to-int #$.Char _.NOP #$.Int]
  [conversion::char-to-long #$.Char _.I2L #$.Long]
  [conversion::byte-to-long #$.Byte _.I2L #$.Long]
  [conversion::short-to-long #$.Short _.I2L #$.Long]
  )

(def: conversion
  Bundle
  (<| (bundle.prefix "conversion")
      (|> (: Bundle bundle.empty)
          (bundle.install "double-to-float" (unary conversion::double-to-float))
          (bundle.install "double-to-int" (unary conversion::double-to-int))
          (bundle.install "double-to-long" (unary conversion::double-to-long))
          (bundle.install "float-to-double" (unary conversion::float-to-double))
          (bundle.install "float-to-int" (unary conversion::float-to-int))
          (bundle.install "float-to-long" (unary conversion::float-to-long))
          (bundle.install "int-to-byte" (unary conversion::int-to-byte))
          (bundle.install "int-to-char" (unary conversion::int-to-char))
          (bundle.install "int-to-double" (unary conversion::int-to-double))
          (bundle.install "int-to-float" (unary conversion::int-to-float))
          (bundle.install "int-to-long" (unary conversion::int-to-long))
          (bundle.install "int-to-short" (unary conversion::int-to-short))
          (bundle.install "long-to-double" (unary conversion::long-to-double))
          (bundle.install "long-to-float" (unary conversion::long-to-float))
          (bundle.install "long-to-int" (unary conversion::long-to-int))
          (bundle.install "long-to-short" (unary conversion::long-to-short))
          (bundle.install "long-to-byte" (unary conversion::long-to-byte))
          (bundle.install "long-to-char" (unary conversion::long-to-char))
          (bundle.install "char-to-byte" (unary conversion::char-to-byte))
          (bundle.install "char-to-short" (unary conversion::char-to-short))
          (bundle.install "char-to-int" (unary conversion::char-to-int))
          (bundle.install "char-to-long" (unary conversion::char-to-long))
          (bundle.install "byte-to-long" (unary conversion::byte-to-long))
          (bundle.install "short-to-long" (unary conversion::short-to-long))
          )))

(template [<name> <op>]
  [(def: (<name> [xI yI])
     (Binary Inst)
     (|>> xI
          yI
          <op>))]

  [int::+ _.IADD]
  [int::- _.ISUB]
  [int::* _.IMUL]
  [int::/ _.IDIV]
  [int::% _.IREM]
  [int::and _.IAND]
  [int::or _.IOR]
  [int::xor _.IXOR]
  [int::shl _.ISHL]
  [int::shr _.ISHR]
  [int::ushr _.IUSHR]
  
  [long::+ _.LADD]
  [long::- _.LSUB]
  [long::* _.LMUL]
  [long::/ _.LDIV]
  [long::% _.LREM]
  [long::and _.LAND]
  [long::or _.LOR]
  [long::xor _.LXOR]
  [long::shl _.LSHL]
  [long::shr _.LSHR]
  [long::ushr _.LUSHR]

  [float::+ _.FADD]
  [float::- _.FSUB]
  [float::* _.FMUL]
  [float::/ _.FDIV]
  [float::% _.FREM]
  
  [double::+ _.DADD]
  [double::- _.DSUB]
  [double::* _.DMUL]
  [double::/ _.DDIV]
  [double::% _.DREM]
  )

(def: boolean-class (_t.class "java.lang.Boolean" (list)))
(def: falseI (_.GETSTATIC "java.lang.Boolean" "FALSE" boolean-class))
(def: trueI (_.GETSTATIC "java.lang.Boolean" "TRUE" boolean-class))

(template [<name> <op>]
  [(def: (<name> [xI yI])
     (Binary Inst)
     (<| _.with-label (function (_ @then))
         _.with-label (function (_ @end))
         (|>> xI
              yI
              (<op> @then)
              falseI
              (_.GOTO @end)
              (_.label @then)
              trueI
              (_.label @end))))]

  [int::= _.IF_ICMPEQ]
  [int::< _.IF_ICMPLT]

  [char::= _.IF_ICMPEQ]
  [char::< _.IF_ICMPLT]
  )

(template [<name> <op> <reference>]
  [(def: (<name> [xI yI])
     (Binary Inst)
     (<| _.with-label (function (_ @then))
         _.with-label (function (_ @end))
         (|>> xI
              yI
              <op>
              (_.int <reference>)
              (_.IF_ICMPEQ @then)
              falseI
              (_.GOTO @end)
              (_.label @then)
              trueI
              (_.label @end))))]

  [long::= _.LCMP +0]
  [long::< _.LCMP -1]
  
  [float::= _.FCMPG +0]
  [float::< _.FCMPG -1]

  [double::= _.DCMPG +0]
  [double::< _.DCMPG -1]
  )

(def: int
  Bundle
  (<| (bundle.prefix "int")
      (|> (: Bundle bundle.empty)
          (bundle.install "+" (binary int::+))
          (bundle.install "-" (binary int::-))
          (bundle.install "*" (binary int::*))
          (bundle.install "/" (binary int::/))
          (bundle.install "%" (binary int::%))
          (bundle.install "=" (binary int::=))
          (bundle.install "<" (binary int::<))
          (bundle.install "and" (binary int::and))
          (bundle.install "or" (binary int::or))
          (bundle.install "xor" (binary int::xor))
          (bundle.install "shl" (binary int::shl))
          (bundle.install "shr" (binary int::shr))
          (bundle.install "ushr" (binary int::ushr))
          )))

(def: long
  Bundle
  (<| (bundle.prefix "long")
      (|> (: Bundle bundle.empty)
          (bundle.install "+" (binary long::+))
          (bundle.install "-" (binary long::-))
          (bundle.install "*" (binary long::*))
          (bundle.install "/" (binary long::/))
          (bundle.install "%" (binary long::%))
          (bundle.install "=" (binary long::=))
          (bundle.install "<" (binary long::<))
          (bundle.install "and" (binary long::and))
          (bundle.install "or" (binary long::or))
          (bundle.install "xor" (binary long::xor))
          (bundle.install "shl" (binary long::shl))
          (bundle.install "shr" (binary long::shr))
          (bundle.install "ushr" (binary long::ushr))
          )))

(def: float
  Bundle
  (<| (bundle.prefix "float")
      (|> (: Bundle bundle.empty)
          (bundle.install "+" (binary float::+))
          (bundle.install "-" (binary float::-))
          (bundle.install "*" (binary float::*))
          (bundle.install "/" (binary float::/))
          (bundle.install "%" (binary float::%))
          (bundle.install "=" (binary float::=))
          (bundle.install "<" (binary float::<))
          )))

(def: double
  Bundle
  (<| (bundle.prefix "double")
      (|> (: Bundle bundle.empty)
          (bundle.install "+" (binary double::+))
          (bundle.install "-" (binary double::-))
          (bundle.install "*" (binary double::*))
          (bundle.install "/" (binary double::/))
          (bundle.install "%" (binary double::%))
          (bundle.install "=" (binary double::=))
          (bundle.install "<" (binary double::<))
          )))

(def: char
  Bundle
  (<| (bundle.prefix "char")
      (|> (: Bundle bundle.empty)
          (bundle.install "=" (binary char::=))
          (bundle.install "<" (binary char::<))
          )))

(def: (array-java-type nesting elem-class)
  (-> Nat Text $.Type)
  (_t.array nesting
            (case elem-class
              "boolean" _t.boolean
              "byte" _t.byte
              "short" _t.short
              "int" _t.int
              "long" _t.long
              "float" _t.float
              "double" _t.double
              "char" _t.char
              _ (_t.class elem-class (list)))))

(def: (array::length proc generate inputs)
  Handler
  (case inputs
    (^ (list (synthesis.i64 nesting)
             (synthesis.text elem-class)
             arrayS))
    (do phase.monad
      [arrayI (generate arrayS)]
      (wrap (|>> arrayI
                 (_.CHECKCAST (_t.descriptor (array-java-type (.nat nesting) elem-class)))
                 _.ARRAYLENGTH)))

    _
    (phase.throw extension.invalid-syntax [proc %synthesis inputs])))

(def: (array::new proc generate inputs)
  Handler
  (case inputs
    (^ (list (synthesis.i64 nesting)
             (synthesis.text elem-class)
             lengthS))
    (do phase.monad
      [lengthI (generate lengthS)]
      (wrap (|>> lengthI
                 (_.array (array-java-type (.nat nesting) elem-class)))))

    _
    (phase.throw extension.invalid-syntax [proc %synthesis inputs])))

(def: (array::read proc generate inputs)
  Handler
  (case inputs
    (^ (list (synthesis.i64 nesting)
             (synthesis.text elem-class)
             idxS
             arrayS))
    (do phase.monad
      [arrayI (generate arrayS)
       idxI (generate idxS)
       #let [loadI (case elem-class
                     "boolean" _.BALOAD
                     "byte" _.BALOAD
                     "short" _.SALOAD
                     "int" _.IALOAD
                     "long" _.LALOAD
                     "float" _.FALOAD
                     "double" _.DALOAD
                     "char" _.CALOAD
                     _ _.AALOAD)]]
      (wrap (|>> arrayI
                 (_.CHECKCAST (_t.descriptor (array-java-type (.nat nesting) elem-class)))
                 idxI
                 loadI)))

    _
    (phase.throw extension.invalid-syntax [proc %synthesis inputs])))

(def: (array::write proc generate inputs)
  Handler
  (case inputs
    (^ (list (synthesis.i64 nesting)
             (synthesis.text elem-class)
             idxS
             valueS
             arrayS))
    (do phase.monad
      [arrayI (generate arrayS)
       idxI (generate idxS)
       valueI (generate valueS)
       #let [storeI (case elem-class
                      "boolean" _.BASTORE
                      "byte" _.BASTORE
                      "short" _.SASTORE
                      "int" _.IASTORE
                      "long" _.LASTORE
                      "float" _.FASTORE
                      "double" _.DASTORE
                      "char" _.CASTORE
                      _ _.AASTORE)]]
      (wrap (|>> arrayI
                 (_.CHECKCAST (_t.descriptor (array-java-type (.nat nesting) elem-class)))
                 _.DUP
                 idxI
                 valueI
                 storeI)))

    _
    (phase.throw extension.invalid-syntax [proc %synthesis inputs])))

(def: array
  Bundle
  (<| (bundle.prefix "array")
      (|> (: Bundle bundle.empty)
          (bundle.install "length" array::length)
          (bundle.install "new" array::new)
          (bundle.install "read" array::read)
          (bundle.install "write" array::write)
          )))

(def: (object::null _)
  (Nullary Inst)
  _.NULL)

(def: (object::null? objectI)
  (Unary Inst)
  (<| _.with-label (function (_ @then))
      _.with-label (function (_ @end))
      (|>> objectI
           (_.IFNULL @then)
           falseI
           (_.GOTO @end)
           (_.label @then)
           trueI
           (_.label @end))))

(def: (object::synchronized [monitorI exprI])
  (Binary Inst)
  (|>> monitorI
       _.DUP
       _.MONITORENTER
       exprI
       _.SWAP
       _.MONITOREXIT))

(def: (object::throw exceptionI)
  (Unary Inst)
  (|>> exceptionI
       _.ATHROW))

(def: (object::class proc generate inputs)
  Handler
  (case inputs
    (^ (list (synthesis.text class)))
    (do phase.monad
      []
      (wrap (|>> (_.string class)
                 (_.INVOKESTATIC "java.lang.Class" "forName"
                                 (_t.method (list (_t.class "java.lang.String" (list)))
                                            (#.Some (_t.class "java.lang.Class" (list)))
                                            (list))
                                 false))))

    _
    (phase.throw extension.invalid-syntax [proc %synthesis inputs])))

(def: (object::instance? proc generate inputs)
  Handler
  (case inputs
    (^ (list (synthesis.text class) objectS))
    (do phase.monad
      [objectI (generate objectS)]
      (wrap (|>> objectI
                 (_.INSTANCEOF class)
                 (_.wrap #$.Boolean))))

    _
    (phase.throw extension.invalid-syntax [proc %synthesis inputs])))

(def: (object::cast proc generate inputs)
  Handler
  (case inputs
    (^ (list (synthesis.text from) (synthesis.text to) valueS))
    (do phase.monad
      [valueI (generate valueS)]
      (case [from to]
        ## Wrap
        (^template [<primitive> <object> <type>]
          [<primitive> <object>]
          (wrap (|>> valueI (_.wrap <type>)))

          [<object> <primitive>]
          (wrap (|>> valueI (_.unwrap <type>))))
        (["boolean" "java.lang.Boolean"   #$.Boolean]
         ["byte"    "java.lang.Byte"      #$.Byte]
         ["short"   "java.lang.Short"     #$.Short]
         ["int"     "java.lang.Integer"   #$.Int]
         ["long"    "java.lang.Long"      #$.Long]
         ["float"   "java.lang.Float"     #$.Float]
         ["double"  "java.lang.Double"    #$.Double]
         ["char"    "java.lang.Character" #$.Char])
        
        _
        (wrap valueI)))

    _
    (phase.throw extension.invalid-syntax [proc %synthesis inputs])))

(def: object
  Bundle
  (<| (bundle.prefix "object")
      (|> (: Bundle bundle.empty)
          (bundle.install "null" (nullary object::null))
          (bundle.install "null?" (unary object::null?))
          (bundle.install "synchronized" (binary object::synchronized))
          (bundle.install "throw" (unary object::throw))
          (bundle.install "class" object::class)
          (bundle.install "instance?" object::instance?)
          (bundle.install "cast" object::cast)
          )))

(def: primitives
  (Dictionary Text Primitive)
  (|> (list ["boolean" #$.Boolean]
            ["byte" #$.Byte]
            ["short" #$.Short]
            ["int" #$.Int]
            ["long" #$.Long]
            ["float" #$.Float]
            ["double" #$.Double]
            ["char" #$.Char])
      (dictionary.from-list text.hash)))

(def: (static::get proc generate inputs)
  Handler
  (case inputs
    (^ (list (synthesis.text class)
             (synthesis.text field)
             (synthesis.text unboxed)))
    (do phase.monad
      []
      (case (dictionary.get unboxed primitives)
        (#.Some primitive)
        (wrap (_.GETSTATIC class field (#$.Primitive primitive)))
        
        #.None
        (wrap (_.GETSTATIC class field (_t.class unboxed (list))))))

    _
    (phase.throw extension.invalid-syntax [proc %synthesis inputs])))

(def: (static::put proc generate inputs)
  Handler
  (case inputs
    (^ (list (synthesis.text class)
             (synthesis.text field)
             (synthesis.text unboxed)
             valueS))
    (do phase.monad
      [valueI (generate valueS)]
      (case (dictionary.get unboxed primitives)
        (#.Some primitive)
        (wrap (|>> valueI
                   (_.PUTSTATIC class field (#$.Primitive primitive))
                   (_.string synthesis.unit)))
        
        #.None
        (wrap (|>> valueI
                   (_.CHECKCAST class)
                   (_.PUTSTATIC class field (_t.class class (list)))
                   (_.string synthesis.unit)))))

    _
    (phase.throw extension.invalid-syntax [proc %synthesis inputs])))

(def: (virtual::get proc generate inputs)
  Handler
  (case inputs
    (^ (list (synthesis.text class)
             (synthesis.text field)
             (synthesis.text unboxed)
             objectS))
    (do phase.monad
      [objectI (generate objectS)]
      (case (dictionary.get unboxed primitives)
        (#.Some primitive)
        (wrap (|>> objectI
                   (_.CHECKCAST class)
                   (_.GETFIELD class field (#$.Primitive primitive))))
        
        #.None
        (wrap (|>> objectI
                   (_.CHECKCAST class)
                   (_.GETFIELD class field (_t.class unboxed (list)))))))

    _
    (phase.throw extension.invalid-syntax [proc %synthesis inputs])))

(def: (virtual::put proc generate inputs)
  Handler
  (case inputs
    (^ (list (synthesis.text class)
             (synthesis.text field)
             (synthesis.text unboxed)
             valueS
             objectS))
    (do phase.monad
      [valueI (generate valueS)
       objectI (generate objectS)]
      (case (dictionary.get unboxed primitives)
        (#.Some primitive)
        (wrap (|>> objectI
                   (_.CHECKCAST class)
                   _.DUP
                   valueI
                   (_.PUTFIELD class field (#$.Primitive primitive))))
        
        #.None
        (wrap (|>> objectI
                   (_.CHECKCAST class)
                   _.DUP
                   valueI
                   (_.CHECKCAST unboxed)
                   (_.PUTFIELD class field (_t.class unboxed (list)))))))

    _
    (phase.throw extension.invalid-syntax [proc %synthesis inputs])))

(def: base-type
  (l.Lexer $.Type)
  ($_ p.either
      (p.after (l.this "boolean") (p@wrap _t.boolean))
      (p.after (l.this "byte") (p@wrap _t.byte))
      (p.after (l.this "short") (p@wrap _t.short))
      (p.after (l.this "int") (p@wrap _t.int))
      (p.after (l.this "long") (p@wrap _t.long))
      (p.after (l.this "float") (p@wrap _t.float))
      (p.after (l.this "double") (p@wrap _t.double))
      (p.after (l.this "char") (p@wrap _t.char))
      (p@map (function (_ name)
               (_t.class name (list)))
             (l.many (l.none-of "[")))
      ))

(def: java-type
  (l.Lexer $.Type)
  (do p.monad
    [raw base-type
     nesting (p.some (l.this "[]"))]
    (wrap (_t.array (list.size nesting) raw))))

(def: (generate-type argD)
  (-> Text (Operation $.Type))
  (case (l.run argD java-type)
    (#error.Failure error)
    (phase.throw invalid-syntax-for-jvm-type argD)

    (#error.Success type)
    (phase@wrap type)))

(def: (generate-arg generate argS)
  (-> (-> Synthesis (Operation Inst)) Synthesis
      (Operation [$.Type Inst]))
  (case argS
    (^ (synthesis.tuple (list (synthesis.text argD) argS)))
    (do phase.monad
      [argT (generate-type argD)
       argI (generate argS)]
      (wrap [argT argI]))

    _
    (phase.throw invalid-syntax-for-argument-generation "")))

(def: (method-return-type description)
  (-> Text (Operation (Maybe $.Type)))
  (case description
    "void"
    (phase@wrap #.None)

    _
    (phase@map (|>> #.Some) (generate-type description))))

(def: (prepare-argI [type argI])
  (-> [$.Type Inst] Inst)
  (case (_t.class-name type)
    (#.Some class-name)
    (|>> argI
         (_.CHECKCAST class-name))

    #.None
    argI))

(def: (invoke::static proc generate inputs)
  Handler
  (case inputs
    (^ (list& (synthesis.text class)
              (synthesis.text method)
              (synthesis.text unboxed)
              argsS))
    (do phase.monad
      [argsTI (monad.map @ (generate-arg generate) argsS)
       returnT (method-return-type unboxed)]
      (wrap (|>> (_.fuse (list@map ..prepare-argI argsTI))
                 (_.INVOKESTATIC class method
                                 (_t.method (list@map product.left argsTI) returnT (list))
                                 false))))

    _
    (phase.throw extension.invalid-syntax [proc %synthesis inputs])))

(template [<name> <invoke> <interface?>]
  [(def: (<name> proc generate inputs)
     Handler
     (case inputs
       (^ (list& (synthesis.text class)
                 (synthesis.text method)
                 (synthesis.text unboxed)
                 objectS
                 argsS))
       (do phase.monad
         [objectI (generate objectS)
          argsTI (monad.map @ (generate-arg generate) argsS)
          returnT (method-return-type unboxed)]
         (wrap (|>> objectI
                    (_.CHECKCAST class)
                    (_.fuse (list@map ..prepare-argI argsTI))
                    (<invoke> class method
                              (_t.method (list@map product.left argsTI) returnT (list))
                              <interface?>))))

       _
       (phase.throw extension.invalid-syntax [proc %synthesis inputs])))]

  [invoke::virtual _.INVOKEVIRTUAL false]
  [invoke::special _.INVOKESPECIAL false]
  [invoke::interface _.INVOKEINTERFACE true]
  )

(def: (invoke::constructor proc generate inputs)
  Handler
  (case inputs
    (^ (list& (synthesis.text class) argsS))
    (do phase.monad
      [argsTI (monad.map @ (generate-arg generate) argsS)]
      (wrap (|>> (_.NEW class)
                 _.DUP
                 (_.fuse (list@map ..prepare-argI argsTI))
                 (_.INVOKESPECIAL class "<init>"
                                  (_t.method (list@map product.left argsTI) #.None (list))
                                  false))))

    _
    (phase.throw extension.invalid-syntax [proc %synthesis inputs])))

(def: member
  Bundle
  (<| (bundle.prefix "member")
      (|> (: Bundle bundle.empty)
          (dictionary.merge (<| (bundle.prefix "static")
                                (|> (: Bundle bundle.empty)
                                    (bundle.install "get" static::get)
                                    (bundle.install "put" static::put))))
          (dictionary.merge (<| (bundle.prefix "virtual")
                                (|> (: Bundle bundle.empty)
                                    (bundle.install "get" virtual::get)
                                    (bundle.install "put" virtual::put))))
          (dictionary.merge (<| (bundle.prefix "invoke")
                                (|> (: Bundle bundle.empty)
                                    (bundle.install "static" invoke::static)
                                    (bundle.install "virtual" invoke::virtual)
                                    (bundle.install "special" invoke::special)
                                    (bundle.install "interface" invoke::interface)
                                    (bundle.install "constructor" invoke::constructor))))
          )))

(def: #export bundle
  Bundle
  (<| (bundle.prefix "jvm")
      (|> ..conversion
          (dictionary.merge ..int)
          (dictionary.merge ..long)
          (dictionary.merge ..float)
          (dictionary.merge ..double)
          (dictionary.merge ..char)
          (dictionary.merge ..array)
          (dictionary.merge ..object)
          (dictionary.merge ..member)
          )))