aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/target/jvm/constant/pool.lux
blob: 7bdb7d977861ab1a6af39809cc4ffa418b4dc8de (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
(.module:
  [library
   [lux "*"
    ["." ffi]
    [abstract
     [equivalence {"+" [Equivalence]}]
     [monad {"+" [Monad do]}]]
    [control
     ["." state {"+" [+State]}]
     ["." try {"+" [Try]}]]
    [data
     ["." product]
     ["." text]
     ["." format "_"
      ["#" binary {"+" [Writer]} ("specification\." monoid)]]
     [collection
      ["." row {"+" [Row]} ("#\." mix)]]]
    [macro
     ["." template]]
    [math
     [number
      ["." i32]
      ["n" nat]
      ["." int]
      ["." frac]]]
    [type
     abstract]]]
  ["." // {"+" [UTF8 String Class Integer Float Long Double Constant Name_And_Type Reference]}
   [//
    [encoding
     ["#." name {"+" [Internal External]}]
     ["#." unsigned]]
    ["#." index {"+" [Index]}]
    [type
     [category {"+" [Value Method]}]
     ["#." descriptor {"+" [Descriptor]}]]]])

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

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

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

(def: .public monad
  (Monad Resource)
  (state.with try.monad))

(template: (!add <tag> <equivalence> <value>)
  [(function (_ [current pool])
     (let [<value>' <value>]
       (with_expansions [<try_again> (as_is (recur (.++ idx)))]
         (loop [idx 0]
           (case (row.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>')]
               (do {! try.monad}
                 [@new (//unsigned.u2 (//.size new))
                  next (: (Try Index)
                          (|> current
                              //index.value
                              (//unsigned.+/2 @new)
                              (\ ! each //index.index)))]
                 (in [[next
                       (row.suffix [current new] pool)]
                      current]))))))))])

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

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

(template [<name> <type> <tag> <equivalence>]
  [(def: .public (<name> value)
     (Adder <type>)
     (!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 ..monad
    [@value (utf8 value)
     .let [value (//.string @value)]]
    (!add #//.String (//.value_equivalence //index.equivalence) value)))

(def: .public (class name)
  (-> Internal (Resource (Index Class)))
  (do ..monad
    [@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)]
    (!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 ..monad
    [@name (utf8 name)
     @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 ..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 Value]
  [method #//.Method Method]
  [interface_method #//.Interface_Method Method]
  )

(def: .public writer
  (Writer Pool)
  (function (_ [next pool])
    (row\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)
   row.empty])