blob: 149a893bb82a12e9fbe6a1cce39b91ec6f5c3a27 (
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
|
(.module:
[lux #*
[abstract
["." equivalence (#+ Equivalence)]
[monad (#+ do)]]
[control
["." state (#+ State)]
["." exception (#+ exception:)]
["<>" parser ("#@." functor)
["<2>" binary (#+ Parser)]]]
[data
["." error (#+ Error)]
["." text ("#;." equivalence)
["%" format]]
[format
[".F" binary (#+ Writer)]]
[collection
["." list ("#;." fold)]
["." row (#+ Row)]]]
[type
abstract]]
["." // (#+ UTF8 Class Constant) ("#;." class-equivalence)
[//
[encoding
["." unsigned]]
["." index (#+ Index)]
["." descriptor (#+ 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 (#error.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
(#error.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 (Error of))))
(type: (Finder of)
(-> of (State Pool (Error (Index of)))))
(def: #export (utf8 value)
(Adder UTF8)
(!add #//.UTF8 text;= value))
(def: #export (fetch-utf8 index)
(Fetcher UTF8)
(!fetch #//.UTF8 index))
(def: #export (find-utf8 reference)
(Finder UTF8)
(!find #//.UTF8 text;= %.text reference))
(def: (class' value)
(Adder Class)
(!add #//.Class //;= value))
(def: #export (class name)
(-> UTF8 (State Pool (Index Class)))
(do state.monad
[@name (utf8 name)]
(class' (//.class @name))))
(def: #export (descriptor value)
(All [kind]
(-> (Descriptor kind)
(State Pool (Index (Descriptor kind)))))
(let [value (descriptor.descriptor value)]
(!add #//.UTF8 text;= value)))
(def: #export parser
(Parser Pool)
(<2>.row/16' ..offset //.parser))
(def: #export writer
(Writer Pool)
(binaryF.row/16' ..offset //.writer))
(def: #export empty
Pool
row.empty)
|