aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/target/jvm/constant/pool.lux
blob: 773607858aabd88cc90317df16bc5599a8a23d87 (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
(.module:
  [lux #*
   [abstract
    ["." equivalence (#+ Equivalence)]
    [monad (#+ do)]]
   [control
    ["." state (#+ State)]
    ["." try (#+ Try)]
    ["." exception (#+ exception:)]]
   [data
    [number
     ["." int]
     ["." frac]]
    ["." text
     ["%" format (#+ format)]]
    [format
     [".F" binary (#+ Writer)]]
    [collection
     ["." list ("#;." fold)]
     ["." row (#+ Row)]]]
   [type
    abstract]
   [macro
    ["." template]]]
  ["." // (#+ UTF8 String Class Long Double Constant Name-And-Type Reference)
   [//
    [encoding
     ["#." name (#+ Internal External)]
     ["#." unsigned]]
    ["#." index (#+ Index)]
    ["#." descriptor (#+ Field Method Descriptor)]]])

(def: offset 1)

(type: #export Pool (Row Constant))

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

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

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

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

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

(template: (!fetch <tag> <index>)
  (function (_ pool)
    (case (row.nth (|> <index> !raw-index (n/- offset))
                   pool)
      (#.Some entry)
      (case entry
        (<tag> value)
        [pool (#try.Success value)]

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

      #.None
      [pool (exception.throw ..invalid-index [<index> (row.size pool)])])))

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

(template: (!find <tag> <=> <%> <expected>)
  (function (_ pool)
    (with-expansions [<index> (as-is (//index.index (//unsigned.u2 (n/+ offset idx))))
                      <try-again> (as-is (recur (.inc idx)))]
      (loop [idx 0]
        (case (row.nth idx pool)
          (#.Some entry)
          (case entry
            (<tag> actual)
            (if (:: <=> = actual <expected>)
              [pool
               (#try.Success <index>)]
              <try-again>)
            
            _
            <try-again>)
          
          #.None
          [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)))]

  [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)
  (binaryF.row/16' ..offset //.writer))

(def: #export empty
  Pool
  row.empty)