aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/target/jvm/type.lux
blob: 23925e468f4463f6bf2ac462fb4bcf5fd5de1943 (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
(.module:
  [lux (#- Type int char)
   [data
    ["." maybe ("#@." functor)]
    ["." text
     format]
    [collection
     ["." list ("#@." functor)]]]])

(def: array-prefix "[")
(def: binary-void-name "V")
(def: binary-boolean-name "Z")
(def: binary-byte-name    "B")
(def: binary-short-name   "S")
(def: binary-int-name     "I")
(def: binary-long-name    "J")
(def: binary-float-name   "F")
(def: binary-double-name  "D")
(def: binary-char-name    "C")
(def: binary-object-prefix "L")
(def: binary-object-suffix ";")
(def: object-class "java.lang.Object")

(type: #export Bound
  #Upper
  #Lower)

(type: #export Primitive
  #Boolean
  #Byte
  #Short
  #Int
  #Long
  #Float
  #Double
  #Char)

(type: #export #rec Generic
  (#Var Text)
  (#Wildcard (Maybe [Bound Generic]))
  (#Class Text (List Generic)))

(type: #export Class
  [Text (List Generic)])

(type: #export Parameter
  [Text Class (List Class)])

(type: #export #rec Type
  (#Primitive Primitive)
  (#Generic Generic)
  (#Array Type))

(type: #export Method
  {#args (List Type)
   #return (Maybe Type)
   #exceptions (List Generic)})

(template [<name> <primitive>]
  [(def: #export <name> Type (#Primitive <primitive>))]

  [boolean #Boolean]
  [byte    #Byte]
  [short   #Short]
  [int     #Int]
  [long    #Long]
  [float   #Float]
  [double  #Double]
  [char    #Char]
  )

(template: #export (class name params)
  (#..Generic (#..Class name params)))

(template: #export (var name)
  (#..Generic (#..Var name)))

(template: #export (wildcard bound)
  (#..Generic (#..Wildcard bound)))

(def: #export (array depth elemT)
  (-> Nat Type Type)
  (case depth
    0 elemT
    _ (#Array (array (dec depth) elemT))))

(def: #export binary-name
  (-> Text Text)
  (text.replace-all "." "/"))

(def: #export (descriptor type)
  (-> Type Text)
  (case type
    (#Primitive prim)
    (case prim
      #Boolean ..binary-boolean-name
      #Byte    ..binary-byte-name
      #Short   ..binary-short-name
      #Int     ..binary-int-name
      #Long    ..binary-long-name
      #Float   ..binary-float-name
      #Double  ..binary-double-name
      #Char    ..binary-char-name)

    (#Array sub)
    (format ..array-prefix (descriptor sub))

    (#Generic generic)
    (case generic
      (#Class class params)
      (format ..binary-object-prefix (binary-name class) ..binary-object-suffix)

      (^or (#Var name) (#Wildcard ?bound))
      (descriptor (#Generic (#Class ..object-class (list)))))
    ))

(def: #export (class-name type)
  (-> Type (Maybe Text))
  (case type
    (#Primitive prim)
    #.None

    (#Array sub)
    (#.Some (descriptor type))

    (#Generic generic)
    (case generic
      (#Class class params)
      (#.Some class)

      (^or (#Var name) (#Wildcard ?bound))
      (#.Some ..object-class))
    ))

(def: #export (signature type)
  (-> Type Text)
  (case type
    (#Primitive prim)
    (case prim
      #Boolean ..binary-boolean-name
      #Byte    ..binary-byte-name
      #Short   ..binary-short-name
      #Int     ..binary-int-name
      #Long    ..binary-long-name
      #Float   ..binary-float-name
      #Double  ..binary-double-name
      #Char    ..binary-char-name)

    (#Array sub)
    (format ..array-prefix (signature sub))

    (#Generic generic)
    (case generic
      (#Class class params)
      (let [=params (if (list.empty? params)
                      ""
                      (format "<"
                              (|> params
                                  (list@map (|>> #Generic signature))
                                  (text.join-with ""))
                              ">"))]
        (format ..binary-object-prefix (binary-name class) =params ..binary-object-suffix))

      (#Var name)
      (format "T" name ..binary-object-suffix)

      (#Wildcard #.None)
      "*"

      (^template [<tag> <prefix>]
        (#Wildcard (#.Some [<tag> bound]))
        (format <prefix> (signature (#Generic bound))))
      ([#Upper "+"]
       [#Lower "-"]))
    ))

(def: #export (method args return exceptions)
  (-> (List Type) (Maybe Type) (List Generic) Method)
  {#args args #return return #exceptions exceptions})

(def: method-args
  (text.enclose ["(" ")"]))

(def: #export (method-descriptor method)
  (-> Method Text)
  (format (|> (get@ #args method) (list@map descriptor) (text.join-with "") ..method-args)
          (case (get@ #return method)
            #.None
            ..binary-void-name

            (#.Some return)
            (descriptor return))))

(def: #export (method-signature method)
  (-> Method Text)
  (format (|> (get@ #args method) (list@map signature) (text.join-with "") ..method-args)
          (case (get@ #return method)
            #.None
            ..binary-void-name

            (#.Some return)
            (signature return))
          (|> (get@ #exceptions method)
              (list@map (|>> #Generic signature (format "^")))
              (text.join-with ""))))