aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/target/jvm/constant/pool.lux
blob: 41f312071e24037358b6c957c11758287c43a6c8 (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
(.using
 [library
  [lux (.full)
   ["[0]" ffi]
   [abstract
    [equivalence (.only Equivalence)]
    [functor (.only Functor)]
    [monad (.only Monad do)]]
   [control
    ["[0]" pipe]
    ["[0]" state (.only +State)]
    ["[0]" try (.only Try)]]
   [data
    ["[0]" product]
    ["[0]" text]
    ["[0]" format
     ["[1]" binary (.only Writer) ("specification#[0]" monoid)]]
    [collection
     ["[0]" sequence (.only Sequence) ("[1]#[0]" mix)]]]
   [math
    [number
     ["[0]" int]
     ["[0]" frac]
     ["[0]" i32]]]]]
 ["[0]" // (.only UTF8 String Class Integer Float Long Double Constant Name_And_Type Reference)
  [//
   ["[1][0]" index (.only Index)]
   [encoding
    ["[1][0]" name (.only Internal External)]
    ["[1][0]" unsigned]]
   [type
    [category (.only Value Method)]
    ["[1][0]" descriptor (.only Descriptor)]]]])

(type: .public Pool
  [Index (Sequence [Index Constant])])

(def: .public equivalence
  (Equivalence Pool)
  (product.equivalence //index.equivalence
                       (sequence.equivalence (product.equivalence //index.equivalence
                                                                  //.equivalence))))

(type: .public (Resource a)
  (+State Try Pool a))

(implementation: .public functor
  (Functor Resource)

  (def: (each $ it)
    (|>> it
         (pipe.case
           {try.#Success [state output]}
           {try.#Success [state ($ output)]}
           
           ... {try.#Failure error}
           failure
           (as_expected failure)))))

(implementation: .public monad
  (Monad Resource)

  (def: functor ..functor)
  
  (def: (in it)
    (function (_ state)
      {try.#Success [state it]}))
  
  (def: (conjoint it)
    (function (_ state)
      (case (it state)
        {try.#Success [state' it']}
        (it' state')

        ... {try.#Failure error}
        failure
        (as_expected failure)))))

(template: (try|each <binding> <value> <body>)
  [(case <value>
     {try.#Success <binding>}
     <body>

     ... {try.#Failure error}
     failure
     (as_expected failure))])

(template: (try|in <it>)
  [{try.#Success <it>}])

(template: (!add <state> <tag> <equivalence> <value>)
  [(let [[current pool] <state>
         <value>' <value>]
     (with_expansions [<try_again> (these (again (.++ idx)))]
       (loop (again [idx 0])
         (case (sequence.item idx pool)
           {try.#Success entry}
           (case entry
             [index {<tag> reference}]
             (if (# <equivalence> = reference <value>')
               {try.#Success [[current pool]
                              index]}
               <try_again>)
             
             _
             <try_again>)
           
           {try.#Failure _}
           (<| (let [new {<tag> <value>'}])
               (try|each @new (//unsigned.u2 (//.size new)))
               (try|each next (is (Try Index)
                                  (|> current
                                      //index.value
                                      (//unsigned.+/2 @new)
                                      (# try.monad each //index.index))))
               (try|in [[next
                         (sequence.suffix [current new] pool)]
                        current]))))))])

(template: (/|do <state> <body>)
  [(function (_ <state>)
     <body>)])

(template: (/|each <state> <binding> <value> <body>)
  [(case (<value> <state>)
     {try.#Success [<state> <binding>]}
     <body>

     ... {try.#Failure error}
     failure
     (as_expected failure))])

(type: (Adder of)
  (-> of (Resource (Index of))))

(template [<name> <type> <tag> <equivalence>]
  [(def: .public (<name> value)
     (Adder <type>)
     (<| (/|do %)
         (!add % <tag> <equivalence> value)))]

  [integer Integer //.#Integer (//.value_equivalence i32.equivalence)]
  [float Float //.#Float (//.value_equivalence //.float_equivalence)]
  [long Long //.#Long (//.value_equivalence int.equivalence)]
  [double Double //.#Double (//.value_equivalence frac.equivalence)]
  [utf8 UTF8 //.#UTF8 text.equivalence]
  )

(def: .public (string value)
  (-> Text (Resource (Index String)))
  (<| (/|do %)
      (/|each % @value (utf8 value))
      (let [value (//.string @value)])
      (!add % //.#String (//.value_equivalence //index.equivalence) value)))

(def: .public (class name)
  (-> Internal (Resource (Index Class)))
  (<| (/|do %)
      (/|each % @name (utf8 (//name.read name)))
      (let [value (//.class @name)])
      (!add % //.#Class //.class_equivalence value)))

(def: .public (descriptor value)
  (All (_ kind)
    (-> (Descriptor kind)
        (Resource (Index (Descriptor kind)))))
  (<| (let [value (//descriptor.descriptor value)])
      (/|do %)
      (!add % //.#UTF8 text.equivalence value)))

(type: .public (Member of)
  (Record
   [#name UTF8
    #descriptor (Descriptor of)]))

(def: .public (name_and_type [name descriptor])
  (All (_ of)
    (-> (Member of) (Resource (Index (Name_And_Type of)))))
  (<| (/|do %)
      (/|each % @name (utf8 name))
      (/|each % @descriptor (..descriptor descriptor))
      (!add % //.#Name_And_Type //.name_and_type_equivalence [//.#name @name //.#descriptor @descriptor])))

(template [<name> <tag> <of>]
  [(def: .public (<name> class member)
     (-> External (Member <of>) (Resource (Index (Reference <of>))))
     (<| (/|do %)
         (/|each % @class (..class (//name.internal class)))
         (/|each % @name_and_type (name_and_type member))
         (!add % <tag> //.reference_equivalence [//.#class @class //.#name_and_type @name_and_type])))]

  [field //.#Field Value]
  [method //.#Method Method]
  [interface_method //.#Interface_Method Method]
  )

(template: (!index <index>)
  [(|> <index> //index.value //unsigned.value)])

(def: .public writer
  (Writer Pool)
  (function (_ [next pool])
    (sequence#mix (function (_ [_index post] pre)
                    (specification#composite pre (//.writer post)))
                  (format.bits_16 (!index next))
                  pool)))

(def: .public empty
  Pool
  [(|> 1 //unsigned.u2 try.trusted //index.index)
   sequence.empty])