aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/target/jvm/constant/pool.lux
blob: 590102a094a3e3bb3c18ec7450165e660ddb5096 (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
212
213
214
215
216
217
218
219
220
(.module:
  [lux #*
   ["." host]
   [abstract
    ["." equivalence (#+ Equivalence)]
    [monad (#+ do)]]
   [control
    ["." state (#+ State)]
    ["." try (#+ Try)]
    ["." exception (#+ exception:)]]
   [data
    [number
     ["." int]
     ["." frac]]
    ["." text
     ["%" format (#+ format)]]
    [format
     [".F" binary (#+ Writer) ("specification@." monoid)]]
    [collection
     ["." row (#+ Row) ("#@." fold)]]]
   [type
    abstract]
   [macro
    ["." template]]]
  ["." // (#+ UTF8 String Class Integer Float Long Double Constant Name-And-Type Reference)
   [//
    [encoding
     ["#." name (#+ Internal External)]
     ["#." unsigned]]
    ["#." index (#+ Index)]
    ["#." descriptor (#+ Field Method Descriptor)]]])

(type: #export Pool [Index (Row [Index Constant])])

(def: #export equivalence
  (Equivalence Pool)
  (equivalence.product //index.equivalence
                       (row.equivalence (equivalence.product //index.equivalence
                                                             //.equivalence))))

(template: (!add <tag> <=> <value>)
  (function (_ [next pool])
    (with-expansions [<try-again> (as-is (recur (.inc idx)))]
      (loop [idx 0]
        (case (row.nth idx pool)
          (#.Some entry)
          (case entry
            [index (<tag> reference)]
            (if (:: <=> = reference <value>)
              [[next pool]
               index]
              <try-again>)
            
            _
            <try-again>)
          
          #.None
          (let [new (<tag> <value>)]
            [[(|> next
                  //index.number
                  (//unsigned.u2/+ (//unsigned.u2 (//.size new)))
                  //index.index)
              (row.add [next new] pool)]
             next]))))))

(template: (!raw-index <index>)
  (|> <index> //index.number //unsigned.nat))

(exception: #export (invalid-index {index (Index Any)})
  (exception.report
   ["Index" (|> index !raw-index %.nat)]))

(exception: #export (invalid-constant {index (Index Any)}
                                      {tag Name})
  (exception.report
   ["Index" (|> index !raw-index %.nat)]
   ["Expected tag" (%.name tag)]))

(template: (!fetch <tag> <index>)
  (with-expansions [<failure> (as-is [[next pool] (exception.throw ..invalid-index [<index>])])]
    (function (_ [next pool])
      (loop [idx 0]
        (case (row.nth idx pool)
          (#.Some [index entry])
          (let [index' (!raw-index index)
                <index>' (!raw-index <index>)]
            (cond (n/< index' <index>')
                  (recur (inc idx))

                  (n/= index' <index>')
                  (case entry
                    (<tag> value)
                    [[next pool] (#try.Success value)]

                    _
                    [[next pool] (exception.throw ..invalid-constant [<index> (name-of <tag>)])])

                  ## (n/> index' <index>')
                  <failure>))

          #.None
          <failure>))
      )))

(exception: #export (cannot-find {tag Name} {value Text})
  (exception.report
   ["Expected tag" (%.name tag)]
   ["Value" value]))

(template: (!find <tag> <=> <%> <expected>)
  (function (_ [next pool])
    (with-expansions [<try-again> (as-is (recur (.inc idx)))]
      (loop [idx 0]
        (case (row.nth idx pool)
          (#.Some [index entry])
          (case entry
            (<tag> actual)
            (if (:: <=> = actual <expected>)
              [[next pool]
               (#try.Success index)]
              <try-again>)
            
            _
            <try-again>)
          
          #.None
          [[next pool]
           (exception.throw ..cannot-find [(name-of <tag>) (<%> <expected>)])])))))

(type: (Adder of)
  (-> of (State Pool (Index of))))

(type: (Fetcher of)
  (-> (Index of) (State Pool (Try of))))

(type: (Finder of)
  (-> of (State Pool (Try (Index of)))))

(template [<name> <type> <tag> <equivalence> <format>]
  [(def: #export (<name> value)
     (Adder <type>)
     (!add <tag> <equivalence> value))

   (`` (def: #export ((~~ (template.identifier ["fetch-" <name>])) index)
         (Fetcher <type>)
         (!fetch <tag> index)))

   (`` (def: #export ((~~ (template.identifier ["find-" <name>])) reference)
         (Finder <type>)
         (!find <tag> <equivalence> <format> reference)))]

  [integer Integer #//.Integer (//.value-equivalence //unsigned.equivalence) (|>> //.value //unsigned.nat %.nat)]
  [float Float #//.Float (//.value-equivalence //.float-equivalence) (|>> //.value host.float-to-double %.frac)]
  [long Long #//.Long (//.value-equivalence int.equivalence) (|>> //.value %.int)]
  [double Double #//.Double (//.value-equivalence frac.equivalence) (|>> //.value %.frac)]
  [utf8 UTF8 #//.UTF8 text.equivalence %.text]
  )

(def: #export (string value)
  (-> Text (State Pool (Index String)))
  (do state.monad
    [@value (utf8 value)
     #let [value (//.string @value)]]
    (!add #//.String (//.value-equivalence //index.equivalence) value)))

(def: #export (class name)
  (-> Internal (State Pool (Index Class)))
  (do state.monad
    [@name (utf8 (//name.read name))
     #let [value (//.class @name)]]
    (!add #//.Class //.class-equivalence value)))

(def: #export (descriptor value)
  (All [kind]
    (-> (Descriptor kind)
        (State Pool (Index (Descriptor kind)))))
  (let [value (//descriptor.descriptor value)]
    (!add #//.UTF8 text.equivalence value)))

(type: #export (Member of)
  {#name UTF8
   #descriptor (Descriptor of)})

(def: #export (name-and-type [name descriptor])
  (All [of]
    (-> (Member of) (State Pool (Index (Name-And-Type of)))))
  (do state.monad
    [@name (utf8 name)
     @descriptor (..descriptor descriptor)]
    (!add #//.Name-And-Type //.name-and-type-equivalence
          {#//.name @name
           #//.descriptor @descriptor})))

(template [<name> <tag> <of>]
  [(def: #export (<name> class member)
     (-> External (Member <of>) (State Pool (Index (Reference <of>))))
     (do state.monad
       [@class (..class (//name.internal class))
        @name-and-type (name-and-type member)]
       (!add <tag> //.reference-equivalence
             {#//.class @class
              #//.name-and-type @name-and-type})))]

  [field #//.Field Field]
  [method #//.Method Method]
  [interface-method #//.Interface-Method Method]
  )

(def: #export writer
  (Writer Pool)
  (function (_ [next pool])
    (row@fold (function (_ [_index post] pre)
                (specification@compose pre (//.writer post)))
              (binaryF.bits/16 (!raw-index next))
              pool)))

(def: #export empty
  Pool
  [(|> 1 //unsigned.u2 //index.index)
   row.empty])