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 / protocol / omnet / om / cbor / test / driver.om

215 lines · 4413 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note driver.om: program-mode test driver for om/cbor/cbor.om. Concatenated after
3note om/blake2s/blake2s.om and om/cbor/cbor.om.
4note
5note driver reads one case per line from stdin: the input bytes as hex (either case, even digit
6note count, at most 65536 bytes) or "-" for the empty input. For each case it writes one line:
7note e<code> cbrdec rejected the input with that code
8note ok NTOK DUP UTF8 K1 CANON HASH cbrdec accepted it:
9note NTOK token count (cbrcount)
10note DUP 1 when the root is a map with duplicate keys (cbrdupk), else 0
11note UTF8 1 when every text string is valid UTF-8 (cbrutf8), else 0
12note K1 major:argument of the value at unsigned key 1 of a root map (cbrmfind), or -
13note CANON cbrenc output as lowercase hex, or - when empty (never: an item is >= 1 byte)
14note HASH BLAKE2s-256 of CANON (b2shash), lowercase hex
15note Exit 0 at end of input, 2 on a malformed line.
16
17global ib uchar 65536
18global ist ulong 2
19
20note next input byte, -1 at end of input
21fn getc to int
22if eq get ist 0 get ist 1
23r = read 0 ib 0 65536
24if le r 0
25ret -1
26end
27put ist 0 0
28put ist 1 ulong r
29end
30p = get ist 0
31put ist 0 add p 1
32ret int get ib p
33end
34
35fn hexv c int to int
36if and ge c 48 le c 57
37ret sub c 48
38end
39if and ge c 97 le c 102
40ret sub c 87
41end
42if and ge c 65 le c 70
43ret sub c 55
44end
45ret -1
46end
47
48fn hexd v ulong to uchar
49if lt v 10
50ret uchar add v 48
51end
52ret uchar add v 87
53end
54
55note one line of hex into a: the byte count; -2 at end of input before any byte; -1 when malformed
56fn rdline a edit uchar 65536 to long
57c = getc
58if eq c -1
59ret -2
60end
61if eq c 45
62if eq getc 10
63ret 0
64end
65ret -1
66end
67t = ulong 0
68while ne c 10 bound 999999
69if ge t 65536
70ret -1
71end
72hi = hexv c
73lo = hexv getc
74if or lt hi 0 lt lo 0
75ret -1
76end
77put a t uchar add mul hi 16 lo
78t = add t 1
79c = getc
80end
81ret long t
82end
83
84note write a[0..n) as hex into h at offset at; returns the new offset
85fn hexout h edit uchar 131200 at ulong a view uchar 65536 n ulong to ulong
86for i ulong n
87v = ulong get a i
88put h add at mul i 2 hexd shr v 4
89put h add at add mul i 2 1 hexd and v 15
90end
91ret add at mul n 2
92end
93
94fn hex32 h edit uchar 131200 at ulong a view uchar 32 to ulong
95for i ulong 32
96v = ulong get a i
97put h add at mul i 2 hexd shr v 4
98put h add at add mul i 2 1 hexd and v 15
99end
100ret add at 64
101end
102
103note decimal v into h at offset at; returns the new offset
104fn decout h edit uchar 131200 at ulong v ulong to ulong
105g = new uchar 24 0
106n = ulong 0
107x = v
108while true bound 24
109put g n uchar add rem x 10 48
110n = add n 1
111x = div x 10
112if eq x 0
113break
114end
115end
116for i ulong n
117put h add at i get g sub sub n i 1
118end
119ret add at n
120end
121
122fn putw h edit uchar 131200 at ulong c ulong to ulong
123put h at uchar c
124ret add at 1
125end
126
127note 1 when every text token of the table is valid UTF-8
128fn allutf8 d view uchar 65536 t view ulong 131074 to ulong
129for i ulong cbrcount t
130if eq cbrmaj t i 3
131if eq cbrutf8 d cbrpoff t i cbrval t i 0
132ret 0
133end
134end
135end
136ret 1
137end
138
139fn rootdup d view uchar 65536 t view ulong 131074 to ulong
140if eq cbrmaj t 0 5
141ret cbrdupk d t 0
142end
143ret 0
144end
145
146note K1 field: major:argument of the value at key 1 of a root map, or -
147fn k1out h edit uchar 131200 at ulong t view ulong 131074 to ulong
148if ne cbrmaj t 0 5
149ret putw h at 45
150end
151v = cbrmfind t 0 1
152if eq v 0
153ret putw h at 45
154end
155q = decout h at cbrmaj t v
156q = putw h q 58
157ret decout h q cbrval t v
158end
159
160note the ok line after a successful decode of d into t
161fn okline h edit uchar 131200 d view uchar 65536 t view ulong 131074 o edit uchar 65536 to ulong
162md = new uchar 32 0
163k = new uchar 32 0
164n = cbrenc d t o
165q = putw h 0 111
166q = putw h q 107
167q = putw h q 32
168q = decout h q cbrcount t
169q = putw h q 32
170q = decout h q rootdup d t
171q = putw h q 32
172q = decout h q allutf8 d t
173q = putw h q 32
174q = k1out h q t
175q = putw h q 32
176q = hexout h q o n
177q = putw h q 32
178b2shash o n k 0 32 md
179q = hex32 h q md
180ret putw h q 10
181end
182
183fn cases to int
184d = new uchar 65536 0
185t = new ulong 131074 0
186o = new uchar 65536 0
187h = new uchar 131200 0
188while true bound 999999
189n = rdline d
190if eq n -2
191ret 0
192end
193if lt n 0
194ret 2
195end
196e = cbrdec d ulong n t
197q = ulong 0
198if eq e 0
199q = okline h d t o
200else
201q = putw h 0 101
202q = decout h q e
203q = putw h q 10
204end
205w = write 1 h 0 q
206if ne w long q
207trap 1
208end
209end
210ret 2
211end
212
213fn main to int
214ret cases
215end