blob: 4601003a8dfb36d5eb9a9a98e945bb044bfaab50 (
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
|
(.module:
[lux (#- Type int char)
[abstract
[equivalence (#+ Equivalence)]]
[control
["." function]
["<>" parser
["<t>" text (#+ Parser)]]]
[data
["." product]
["." maybe ("#@." functor)]
["." text ("#@." equivalence)
["%" format (#+ format)]]
[collection
["." list ("#@." functor)]]]]
["." // #_
[encoding
["#." name]]])
(template [<descriptor> <definition>]
[(def: #export <definition> <descriptor>)]
["V" void-descriptor]
["Z" boolean-descriptor]
["B" byte-descriptor]
["S" short-descriptor]
["I" int-descriptor]
["J" long-descriptor]
["F" float-descriptor]
["D" double-descriptor]
["C" char-descriptor]
)
(def: #export array-prefix "[")
(def: object-prefix "L")
(def: var-prefix "T")
(def: wildcard-signature "*")
(def: lower-prefix "-")
(def: upper-prefix "+")
(def: object-suffix ";")
(def: #export object-class "java.lang.Object")
(def: valid-var-characters/head
(format "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"_"))
(def: valid-var-characters/tail
(format valid-var-characters/head
"0123456789"))
(def: valid-class-characters/head
(format valid-var-characters/head //name.internal-separator))
(def: valid-class-characters/tail
(format valid-var-characters/tail //name.internal-separator))
(type: #export Bound
#Lower
#Upper)
## TODO: Replace with polytypism.
(structure: #export bound-equivalence
(Equivalence Bound)
(def: (= parameter subject)
(case [parameter subject]
(^template [<tag>]
[<tag> <tag>]
true)
([#Lower]
[#Upper])
_
false)))
(type: #export Primitive
#Boolean
#Byte
#Short
#Int
#Long
#Float
#Double
#Char)
## TODO: Replace with polytypism.
(structure: #export primitive-equivalence
(Equivalence Primitive)
(def: (= parameter subject)
(case [parameter subject]
(^template [<tag>]
[<tag> <tag>]
true)
([#Boolean]
[#Byte]
[#Short]
[#Int]
[#Long]
[#Float]
[#Double]
[#Char])
_
false)))
(type: #export Var Text)
(with-expansions [<Class> (as-is [Text (List Generic)])]
(type: #export #rec Generic
(#Var Var)
(#Wildcard (Maybe [Bound Generic]))
(#Class <Class>))
(type: #export Class
<Class>)
)
(structure: #export generic-equivalence
(Equivalence Generic)
(def: (= parameter subject)
(case [parameter subject]
[(#Var parameter) (#Var subject)]
(text@= parameter subject)
[(#Wildcard parameter) (#Wildcard subject)]
(:: (maybe.equivalence (product.equivalence bound-equivalence =))
= parameter subject)
[(#Class [nameP paramsP]) (#Class [nameS paramsS])]
(and (text@= nameP nameS)
(:: (list.equivalence =) = paramsP paramsS))
_
false)))
(type: #export Parameter
[Text Class (List Class)])
(type: #export #rec Type
(#Primitive Primitive)
(#Generic Generic)
(#Array Type))
(structure: #export type-equivalence
(Equivalence Type)
(def: (= parameter subject)
(case [parameter subject]
[(#Primitive parameter) (#Primitive subject)]
(:: ..primitive-equivalence = parameter subject)
[(#Generic parameter) (#Generic subject)]
(:: ..generic-equivalence = parameter subject)
[(#Array parameter) (#Array subject)]
(= parameter subject)
_
false)))
(type: #export Argument
[Text Type])
(type: #export Return
(Maybe Type))
(def: #export return-equivalence
(Equivalence Return)
(maybe.equivalence ..type-equivalence))
(type: #export Method
{#args (List Type)
#return Return
#exceptions (List Generic)})
(structure: #export method-equivalence
(Equivalence Method)
(def: (= [argsP returnP exceptionsP] [argsS returnS exceptionsS])
(and (:: (list.equivalence ..type-equivalence) = argsP argsS)
(:: ..return-equivalence = returnP returnS)
(:: (list.equivalence ..generic-equivalence) = exceptionsP exceptionsS))))
(type: #export (Typed a)
[Type a])
(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 (signature type)
(-> Type Text)
(case type
(#Primitive prim)
(case prim
#Boolean ..boolean-descriptor
#Byte ..byte-descriptor
#Short ..short-descriptor
#Int ..int-descriptor
#Long ..long-descriptor
#Float ..float-descriptor
#Double ..double-descriptor
#Char ..char-descriptor)
(#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 ..object-prefix (|> class //name.internal //name.read) =params ..object-suffix))
(#Var name)
(format ..var-prefix name ..object-suffix)
(#Wildcard #.None)
..wildcard-signature
(^template [<tag> <prefix>]
(#Wildcard (#.Some [<tag> bound]))
(format <prefix> (signature (#Generic bound))))
([#Lower ..lower-prefix]
[#Upper ..upper-prefix]))
))
(template [<name> <head> <tail> <adapter>]
[(def: <name>
(Parser Text)
(:: <>.functor map <adapter>
(<t>.slice (<t>.and! (<t>.one-of! <head>)
(<t>.some! (<t>.one-of! <tail>))))))]
[parse-class-name valid-class-characters/head valid-class-characters/tail (|>> //name.internal //name.external)]
[parse-var-name valid-var-characters/head valid-var-characters/tail function.identity]
)
(def: parse-var
(Parser Var)
(|> ..parse-var-name
(<>.after (<t>.this ..var-prefix))
(<>.before (<t>.this ..object-suffix))))
(def: parse-bound
(Parser Bound)
($_ <>.or
(<t>.this ..lower-prefix)
(<t>.this ..upper-prefix)))
(def: parse-generic
(Parser Generic)
(<>.rec
(function (_ recur)
($_ <>.or
..parse-var
($_ <>.or
(<t>.this ..wildcard-signature)
(<>.and ..parse-bound recur)
)
(|> (<>.and ..parse-class-name
(|> (<>.some recur)
(<>.after (<t>.this "<"))
(<>.before (<t>.this ">"))
(<>.default (list))))
(<>.after (<t>.this ..object-prefix))
(<>.before (<t>.this ..object-suffix)))
))))
(def: #export parse-signature
(Parser Type)
(<>.rec
(function (_ recur)
($_ <>.or
($_ <>.or
(<t>.this ..boolean-descriptor)
(<t>.this ..byte-descriptor)
(<t>.this ..short-descriptor)
(<t>.this ..int-descriptor)
(<t>.this ..long-descriptor)
(<t>.this ..float-descriptor)
(<t>.this ..double-descriptor)
(<t>.this ..char-descriptor)
)
..parse-generic
(<>.after (<t>.this ..array-prefix)
recur)
))))
(def: #export (method args return exceptions)
(-> (List Type) (Maybe Type) (List Generic) Method)
{#args args #return return #exceptions exceptions})
(def: #export (method-signature method)
(-> Method Text)
(format (|> (get@ #args method)
(list@map signature)
(text.join-with "")
(text.enclose ["(" ")"]))
(case (get@ #return method)
#.None
..void-descriptor
(#.Some return)
(signature return))
(|> (get@ #exceptions method)
(list@map (|>> #Generic signature (format "^")))
(text.join-with ""))))
|