interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / research / lib / jflat.om

606 lines · 9010 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 []; nonempty objects/arrays start with {/[ markers at their own paths. 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 JSON numbers: optional minus, integer without leading zero, fraction and exponent.
367fn jdig c uchar to bool
368ret and ge c 48 le c 57
369end
370fn jnumok s ulong e ulong to bool
371i = s
372if ge i e
373ret false
374end
375if eq get src i 45
376i = add i 1
377end
378if ge i e
379ret false
380end
381if eq get src i 48
382i = add i 1
383if lt i e
384if jdig get src i
385ret false
386end
387end
388else
389c = get src i
390if not and ge c 49 le c 57
391ret false
392end
393while lt i e bound 16777216
394if not jdig get src i
395break
396end
397i = add i 1
398end
399end
400if lt i e
401if eq get src i 46
402i = add i 1
403a = i
404while lt i e bound 16777216
405if not jdig get src i
406break
407end
408i = add i 1
409end
410if eq i a
411ret false
412end
413end
414end
415if lt i e
416c = get src i
417if or eq c 101 eq c 69
418i = add i 1
419if lt i e
420c = get src i
421if or eq c 43 eq c 45
422i = add i 1
423end
424end
425a = i
426while lt i e bound 16777216
427if not jdig get src i
428break
429end
430i = add i 1
431end
432if eq i a
433ret false
434end
435end
436end
437ret eq i e
438end
439note lit: a number, true, false or null, copied as written
440fn lit to void
441s = get st c_p
442p = s
443n = get st c_n
444while lt p n bound 16777216
445c = get src p
446if or isws c or eq c 44 or eq c 125 eq c 93
447break
448end
449p = add p 1
450end
451if eq p s
452bad
453end
454c0 = get src s
455if or eq c0 116 or eq c0 102 eq c0 110
456if not or same s p k_true or same s p k_false same s p k_null
457bad
458end
459else
460if not jnumok s p
461bad
462end
463end
464opath
465i = s
466while lt i p bound 16777216
467ob get src i
468i = add i 1
469end
470ob uchar 10
471put st c_p p
472end
473
474note empty: an empty object or array, written as its two brackets
475fn empty a uchar b uchar to void
476opath
477ob a
478ob b
479ob uchar 10
480end
481
482fn obj to void
483step
484skipws
485if eq peek 125
486step
487empty uchar 123 uchar 125
488ret
489end
490note Nonempty object marker preserves duplicate container occurrences for security consumers.
491opath
492ob uchar 123
493ob uchar 10
494base = get st c_pl
495while true bound 16777216
496skipws
497if ne peek 34
498bad
499end
500padd uchar 47
501str true
502skipws
503if ne peek 58
504bad
505end
506step
507val
508put st c_pl base
509skipws
510c = peek
511if eq c 125
512step
513ret
514end
515if ne c 44
516bad
517end
518step
519end
520end
521
522fn arr to void
523step
524skipws
525if eq peek 93
526step
527empty uchar 91 uchar 93
528ret
529end
530note Preserve nonempty array occurrences as well as their indexed children.
531opath
532ob uchar 91
533ob uchar 10
534base = get st c_pl
535i = ulong 0
536while true bound 16777216
537padd uchar 47
538pnum i
539val
540put st c_pl base
541skipws
542c = peek
543if eq c 93
544step
545ret
546end
547if ne c 44
548bad
549end
550step
551i = add i 1
552end
553end
554
555fn val to void
556skipws
557if ge get st c_p get st c_n
558bad
559end
560c = peek
561if eq c 123
562obj
563ret
564end
565if eq c 91
566arr
567ret
568end
569if eq c 34
570opath
571str false
572ob uchar 10
573ret
574end
575lit
576end
577
578fn main to int
579n = ulong 0
580while true bound 16777217
581if eq n 16777216
582ow2 m_big
583ret 2
584end
585k = read 0 src n sub 16777216 n
586if lt k 0
587ow2 m_big
588ret 2
589end
590if eq k 0
591break
592end
593n = add n ulong k
594end
595put st c_n n
596put st c_p 0
597put st c_pl 0
598put st c_ol 0
599val
600skipws
601if ne get st c_p n
602bad
603end
604flush
605ret 0
606end