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

(exception: .public (not_a_tuple {size Nat})
  (exception.report
   ["Expected size" ">= 2"]
   ["Actual size" (%.nat size)]))

(def: .public (tuple generate archive members)
  (Generator (List Synthesis))
  (do {@ phase.monad}
    [.let [size (list.size members)]
     _ (phase.assertion ..not_a_tuple size
                        (n.>= 2 size))
     membersI (|> members
                  list.enumeration
                  (monad.each @ (function (_ [idx member])
                                  (do @
                                    [memberI (generate archive member)]
                                    (in (|>> _.DUP
                                             (_.int (.int idx))
                                             memberI
                                             _.AASTORE)))))
                  (\ @ each _.fuse))]
    (in (|>> (_.int (.int size))
             (_.array //runtime.$Value)
             membersI))))

(import: java/lang/Byte
  ["#::."
   ("static" MAX_VALUE byte)
   ("static" MIN_VALUE byte)])

(import: java/lang/Short
  ["#::."
   ("static" MAX_VALUE short)
   ("static" MIN_VALUE short)])

(def: .public (tagI lefts right?)
  (-> Nat Bit Inst)
  (case (if right?
          (.++ lefts)
          lefts)
    0 _.ICONST_0
    1 _.ICONST_1
    2 _.ICONST_2
    3 _.ICONST_3
    4 _.ICONST_4
    5 _.ICONST_5
    tag (let [tag (.int tag)]
          (cond (and (i.>= (java/lang/Byte::MIN_VALUE) tag)
                     (i.<= (java/lang/Byte::MAX_VALUE) tag))
                (_.BIPUSH tag)
                
                (and (i.>= (java/lang/Short::MIN_VALUE) tag)
                     (i.<= (java/lang/Short::MAX_VALUE) tag))
                (_.SIPUSH tag)

                ... else
                (_.int tag)))))

(def: .public leftI _.NULL)
(def: .public rightI (_.string ""))

(def: .public (flagI right?)
  (-> Bit Inst)
  (if right?
    ..rightI
    ..leftI))

(def: .public (variant generate archive [lefts right? member])
  (Generator [Nat Bit Synthesis])
  (do phase.monad
    [memberI (generate archive member)
     .let [tagI (..tagI lefts right?)]]
    (in (|>> tagI
             (flagI right?)
             memberI
             (_.INVOKESTATIC //.$Runtime
                             "variant_make"
                             (type.method [(list)
                                           (list //runtime.$Tag //runtime.$Flag //runtime.$Value)
                                           //.$Variant
                                           (list)]))))))