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 / text / tests / txdrv.om

343 lines · 4988 bytes Raw Blame History
1note txdrv: test driver for seed/text/text.om. Composed as: om2, text.om, this file (gate.sh does it).
2note stdin: records, each an op byte and its fields; a byte string is a u32 little-endian length and the
3note bytes, a number a u64 little-endian. One output line per record (see gen.om for the layout).
4note All of stdin is read into one 3 MiB local array, which every case slices with part or (o, n).
5global obuf uchar 1048576
6global oc ulong 1
7global gp ulong 1
8
9fn oflush to void
10off = ulong 0
11n = get oc 0
12while lt off n bound 1048576
13w = write 1 obuf off sub n off
14if le w 0
15exit int 90
16end
17off = add off ulong w
18end
19put oc 0 0
20end
21
22fn oputb b uchar to void
23m = get oc 0
24put obuf m b
25put oc 0 add m 1
26end
27
28fn oputn v ulong to void
29d = new uchar 20 0
30i = ulong 0
31x = v
32put d 0 uchar add rem x 10 48
33x = div x 10
34i = add i 1
35while gt x 0 bound 20
36put d i uchar add rem x 10 48
37x = div x 10
38i = add i 1
39end
40while gt i 0 bound 20
41i = sub i 1
42oputb get d i
43end
44end
45
46fn oputl v long to void
47if lt v 0
48oputb 45
49oputn add ulong neg add v 1 1
50ret
51end
52oputn ulong v
53end
54
55fn oend to void
56oputb 10
57if gt get oc 0 1040000
58oflush
59end
60end
61
62fn rd8 b view uchar * to ulong
63p = get gp 0
64put gp 0 add p 1
65ret ulong get b p
66end
67
68fn rdn b view uchar * k ulong to ulong
69p = get gp 0
70x = ulong 0
71for i ulong k
72x = or x shl ulong get b add p i mul i 8
73end
74put gp 0 add p k
75ret x
76end
77
78note rds: a byte string; leaves its offset in gp and returns its length after skipping it.
79fn rds b view uchar * to ulong
80n = rdn b 4
81p = get gp 0
82put gp 0 add p n
83ret n
84end
85
86note copyto: b[p .. p+n) into a (the second string of a case: one array may not be passed twice in a call).
87fn copyto b view uchar * p ulong n ulong a edit uchar * to void
88for i ulong n
89put a i get b add p i
90end
91end
92
93fn cmpcase b view uchar * to void
94sb = new uchar 262144 0
95na = rds b
96pa = sub get gp 0 na
97nb = rds b
98pb = sub get gp 0 nb
99copyto b pb nb sb
100oputb 69
101oputb 32
102e = tx_eq part b pa na part sb 0 nb
103if e
104oputb 49
105else
106oputb 48
107end
108oputb 32
109c = tx_cmp part b pa na part sb 0 nb
110oputl long c
111oend
112end
113
114fn findcase b view uchar * to void
115sb = new uchar 262144 0
116nh = rds b
117ph = sub get gp 0 nh
118nn = rds b
119pn = sub get gp 0 nn
120from = rdn b 8
121copyto b pn nn sb
122r = tx_find part b ph nh part sb 0 nn from
123oputb 70
124oputb 32
125if eq r txnone
126oputb 110
127else
128oputn r
129end
130oend
131end
132
133fn splitcase b view uchar * to void
134ns = rds b
135ps = sub get gp 0 ns
136sep = uchar rd8 b
137cnt = ulong 0
138p = ulong 0
139lens = new ulong 4096 0
140while true bound 4096
141e = tx_field part b ps ns sep p
142put lens cnt sub e p
143cnt = add cnt 1
144if eq e ns
145break
146end
147p = add e 1
148end
149oputb 83
150oputb 32
151oputn cnt
152for i ulong cnt
153oputb 32
154oputn get lens i
155end
156oend
157end
158
159fn parsecase b view uchar * op ulong to void
160n = rds b
161o = sub get gp 0 n
162v = new ulong 1 0
163s = new long 1 0
164r = long 0
165if eq op 68
166r = tx_pdec b o n v
167end
168if eq op 72
169r = tx_phex b o n v
170end
171if eq op 73
172r = tx_pdeci b o n s
173end
174oputb uchar op
175if lt r 0
176oputt3 101 114 114
177oputn ulong neg r
178oend
179ret
180end
181oputb 32
182oputb 111
183oputb 107
184oputb 32
185if eq op 73
186oputl get s 0
187else
188oputn get v 0
189end
190oend
191end
192
193note oputt3: a space, three bytes and a space.
194fn oputt3 a uchar b uchar c uchar to void
195oputb 32
196oputb a
197oputb b
198oputb c
199oputb 32
200end
201
202fn fmtcase b view uchar * op ulong to void
203v = rdn b 8
204w = rdn b 8
205cap = rdn b 8
206fb = new uchar 64 0
207o = sub 64 cap
208r = long 0
209if eq op 102
210r = tx_fdec v fb o
211end
212if eq op 103
213r = tx_fdeci bitcast long v fb o
214end
215if eq op 120
216r = tx_fhex v w fb o
217end
218oputb uchar op
219oputb 32
220if lt r 0
221oputb 69
222oend
223ret
224end
225for i ulong sub ulong r o
226oputb get fb add o i
227end
228oend
229end
230
231fn u8case b view uchar * to void
232n = rds b
233o = sub get gp 0 n
234r = tx_u8ok b o n
235oputb 85
236oputb 32
237oputn sub r o
238oend
239end
240
241note dec: "P cp len" or "P n"; enc: "Q" and the bytes in hex, "Q E" (no room) or "Q R" (range).
242fn deccase b view uchar * to void
243n = rds b
244o = sub get gp 0 n
245r = tx_u8dec b o n
246oputb 80
247oputb 32
248if eq r txnone
249oputb 110
250oend
251ret
252end
253oputn shr r 3
254oputb 32
255oputn and r 7
256oend
257end
258
259fn enccase b view uchar * to void
260cp = rdn b 8
261cap = rdn b 8
262fb = new uchar 8 0
263o = sub 8 cap
264r = tx_u8enc cp fb o
265oputb 81
266oputb 32
267if eq r txespace
268oputb 69
269oend
270ret
271end
272if eq r txerange
273oputb 82
274oend
275ret
276end
277for i ulong sub ulong r o
278v = get fb add o i
279oputb uchar hexc shr v 4
280oputb uchar hexc and v 15
281end
282oend
283end
284
285fn hexc d uchar to uchar
286if lt d 10
287ret add d 48
288end
289ret add d 87
290end
291
292fn main to int
293inb = new uchar 3145728 0
294off = ulong 0
295k = long 1
296while and gt k 0 lt off len inb bound 999999
297k = read 0 inb off sub len inb off
298if gt k 0
299off = add off ulong k
300end
301end
302if eq off len inb
303ret 91
304end
305while lt get gp 0 off bound 999999
306op = rd8 inb
307if eq op 69
308cmpcase inb
309else
310if eq op 70
311findcase inb
312else
313if eq op 83
314splitcase inb
315else
316if eq op 85
317u8case inb
318else
319if eq op 80
320deccase inb
321else
322if eq op 81
323enccase inb
324else
325if or eq op 68 or eq op 72 eq op 73
326parsecase inb op
327else
328if or eq op 102 or eq op 103 eq op 120
329fmtcase inb op
330else
331ret 92
332end
333end
334end
335end
336end
337end
338end
339end
340end
341oflush
342ret 0
343end