aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/generator/structure.jvm.lux
blob: e3a4bed7539756bdcd2ea2ad2a817a27dd9810d6 (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
(;module:
  lux
  (lux (control monad)
       (data text/format
             (coll [list]))
       [macro #+ Monad<Lux> "Lux/" Monad<Lux>]
       [host #+ jvm-import do-to])
  (luxc ["&" base]
        (lang ["la" analysis]
              ["ls" synthesis])
        ["&;" analyser]
        ["&;" synthesizer]
        (generator ["&;" common]
                   (host ["$" jvm]
                         (jvm ["$t" type]
                              ["$d" def]
                              ["$i" inst]))))
  [../runtime])

(def: $Object $;Type ($t;class "java.lang.Object" (list)))

(def: #export (generate-tuple generate members)
  (-> (-> ls;Synthesis (Lux $;Inst)) (List ls;Synthesis) (Lux $;Inst))
  (do Monad<Lux>
    [#let [size (list;size members)]
     _ (&;assert "Cannot generate tuples with less than 2 elements."
                 (n.>= +2 size))
     membersI (|> members
                  list;enumerate
                  (mapM @ (function [[idx member]]
                            (do @
                              [memberI (generate member)]
                              (wrap (|>. $i;DUP
                                         ($i;int (nat-to-int idx))
                                         memberI
                                         $i;AASTORE)))))
                  (:: @ map $i;fuse))]
    (wrap (|>. ($i;array $Object size) membersI))))

(def: (flagI tail?)
  (-> Bool $;Inst)
  (if tail?
    ($i;string "")
    $i;NULL))

(def: #export (generate-variant generate tag tail? member)
  (-> (-> ls;Synthesis (Lux $;Inst)) Nat Bool ls;Synthesis (Lux $;Inst))
  (do Monad<Lux>
    [memberI (generate member)]
    (wrap (|>. ($i;int (nat-to-int tag))
               (flagI tail?)
               memberI
               ($i;INVOKESTATIC ../runtime;runtime-name
                                "sum_make"
                                ($t;method (list $t;int $Object $Object)
                                           (#;Some ($t;array +1 $Object))
                                           (list))
                                false)))))