aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/analyser/procedure/host.jvm.lux
blob: c75d6efd47fcdd6092b5cca0c23fc581c898c7e6 (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
(;module:
  [lux #- char]
  (lux (control [monad #+ do]
                ["p" parser])
       (concurrency ["A" atom])
       (data ["R" result]
             [text]
             (text format
                   ["l" lexer])
             (coll [list "list/" Fold<List>]
                   [array #+ Array]
                   ["d" dict]))
       [macro #+ Monad<Lux>]
       [type]
       (type ["TC" check])
       [host])
  (luxc ["&" base]
        ["&;" host]
        (lang ["la" analysis #+ Analysis])
        (analyser ["&;" common]))
  ["@" ../common]
  )

(do-template [<name> <class>]
  [(def: #export <name> Type (#;Host <class> (list)))]

  ## Boxes
  [Boolean   "java.lang.Boolean"]
  [Byte      "java.lang.Byte"]
  [Short     "java.lang.Short"]
  [Integer   "java.lang.Integer"]
  [Long      "java.lang.Long"]
  [Float     "java.lang.Float"]
  [Double    "java.lang.Double"]
  [Character "java.lang.Character"]
  [String    "java.lang.String"]

  ## Primitives
  [boolean   "boolean"]
  [byte      "byte"]
  [short     "short"]
  [int       "int"]
  [long      "long"]
  [float     "float"]
  [double    "double"]
  [char      "char"]
  )

(def: conversion-procs
  @;Bundle
  (<| (@;prefix "convert")
      (|> (d;new text;Hash<Text>)
          (@;install "double-to-float" (@;unary Double Float))
          (@;install "double-to-int" (@;unary Double Integer))
          (@;install "double-to-long" (@;unary Double Long))
          (@;install "float-to-double" (@;unary Float Double))
          (@;install "float-to-int" (@;unary Float Integer))
          (@;install "float-to-long" (@;unary Float Long))
          (@;install "int-to-byte" (@;unary Integer Byte))
          (@;install "int-to-char" (@;unary Integer Character))
          (@;install "int-to-double" (@;unary Integer Double))
          (@;install "int-to-float" (@;unary Integer Float))
          (@;install "int-to-long" (@;unary Integer Long))
          (@;install "int-to-short" (@;unary Integer Short))
          (@;install "long-to-double" (@;unary Long Double))
          (@;install "long-to-float" (@;unary Long Float))
          (@;install "long-to-int" (@;unary Long Integer))
          (@;install "long-to-short" (@;unary Long Short))
          (@;install "long-to-byte" (@;unary Long Byte))
          (@;install "char-to-byte" (@;unary Character Byte))
          (@;install "char-to-short" (@;unary Character Short))
          (@;install "char-to-int" (@;unary Character Integer))
          (@;install "char-to-long" (@;unary Character Long))
          (@;install "byte-to-long" (@;unary Byte Long))
          (@;install "short-to-long" (@;unary Short Long))
          )))

(do-template [<name> <prefix> <type>]
  [(def: <name>
     @;Bundle
     (<| (@;prefix <prefix>)
         (|> (d;new text;Hash<Text>)
             (@;install "+" (@;binary <type> <type> <type>))
             (@;install "-" (@;binary <type> <type> <type>))
             (@;install "*" (@;binary <type> <type> <type>))
             (@;install "/" (@;binary <type> <type> <type>))
             (@;install "%" (@;binary <type> <type> <type>))
             (@;install "=" (@;binary <type> <type> Boolean))
             (@;install "<" (@;binary <type> <type> Boolean))
             (@;install ">" (@;binary <type> <type> Boolean))
             (@;install "and" (@;binary <type> <type> <type>))
             (@;install "or" (@;binary <type> <type> <type>))
             (@;install "xor" (@;binary <type> <type> <type>))
             (@;install "shl" (@;binary <type> Integer <type>))
             (@;install "shr" (@;binary <type> Integer <type>))
             (@;install "ushr" (@;binary <type> Integer <type>))
             )))]

  [int-procs  "int"  Integer]
  [long-procs "long" Long]
  )

(do-template [<name> <prefix> <type>]
  [(def: <name>
     @;Bundle
     (<| (@;prefix <prefix>)
         (|> (d;new text;Hash<Text>)
             (@;install "+" (@;binary <type> <type> <type>))
             (@;install "-" (@;binary <type> <type> <type>))
             (@;install "*" (@;binary <type> <type> <type>))
             (@;install "/" (@;binary <type> <type> <type>))
             (@;install "%" (@;binary <type> <type> <type>))
             (@;install "=" (@;binary <type> <type> Boolean))
             (@;install "<" (@;binary <type> <type> Boolean))
             (@;install ">" (@;binary <type> <type> Boolean))
             )))]

  [float-procs  "float"  Float]
  [double-procs "double" Double]
  )

(def: char-procs
  @;Bundle
  (<| (@;prefix "char")
      (|> (d;new text;Hash<Text>)
          (@;install "=" (@;binary Character Character Boolean))
          (@;install "<" (@;binary Character Character Boolean))
          (@;install ">" (@;binary Character Character Boolean))
          )))

(def: #export boxes
  (d;Dict Text Text)
  (|> (list ["boolean" "java.lang.Boolean"]
            ["byte" "java.lang.Byte"]
            ["short" "java.lang.Short"]
            ["int" "java.lang.Integer"]
            ["long" "java.lang.Long"]
            ["float" "java.lang.Float"]
            ["double" "java.lang.Double"]
            ["char" "java.lang.Character"])
      (d;from-list text;Hash<Text>)))

(def: (array-length proc)
  (-> Text @;Proc)
  (function [analyse args]
    (&common;with-var
      (function [[var-id varT]]
        (case args
          (^ (list arrayC))
          (do Monad<Lux>
            [arrayA (&;with-expected-type (type (Array varT))
                      (analyse arrayC))
             expectedT macro;expected-type
             _ (&;within-type-env
                (TC;check expectedT Nat))]
            (wrap (#la;Procedure proc (list arrayA))))

          _
          (&;fail (@;wrong-amount-error proc +1 (list;size args))))))))

(def: (invalid-array-type arrayT)
  (-> Type Text)
  (format "Invalid type for array: " (%type arrayT)))

(def: (array-new proc)
  (-> Text @;Proc)
  (function [analyse args]
    (case args
      (^ (list lengthC))
      (do Monad<Lux>
        [lengthA (&;with-expected-type Nat
                   (analyse lengthC))
         expectedT macro;expected-type
         [level elem-class] (: (Lux [Nat Text])
                               (loop [analysisT expectedT
                                      level +0]
                                 (case analysisT
                                   (#;Apply inputT funcT)
                                   (case (type;apply (list inputT) funcT)
                                     (#;Some outputT)
                                     (recur outputT level)

                                     #;None
                                     (&;fail (invalid-array-type expectedT)))

                                   (^ (#;Host "#Array" (list elemT)))
                                   (recur elemT (n.inc level))

                                   (#;Host class _)
                                   (wrap [level class])
                                   
                                   _
                                   (&;fail (invalid-array-type expectedT)))))
         _ (&;assert "Must have at least 1 level of nesting in array type."
                     (n.> +0 level))]
        (wrap (#la;Procedure proc (list (#la;Nat level) (#la;Text elem-class) lengthA))))

      _
      (&;fail (@;wrong-amount-error proc +1 (list;size args))))))

(def: (check-object objectT)
  (-> Type (Lux Text))
  (case objectT
    (#;Host name _)
    (if (d;contains? name boxes)
      (&;fail (format "Primitives are not objects: " name))
      (:: Monad<Lux> wrap name))

    _
    (&;fail (format "Non-object type: " (%type objectT)))))

(def: (box-array-element-type elemT)
  (-> Type (Lux [Type Text]))
  (do Monad<Lux>
    []
    (case elemT
      (#;Host name #;Nil)
      (let [boxed-name (|> (d;get name boxes)
                           (default name))]
        (wrap [(#;Host boxed-name #;Nil)
               boxed-name]))

      (#;Host name _)
      (if (d;contains? name boxes)
        (&;fail (format "Primitives cannot be parameterized: " name))
        (:: Monad<Lux> wrap [elemT name]))

      _
      (&;fail (format "Invalid type for array element: " (%type elemT))))))

(def: (array-read proc)
  (-> Text @;Proc)
  (function [analyse args]
    (&common;with-var
      (function [[var-id varT]]
        (case args
          (^ (list arrayC idxC))
          (do Monad<Lux>
            [arrayA (&;with-expected-type (type (Array varT))
                      (analyse arrayC))
             elemT (&;within-type-env
                    (TC;read-var var-id))
             [elemT elem-class] (box-array-element-type elemT)
             idxA (&;with-expected-type Nat
                    (analyse idxC))
             expectedT macro;expected-type
             _ (&;within-type-env
                (TC;check expectedT elemT))]
            (wrap (#la;Procedure proc (list (#la;Text elem-class) arrayA idxA))))

          _
          (&;fail (@;wrong-amount-error proc +2 (list;size args))))))))

(def: (array-write proc)
  (-> Text @;Proc)
  (function [analyse args]
    (&common;with-var
      (function [[var-id varT]]
        (case args
          (^ (list arrayC idxC valueC))
          (do Monad<Lux>
            [arrayA (&;with-expected-type (type (Array varT))
                      (analyse arrayC))
             elemT (&;within-type-env
                    (TC;read-var var-id))
             [valueT elem-class] (box-array-element-type elemT)
             idxA (&;with-expected-type Nat
                    (analyse idxC))
             valueA (&;with-expected-type valueT
                      (analyse valueC))
             expectedT macro;expected-type
             _ (&;within-type-env
                (TC;check expectedT (type (Array elemT))))]
            (wrap (#la;Procedure proc (list (#la;Text elem-class) arrayA idxA valueA))))

          _
          (&;fail (@;wrong-amount-error proc +3 (list;size args))))))))

(def: array-procs
  @;Bundle
  (<| (@;prefix "array")
      (|> (d;new text;Hash<Text>)
          (@;install "length" array-length)
          (@;install "new" array-new)
          (@;install "read" array-read)
          (@;install "write" array-write)
          )))

(def: (object-null proc)
  (-> Text @;Proc)
  (function [analyse args]
    (case args
      (^ (list))
      (do Monad<Lux>
        [expectedT macro;expected-type
         _ (check-object expectedT)]
        (wrap (#la;Procedure proc (list))))

      _
      (&;fail (@;wrong-amount-error proc +0 (list;size args))))))

(def: (object-null? proc)
  (-> Text @;Proc)
  (function [analyse args]
    (&common;with-var
      (function [[var-id varT]]
        (case args
          (^ (list objectC))
          (do Monad<Lux>
            [objectA (&;with-expected-type (type varT)
                       (analyse objectC))
             objectT (&;within-type-env
                      (TC;read-var var-id))
             _ (check-object objectT)
             expectedT macro;expected-type
             _ (&;within-type-env
                (TC;check expectedT Bool))]
            (wrap (#la;Procedure proc (list objectA))))

          _
          (&;fail (@;wrong-amount-error proc +1 (list;size args))))))))

(def: (object-synchronized proc)
  (-> Text @;Proc)
  (function [analyse args]
    (&common;with-var
      (function [[var-id varT]]
        (case args
          (^ (list monitorC exprC))
          (do Monad<Lux>
            [monitorA (&;with-expected-type (type varT)
                        (analyse monitorC))
             monitorT (&;within-type-env
                       (TC;read-var var-id))
             _ (check-object monitorT)
             exprA (analyse exprC)]
            (wrap (#la;Procedure proc (list monitorA exprA))))

          _
          (&;fail (@;wrong-amount-error proc +2 (list;size args))))))))

(host;import java.lang.Object)

(host;import java.lang.ClassLoader)

(host;import (java.lang.Class c)
  (#static forName [String boolean ClassLoader] #try (Class Object))
  (isAssignableFrom [(Class Object)] boolean))

(def: (load-class name)
  (-> Text (Lux (Class Object)))
  (do Monad<Lux>
    [class-loader &host;class-loader]
    (case (Class.forName [name false class-loader])
      (#R;Success [class])
      (wrap class)

      (#R;Error error)
      (&;fail (format "Unknown class: " name)))))

(def: (sub-class? super sub)
  (-> Text Text (Lux Bool))
  (do Monad<Lux>
    [super (load-class super)
     sub (load-class sub)]
    (wrap (Class.isAssignableFrom [sub] super))))

(def: (object-throw proc)
  (-> Text @;Proc)
  (function [analyse args]
    (&common;with-var
      (function [[var-id varT]]
        (case args
          (^ (list exceptionC))
          (do Monad<Lux>
            [exceptionA (&;with-expected-type (type varT)
                          (analyse exceptionC))
             exceptionT (&;within-type-env
                         (TC;read-var var-id))
             exception-class (check-object exceptionT)
             ? (sub-class? "java.lang.Throwable" exception-class)
             _ (: (Lux Unit)
                  (if ?
                    (wrap [])
                    (&;fail (format "Must throw a sub-class of java.lang.Throwable: " exception-class))))
             expectedT macro;expected-type
             _ (&;within-type-env
                (TC;check expectedT Bottom))]
            (wrap (#la;Procedure proc (list exceptionA))))

          _
          (&;fail (@;wrong-amount-error proc +1 (list;size args))))))))

(def: (object-class proc)
  (-> Text @;Proc)
  (function [analyse args]
    (&common;with-var
      (function [[var-id varT]]
        (case args
          (^ (list classC))
          (case classC
            [_ (#;Text class)]
            (do Monad<Lux>
              [_ (load-class class)
               expectedT macro;expected-type
               _ (&;within-type-env
                  (TC;check expectedT (#;Host "java.lang.Class" (list (#;Host class (list))))))]
              (wrap (#la;Procedure proc (list (#la;Text class)))))

            _
            (&;fail (format "Wrong syntax for '" proc "'.")))

          _
          (&;fail (@;wrong-amount-error proc +1 (list;size args))))))))

(def: object-procs
  @;Bundle
  (<| (@;prefix "object")
      (|> (d;new text;Hash<Text>)
          (@;install "null" object-null)
          (@;install "null?" object-null?)
          (@;install "synchronized" object-synchronized)
          (@;install "throw" object-throw)
          (@;install "class" object-class)
          )))

(def: #export procedures
  @;Bundle
  (<| (@;prefix "jvm")
      (|> (d;new text;Hash<Text>)
          (d;merge conversion-procs)
          (d;merge int-procs)
          (d;merge long-procs)
          (d;merge float-procs)
          (d;merge double-procs)
          (d;merge char-procs)
          (d;merge array-procs)
          (d;merge object-procs)
          )))