blob: 2e4ff99377d46cffcefdaed12ee12018a31b1f8e (
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
|
(.module:
[lux #*
["." host]
[abstract
["." equivalence (#+ Equivalence)]
[monad (#+ Monad do)]]
[control
["." state (#+ State')]
["." try (#+ Try)]]
[data
[number
["." i32]
["n" nat]
["." int]
["." frac]]
["." text]
["." format #_
["#" 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)]
[type
[category (#+ Value Method)]
["#." descriptor (#+ Descriptor)]]]])
(type: #export Pool [Index (Row [Index Constant])])
(def: #export equivalence
(Equivalence Pool)
(equivalence.product //index.equivalence
(row.equivalence (equivalence.product //index.equivalence
//.equivalence))))
(type: #export (Resource a)
(State' Try Pool a))
(def: #export 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 (.inc idx)))]
(loop [idx 0]
(case (row.nth 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)
(:: @ map //index.index)))]
(wrap [[next
(row.add [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: #export (<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: #export (string value)
(-> Text (Resource (Index String)))
(do ..monad
[@value (utf8 value)
#let [value (//.string @value)]]
(!add #//.String (//.value-equivalence //index.equivalence) value)))
(def: #export (class name)
(-> Internal (Resource (Index Class)))
(do ..monad
[@name (utf8 (//name.read name))
#let [value (//.class @name)]]
(!add #//.Class //.class-equivalence value)))
(def: #export (descriptor value)
(All [kind]
(-> (Descriptor kind)
(Resource (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) (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: #export (<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: #export writer
(Writer Pool)
(function (_ [next pool])
(row@fold (function (_ [_index post] pre)
(specification@compose pre (//.writer post)))
(format.bits/16 (!index next))
pool)))
(def: #export empty
Pool
[(|> 1 //unsigned.u2 try.assume //index.index)
row.empty])
|