interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / net / tests / parse.om

283 lines · 6071 bytes Raw Blame History
1note seed/net/tests/parse.om -- memory-mode parser driver for the corpus run by check.py.
2note Compose: { printf 'om2\n'; cat seed/net/http.om seed/net/tests/parse.om; } | omc
3note stdin: records `C <mode> <len>` LF, then len message bytes, then LF. mode q = request,
4note s = response, h = response to a HEAD request. Caps used here: head 2048 bytes, 16 header
5note fields, body 4096 bytes, chunk framing allowance 1024 bytes (README "Checks").
6note stdout, one line per record, idx counting from 0:
7note idx ok status version keep bodymode fields headhash bodylen bodyhash
8note idx err CODE (CODE = minus the result, e.g. 400, 431, 499)
9note idx eof (a request stream that ended before any byte)
10note headhash = FNV-1a 64 over [method SP target LF for requests] then, per field,
11note lowercase(name) ':' value LF; bodyhash = FNV-1a 64 of the decoded body. Both 16 hex digits.
12note Exit 0 after the last record; 2..8 = malformed corpus or I/O failure.
13fn fnvb h ulong b uchar to ulong
14ret mul xor h ulong b 1099511628211
15end
16fn fnvs h ulong a view uchar * to ulong
17x = h
18for i ulong len a
19x = fnvb x get a i
20end
21ret x
22end
23fn fnvl h ulong a view uchar * to ulong
24x = h
25for i ulong len a
26x = fnvb x hlow get a i
27end
28ret x
29end
30fn hdhash cs edit ulong 320 io edit uchar 65536 to ulong
31h = ulong 14695981039346656037
32if eq get cs HSERVER 1
33h = fnvs h view part io get cs HMOFF get cs HMLEN
34h = fnvb h 32
35h = fnvs h view part io get cs HTOFF get cs HTLEN
36h = fnvb h 10
37end
38for i ulong get cs HHN
39h = fnvl h view part io hhnoff cs i hhnlen cs i
40h = fnvb h 58
41h = fnvs h view part io hhvoff cs i hhvlen cs i
42h = fnvb h 10
43end
44ret h
45end
46note the output line is built in aux[65536, 65792)
47fn obch aux edit uchar 65792 p ulong c uchar to ulong
48put aux p c
49ret add p 1
50end
51fn obdec aux edit uchar 65792 p ulong v ulong to ulong
52d = new uchar 20 0
53i = ulong 20
54x = v
55for it ulong 20
56i = sub i 1
57put d i uchar add rem x 10 48
58x = div x 10
59if eq x 0
60break
61end
62end
63q = p
64for k ulong sub 20 i
65put aux q get d add i k
66q = add q 1
67end
68ret q
69end
70fn obhex aux edit uchar 65792 p ulong v ulong to ulong
71for k ulong 16
72put aux add p k hhexd and shr v mul sub 15 k 4 15
73end
74ret add p 16
75end
76fn wrall aux edit uchar 65792 s ulong e ulong to long
77p = s
78for it ulong 4096
79if ge p e
80ret 0
81end
82w = write 1 aux p sub e p
83if le w 0
84ret 7
85end
86p = add p ulong w
87end
88ret 7
89end
90fn prerr aux edit uchar 65792 idx ulong r long to long
91p = obdec aux 65536 idx
92p = obch aux p 32
93if eq r 0
94p = obch aux p 101
95p = obch aux p 111
96p = obch aux p 102
97else
98p = obch aux p 101
99p = obch aux p 114
100p = obch aux p 114
101p = obch aux p 32
102p = obdec aux p ulong neg r
103end
104p = obch aux p 10
105ret wrall aux 65536 p
106end
107fn prok cs edit ulong 320 io edit uchar 65536 aux edit uchar 65792 idx ulong bl ulong to long
108hh = hdhash cs io
109bh = fnvs 14695981039346656037 view part aux 0 bl
110p = obdec aux 65536 idx
111p = obch aux p 32
112p = obch aux p 111
113p = obch aux p 107
114p = obch aux p 32
115p = obdec aux p get cs HSTAT
116p = obch aux p 32
117p = obdec aux p get cs HVER
118p = obch aux p 32
119p = obdec aux p get cs HKEEP
120p = obch aux p 32
121p = obdec aux p get cs HBMODE
122p = obch aux p 32
123p = obdec aux p get cs HHN
124p = obch aux p 32
125p = obhex aux p hh
126p = obch aux p 32
127p = obdec aux p bl
128p = obch aux p 32
129p = obhex aux p bh
130p = obch aux p 10
131ret wrall aux 65536 p
132end
133fn docase cs edit ulong 320 io edit uchar 65536 aux edit uchar 65792 md uchar idx ulong to long
134put cs HHEADMX 2048
135put cs HHMAX 16
136put cs HBCAP 4096
137put cs HFRMAX 1024
138r = long 0
139if eq md 113
140r = hreqrd cs io
141else
142if eq md 104
143put cs HISHEAD 1
144end
145r = hrsprd cs io
146end
147if le r 0
148ret prerr aux idx r
149end
150b = hbodyall cs io part aux 0 65536
151if lt b 0
152ret prerr aux idx b
153end
154ret prok cs io aux idx ulong b
155end
156note hdrq: index after the LF of the record header at p, 0 when malformed
157fn hdrq inb view uchar 1048576 p ulong n ulong to ulong
158if gt add p 6 n
159ret 0
160end
161if or ne get inb p 67 ne get inb add p 1 32
162ret 0
163end
164if ne get inb add p 3 32
165ret 0
166end
167q = add p 4
168for k ulong 30
169if ge q n
170ret 0
171end
172c = get inb q
173if eq c 10
174break
175end
176if or lt c 48 gt c 57
177ret 0
178end
179q = add q 1
180end
181if ge q n
182ret 0
183end
184ret add q 1
185end
186fn hdrlen inb view uchar 1048576 p ulong q ulong to ulong
187v = ulong 0
188k = add p 4
189while lt k sub q 1 bound 30
190v = add mul v 10 ulong sub get inb k 48
191k = add k 1
192end
193ret v
194end
195fn rolei md uchar to ulong
196if eq md 113
197ret 1
198end
199ret 0
200end
201note onerec: the record at sb[0] (input end sb[1]); the index after it, or 0 on a failure
202fn onerec inb view uchar 1048576 sb edit ulong 4 io edit uchar 65536 cs edit ulong 320 aux edit uchar 65792 idx ulong to ulong
203p = get sb 0
204n = get sb 1
205q = hdrq inb p n
206if eq q 0
207ret 0
208end
209ln = hdrlen inb p q
210if gt add q add ln 1 n
211ret 0
212end
213r = hinit cs io -1 rolei get inb add p 2
214if lt r 0
215ret 0
216end
217r = hmemld cs io view part inb q ln
218if lt r 0
219ret 0
220end
221r = docase cs io aux get inb add p 2 idx
222if ne r 0
223ret 0
224end
225ret add add q ln 1
226end
227note refill: keep inb[p, n) and read until the buffer is full or stdin ends.
228note sb: 0 = p, 1 = n, 2 = end of input seen
229fn refill inb edit uchar 1048576 sb edit ulong 4 to long
230p = get sb 0
231n = get sb 1
232for i ulong sub n p
233put inb i get inb add p i
234end
235n = sub n p
236put sb 0 0
237for it ulong 1048576
238if ge n len inb
239break
240end
241r = read 0 inb n sub len inb n
242if lt r 0
243ret r
244end
245if eq r 0
246put sb 2 1
247break
248end
249n = add n ulong r
250end
251put sb 1 n
252ret 0
253end
254note runall: records stream through a 1 MiB buffer that always holds at least 65536 + 32 bytes
255note ahead (or the rest of the input), more than one record (hmemld caps a message at 49152)
256fn runall inb edit uchar 1048576 sb edit ulong 4 io edit uchar 65536 cs edit ulong 320 aux edit uchar 65792 to int
257idx = ulong 0
258for it ulong 1000000
259if and eq get sb 2 0 lt sub get sb 1 get sb 0 65568
260if lt refill inb sb 0
261ret 2
262end
263end
264if ge get sb 0 get sb 1
265ret 0
266end
267np = onerec inb sb io cs aux idx
268if eq np 0
269ret 3
270end
271put sb 0 np
272idx = add idx 1
273end
274ret 8
275end
276fn main to int
277inb = new uchar 1048576 0
278io = new uchar 65536 0
279cs = new ulong 320 0
280aux = new uchar 65792 0
281sb = new ulong 4 0
282ret runall inb sb io cs aux
283end