interplane.Get Omega
Read in your language

omega-harness

Agent skills for operating, encoding and communicating in Omega (.om) and the AIEN repositories

Brought by drakestapleton

branch main Branches Tags History

omega-harness / tools / jflat.om

533 lines · 7940 bytes Raw Blame History
1om2
2note jflat: JSON on stdin, one line per value on stdout: PATH, a tab, VALUE, a newline.
3note PATH is "/" followed by the object keys and array indices from the root, joined by "/"
4note (`/errors/7/meaning`, `/words/fopen/types/0`); the root value alone has the path "/".
5note VALUE is a string with its JSON escapes decoded (\uXXXX and surrogate pairs become UTF-8),
6note then newline, tab and backslash written as \n, \t and \\ so that every value stays on one
7note line; numbers, true, false and null are copied as written; an empty object or array is
8note written as {} or []. In a key, "/" and "\" are written \/ and \\, and newline and tab \n and \t,
9note so a path splits only on its separators and never holds a tab. Lines come in document order, so the
10note output is a deterministic function of the input bytes. Nesting is limited by the 8 MiB stack: a
11note document nested deeper than it can hold stops with trap 6 (exit 106).
12note Exit 0 on success; 2 when the input is not one JSON value (stderr names the byte offset),
13note when it is 16777216 bytes or longer, or when a write fails; on an error, output not yet written
14note is dropped. The input refusal is the size of the largest global array at the pinned omc (omega-mv
15note has no run-time memory growth yet); `guides check` alarms when data/reference.tsv passes half of it.
16
17global src uchar 16777216
18global obf uchar 1048576
19global pth uchar 1048576
20global nb uchar 32
21global st ulong 8
22
23note st cells: input position, input length, path length, output length
24const c_p ulong 0
25const c_n ulong 1
26const c_pl ulong 2
27const c_ol ulong 3
28
29text m_big "jflat: input is 16777216 bytes or longer\n"
30text m_bad "jflat: malformed JSON at byte "
31text m_deep "jflat: a path is 1048576 bytes or longer\n"
32text m_tail ": expected one JSON value\n"
33text k_true "true"
34text k_false "false"
35text k_null "null"
36
37fn ow2 t view uchar * to void
38n = len t
39p = ulong 0
40while lt p n bound 1048576
41k = write 2 t p sub n p
42if le k 0
43exit 2
44end
45p = add p ulong k
46end
47end
48
49fn flush to void
50n = get st c_ol
51p = ulong 0
52while lt p n bound 1048576
53k = write 1 obf p sub n p
54if le k 0
55exit 2
56end
57p = add p ulong k
58end
59put st c_ol 0
60end
61
62fn ob c uchar to void
63n = get st c_ol
64if eq n 1048576
65flush
66n = ulong 0
67end
68put obf n c
69put st c_ol add n 1
70end
71
72note bad: report the position and stop with exit 2
73fn bad to void
74ow2 m_bad
75v = get st c_p
76i = ulong 32
77while true bound 21
78i = sub i 1
79put nb i uchar add rem v 10 48
80v = div v 10
81if eq v 0
82break
83end
84end
85p = i
86while lt p 32 bound 32
87k = write 2 nb p sub 32 p
88if le k 0
89exit 2
90end
91p = add p ulong k
92end
93ow2 m_tail
94exit 2
95end
96
97fn padd c uchar to void
98n = get st c_pl
99if eq n 1048576
100ow2 m_deep
101exit 2
102end
103put pth n c
104put st c_pl add n 1
105end
106
107note pnum: v in decimal onto the path
108fn pnum v ulong to void
109i = ulong 32
110n = v
111while true bound 21
112i = sub i 1
113put nb i uchar add rem n 10 48
114n = div n 10
115if eq n 0
116break
117end
118end
119while lt i 32 bound 32
120padd get nb i
121i = add i 1
122end
123end
124
125note opath: the current path and a tab
126fn opath to void
127n = get st c_pl
128if eq n 0
129ob uchar 47
130end
131i = ulong 0
132while lt i n bound 1048576
133ob get pth i
134i = add i 1
135end
136ob uchar 9
137end
138
139fn peek to uchar
140p = get st c_p
141if lt p get st c_n
142ret get src p
143end
144ret uchar 0
145end
146
147fn step to void
148put st c_p add get st c_p 1
149end
150
151fn isws c uchar to bool
152if or eq c 32 eq c 10
153ret true
154end
155ret or eq c 13 eq c 9
156end
157
158fn skipws to void
159p = get st c_p
160n = get st c_n
161while lt p n bound 16777216
162if not isws get src p
163break
164end
165p = add p 1
166end
167put st c_p p
168end
169
170note sput: one decoded byte of a string, into the path (key) or the output (value)
171fn sput c uchar k bool to void
172if k
173if or eq c 47 eq c 92
174padd uchar 92
175end
176if eq c 10
177padd uchar 92
178padd uchar 110
179ret
180end
181if eq c 9
182padd uchar 92
183padd uchar 116
184ret
185end
186padd c
187ret
188end
189if eq c 10
190ob uchar 92
191ob uchar 110
192ret
193end
194if eq c 9
195ob uchar 92
196ob uchar 116
197ret
198end
199if eq c 92
200ob uchar 92
201end
202ob c
203end
204
205note hexv: the value of one hex digit, or 16 when c is not one
206fn hexv c uchar to ulong
207if and ge c 48 le c 57
208ret ulong sub c 48
209end
210if and ge c 97 le c 102
211ret ulong sub c 87
212end
213if and ge c 65 le c 70
214ret ulong sub c 55
215end
216ret ulong 16
217end
218
219note hex4: the 4 hex digits at the position; the position moves past them
220fn hex4 to ulong
221p = get st c_p
222if gt add p 4 get st c_n
223bad
224end
225v = ulong 0
226for i ulong 4
227d = hexv get src add p i
228if eq d 16
229bad
230end
231v = add mul v 16 d
232end
233put st c_p add p 4
234ret v
235end
236
237note utf8: code point v as UTF-8
238fn utf8 v ulong k bool to void
239if lt v 128
240sput uchar v k
241ret
242end
243if lt v 2048
244sput uchar or 192 shr v 6 k
245sput uchar or 128 and v 63 k
246ret
247end
248if lt v 65536
249sput uchar or 224 shr v 12 k
250sput uchar or 128 and shr v 6 63 k
251sput uchar or 128 and v 63 k
252ret
253end
254sput uchar or 240 shr v 18 k
255sput uchar or 128 and shr v 12 63 k
256sput uchar or 128 and shr v 6 63 k
257sput uchar or 128 and v 63 k
258end
259
260note uesc: after `\u`; a high surrogate must be followed by `\u` and a low one
261fn uesc k bool to void
262v = hex4
263if and ge v 56320 le v 57343
264bad
265end
266if and ge v 55296 le v 56319
267if ne peek 92
268bad
269end
270step
271if ne peek 117
272bad
273end
274step
275w = hex4
276if or lt w 56320 gt w 57343
277bad
278end
279v = add 65536 add shl sub v 55296 10 sub w 56320
280end
281utf8 v k
282end
283
284note esc: after a backslash
285fn esc k bool to void
286e = peek
287step
288if or eq e 34 or eq e 92 eq e 47
289sput e k
290ret
291end
292if eq e 98
293sput uchar 8 k
294ret
295end
296if eq e 102
297sput uchar 12 k
298ret
299end
300if eq e 110
301sput uchar 10 k
302ret
303end
304if eq e 114
305sput uchar 13 k
306ret
307end
308if eq e 116
309sput uchar 9 k
310ret
311end
312if eq e 117
313uesc k
314ret
315end
316put st c_p sub get st c_p 1
317bad
318end
319
320note str: the string at the position (its opening quote), decoded into the path or the output
321fn str k bool to void
322step
323while true bound 16777216
324if ge get st c_p get st c_n
325bad
326end
327c = peek
328step
329if eq c 34
330ret
331end
332if lt c 32
333put st c_p sub get st c_p 1
334bad
335end
336if eq c 92
337esc k
338else
339sput c k
340end
341end
342end
343
344fn same s ulong e ulong t view uchar * to bool
345if ne sub e s len t
346ret false
347end
348for i ulong len t bound 8
349if ne get src add s i get t i
350ret false
351end
352end
353ret true
354end
355
356fn isnum c uchar to bool
357if and ge c 48 le c 57
358ret true
359end
360if or eq c 45 eq c 43
361ret true
362end
363ret or eq c 46 or eq c 101 eq c 69
364end
365
366note lit: a number, true, false or null, copied as written
367fn lit to void
368s = get st c_p
369p = s
370n = get st c_n
371while lt p n bound 16777216
372c = get src p
373if or isws c or eq c 44 or eq c 125 eq c 93
374break
375end
376p = add p 1
377end
378if eq p s
379bad
380end
381c0 = get src s
382if or eq c0 116 or eq c0 102 eq c0 110
383if not or same s p k_true or same s p k_false same s p k_null
384bad
385end
386else
387if not or eq c0 45 and ge c0 48 le c0 57
388bad
389end
390i = s
391while lt i p bound 16777216
392if not isnum get src i
393put st c_p i
394bad
395end
396i = add i 1
397end
398end
399opath
400i = s
401while lt i p bound 16777216
402ob get src i
403i = add i 1
404end
405ob uchar 10
406put st c_p p
407end
408
409note empty: an empty object or array, written as its two brackets
410fn empty a uchar b uchar to void
411opath
412ob a
413ob b
414ob uchar 10
415end
416
417fn obj to void
418step
419skipws
420if eq peek 125
421step
422empty uchar 123 uchar 125
423ret
424end
425base = get st c_pl
426while true bound 16777216
427skipws
428if ne peek 34
429bad
430end
431padd uchar 47
432str true
433skipws
434if ne peek 58
435bad
436end
437step
438val
439put st c_pl base
440skipws
441c = peek
442if eq c 125
443step
444ret
445end
446if ne c 44
447bad
448end
449step
450end
451end
452
453fn arr to void
454step
455skipws
456if eq peek 93
457step
458empty uchar 91 uchar 93
459ret
460end
461base = get st c_pl
462i = ulong 0
463while true bound 16777216
464padd uchar 47
465pnum i
466val
467put st c_pl base
468skipws
469c = peek
470if eq c 93
471step
472ret
473end
474if ne c 44
475bad
476end
477step
478i = add i 1
479end
480end
481
482fn val to void
483skipws
484if ge get st c_p get st c_n
485bad
486end
487c = peek
488if eq c 123
489obj
490ret
491end
492if eq c 91
493arr
494ret
495end
496if eq c 34
497opath
498str false
499ob uchar 10
500ret
501end
502lit
503end
504
505fn main to int
506n = ulong 0
507while true bound 16777217
508if eq n 16777216
509ow2 m_big
510ret 2
511end
512k = read 0 src n sub 16777216 n
513if lt k 0
514ow2 m_big
515ret 2
516end
517if eq k 0
518break
519end
520n = add n ulong k
521end
522put st c_n n
523put st c_p 0
524put st c_pl 0
525put st c_ol 0
526val
527skipws
528if ne get st c_p n
529bad
530end
531flush
532ret 0
533end