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