aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/structure.jvm.lux
blob: 2c04eaa0c7c2adee412c54188d923cfa17b05b1d (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
(;module:
  lux
  (lux (control [monad #+ do]
                ["ex" exception #+ exception:])
       (data text/format
             (coll [list]))
       [meta]
       [host #+ do-to])
  (luxc ["&" lang]
        (lang [";L" host]
              (host ["$" jvm]
                    (jvm ["$t" type]
                         ["$d" def]
                         ["$i" inst]))
              ["la" analysis]
              ["ls" synthesis]
              (translation [";T" common])))
  [../runtime])

(exception: #export Not-A-Tuple)

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

(def: #export (translate-tuple translate members)
  (-> (-> ls;Synthesis (Meta $;Inst)) (List ls;Synthesis) (Meta $;Inst))
  (do meta;Monad<Meta>
    [#let [size (list;size members)]
     _ (&;assert Not-A-Tuple (%code (` [(~@ members)]))
                 (n.>= +2 size))
     membersI (|> members
                  list;enumerate
                  (monad;map @ (function [[idx member]]
                                 (do @
                                   [memberI (translate member)]
                                   (wrap (|>. $i;DUP
                                              ($i;int (nat-to-int idx))
                                              memberI
                                              $i;AASTORE)))))
                  (:: @ map $i;fuse))]
    (wrap (|>. ($i;int (nat-to-int size))
               ($i;array $Object)
               membersI))))

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

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