blob: dc500c885ba13a18a5a100bffdb2c9464913cd90 (
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
|
(.module:
[lux #*
[abstract
["." equivalence (#+ Equivalence)]
[monad (#+ do)]]
[control
["." state (#+ State)]
["." try (#+ Try)]
["." exception (#+ exception:)]
["<>" parser ("#@." functor)
["<2>" binary (#+ Parser)]]]
[data
[number
["." int]
["." frac]]
["." text
["%" format]]
[format
[".F" binary (#+ Writer)]]
[collection
["." list ("#;." fold)]
["." row (#+ Row)]]]
[type
abstract]
[macro
["." template]]]
["." // (#+ UTF8 Class Long Double Constant)
[//
[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 (#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: (class' value)
(Adder Class)
(!add #//.Class //.class-equivalence 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.equivalence 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)
|