aboutsummaryrefslogtreecommitdiff
path: root/lux-jvm/source/luxc/lang/translation/jvm/structure.lux
blob: 100bce9d9db6d88186db15ee5fb7ac535d42c81b (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
(.module:
  [lux (#- Type)
   ["." ffi (#+ import:)]
   [abstract
    ["." monad (#+ do)]]
   [control
    ["." exception (#+ exception:)]]
   [data
    [text
     ["%" format (#+ format)]]
    [collection
     ["." list]]]
   [math
    [number
     ["n" nat]
     ["i" int]]]
   [target
    [jvm
     ["." type (#+ Type)
      ["." category (#+ Void Value Return Primitive Object Class Array Var Parameter Method)]
      ["." descriptor (#+ Descriptor)]
      ["." signature (#+ Signature)]]]]
   [tool
    [compiler
     ["." phase]
     [meta
      [archive (#+ Archive)]]
     [language
      [lux
       [synthesis (#+ Synthesis)]]]]]]
  [luxc
   [lang
    [host
     [jvm (#+ Inst Operation Phase Generator)
      ["_" inst]]]]]
  ["." //
   ["#." runtime]])

(exception: #export (not_a_tuple {size Nat})
  (exception.report
   ["Expected size" ">= 2"]
   ["Actual size" (%.nat size)]))

(def: #export (tuple generate archive members)
  (Generator (List Synthesis))
  (do {@ phase.monad}
    [#let [size (list.size members)]
     _ (phase.assert ..not_a_tuple size
                     (n.>= 2 size))
     membersI (|> members
                  list.enumeration
                  (monad.map @ (function (_ [idx member])
                                 (do @
                                   [memberI (generate archive member)]
                                   (wrap (|>> _.DUP
                                              (_.int (.int idx))
                                              memberI
                                              _.AASTORE)))))
                  (\ @ map _.fuse))]
    (wrap (|>> (_.int (.int size))
               (_.array //runtime.$Value)
               membersI))))

(import: java/lang/Byte
  ["#::."
   (#static MAX_VALUE byte)
   (#static MIN_VALUE byte)])

(import: java/lang/Short
  ["#::."
   (#static MAX_VALUE short)
   (#static MIN_VALUE short)])

(def: #export (tagI lefts right?)
  (-> Nat Bit Inst)
  (case (if right?
          (.inc lefts)
          lefts)
    0 _.ICONST_0
    1 _.ICONST_1
    2 _.ICONST_2
    3 _.ICONST_3
    4 _.ICONST_4
    5 _.ICONST_5
    tag (let [tag (.int tag)]
          (cond (and (i.>= (java/lang/Byte::MIN_VALUE) tag)
                     (i.<= (java/lang/Byte::MAX_VALUE) tag))
                (_.BIPUSH tag)
                
                (and (i.>= (java/lang/Short::MIN_VALUE) tag)
                     (i.<= (java/lang/Short::MAX_VALUE) tag))
                (_.SIPUSH tag)

                ## else
                (_.int tag)))))

(def: #export leftI _.NULL)
(def: #export rightI (_.string ""))

(def: #export (flagI right?)
  (-> Bit Inst)
  (if right?
    ..rightI
    ..leftI))

(def: #export (variant generate archive [lefts right? member])
  (Generator [Nat Bit Synthesis])
  (do phase.monad
    [memberI (generate archive member)
     #let [tagI (..tagI lefts right?)]]
    (wrap (|>> tagI
               (flagI right?)
               memberI
               (_.INVOKESTATIC //.$Runtime
                               "variant_make"
                               (type.method [(list //runtime.$Tag //runtime.$Flag //runtime.$Value)
                                             //.$Variant
                                             (list)]))))))