aboutsummaryrefslogtreecommitdiff
path: root/new-luxc/source/luxc/lang/translation/jvm/structure.lux
blob: 10c9bacb9d35a448943612c615901d90e8087797 (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
(.module:
  [lux (#- Type)
   [abstract
    ["." monad (#+ do)]]
   [control
    ["ex" exception (#+ exception:)]]
   [data
    [number
     ["n" nat]]
    [text
     ["%" format (#+ format)]]
    [collection
     ["." list]]]
   [target
    [jvm
     ["." type (#+ Type)
      ["." category (#+ Void Value Return Primitive Object Class Array Var Parameter Method)]
      ["." descriptor (#+ Descriptor)]
      ["." signature (#+ Signature)]]]]
   [tool
    [compiler
     [synthesis (#+ Synthesis)]
     ["." phase]]]]
  [luxc
   [lang
    [host
     [jvm (#+ Inst Operation Phase)
      ["_" inst]]]]]
  ["." //
   ["#." runtime]])

(exception: #export (not-a-tuple {size Nat})
  (ex.report ["Expected size" ">= 2"]
             ["Actual size" (%.nat size)]))

(def: #export (tuple generate members)
  (-> Phase (List Synthesis) (Operation Inst))
  (do phase.monad
    [#let [size (list.size members)]
     _ (phase.assert not-a-tuple size
                     (n.>= 2 size))
     membersI (|> members
                  list.enumerate
                  (monad.map @ (function (_ [idx member])
                                 (do @
                                   [memberI (generate member)]
                                   (wrap (|>> _.DUP
                                              (_.int (.int idx))
                                              memberI
                                              _.AASTORE)))))
                  (:: @ map _.fuse))]
    (wrap (|>> (_.int (.int size))
               (_.array //runtime.$Value)
               membersI))))

(def: (flagI right?)
  (-> Bit Inst)
  (if right?
    (_.string "")
    _.NULL))

(def: #export (variant generate lefts right? member)
  (-> Phase Nat Bit Synthesis (Operation Inst))
  (do phase.monad
    [memberI (generate member)]
    (wrap (|>> (_.int (.int (if right?
                              (.inc lefts)
                              lefts)))
               (flagI right?)
               memberI
               (_.INVOKESTATIC //.$Runtime
                               "variant_make"
                               (type.method [(list //runtime.$Tag //runtime.$Flag //runtime.$Value)
                                             //.$Variant
                                             (list)]))))))