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
omega-mv / tools / json_canon.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | note |
| 4 | note tools/json_canon.om -- strict JSON check and Python-compatible canonical JSON. |
| 5 | note Pure seeded OM-2 replacement for tools/json_canon.c. |
| 6 | note |
| 7 | note json_canon --check exit 0 if stdin is one valid JSON document, 1 otherwise |
| 8 | note json_canon --check-lines print 1-based line numbers of invalid JSON lines |
| 9 | note json_canon compact sorted UTF-8 JSON (Python json.dumps) |
| 10 | note json_canon --sha256 SHA-256 hex of that compact form |
| 11 | note json_canon --pretty indent=2 sorted ASCII-escaped JSON with trailing newline |
| 12 | note json_canon --write-exclusive P write stdin to P (O_WRONLY|O_CREAT|O_EXCL, 0444, fsync) |
| 13 | om2 |
| 14 | |
| 15 | text k_sha_tbl "1116352408 1899447441 3049323471 3921009573 961987163 1508970993 2453635748 2870763221 3624381080 310598401 607225278 1426881987 1925078388 2162078206 2614888103 3248222580 3835390401 4022224774 264347078 604807628 770255983 1249150122 1555081692 1996064986 2554220882 2821834349 2952996808 3210313671 3336571891 3584528711 113926993 338241895 666307205 773529912 1294757372 1396182291 1695183700 1986661051 2177026350 2456956037 2730485921 2820302411 3259730800 3345764771 3516065817 3600352804 4094571909 275423344 430227734 506948616 659060556 883997877 958139571 1322822218 1537002063 1747873779 1955562222 2024104815 2227730452 2361852424 2428436474 2756734187 3204031479 3329325298 1779033703 3144134277 1013904242 2773480762 1359893119 2600822924 528734635 1541459225" |
| 16 | text t_hex "0123456789abcdef" |
| 17 | text t_usage "usage: json_canon [--check | --pretty | --sha256 | --write-exclusive PATH] < json\n" |
| 18 | text t_f_check "--check" |
| 19 | text t_f_lines "--check-lines" |
| 20 | text t_f_pretty "--pretty" |
| 21 | text t_f_sha "--sha256" |
| 22 | text t_f_wex "--write-exclusive" |
| 23 | text t_null "null" |
| 24 | text t_true "true" |
| 25 | text t_false "false" |
| 26 | text t_nan "NaN" |
| 27 | text t_inf "Infinity" |
| 28 | text t_ninf "-Infinity" |
| 29 | text t_pzero "0.0" |
| 30 | text t_nzero "-0.0" |
| 31 | text t_epfx "json_canon: " |
| 32 | text t_ebyt " at byte " |
| 33 | text t_eoom "json_canon: out of memory\n" |
| 34 | text t_eread "json_canon: read error\n" |
| 35 | text t_ewrite "json_canon: write error\n" |
| 36 | text t_esurr "json_canon: lone surrogate cannot be encoded as UTF-8\n" |
| 37 | text t_eexist "json_canon: [Errno 17] File exists: '" |
| 38 | text t_eerrno1 "json_canon: [Errno " |
| 39 | text t_eerrno2 "] open failed: '" |
| 40 | text t_eend "'\n" |
| 41 | text t_efsync "json_canon: fsync error\n" |
| 42 | text t_eclose "json_canon: close error\n" |
| 43 | text t_eutf8 "invalid UTF-8" |
| 44 | text t_etutf8 "truncated UTF-8" |
| 45 | text t_euesc "truncated \\u escape" |
| 46 | text t_ebuesc "bad \\u escape" |
| 47 | text t_eunterm "unterminated string" |
| 48 | text t_ectrl "control character in string" |
| 49 | text t_eeesc "unterminated escape" |
| 50 | text t_ebesc "bad escape" |
| 51 | text t_enum "bad number" |
| 52 | text t_edeep "nesting too deep" |
| 53 | text t_eval "expecting value" |
| 54 | text t_eprop "expecting property name" |
| 55 | text t_ecolon "expecting ':'" |
| 56 | text t_eobj "expecting ',' or '}'" |
| 57 | text t_earr "expecting ',' or ']'" |
| 58 | text t_eextra "extra data" |
| 59 | text t_nl "\n" |
| 60 | |
| 61 | note Dynamic pools (no hard caps) |
| 62 | global g_in uchar * |
| 63 | global g_out uchar * |
| 64 | global g_str uchar * |
| 65 | global g_nt uchar * |
| 66 | global g_na ulong * |
| 67 | global g_nb ulong * |
| 68 | global g_ekoff ulong * |
| 69 | global g_eklen ulong * |
| 70 | global g_eval ulong * |
| 71 | global g_enext ulong * |
| 72 | global g_ord ulong * |
| 73 | |
| 74 | note Pool sizes and capacities: |
| 75 | note [0]=in_len [1]=in_cap [2]=out_len [3]=out_cap [4]=str_len [5]=str_cap |
| 76 | note [6]=n_len [7]=n_cap [8]=e_len [9]=e_cap [10]=o_len [11]=o_cap |
| 77 | global g_p ulong 16 |
| 78 | |
| 79 | note Parser state: [0]=src_base [1]=src_end [2]=pos [3]=lone_surr [4]=soft [5]=err |
| 80 | global g_st ulong 8 |
| 81 | global g_arg1 uchar 4096 |
| 82 | global g_arg2 uchar 4096 |
| 83 | global g_errbuf uchar 256 |
| 84 | |
| 85 | note SHA-256 state |
| 86 | global g_sk uint 72 |
| 87 | global g_sw uint 64 |
| 88 | global g_sv uint 8 |
| 89 | global g_sh uint 8 |
| 90 | global g_spad uchar 128 |
| 91 | global g_sdig uchar 32 |
| 92 | global g_sinit ulong 1 |
| 93 | |
| 94 | note BigInt registers (128 x 32-bit limbs = 4096 bits each) for exact strtod + Dragon4 |
| 95 | note 0=A 1=B 2=C 3=D 4=E 5=T (each 128 uint limbs, len in g_blen[0..5]) |
| 96 | global g_bi uint 768 |
| 97 | global g_blen ulong 6 |
| 98 | global g_fdig uchar 1024 |
| 99 | global g_fmeta ulong 8 |
| 100 | |
| 101 | fn wr_fd_txt fd int s view uchar * to void |
| 102 | off = ulong 0 |
| 103 | n = len s |
| 104 | while lt off n bound 4096 |
| 105 | k = write fd s off sub n off |
| 106 | if eq k neg long 4 |
| 107 | continue |
| 108 | end |
| 109 | if le k 0 |
| 110 | ret |
| 111 | end |
| 112 | off = add off ulong k |
| 113 | end |
| 114 | end |
| 115 | |
| 116 | fn wr_fd_u64 fd int v ulong to void |
| 117 | b = new uchar 24 0 |
| 118 | n = ulong 0 |
| 119 | x = v |
| 120 | while true bound 24 |
| 121 | put b n uchar add 48 rem x 10 |
| 122 | n = add n 1 |
| 123 | x = div x 10 |
| 124 | if eq x 0 |
| 125 | break |
| 126 | end |
| 127 | end |
| 128 | r = new uchar 24 0 |
| 129 | for i ulong n bound 24 |
| 130 | put r i get b sub sub n 1 i |
| 131 | end |
| 132 | off = ulong 0 |
| 133 | while lt off n bound 24 |
| 134 | k = write fd r off sub n off |
| 135 | if eq k neg long 4 |
| 136 | continue |
| 137 | end |
| 138 | if le k 0 |
| 139 | ret |
| 140 | end |
| 141 | off = add off ulong k |
| 142 | end |
| 143 | end |
| 144 | |
| 145 | fn oom_die to void |
| 146 | wr_fd_txt 2 t_eoom |
| 147 | exit 1 |
| 148 | end |
| 149 | |
| 150 | fn fail_msg s view uchar * to void |
| 151 | put g_st 5 1 |
| 152 | if ne get g_st 4 0 |
| 153 | ret |
| 154 | end |
| 155 | wr_fd_txt 2 t_epfx |
| 156 | wr_fd_txt 2 s |
| 157 | wr_fd_txt 2 t_ebyt |
| 158 | wr_fd_u64 2 sub get g_st 2 get g_st 0 |
| 159 | wr_fd_txt 2 t_nl |
| 160 | exit 1 |
| 161 | end |
| 162 | |
| 163 | fn ensure_out need ulong to void |
| 164 | cur = get g_p 2 |
| 165 | cap = get g_p 3 |
| 166 | if le add cur need cap |
| 167 | ret |
| 168 | end |
| 169 | ncap = cap |
| 170 | if eq ncap 0 |
| 171 | ncap = 4096 |
| 172 | end |
| 173 | while lt ncap add cur need bound 64 |
| 174 | ncap = mul ncap 2 |
| 175 | end |
| 176 | r = enlarge g_out ncap |
| 177 | if lt r 0 |
| 178 | oom_die |
| 179 | end |
| 180 | put g_p 3 ncap |
| 181 | end |
| 182 | |
| 183 | fn out_c c uchar to void |
| 184 | ensure_out 1 |
| 185 | p = get g_p 2 |
| 186 | put g_out p c |
| 187 | put g_p 2 add p 1 |
| 188 | end |
| 189 | |
| 190 | fn out_txt s view uchar * to void |
| 191 | n = len s |
| 192 | ensure_out n |
| 193 | p = get g_p 2 |
| 194 | for i ulong n bound 256 |
| 195 | put g_out add p i get s i |
| 196 | end |
| 197 | put g_p 2 add p n |
| 198 | end |
| 199 | |
| 200 | fn out_str_slice off ulong n ulong to void |
| 201 | ensure_out n |
| 202 | p = get g_p 2 |
| 203 | for i ulong n bound 16777216 |
| 204 | put g_out add p i get g_str add off i |
| 205 | end |
| 206 | put g_p 2 add p n |
| 207 | end |
| 208 | |
| 209 | fn ensure_str need ulong to void |
| 210 | cur = get g_p 4 |
| 211 | cap = get g_p 5 |
| 212 | if le add cur need cap |
| 213 | ret |
| 214 | end |
| 215 | ncap = cap |
| 216 | if eq ncap 0 |
| 217 | ncap = 4096 |
| 218 | end |
| 219 | while lt ncap add cur need bound 64 |
| 220 | ncap = mul ncap 2 |
| 221 | end |
| 222 | r = enlarge g_str ncap |
| 223 | if lt r 0 |
| 224 | oom_die |
| 225 | end |
| 226 | put g_p 5 ncap |
| 227 | end |
| 228 | |
| 229 | fn str_push c uchar to void |
| 230 | ensure_str 1 |
| 231 | p = get g_p 4 |
| 232 | put g_str p c |
| 233 | put g_p 4 add p 1 |
| 234 | end |
| 235 | |
| 236 | fn alloc_node ty uchar to ulong |
| 237 | cur = get g_p 6 |
| 238 | cap = get g_p 7 |
| 239 | if ge cur cap |
| 240 | ncap = cap |
| 241 | if eq ncap 0 |
| 242 | ncap = 1024 |
| 243 | else |
| 244 | ncap = mul ncap 2 |
| 245 | end |
| 246 | r = enlarge g_nt ncap |
| 247 | if lt r 0 |
| 248 | oom_die |
| 249 | end |
| 250 | r = enlarge g_na ncap |
| 251 | if lt r 0 |
| 252 | oom_die |
| 253 | end |
| 254 | r = enlarge g_nb ncap |
| 255 | if lt r 0 |
| 256 | oom_die |
| 257 | end |
| 258 | put g_p 7 ncap |
| 259 | end |
| 260 | put g_nt cur ty |
| 261 | put g_na cur 0 |
| 262 | put g_nb cur 0 |
| 263 | put g_p 6 add cur 1 |
| 264 | ret cur |
| 265 | end |
| 266 | |
| 267 | fn alloc_entry ko ulong kl ulong val ulong to ulong |
| 268 | cur = get g_p 8 |
| 269 | cap = get g_p 9 |
| 270 | if ge cur cap |
| 271 | ncap = cap |
| 272 | if eq ncap 0 |
| 273 | ncap = 1024 |
| 274 | else |
| 275 | ncap = mul ncap 2 |
| 276 | end |
| 277 | r = enlarge g_ekoff ncap |
| 278 | if lt r 0 |
| 279 | oom_die |
| 280 | end |
| 281 | r = enlarge g_eklen ncap |
| 282 | if lt r 0 |
| 283 | oom_die |
| 284 | end |
| 285 | r = enlarge g_eval ncap |
| 286 | if lt r 0 |
| 287 | oom_die |
| 288 | end |
| 289 | r = enlarge g_enext ncap |
| 290 | if lt r 0 |
| 291 | oom_die |
| 292 | end |
| 293 | put g_p 9 ncap |
| 294 | end |
| 295 | put g_ekoff cur ko |
| 296 | put g_eklen cur kl |
| 297 | put g_eval cur val |
| 298 | put g_enext cur 18446744073709551615 |
| 299 | put g_p 8 add cur 1 |
| 300 | ret cur |
| 301 | end |
| 302 | |
| 303 | fn reserve_ord n ulong to ulong |
| 304 | cur = get g_p 10 |
| 305 | cap = get g_p 11 |
| 306 | if gt add cur n cap |
| 307 | ncap = cap |
| 308 | if eq ncap 0 |
| 309 | ncap = 1024 |
| 310 | end |
| 311 | while lt ncap add cur n bound 64 |
| 312 | ncap = mul ncap 2 |
| 313 | end |
| 314 | r = enlarge g_ord ncap |
| 315 | if lt r 0 |
| 316 | oom_die |
| 317 | end |
| 318 | put g_p 11 ncap |
| 319 | end |
| 320 | put g_p 10 add cur n |
| 321 | ret cur |
| 322 | end |
| 323 | |
| 324 | note ========= BigInt (base 2^32, 128 limbs per reg) ========= |
| 325 | fn bi_zero id ulong to void |
| 326 | put g_blen id 0 |
| 327 | end |
| 328 | |
| 329 | fn bi_set_u64 id ulong v ulong to void |
| 330 | base = mul id 128 |
| 331 | if eq v 0 |
| 332 | put g_blen id 0 |
| 333 | ret |
| 334 | end |
| 335 | put g_bi base uint and v 4294967295 |
| 336 | hi = shr v 32 |
| 337 | if eq hi 0 |
| 338 | put g_blen id 1 |
| 339 | else |
| 340 | put g_bi add base 1 uint hi |
| 341 | put g_blen id 2 |
| 342 | end |
| 343 | end |
| 344 | |
| 345 | fn bi_copy dst ulong src ulong to void |
| 346 | n = get g_blen src |
| 347 | db = mul dst 128 |
| 348 | sb = mul src 128 |
| 349 | for i ulong n bound 128 |
| 350 | put g_bi add db i get g_bi add sb i |
| 351 | end |
| 352 | put g_blen dst n |
| 353 | end |
| 354 | |
| 355 | fn bi_norm id ulong to void |
| 356 | n = get g_blen id |
| 357 | base = mul id 128 |
| 358 | while gt n 0 bound 128 |
| 359 | if ne get g_bi add base sub n 1 0 |
| 360 | break |
| 361 | end |
| 362 | n = sub n 1 |
| 363 | end |
| 364 | put g_blen id n |
| 365 | end |
| 366 | |
| 367 | fn bi_cmp a ulong b ulong to long |
| 368 | na = get g_blen a |
| 369 | nb = get g_blen b |
| 370 | if lt na nb |
| 371 | ret neg long 1 |
| 372 | end |
| 373 | if gt na nb |
| 374 | ret 1 |
| 375 | end |
| 376 | ab = mul a 128 |
| 377 | bb = mul b 128 |
| 378 | i = na |
| 379 | while gt i 0 bound 128 |
| 380 | i = sub i 1 |
| 381 | va = get g_bi add ab i |
| 382 | vb = get g_bi add bb i |
| 383 | if lt va vb |
| 384 | ret neg long 1 |
| 385 | end |
| 386 | if gt va vb |
| 387 | ret 1 |
| 388 | end |
| 389 | end |
| 390 | ret 0 |
| 391 | end |
| 392 | |
| 393 | fn bi_mul_add id ulong m ulong a ulong to void |
| 394 | n = get g_blen id |
| 395 | base = mul id 128 |
| 396 | carry = a |
| 397 | for i ulong n bound 128 |
| 398 | prod = add mul ulong get g_bi add base i m carry |
| 399 | put g_bi add base i uint and prod 4294967295 |
| 400 | carry = shr prod 32 |
| 401 | end |
| 402 | if ne carry 0 |
| 403 | if lt n 128 |
| 404 | put g_bi add base n uint carry |
| 405 | put g_blen id add n 1 |
| 406 | end |
| 407 | end |
| 408 | end |
| 409 | |
| 410 | fn bi_get id ulong i ulong to ulong |
| 411 | if ge i get g_blen id |
| 412 | ret 0 |
| 413 | end |
| 414 | ret ulong get g_bi add mul id 128 i |
| 415 | end |
| 416 | |
| 417 | fn bi_put id ulong i ulong v ulong to void |
| 418 | put g_bi add mul id 128 i uint and v 4294967295 |
| 419 | end |
| 420 | |
| 421 | fn bi_add_to dst ulong src ulong to void |
| 422 | n = get g_blen dst |
| 423 | ns = get g_blen src |
| 424 | if gt ns n |
| 425 | n = ns |
| 426 | end |
| 427 | carry = ulong 0 |
| 428 | for i ulong n bound 128 |
| 429 | s = add add bi_get dst i bi_get src i carry |
| 430 | bi_put dst i s |
| 431 | carry = shr s 32 |
| 432 | end |
| 433 | if ne carry 0 |
| 434 | if lt n 128 |
| 435 | bi_put dst n carry |
| 436 | n = add n 1 |
| 437 | end |
| 438 | end |
| 439 | put g_blen dst n |
| 440 | end |
| 441 | |
| 442 | fn bi_sub_from dst ulong src ulong to void |
| 443 | nd = get g_blen dst |
| 444 | borrow = ulong 0 |
| 445 | for i ulong nd bound 128 |
| 446 | va = bi_get dst i |
| 447 | subv = add bi_get src i borrow |
| 448 | if ge va subv |
| 449 | bi_put dst i sub va subv |
| 450 | borrow = 0 |
| 451 | else |
| 452 | bi_put dst i sub add va 4294967296 subv |
| 453 | borrow = 1 |
| 454 | end |
| 455 | end |
| 456 | bi_norm dst |
| 457 | end |
| 458 | |
| 459 | fn bi_shl id ulong bits ulong to void |
| 460 | n = get g_blen id |
| 461 | if or eq n 0 eq bits 0 |
| 462 | ret |
| 463 | end |
| 464 | ws = div bits 32 |
| 465 | bs = rem bits 32 |
| 466 | base = mul id 128 |
| 467 | if gt bs 0 |
| 468 | carry = ulong 0 |
| 469 | for i ulong n bound 128 |
| 470 | v = ulong get g_bi add base i |
| 471 | nv = or shl v bs carry |
| 472 | put g_bi add base i uint and nv 4294967295 |
| 473 | carry = shr nv 32 |
| 474 | end |
| 475 | if ne carry 0 |
| 476 | if lt n 128 |
| 477 | put g_bi add base n uint carry |
| 478 | n = add n 1 |
| 479 | end |
| 480 | end |
| 481 | end |
| 482 | if gt ws 0 |
| 483 | nn = add n ws |
| 484 | if gt nn 128 |
| 485 | nn = 128 |
| 486 | end |
| 487 | i = n |
| 488 | while gt i 0 bound 128 |
| 489 | i = sub i 1 |
| 490 | dsti = add i ws |
| 491 | if lt dsti 128 |
| 492 | put g_bi add base dsti get g_bi add base i |
| 493 | end |
| 494 | end |
| 495 | for j ulong ws bound 128 |
| 496 | put g_bi add base j 0 |
| 497 | end |
| 498 | n = nn |
| 499 | end |
| 500 | put g_blen id n |
| 501 | end |
| 502 | |
| 503 | fn bi_mul_pow10 id ulong p ulong to void |
| 504 | remp = p |
| 505 | while ge remp 9 bound 256 |
| 506 | bi_mul_add id 1000000000 0 |
| 507 | remp = sub remp 9 |
| 508 | end |
| 509 | while gt remp 0 bound 16 |
| 510 | bi_mul_add id 10 0 |
| 511 | remp = sub remp 1 |
| 512 | end |
| 513 | end |
| 514 | |
| 515 | fn bi_bitlen id ulong to ulong |
| 516 | n = get g_blen id |
| 517 | if eq n 0 |
| 518 | ret 0 |
| 519 | end |
| 520 | top = ulong get g_bi add mul id 128 sub n 1 |
| 521 | bl = mul sub n 1 32 |
| 522 | while gt top 0 bound 33 |
| 523 | bl = add bl 1 |
| 524 | top = shr top 1 |
| 525 | end |
| 526 | ret bl |
| 527 | end |
| 528 | |
| 529 | fn dec_build_ab nd ulong exp10 long to void |
| 530 | bi_zero 0 |
| 531 | use_nd = nd |
| 532 | eff_e10 = exp10 |
| 533 | if gt use_nd 768 |
| 534 | eff_e10 = add eff_e10 long sub use_nd 768 |
| 535 | use_nd = 768 |
| 536 | end |
| 537 | for i ulong use_nd bound 768 |
| 538 | bi_mul_add 0 10 ulong get g_fdig i |
| 539 | end |
| 540 | if gt nd 768 |
| 541 | has_nz = false |
| 542 | for i ulong sub nd 768 bound 1024 |
| 543 | if ne get g_fdig add 768 i 0 |
| 544 | has_nz = true |
| 545 | break |
| 546 | end |
| 547 | end |
| 548 | if has_nz |
| 549 | bi_mul_add 0 10 1 |
| 550 | eff_e10 = sub eff_e10 1 |
| 551 | end |
| 552 | end |
| 553 | bi_set_u64 1 1 |
| 554 | if ge eff_e10 0 |
| 555 | bi_mul_pow10 0 ulong eff_e10 |
| 556 | else |
| 557 | bi_mul_pow10 1 ulong neg eff_e10 |
| 558 | end |
| 559 | end |
| 560 | |
| 561 | fn dec_find_e2 to long |
| 562 | bna = bi_bitlen 0 |
| 563 | bnb = bi_bitlen 1 |
| 564 | e2 = sub long bna long bnb |
| 565 | bi_copy 2 0 |
| 566 | bi_copy 3 1 |
| 567 | if ge e2 0 |
| 568 | bi_shl 3 ulong e2 |
| 569 | else |
| 570 | bi_shl 2 ulong neg e2 |
| 571 | end |
| 572 | if lt bi_cmp 2 3 0 |
| 573 | e2 = sub e2 1 |
| 574 | end |
| 575 | ret e2 |
| 576 | end |
| 577 | |
| 578 | fn dec_div_round e2 long signb ulong to ulong |
| 579 | sh = long 0 |
| 580 | if ge e2 neg long 1022 |
| 581 | sh = sub e2 52 |
| 582 | else |
| 583 | sh = neg long 1074 |
| 584 | end |
| 585 | if ge sh 0 |
| 586 | bi_shl 1 ulong sh |
| 587 | else |
| 588 | bi_shl 0 ulong neg sh |
| 589 | end |
| 590 | q = ulong 0 |
| 591 | bi_copy 2 1 |
| 592 | bi_shl 2 53 |
| 593 | for bit ulong 54 |
| 594 | q = shl q 1 |
| 595 | if ge bi_cmp 0 2 0 |
| 596 | bi_sub_from 0 2 |
| 597 | q = or q 1 |
| 598 | end |
| 599 | if lt bit 53 |
| 600 | bi_shl 0 1 |
| 601 | end |
| 602 | end |
| 603 | bi_shl 0 1 |
| 604 | cmp2 = bi_cmp 0 2 |
| 605 | round_up = false |
| 606 | if gt cmp2 0 |
| 607 | round_up = true |
| 608 | else |
| 609 | if eq cmp2 0 |
| 610 | if eq and q 1 1 |
| 611 | round_up = true |
| 612 | end |
| 613 | end |
| 614 | end |
| 615 | if round_up |
| 616 | q = add q 1 |
| 617 | end |
| 618 | if ge e2 neg long 1022 |
| 619 | e2v = e2 |
| 620 | if ge q 9007199254740992 |
| 621 | q = shr q 1 |
| 622 | e2v = add e2v 1 |
| 623 | end |
| 624 | if gt e2v 1023 |
| 625 | ret or signb 0x7ff0000000000000 |
| 626 | end |
| 627 | bexp = ulong add e2v 1023 |
| 628 | ret or signb or shl bexp 52 and q 4503599627370495 |
| 629 | end |
| 630 | if ge q 4503599627370496 |
| 631 | ret or signb or shl ulong 1 52 and q 4503599627370495 |
| 632 | end |
| 633 | ret or signb and q 4503599627370495 |
| 634 | end |
| 635 | |
| 636 | fn dec_to_f64_bits nd ulong exp10 long negf ulong to ulong |
| 637 | signb = shl negf 63 |
| 638 | if eq nd 0 |
| 639 | ret signb |
| 640 | end |
| 641 | top10 = add exp10 long nd |
| 642 | if gt top10 310 |
| 643 | ret or signb 0x7ff0000000000000 |
| 644 | end |
| 645 | if lt top10 neg long 325 |
| 646 | ret signb |
| 647 | end |
| 648 | dec_build_ab nd exp10 |
| 649 | e2 = dec_find_e2 |
| 650 | if gt e2 1023 |
| 651 | ret or signb 0x7ff0000000000000 |
| 652 | end |
| 653 | if lt e2 neg long 1075 |
| 654 | ret signb |
| 655 | end |
| 656 | ret dec_div_round e2 signb |
| 657 | end |
| 658 | |
| 659 | note Dragon4 shortest round-trip decimal digits for finite positive f64 bits. |
| 660 | note Writes digits (0..9) into g_fdig[0..nd), stores nd in g_fmeta[0], exp10 in g_fmeta[1] (as bits of long). |
| 661 | fn dragon4_step is_even bool to ulong |
| 662 | note reg0=r, reg1=s, reg2=m+, reg3=m- |
| 663 | bi_mul_add 0 10 0 |
| 664 | bi_mul_add 2 10 0 |
| 665 | bi_mul_add 3 10 0 |
| 666 | d = ulong 0 |
| 667 | while ge bi_cmp 0 1 0 bound 12 |
| 668 | bi_sub_from 0 1 |
| 669 | d = add d 1 |
| 670 | end |
| 671 | low = false |
| 672 | c_lo = bi_cmp 0 3 |
| 673 | if is_even |
| 674 | low = le c_lo 0 |
| 675 | else |
| 676 | low = lt c_lo 0 |
| 677 | end |
| 678 | bi_copy 4 0 |
| 679 | bi_add_to 4 2 |
| 680 | c_hi = bi_cmp 4 1 |
| 681 | high = false |
| 682 | if is_even |
| 683 | high = ge c_hi 0 |
| 684 | else |
| 685 | high = gt c_hi 0 |
| 686 | end |
| 687 | if and not low not high |
| 688 | ret d |
| 689 | end |
| 690 | if and low not high |
| 691 | ret add 16 d |
| 692 | end |
| 693 | if and not low high |
| 694 | ret add 16 add d 1 |
| 695 | end |
| 696 | note Both low and high: compare 2*r vs s |
| 697 | bi_copy 4 0 |
| 698 | bi_shl 4 1 |
| 699 | cm = bi_cmp 4 1 |
| 700 | if lt cm 0 |
| 701 | ret add 16 d |
| 702 | end |
| 703 | if gt cm 0 |
| 704 | ret add 16 add d 1 |
| 705 | end |
| 706 | if eq and d 1 0 |
| 707 | ret add 16 d |
| 708 | end |
| 709 | ret add 16 add d 1 |
| 710 | end |
| 711 | |
| 712 | fn dragon4_init bits ulong to long |
| 713 | raw_e = and shr bits 52 2047 |
| 714 | frac = and bits 4503599627370495 |
| 715 | f = ulong 0 |
| 716 | e = long 0 |
| 717 | low_half = false |
| 718 | if eq raw_e 0 |
| 719 | f = frac |
| 720 | e = neg long 1074 |
| 721 | else |
| 722 | f = or frac 4503599627370496 |
| 723 | e = sub long raw_e 1075 |
| 724 | if and eq frac 0 gt raw_e 1 |
| 725 | low_half = true |
| 726 | end |
| 727 | end |
| 728 | note reg0=r, reg1=s, reg2=m+, reg3=m- |
| 729 | bi_set_u64 0 f |
| 730 | bi_set_u64 1 1 |
| 731 | bi_set_u64 2 1 |
| 732 | bi_set_u64 3 1 |
| 733 | if ge e 0 |
| 734 | bi_shl 0 ulong e |
| 735 | bi_shl 2 ulong e |
| 736 | bi_shl 3 ulong e |
| 737 | else |
| 738 | bi_shl 1 ulong neg e |
| 739 | end |
| 740 | if low_half |
| 741 | bi_shl 0 2 |
| 742 | bi_shl 1 2 |
| 743 | bi_shl 2 2 |
| 744 | bi_shl 3 1 |
| 745 | else |
| 746 | bi_shl 0 1 |
| 747 | bi_shl 1 1 |
| 748 | end |
| 749 | note Estimate k = ceil(log10(v)) using bitlen(f)+e: log10(2) ~ 78913 / 262144 |
| 750 | bi_copy 4 0 |
| 751 | bi_add_to 4 2 |
| 752 | b_hi = long bi_bitlen 4 |
| 753 | b_s = long bi_bitlen 1 |
| 754 | e2 = sub b_hi b_s |
| 755 | k = div mul e2 78913 262144 |
| 756 | if gt k 0 |
| 757 | bi_mul_pow10 1 ulong k |
| 758 | else |
| 759 | if lt k 0 |
| 760 | p10 = ulong neg k |
| 761 | bi_mul_pow10 0 p10 |
| 762 | bi_mul_pow10 2 p10 |
| 763 | bi_mul_pow10 3 p10 |
| 764 | end |
| 765 | end |
| 766 | is_even = eq and f 1 0 |
| 767 | while true bound 8 |
| 768 | bi_copy 4 0 |
| 769 | bi_add_to 4 2 |
| 770 | c = bi_cmp 4 1 |
| 771 | need_up = false |
| 772 | if is_even |
| 773 | need_up = ge c 0 |
| 774 | else |
| 775 | need_up = gt c 0 |
| 776 | end |
| 777 | if not need_up |
| 778 | break |
| 779 | end |
| 780 | bi_mul_add 1 10 0 |
| 781 | k = add k 1 |
| 782 | end |
| 783 | while true bound 8 |
| 784 | bi_copy 4 0 |
| 785 | bi_add_to 4 2 |
| 786 | bi_mul_add 4 10 0 |
| 787 | c = bi_cmp 4 1 |
| 788 | can_dn = false |
| 789 | if is_even |
| 790 | can_dn = lt c 0 |
| 791 | else |
| 792 | can_dn = le c 0 |
| 793 | end |
| 794 | if not can_dn |
| 795 | break |
| 796 | end |
| 797 | bi_mul_add 0 10 0 |
| 798 | bi_mul_add 2 10 0 |
| 799 | bi_mul_add 3 10 0 |
| 800 | k = sub k 1 |
| 801 | end |
| 802 | ret k |
| 803 | end |
| 804 | |
| 805 | fn out_exp10 exp10 long to void |
| 806 | b = new uchar 8 0 |
| 807 | out_c 101 |
| 808 | ae = ulong 0 |
| 809 | if lt exp10 0 |
| 810 | out_c 45 |
| 811 | ae = ulong neg exp10 |
| 812 | else |
| 813 | out_c 43 |
| 814 | ae = ulong exp10 |
| 815 | end |
| 816 | if lt ae 10 |
| 817 | out_c 48 |
| 818 | end |
| 819 | nb = ulong 0 |
| 820 | x = ae |
| 821 | while true bound 8 |
| 822 | put b nb uchar add 48 rem x 10 |
| 823 | nb = add nb 1 |
| 824 | x = div x 10 |
| 825 | if eq x 0 |
| 826 | break |
| 827 | end |
| 828 | end |
| 829 | for i ulong nb bound 8 |
| 830 | out_c get b sub sub nb 1 i |
| 831 | end |
| 832 | end |
| 833 | |
| 834 | fn out_f64_fmt nd ulong k long negf ulong to void |
| 835 | exp10 = sub k 1 |
| 836 | if ne negf 0 |
| 837 | out_c 45 |
| 838 | end |
| 839 | if or le k neg long 4 gt k 16 |
| 840 | out_c add 48 get g_fdig 0 |
| 841 | if gt nd 1 |
| 842 | out_c 46 |
| 843 | for i ulong sub nd 1 bound 32 |
| 844 | out_c add 48 get g_fdig add i 1 |
| 845 | end |
| 846 | end |
| 847 | out_exp10 exp10 |
| 848 | ret |
| 849 | end |
| 850 | if le k 0 |
| 851 | out_c 48 |
| 852 | out_c 46 |
| 853 | nz = ulong neg k |
| 854 | for i ulong nz bound 8 |
| 855 | out_c 48 |
| 856 | end |
| 857 | for i ulong nd bound 32 |
| 858 | out_c add 48 get g_fdig i |
| 859 | end |
| 860 | ret |
| 861 | end |
| 862 | uk = ulong k |
| 863 | if ge uk nd |
| 864 | for i ulong nd bound 32 |
| 865 | out_c add 48 get g_fdig i |
| 866 | end |
| 867 | nz = sub uk nd |
| 868 | for i ulong nz bound 32 |
| 869 | out_c 48 |
| 870 | end |
| 871 | out_c 46 |
| 872 | out_c 48 |
| 873 | ret |
| 874 | end |
| 875 | for i ulong uk bound 32 |
| 876 | out_c add 48 get g_fdig i |
| 877 | end |
| 878 | out_c 46 |
| 879 | for i ulong sub nd uk bound 32 |
| 880 | out_c add 48 get g_fdig add uk i |
| 881 | end |
| 882 | end |
| 883 | |
| 884 | fn out_f64_bits bits ulong to void |
| 885 | negf = and shr bits 63 1 |
| 886 | raw_e = and shr bits 52 2047 |
| 887 | frac = and bits 4503599627370495 |
| 888 | if eq raw_e 2047 |
| 889 | if ne frac 0 |
| 890 | out_txt t_nan |
| 891 | else |
| 892 | if ne negf 0 |
| 893 | out_txt t_ninf |
| 894 | else |
| 895 | out_txt t_inf |
| 896 | end |
| 897 | end |
| 898 | ret |
| 899 | end |
| 900 | if and eq raw_e 0 eq frac 0 |
| 901 | if ne negf 0 |
| 902 | out_txt t_nzero |
| 903 | else |
| 904 | out_txt t_pzero |
| 905 | end |
| 906 | ret |
| 907 | end |
| 908 | is_even = eq and frac 1 0 |
| 909 | k = dragon4_init bits |
| 910 | nd = ulong 0 |
| 911 | while lt nd 24 bound 32 |
| 912 | st = dragon4_step is_even |
| 913 | put g_fdig nd uchar and st 15 |
| 914 | nd = add nd 1 |
| 915 | if ge st 16 |
| 916 | break |
| 917 | end |
| 918 | end |
| 919 | while gt nd 1 bound 32 |
| 920 | if ne get g_fdig sub nd 1 0 |
| 921 | break |
| 922 | end |
| 923 | nd = sub nd 1 |
| 924 | end |
| 925 | out_f64_fmt nd k negf |
| 926 | end |
| 927 | |
| 928 | note ========= SHA-256 over g_out[0..n) ========= |
| 929 | fn sha_init_tbl to void |
| 930 | if ne get g_sinit 0 0 |
| 931 | ret |
| 932 | end |
| 933 | put g_sinit 0 1 |
| 934 | p = ulong 0 |
| 935 | for i ulong 72 |
| 936 | v = ulong 0 |
| 937 | while lt get k_sha_tbl p 48 bound 4 |
| 938 | p = add p 1 |
| 939 | end |
| 940 | while ge get k_sha_tbl p 48 bound 11 |
| 941 | v = add mul v 10 ulong sub get k_sha_tbl p 48 |
| 942 | p = add p 1 |
| 943 | if ge p len k_sha_tbl |
| 944 | put g_sk i uint v |
| 945 | ret |
| 946 | end |
| 947 | end |
| 948 | put g_sk i uint v |
| 949 | end |
| 950 | end |
| 951 | |
| 952 | fn sha_s0 a uint to uint |
| 953 | ret xor xor rotr a 2 rotr a 13 rotr a 22 |
| 954 | end |
| 955 | |
| 956 | fn sha_s1 e uint to uint |
| 957 | ret xor xor rotr e 6 rotr e 11 rotr e 25 |
| 958 | end |
| 959 | |
| 960 | fn sha_g0 x uint to uint |
| 961 | ret xor xor rotr x 7 rotr x 18 shr x 3 |
| 962 | end |
| 963 | |
| 964 | fn sha_g1 x uint to uint |
| 965 | ret xor xor rotr x 17 rotr x 19 shr x 10 |
| 966 | end |
| 967 | |
| 968 | fn sha_maj a uint b uint c uint to uint |
| 969 | ret xor xor and a b and a c and b c |
| 970 | end |
| 971 | |
| 972 | fn sha_be src ulong o ulong to uint |
| 973 | v = uint 0 |
| 974 | for j ulong 4 |
| 975 | b = uint 0 |
| 976 | if eq src 0 |
| 977 | b = uint get g_out add o j |
| 978 | else |
| 979 | b = uint get g_spad add o j |
| 980 | end |
| 981 | v = or shl v 8 b |
| 982 | end |
| 983 | ret v |
| 984 | end |
| 985 | |
| 986 | fn sha_rnd i ulong to void |
| 987 | e = get g_sv 4 |
| 988 | t1 = add get g_sv 7 sha_s1 e |
| 989 | t1 = add t1 xor and e get g_sv 5 and not e get g_sv 6 |
| 990 | t1 = add t1 add get g_sk i get g_sw i |
| 991 | a = get g_sv 0 |
| 992 | t2 = add sha_s0 a sha_maj a get g_sv 1 get g_sv 2 |
| 993 | put g_sv 7 get g_sv 6 |
| 994 | put g_sv 6 get g_sv 5 |
| 995 | put g_sv 5 e |
| 996 | put g_sv 4 add get g_sv 3 t1 |
| 997 | put g_sv 3 get g_sv 2 |
| 998 | put g_sv 2 get g_sv 1 |
| 999 | put g_sv 1 a |
| 1000 | put g_sv 0 add t1 t2 |
| 1001 | end |
| 1002 | |
| 1003 | fn sha_blk src ulong o ulong to void |
| 1004 | for t ulong 16 |
| 1005 | put g_sw t sha_be src add o mul t 4 |
| 1006 | end |
| 1007 | for t ulong 48 |
| 1008 | u = add t 16 |
| 1009 | x = add sha_g1 get g_sw sub u 2 get g_sw sub u 7 |
| 1010 | put g_sw u add x add sha_g0 get g_sw sub u 15 get g_sw sub u 16 |
| 1011 | end |
| 1012 | for j ulong 8 |
| 1013 | put g_sv j get g_sh j |
| 1014 | end |
| 1015 | for i ulong 64 |
| 1016 | sha_rnd i |
| 1017 | end |
| 1018 | for j ulong 8 |
| 1019 | put g_sh j add get g_sh j get g_sv j |
| 1020 | end |
| 1021 | end |
| 1022 | |
| 1023 | fn sha256_out n ulong to void |
| 1024 | sha_init_tbl |
| 1025 | for j ulong 8 |
| 1026 | put g_sh j get g_sk add j 64 |
| 1027 | end |
| 1028 | nb = div n 64 |
| 1029 | for b ulong nb bound 16777216 |
| 1030 | sha_blk 0 mul b 64 |
| 1031 | end |
| 1032 | r = rem n 64 |
| 1033 | o = mul nb 64 |
| 1034 | for j ulong 128 |
| 1035 | put g_spad j 0 |
| 1036 | end |
| 1037 | for j ulong r bound 64 |
| 1038 | put g_spad j get g_out add o j |
| 1039 | end |
| 1040 | put g_spad r 0x80 |
| 1041 | tot = ulong 64 |
| 1042 | if ge add r 1 57 |
| 1043 | tot = 128 |
| 1044 | end |
| 1045 | bits = mul n 8 |
| 1046 | for k ulong 8 |
| 1047 | put g_spad sub sub tot 1 k uchar and shr bits mul k 8 255 |
| 1048 | end |
| 1049 | sha_blk 1 0 |
| 1050 | if eq tot 128 |
| 1051 | sha_blk 1 64 |
| 1052 | end |
| 1053 | for j ulong 8 |
| 1054 | w = get g_sh j |
| 1055 | put g_sdig mul j 4 uchar and shr w 24 255 |
| 1056 | put g_sdig add mul j 4 1 uchar and shr w 16 255 |
| 1057 | put g_sdig add mul j 4 2 uchar and shr w 8 255 |
| 1058 | put g_sdig add mul j 4 3 uchar and w 255 |
| 1059 | end |
| 1060 | end |
| 1061 | |
| 1062 | note ========= UTF-8 & JSON Parser ========= |
| 1063 | fn skip_ws to void |
| 1064 | pos = get g_st 2 |
| 1065 | endp = get g_st 1 |
| 1066 | while lt pos endp bound 16777216 |
| 1067 | c = get g_in pos |
| 1068 | if or eq c 32 or eq c 9 or eq c 10 eq c 13 |
| 1069 | pos = add pos 1 |
| 1070 | else |
| 1071 | break |
| 1072 | end |
| 1073 | end |
| 1074 | put g_st 2 pos |
| 1075 | end |
| 1076 | |
| 1077 | fn match_lit s view uchar * to bool |
| 1078 | pos = get g_st 2 |
| 1079 | endp = get g_st 1 |
| 1080 | n = len s |
| 1081 | if gt add pos n endp |
| 1082 | ret false |
| 1083 | end |
| 1084 | for i ulong n bound 16 |
| 1085 | if ne get g_in add pos i get s i |
| 1086 | ret false |
| 1087 | end |
| 1088 | end |
| 1089 | put g_st 2 add pos n |
| 1090 | ret true |
| 1091 | end |
| 1092 | |
| 1093 | fn put_utf8_str cp ulong to void |
| 1094 | if lt cp 128 |
| 1095 | str_push uchar cp |
| 1096 | ret |
| 1097 | end |
| 1098 | if lt cp 2048 |
| 1099 | str_push uchar or 192 shr cp 6 |
| 1100 | str_push uchar or 128 and cp 63 |
| 1101 | ret |
| 1102 | end |
| 1103 | if lt cp 65536 |
| 1104 | str_push uchar or 224 shr cp 12 |
| 1105 | str_push uchar or 128 and shr cp 6 63 |
| 1106 | str_push uchar or 128 and cp 63 |
| 1107 | ret |
| 1108 | end |
| 1109 | str_push uchar or 240 shr cp 18 |
| 1110 | str_push uchar or 128 and shr cp 12 63 |
| 1111 | str_push uchar or 128 and shr cp 6 63 |
| 1112 | str_push uchar or 128 and cp 63 |
| 1113 | end |
| 1114 | |
| 1115 | fn decode_utf8_in to long |
| 1116 | pos = get g_st 2 |
| 1117 | endp = get g_st 1 |
| 1118 | c = ulong get g_in pos |
| 1119 | if lt c 128 |
| 1120 | put g_st 2 add pos 1 |
| 1121 | ret long c |
| 1122 | end |
| 1123 | cp = ulong 0 |
| 1124 | k = ulong 0 |
| 1125 | if eq and c 224 192 |
| 1126 | cp = and c 31 |
| 1127 | k = 1 |
| 1128 | else |
| 1129 | if eq and c 240 224 |
| 1130 | cp = and c 15 |
| 1131 | k = 2 |
| 1132 | else |
| 1133 | if eq and c 248 240 |
| 1134 | cp = and c 7 |
| 1135 | k = 3 |
| 1136 | else |
| 1137 | fail_msg t_eutf8 |
| 1138 | ret neg long 1 |
| 1139 | end |
| 1140 | end |
| 1141 | end |
| 1142 | if ge add pos k endp |
| 1143 | fail_msg t_etutf8 |
| 1144 | ret neg long 1 |
| 1145 | end |
| 1146 | for j ulong k bound 4 |
| 1147 | b = ulong get g_in add pos add j 1 |
| 1148 | if ne and b 192 128 |
| 1149 | fail_msg t_eutf8 |
| 1150 | ret neg long 1 |
| 1151 | end |
| 1152 | cp = or shl cp 6 and b 63 |
| 1153 | end |
| 1154 | bad = false |
| 1155 | if and eq k 1 lt cp 128 |
| 1156 | bad = true |
| 1157 | end |
| 1158 | if and eq k 2 lt cp 2048 |
| 1159 | bad = true |
| 1160 | end |
| 1161 | if and eq k 3 lt cp 65536 |
| 1162 | bad = true |
| 1163 | end |
| 1164 | if gt cp 1114111 |
| 1165 | bad = true |
| 1166 | end |
| 1167 | if and ge cp 55296 le cp 57343 |
| 1168 | bad = true |
| 1169 | end |
| 1170 | if bad |
| 1171 | fail_msg t_eutf8 |
| 1172 | ret neg long 1 |
| 1173 | end |
| 1174 | put g_st 2 add pos add k 1 |
| 1175 | ret long cp |
| 1176 | end |
| 1177 | |
| 1178 | fn parse_hex4 to long |
| 1179 | pos = get g_st 2 |
| 1180 | endp = get g_st 1 |
| 1181 | if gt add pos 4 endp |
| 1182 | fail_msg t_euesc |
| 1183 | ret neg long 1 |
| 1184 | end |
| 1185 | v = ulong 0 |
| 1186 | for i ulong 4 |
| 1187 | c = get g_in add pos i |
| 1188 | d = ulong 0 |
| 1189 | if and ge c 48 le c 57 |
| 1190 | d = ulong sub c 48 |
| 1191 | else |
| 1192 | if and ge c 97 le c 102 |
| 1193 | d = add 10 ulong sub c 97 |
| 1194 | else |
| 1195 | if and ge c 65 le c 70 |
| 1196 | d = add 10 ulong sub c 65 |
| 1197 | else |
| 1198 | put g_st 2 add pos i |
| 1199 | fail_msg t_ebuesc |
| 1200 | ret neg long 1 |
| 1201 | end |
| 1202 | end |
| 1203 | end |
| 1204 | v = or shl v 4 d |
| 1205 | end |
| 1206 | put g_st 2 add pos 4 |
| 1207 | ret long v |
| 1208 | end |
| 1209 | |
| 1210 | fn parse_escape to bool |
| 1211 | pos = get g_st 2 |
| 1212 | endp = get g_st 1 |
| 1213 | if ge pos endp |
| 1214 | fail_msg t_eeesc |
| 1215 | ret false |
| 1216 | end |
| 1217 | e = get g_in pos |
| 1218 | put g_st 2 add pos 1 |
| 1219 | if eq e 34 |
| 1220 | str_push 34 |
| 1221 | ret true |
| 1222 | end |
| 1223 | if eq e 92 |
| 1224 | str_push 92 |
| 1225 | ret true |
| 1226 | end |
| 1227 | if eq e 47 |
| 1228 | str_push 47 |
| 1229 | ret true |
| 1230 | end |
| 1231 | if eq e 98 |
| 1232 | str_push 8 |
| 1233 | ret true |
| 1234 | end |
| 1235 | if eq e 102 |
| 1236 | str_push 12 |
| 1237 | ret true |
| 1238 | end |
| 1239 | if eq e 110 |
| 1240 | str_push 10 |
| 1241 | ret true |
| 1242 | end |
| 1243 | if eq e 114 |
| 1244 | str_push 13 |
| 1245 | ret true |
| 1246 | end |
| 1247 | if eq e 116 |
| 1248 | str_push 9 |
| 1249 | ret true |
| 1250 | end |
| 1251 | if eq e 117 |
| 1252 | cp1 = parse_hex4 |
| 1253 | if lt cp1 0 |
| 1254 | ret false |
| 1255 | end |
| 1256 | cp = ulong cp1 |
| 1257 | if and ge cp 55296 le cp 56319 |
| 1258 | p2 = get g_st 2 |
| 1259 | if lt add p2 1 endp |
| 1260 | if and eq get g_in p2 92 eq get g_in add p2 1 117 |
| 1261 | put g_st 2 add p2 2 |
| 1262 | lo1 = parse_hex4 |
| 1263 | if lt lo1 0 |
| 1264 | ret false |
| 1265 | end |
| 1266 | lo = ulong lo1 |
| 1267 | if and ge lo 56320 le lo 57343 |
| 1268 | cp = add 65536 add shl sub cp 55296 10 sub lo 56320 |
| 1269 | else |
| 1270 | put g_st 2 p2 |
| 1271 | end |
| 1272 | end |
| 1273 | end |
| 1274 | end |
| 1275 | if and ge cp 55296 le cp 57343 |
| 1276 | put g_st 3 1 |
| 1277 | end |
| 1278 | put_utf8_str cp |
| 1279 | ret true |
| 1280 | end |
| 1281 | fail_msg t_ebesc |
| 1282 | ret false |
| 1283 | end |
| 1284 | |
| 1285 | note Parses JSON string starting at '"', returns start offset in g_str (length in g_fmeta[7]), or -1 on error. |
| 1286 | fn parse_string to long |
| 1287 | start_str = get g_p 4 |
| 1288 | put g_st 2 add get g_st 2 1 |
| 1289 | endp = get g_st 1 |
| 1290 | while true bound 16777216 |
| 1291 | pos = get g_st 2 |
| 1292 | if ge pos endp |
| 1293 | fail_msg t_eunterm |
| 1294 | ret neg long 1 |
| 1295 | end |
| 1296 | c = get g_in pos |
| 1297 | if eq c 34 |
| 1298 | put g_st 2 add pos 1 |
| 1299 | break |
| 1300 | end |
| 1301 | if lt c 32 |
| 1302 | fail_msg t_ectrl |
| 1303 | ret neg long 1 |
| 1304 | end |
| 1305 | if ne c 92 |
| 1306 | p0 = pos |
| 1307 | cp = decode_utf8_in |
| 1308 | if lt cp 0 |
| 1309 | ret neg long 1 |
| 1310 | end |
| 1311 | p1 = get g_st 2 |
| 1312 | for i ulong sub p1 p0 bound 8 |
| 1313 | str_push get g_in add p0 i |
| 1314 | end |
| 1315 | else |
| 1316 | put g_st 2 add pos 1 |
| 1317 | if not parse_escape |
| 1318 | ret neg long 1 |
| 1319 | end |
| 1320 | end |
| 1321 | end |
| 1322 | put g_fmeta 7 sub get g_p 4 start_str |
| 1323 | ret long start_str |
| 1324 | end |
| 1325 | |
| 1326 | fn is_dig c uchar to bool |
| 1327 | ret and ge c 48 le c 57 |
| 1328 | end |
| 1329 | |
| 1330 | fn parse_exp_part p_in ulong pos ulong to long |
| 1331 | p = p_in |
| 1332 | eneg = false |
| 1333 | if eq get g_in p 45 |
| 1334 | eneg = true |
| 1335 | p = add p 1 |
| 1336 | else |
| 1337 | if eq get g_in p 43 |
| 1338 | p = add p 1 |
| 1339 | end |
| 1340 | end |
| 1341 | ev = long 0 |
| 1342 | while lt p pos bound 1024 |
| 1343 | if lt ev 1000000 |
| 1344 | ev = add mul ev 10 long sub get g_in p 48 |
| 1345 | end |
| 1346 | p = add p 1 |
| 1347 | end |
| 1348 | if eneg |
| 1349 | ret neg ev |
| 1350 | end |
| 1351 | ret ev |
| 1352 | end |
| 1353 | |
| 1354 | fn parse_float_span start ulong pos ulong negf ulong to long |
| 1355 | p = start |
| 1356 | if eq negf 1 |
| 1357 | p = add p 1 |
| 1358 | end |
| 1359 | nd = ulong 0 |
| 1360 | seen_nz = false |
| 1361 | exp10 = long 0 |
| 1362 | in_frac = false |
| 1363 | while lt p pos bound 16777216 |
| 1364 | c = get g_in p |
| 1365 | if or eq c 101 eq c 69 |
| 1366 | p = add p 1 |
| 1367 | break |
| 1368 | end |
| 1369 | if eq c 46 |
| 1370 | in_frac = true |
| 1371 | p = add p 1 |
| 1372 | continue |
| 1373 | end |
| 1374 | d = sub c 48 |
| 1375 | if or seen_nz ne d 0 |
| 1376 | seen_nz = true |
| 1377 | if lt nd 1020 |
| 1378 | put g_fdig nd d |
| 1379 | nd = add nd 1 |
| 1380 | if in_frac |
| 1381 | exp10 = sub exp10 1 |
| 1382 | end |
| 1383 | else |
| 1384 | if not in_frac |
| 1385 | exp10 = add exp10 1 |
| 1386 | end |
| 1387 | if ne d 0 |
| 1388 | put g_fdig 1019 1 |
| 1389 | end |
| 1390 | end |
| 1391 | else |
| 1392 | if in_frac |
| 1393 | exp10 = sub exp10 1 |
| 1394 | end |
| 1395 | end |
| 1396 | p = add p 1 |
| 1397 | end |
| 1398 | if lt p pos |
| 1399 | exp10 = add exp10 parse_exp_part p pos |
| 1400 | end |
| 1401 | while gt nd 0 bound 1024 |
| 1402 | if ne get g_fdig sub nd 1 0 |
| 1403 | break |
| 1404 | end |
| 1405 | nd = sub nd 1 |
| 1406 | exp10 = add exp10 1 |
| 1407 | end |
| 1408 | fbits = dec_to_f64_bits nd exp10 negf |
| 1409 | nd_id = alloc_node 4 |
| 1410 | put g_na nd_id fbits |
| 1411 | ret long nd_id |
| 1412 | end |
| 1413 | |
| 1414 | fn make_int_node start ulong pos ulong to long |
| 1415 | soff = get g_p 4 |
| 1416 | n = sub pos start |
| 1417 | is_neg0 = false |
| 1418 | if eq n 2 |
| 1419 | if eq get g_in start 45 |
| 1420 | if eq get g_in add start 1 48 |
| 1421 | is_neg0 = true |
| 1422 | end |
| 1423 | end |
| 1424 | end |
| 1425 | if is_neg0 |
| 1426 | str_push 48 |
| 1427 | n = 1 |
| 1428 | else |
| 1429 | for i ulong n bound 16777216 |
| 1430 | str_push get g_in add start i |
| 1431 | end |
| 1432 | end |
| 1433 | nd_id = alloc_node 3 |
| 1434 | put g_na nd_id soff |
| 1435 | put g_nb nd_id n |
| 1436 | ret long nd_id |
| 1437 | end |
| 1438 | |
| 1439 | fn parse_number to long |
| 1440 | pos = get g_st 2 |
| 1441 | endp = get g_st 1 |
| 1442 | start = pos |
| 1443 | negf = ulong 0 |
| 1444 | if eq get g_in pos 45 |
| 1445 | negf = 1 |
| 1446 | pos = add pos 1 |
| 1447 | put g_st 2 pos |
| 1448 | end |
| 1449 | if lt pos endp |
| 1450 | if eq get g_in pos 73 |
| 1451 | if not match_lit t_inf |
| 1452 | fail_msg t_enum |
| 1453 | ret neg long 1 |
| 1454 | end |
| 1455 | nd_id = alloc_node 4 |
| 1456 | put g_na nd_id 0xfff0000000000000 |
| 1457 | ret long nd_id |
| 1458 | end |
| 1459 | end |
| 1460 | if ge pos endp |
| 1461 | fail_msg t_enum |
| 1462 | ret neg long 1 |
| 1463 | end |
| 1464 | if not is_dig get g_in pos |
| 1465 | fail_msg t_enum |
| 1466 | ret neg long 1 |
| 1467 | end |
| 1468 | is_fl = false |
| 1469 | if eq get g_in pos 48 |
| 1470 | pos = add pos 1 |
| 1471 | else |
| 1472 | while lt pos endp bound 16777216 |
| 1473 | if not is_dig get g_in pos |
| 1474 | break |
| 1475 | end |
| 1476 | pos = add pos 1 |
| 1477 | end |
| 1478 | end |
| 1479 | if lt add pos 1 endp |
| 1480 | if and eq get g_in pos 46 is_dig get g_in add pos 1 |
| 1481 | is_fl = true |
| 1482 | pos = add pos 1 |
| 1483 | while lt pos endp bound 16777216 |
| 1484 | if not is_dig get g_in pos |
| 1485 | break |
| 1486 | end |
| 1487 | pos = add pos 1 |
| 1488 | end |
| 1489 | end |
| 1490 | end |
| 1491 | if lt pos endp |
| 1492 | ce = get g_in pos |
| 1493 | if or eq ce 101 eq ce 69 |
| 1494 | p = add pos 1 |
| 1495 | if lt p endp |
| 1496 | cs = get g_in p |
| 1497 | if or eq cs 43 eq cs 45 |
| 1498 | p = add p 1 |
| 1499 | end |
| 1500 | end |
| 1501 | if lt p endp |
| 1502 | if is_dig get g_in p |
| 1503 | is_fl = true |
| 1504 | pos = p |
| 1505 | while lt pos endp bound 16777216 |
| 1506 | if not is_dig get g_in pos |
| 1507 | break |
| 1508 | end |
| 1509 | pos = add pos 1 |
| 1510 | end |
| 1511 | end |
| 1512 | end |
| 1513 | end |
| 1514 | end |
| 1515 | put g_st 2 pos |
| 1516 | if not is_fl |
| 1517 | ret make_int_node start pos |
| 1518 | end |
| 1519 | ret parse_float_span start pos negf |
| 1520 | end |
| 1521 | |
| 1522 | fn cmp_entry_keys ea ulong eb ulong to long |
| 1523 | oa = get g_ekoff ea |
| 1524 | la = get g_eklen ea |
| 1525 | ob = get g_ekoff eb |
| 1526 | lb = get g_eklen eb |
| 1527 | m = la |
| 1528 | if lt lb m |
| 1529 | m = lb |
| 1530 | end |
| 1531 | for i ulong m bound 16777216 |
| 1532 | ca = get g_str add oa i |
| 1533 | cb = get g_str add ob i |
| 1534 | if lt ca cb |
| 1535 | ret neg long 1 |
| 1536 | end |
| 1537 | if gt ca cb |
| 1538 | ret 1 |
| 1539 | end |
| 1540 | end |
| 1541 | if lt la lb |
| 1542 | ret neg long 1 |
| 1543 | end |
| 1544 | if gt la lb |
| 1545 | ret 1 |
| 1546 | end |
| 1547 | ret 0 |
| 1548 | end |
| 1549 | |
| 1550 | fn sort_dedup_obj nd_id ulong head ulong cnt ulong to void |
| 1551 | if eq cnt 0 |
| 1552 | put g_na nd_id 0 |
| 1553 | put g_nb nd_id 0 |
| 1554 | ret |
| 1555 | end |
| 1556 | base = reserve_ord cnt |
| 1557 | cur = head |
| 1558 | for i ulong cnt bound 16777216 |
| 1559 | put g_ord add base i cur |
| 1560 | cur = get g_enext cur |
| 1561 | end |
| 1562 | note Stable insertion sort by key |
| 1563 | for i ulong cnt bound 16777216 |
| 1564 | key_e = get g_ord add base i |
| 1565 | j = i |
| 1566 | while gt j 0 bound 16777216 |
| 1567 | prev_e = get g_ord add base sub j 1 |
| 1568 | if le cmp_entry_keys prev_e key_e 0 |
| 1569 | break |
| 1570 | end |
| 1571 | put g_ord add base j prev_e |
| 1572 | j = sub j 1 |
| 1573 | end |
| 1574 | put g_ord add base j key_e |
| 1575 | end |
| 1576 | note Deduplicate equal keys (keep last) |
| 1577 | nout = ulong 0 |
| 1578 | for i ulong cnt bound 16777216 |
| 1579 | e = get g_ord add base i |
| 1580 | skip = false |
| 1581 | if lt add i 1 cnt |
| 1582 | en = get g_ord add base add i 1 |
| 1583 | if eq cmp_entry_keys e en 0 |
| 1584 | skip = true |
| 1585 | end |
| 1586 | end |
| 1587 | if not skip |
| 1588 | put g_ord add base nout e |
| 1589 | nout = add nout 1 |
| 1590 | end |
| 1591 | end |
| 1592 | put g_na nd_id base |
| 1593 | put g_nb nd_id nout |
| 1594 | end |
| 1595 | |
| 1596 | fn flatten_arr nd_id ulong head ulong cnt ulong to void |
| 1597 | if eq cnt 0 |
| 1598 | put g_na nd_id 0 |
| 1599 | put g_nb nd_id 0 |
| 1600 | ret |
| 1601 | end |
| 1602 | base = reserve_ord cnt |
| 1603 | cur = head |
| 1604 | for i ulong cnt bound 16777216 |
| 1605 | put g_ord add base i cur |
| 1606 | cur = get g_enext cur |
| 1607 | end |
| 1608 | put g_na nd_id base |
| 1609 | put g_nb nd_id cnt |
| 1610 | end |
| 1611 | |
| 1612 | fn parse_obj depth ulong to long |
| 1613 | put g_st 2 add get g_st 2 1 |
| 1614 | skip_ws |
| 1615 | pos = get g_st 2 |
| 1616 | endp = get g_st 1 |
| 1617 | nd_id = alloc_node 7 |
| 1618 | if lt pos endp |
| 1619 | if eq get g_in pos 125 |
| 1620 | put g_st 2 add pos 1 |
| 1621 | ret long nd_id |
| 1622 | end |
| 1623 | end |
| 1624 | head = ulong 18446744073709551615 |
| 1625 | tail = ulong 18446744073709551615 |
| 1626 | cnt = ulong 0 |
| 1627 | while true bound 16777216 |
| 1628 | skip_ws |
| 1629 | pos = get g_st 2 |
| 1630 | if ge pos endp |
| 1631 | fail_msg t_eprop |
| 1632 | ret neg long 1 |
| 1633 | end |
| 1634 | if ne get g_in pos 34 |
| 1635 | fail_msg t_eprop |
| 1636 | ret neg long 1 |
| 1637 | end |
| 1638 | ko = parse_string |
| 1639 | if lt ko 0 |
| 1640 | ret neg long 1 |
| 1641 | end |
| 1642 | kl = get g_fmeta 7 |
| 1643 | skip_ws |
| 1644 | pos = get g_st 2 |
| 1645 | if ge pos endp |
| 1646 | fail_msg t_ecolon |
| 1647 | ret neg long 1 |
| 1648 | end |
| 1649 | if ne get g_in pos 58 |
| 1650 | fail_msg t_ecolon |
| 1651 | ret neg long 1 |
| 1652 | end |
| 1653 | put g_st 2 add pos 1 |
| 1654 | val = parse_value add depth 1 |
| 1655 | if lt val 0 |
| 1656 | ret neg long 1 |
| 1657 | end |
| 1658 | e = alloc_entry ulong ko kl ulong val |
| 1659 | if eq cnt 0 |
| 1660 | head = e |
| 1661 | else |
| 1662 | put g_enext tail e |
| 1663 | end |
| 1664 | tail = e |
| 1665 | cnt = add cnt 1 |
| 1666 | skip_ws |
| 1667 | pos = get g_st 2 |
| 1668 | if ge pos endp |
| 1669 | fail_msg t_eobj |
| 1670 | ret neg long 1 |
| 1671 | end |
| 1672 | c = get g_in pos |
| 1673 | if eq c 44 |
| 1674 | put g_st 2 add pos 1 |
| 1675 | continue |
| 1676 | end |
| 1677 | if eq c 125 |
| 1678 | put g_st 2 add pos 1 |
| 1679 | break |
| 1680 | end |
| 1681 | fail_msg t_eobj |
| 1682 | ret neg long 1 |
| 1683 | end |
| 1684 | sort_dedup_obj nd_id head cnt |
| 1685 | ret long nd_id |
| 1686 | end |
| 1687 | |
| 1688 | fn parse_arr depth ulong to long |
| 1689 | put g_st 2 add get g_st 2 1 |
| 1690 | skip_ws |
| 1691 | pos = get g_st 2 |
| 1692 | endp = get g_st 1 |
| 1693 | nd_id = alloc_node 6 |
| 1694 | if lt pos endp |
| 1695 | if eq get g_in pos 93 |
| 1696 | put g_st 2 add pos 1 |
| 1697 | ret long nd_id |
| 1698 | end |
| 1699 | end |
| 1700 | head = ulong 18446744073709551615 |
| 1701 | tail = ulong 18446744073709551615 |
| 1702 | cnt = ulong 0 |
| 1703 | while true bound 16777216 |
| 1704 | val = parse_value add depth 1 |
| 1705 | if lt val 0 |
| 1706 | ret neg long 1 |
| 1707 | end |
| 1708 | e = alloc_entry 0 0 ulong val |
| 1709 | if eq cnt 0 |
| 1710 | head = e |
| 1711 | else |
| 1712 | put g_enext tail e |
| 1713 | end |
| 1714 | tail = e |
| 1715 | cnt = add cnt 1 |
| 1716 | skip_ws |
| 1717 | pos = get g_st 2 |
| 1718 | if ge pos endp |
| 1719 | fail_msg t_earr |
| 1720 | ret neg long 1 |
| 1721 | end |
| 1722 | c = get g_in pos |
| 1723 | if eq c 44 |
| 1724 | put g_st 2 add pos 1 |
| 1725 | continue |
| 1726 | end |
| 1727 | if eq c 93 |
| 1728 | put g_st 2 add pos 1 |
| 1729 | break |
| 1730 | end |
| 1731 | fail_msg t_earr |
| 1732 | ret neg long 1 |
| 1733 | end |
| 1734 | flatten_arr nd_id head cnt |
| 1735 | ret long nd_id |
| 1736 | end |
| 1737 | |
| 1738 | fn parse_value depth ulong to long |
| 1739 | if gt depth 512 |
| 1740 | fail_msg t_edeep |
| 1741 | ret neg long 1 |
| 1742 | end |
| 1743 | skip_ws |
| 1744 | pos = get g_st 2 |
| 1745 | endp = get g_st 1 |
| 1746 | if ge pos endp |
| 1747 | fail_msg t_eval |
| 1748 | ret neg long 1 |
| 1749 | end |
| 1750 | c = get g_in pos |
| 1751 | if eq c 123 |
| 1752 | ret parse_obj depth |
| 1753 | end |
| 1754 | if eq c 91 |
| 1755 | ret parse_arr depth |
| 1756 | end |
| 1757 | if eq c 34 |
| 1758 | so = parse_string |
| 1759 | if lt so 0 |
| 1760 | ret neg long 1 |
| 1761 | end |
| 1762 | nd_id = alloc_node 5 |
| 1763 | put g_na nd_id ulong so |
| 1764 | put g_nb nd_id get g_fmeta 7 |
| 1765 | ret long nd_id |
| 1766 | end |
| 1767 | if match_lit t_null |
| 1768 | ret long alloc_node 0 |
| 1769 | end |
| 1770 | if match_lit t_true |
| 1771 | ret long alloc_node 1 |
| 1772 | end |
| 1773 | if match_lit t_false |
| 1774 | ret long alloc_node 2 |
| 1775 | end |
| 1776 | if match_lit t_nan |
| 1777 | nd_id = alloc_node 4 |
| 1778 | put g_na nd_id 0x7ff8000000000000 |
| 1779 | ret long nd_id |
| 1780 | end |
| 1781 | if match_lit t_inf |
| 1782 | nd_id = alloc_node 4 |
| 1783 | put g_na nd_id 0x7ff0000000000000 |
| 1784 | ret long nd_id |
| 1785 | end |
| 1786 | if or eq c 45 is_dig c |
| 1787 | ret parse_number |
| 1788 | end |
| 1789 | fail_msg t_eval |
| 1790 | ret neg long 1 |
| 1791 | end |
| 1792 | |
| 1793 | note ========= Emitter ========= |
| 1794 | fn out_u4hex u ulong to void |
| 1795 | out_c 92 |
| 1796 | out_c 117 |
| 1797 | out_c get t_hex and shr u 12 15 |
| 1798 | out_c get t_hex and shr u 8 15 |
| 1799 | out_c get t_hex and shr u 4 15 |
| 1800 | out_c get t_hex and u 15 |
| 1801 | end |
| 1802 | |
| 1803 | fn emit_wtf8 off ulong i ulong to ulong |
| 1804 | cu = ulong get g_str add off i |
| 1805 | cp = ulong 0 |
| 1806 | step = ulong 1 |
| 1807 | if lt cu 128 |
| 1808 | cp = cu |
| 1809 | else |
| 1810 | if eq and cu 224 192 |
| 1811 | cp = or shl and cu 31 6 and ulong get g_str add off add i 1 63 |
| 1812 | step = 2 |
| 1813 | else |
| 1814 | if eq and cu 240 224 |
| 1815 | b1 = and ulong get g_str add off add i 1 63 |
| 1816 | b2 = and ulong get g_str add off add i 2 63 |
| 1817 | cp = or shl and cu 15 12 or shl b1 6 b2 |
| 1818 | step = 3 |
| 1819 | else |
| 1820 | b1 = and ulong get g_str add off add i 1 63 |
| 1821 | b2 = and ulong get g_str add off add i 2 63 |
| 1822 | b3 = and ulong get g_str add off add i 3 63 |
| 1823 | cp = or shl and cu 7 18 or shl b1 12 or shl b2 6 b3 |
| 1824 | step = 4 |
| 1825 | end |
| 1826 | end |
| 1827 | end |
| 1828 | if ge cp 65536 |
| 1829 | u = sub cp 65536 |
| 1830 | out_u4hex or 55296 shr u 10 |
| 1831 | out_u4hex or 56320 and u 1023 |
| 1832 | else |
| 1833 | out_u4hex cp |
| 1834 | end |
| 1835 | ret add i step |
| 1836 | end |
| 1837 | |
| 1838 | fn emit_str off ulong n ulong ascii ulong to void |
| 1839 | out_c 34 |
| 1840 | i = ulong 0 |
| 1841 | while lt i n bound 16777216 |
| 1842 | c = get g_str add off i |
| 1843 | if eq c 34 |
| 1844 | out_c 92 |
| 1845 | out_c 34 |
| 1846 | i = add i 1 |
| 1847 | continue |
| 1848 | end |
| 1849 | if eq c 92 |
| 1850 | out_c 92 |
| 1851 | out_c 92 |
| 1852 | i = add i 1 |
| 1853 | continue |
| 1854 | end |
| 1855 | if eq c 10 |
| 1856 | out_c 92 |
| 1857 | out_c 110 |
| 1858 | i = add i 1 |
| 1859 | continue |
| 1860 | end |
| 1861 | if eq c 13 |
| 1862 | out_c 92 |
| 1863 | out_c 114 |
| 1864 | i = add i 1 |
| 1865 | continue |
| 1866 | end |
| 1867 | if eq c 9 |
| 1868 | out_c 92 |
| 1869 | out_c 116 |
| 1870 | i = add i 1 |
| 1871 | continue |
| 1872 | end |
| 1873 | if eq c 8 |
| 1874 | out_c 92 |
| 1875 | out_c 98 |
| 1876 | i = add i 1 |
| 1877 | continue |
| 1878 | end |
| 1879 | if eq c 12 |
| 1880 | out_c 92 |
| 1881 | out_c 102 |
| 1882 | i = add i 1 |
| 1883 | continue |
| 1884 | end |
| 1885 | if lt c 32 |
| 1886 | out_u4hex ulong c |
| 1887 | i = add i 1 |
| 1888 | continue |
| 1889 | end |
| 1890 | if or lt c 127 eq ascii 0 |
| 1891 | out_c c |
| 1892 | i = add i 1 |
| 1893 | continue |
| 1894 | end |
| 1895 | i = emit_wtf8 off i |
| 1896 | end |
| 1897 | out_c 34 |
| 1898 | end |
| 1899 | |
| 1900 | fn emit_nl indent ulong level ulong to void |
| 1901 | out_c 10 |
| 1902 | sp = mul indent level |
| 1903 | for i ulong sp bound 4096 |
| 1904 | out_c 32 |
| 1905 | end |
| 1906 | end |
| 1907 | |
| 1908 | fn emit_node nd_id ulong indent ulong ascii ulong level ulong to void |
| 1909 | ty = get g_nt nd_id |
| 1910 | if eq ty 0 |
| 1911 | out_txt t_null |
| 1912 | ret |
| 1913 | end |
| 1914 | if eq ty 1 |
| 1915 | out_txt t_true |
| 1916 | ret |
| 1917 | end |
| 1918 | if eq ty 2 |
| 1919 | out_txt t_false |
| 1920 | ret |
| 1921 | end |
| 1922 | if eq ty 3 |
| 1923 | out_str_slice get g_na nd_id get g_nb nd_id |
| 1924 | ret |
| 1925 | end |
| 1926 | if eq ty 4 |
| 1927 | out_f64_bits get g_na nd_id |
| 1928 | ret |
| 1929 | end |
| 1930 | if eq ty 5 |
| 1931 | emit_str get g_na nd_id get g_nb nd_id ascii |
| 1932 | ret |
| 1933 | end |
| 1934 | if eq ty 6 |
| 1935 | n = get g_nb nd_id |
| 1936 | if eq n 0 |
| 1937 | out_c 91 |
| 1938 | out_c 93 |
| 1939 | ret |
| 1940 | end |
| 1941 | base = get g_na nd_id |
| 1942 | out_c 91 |
| 1943 | for i ulong n bound 16777216 |
| 1944 | if gt i 0 |
| 1945 | out_c 44 |
| 1946 | end |
| 1947 | if gt indent 0 |
| 1948 | emit_nl indent add level 1 |
| 1949 | end |
| 1950 | e = get g_ord add base i |
| 1951 | emit_node get g_eval e indent ascii add level 1 |
| 1952 | end |
| 1953 | if gt indent 0 |
| 1954 | emit_nl indent level |
| 1955 | end |
| 1956 | out_c 93 |
| 1957 | ret |
| 1958 | end |
| 1959 | n = get g_nb nd_id |
| 1960 | if eq n 0 |
| 1961 | out_c 123 |
| 1962 | out_c 125 |
| 1963 | ret |
| 1964 | end |
| 1965 | base = get g_na nd_id |
| 1966 | out_c 123 |
| 1967 | for i ulong n bound 16777216 |
| 1968 | if gt i 0 |
| 1969 | out_c 44 |
| 1970 | end |
| 1971 | if gt indent 0 |
| 1972 | emit_nl indent add level 1 |
| 1973 | end |
| 1974 | e = get g_ord add base i |
| 1975 | emit_str get g_ekoff e get g_eklen e ascii |
| 1976 | out_c 58 |
| 1977 | if gt indent 0 |
| 1978 | out_c 32 |
| 1979 | end |
| 1980 | emit_node get g_eval e indent ascii add level 1 |
| 1981 | end |
| 1982 | if gt indent 0 |
| 1983 | emit_nl indent level |
| 1984 | end |
| 1985 | out_c 125 |
| 1986 | end |
| 1987 | |
| 1988 | note ========= I/O & CLI ========= |
| 1989 | fn read_all_stdin to void |
| 1990 | cap = ulong 65536 |
| 1991 | r = enlarge g_in cap |
| 1992 | if lt r 0 |
| 1993 | oom_die |
| 1994 | end |
| 1995 | tot = ulong 0 |
| 1996 | while true bound 16777216 |
| 1997 | if eq tot cap |
| 1998 | ncap = mul cap 2 |
| 1999 | r2 = enlarge g_in ncap |
| 2000 | if lt r2 0 |
| 2001 | oom_die |
| 2002 | end |
| 2003 | cap = ncap |
| 2004 | end |
| 2005 | k = read 0 g_in tot sub cap tot |
| 2006 | if eq k neg long 4 |
| 2007 | continue |
| 2008 | end |
| 2009 | if lt k 0 |
| 2010 | wr_fd_txt 2 t_eread |
| 2011 | exit 1 |
| 2012 | end |
| 2013 | if eq k 0 |
| 2014 | break |
| 2015 | end |
| 2016 | tot = add tot ulong k |
| 2017 | end |
| 2018 | put g_p 0 tot |
| 2019 | put g_p 1 cap |
| 2020 | end |
| 2021 | |
| 2022 | fn write_gout_all fd int n ulong to bool |
| 2023 | off = ulong 0 |
| 2024 | while lt off n bound 16777216 |
| 2025 | k = write fd g_out off sub n off |
| 2026 | if eq k neg long 4 |
| 2027 | continue |
| 2028 | end |
| 2029 | if le k 0 |
| 2030 | ret false |
| 2031 | end |
| 2032 | off = add off ulong k |
| 2033 | end |
| 2034 | ret true |
| 2035 | end |
| 2036 | |
| 2037 | fn write_gin_all fd int n ulong to bool |
| 2038 | off = ulong 0 |
| 2039 | while lt off n bound 16777216 |
| 2040 | k = write fd g_in off sub n off |
| 2041 | if eq k neg long 4 |
| 2042 | continue |
| 2043 | end |
| 2044 | if le k 0 |
| 2045 | ret false |
| 2046 | end |
| 2047 | off = add off ulong k |
| 2048 | end |
| 2049 | ret true |
| 2050 | end |
| 2051 | |
| 2052 | fn wr_arg2_err n ulong to void |
| 2053 | off = ulong 0 |
| 2054 | while lt off n bound 4096 |
| 2055 | k = write 2 g_arg2 off sub n off |
| 2056 | if eq k neg long 4 |
| 2057 | continue |
| 2058 | end |
| 2059 | if le k 0 |
| 2060 | ret |
| 2061 | end |
| 2062 | off = add off ulong k |
| 2063 | end |
| 2064 | end |
| 2065 | |
| 2066 | fn do_write_exclusive plen ulong to int |
| 2067 | read_all_stdin |
| 2068 | note O_WRONLY|O_CREAT|O_EXCL = 193, mode 0444 = 292 |
| 2069 | fd = fopen g_arg2 0 plen 193 292 |
| 2070 | if lt fd 0 |
| 2071 | eno = ulong neg fd |
| 2072 | if eq eno 17 |
| 2073 | wr_fd_txt 2 t_eexist |
| 2074 | else |
| 2075 | wr_fd_txt 2 t_eerrno1 |
| 2076 | wr_fd_u64 2 eno |
| 2077 | wr_fd_txt 2 t_eerrno2 |
| 2078 | end |
| 2079 | wr_arg2_err plen |
| 2080 | wr_fd_txt 2 t_eend |
| 2081 | ret 1 |
| 2082 | end |
| 2083 | if not write_gin_all int fd get g_p 0 |
| 2084 | cl = fclose int fd |
| 2085 | wr_fd_txt 2 t_ewrite |
| 2086 | ret 1 |
| 2087 | end |
| 2088 | if ne fsync int fd 0 |
| 2089 | cl = fclose int fd |
| 2090 | wr_fd_txt 2 t_efsync |
| 2091 | ret 1 |
| 2092 | end |
| 2093 | if ne fclose int fd 0 |
| 2094 | wr_fd_txt 2 t_eclose |
| 2095 | ret 1 |
| 2096 | end |
| 2097 | ret 0 |
| 2098 | end |
| 2099 | |
| 2100 | fn valid_doc start ulong endp ulong to bool |
| 2101 | put g_p 4 0 |
| 2102 | put g_p 6 0 |
| 2103 | put g_p 8 0 |
| 2104 | put g_p 10 0 |
| 2105 | put g_st 0 start |
| 2106 | put g_st 1 endp |
| 2107 | put g_st 2 start |
| 2108 | put g_st 3 0 |
| 2109 | put g_st 4 1 |
| 2110 | put g_st 5 0 |
| 2111 | v = parse_value 0 |
| 2112 | if or lt v 0 ne get g_st 5 0 |
| 2113 | ret false |
| 2114 | end |
| 2115 | skip_ws |
| 2116 | ret eq get g_st 2 endp |
| 2117 | end |
| 2118 | |
| 2119 | fn do_check_lines to int |
| 2120 | read_all_stdin |
| 2121 | n = get g_p 0 |
| 2122 | start = ulong 0 |
| 2123 | line = ulong 0 |
| 2124 | bad = int 0 |
| 2125 | while lt start n bound 16777216 |
| 2126 | endp = start |
| 2127 | while lt endp n bound 16777216 |
| 2128 | if eq get g_in endp 10 |
| 2129 | break |
| 2130 | end |
| 2131 | endp = add endp 1 |
| 2132 | end |
| 2133 | line = add line 1 |
| 2134 | doc_end = endp |
| 2135 | if lt endp n |
| 2136 | doc_end = add endp 1 |
| 2137 | end |
| 2138 | if not valid_doc start doc_end |
| 2139 | wr_fd_u64 1 line |
| 2140 | wr_fd_txt 1 t_nl |
| 2141 | bad = 1 |
| 2142 | end |
| 2143 | start = add endp 1 |
| 2144 | end |
| 2145 | ret bad |
| 2146 | end |
| 2147 | |
| 2148 | fn eq_arg1 s view uchar * n ulong to bool |
| 2149 | if ne n len s |
| 2150 | ret false |
| 2151 | end |
| 2152 | for i ulong len s bound 64 |
| 2153 | if ne get g_arg1 i get s i |
| 2154 | ret false |
| 2155 | end |
| 2156 | end |
| 2157 | ret true |
| 2158 | end |
| 2159 | |
| 2160 | fn emit_sha256_stdout to int |
| 2161 | sha256_out get g_p 2 |
| 2162 | hb = new uchar 65 0 |
| 2163 | for i ulong 32 |
| 2164 | b = ulong get g_sdig i |
| 2165 | put hb mul i 2 get t_hex shr b 4 |
| 2166 | put hb add mul i 2 1 get t_hex and b 15 |
| 2167 | end |
| 2168 | put hb 64 10 |
| 2169 | off = ulong 0 |
| 2170 | while lt off 65 bound 65 |
| 2171 | k = write 1 hb off sub 65 off |
| 2172 | if eq k neg long 4 |
| 2173 | continue |
| 2174 | end |
| 2175 | if le k 0 |
| 2176 | wr_fd_txt 2 t_ewrite |
| 2177 | ret 1 |
| 2178 | end |
| 2179 | off = add off ulong k |
| 2180 | end |
| 2181 | ret 0 |
| 2182 | end |
| 2183 | |
| 2184 | fn main to int |
| 2185 | ac = argcnt |
| 2186 | mode = ulong 0 |
| 2187 | if eq ac 3 |
| 2188 | n1 = argread 1 g_arg1 0 4095 |
| 2189 | n2 = argread 2 g_arg2 0 4095 |
| 2190 | if and ge n1 0 gt n2 0 |
| 2191 | if eq_arg1 t_f_wex ulong n1 |
| 2192 | ret do_write_exclusive ulong n2 |
| 2193 | end |
| 2194 | end |
| 2195 | wr_fd_txt 2 t_usage |
| 2196 | ret 2 |
| 2197 | end |
| 2198 | if eq ac 2 |
| 2199 | n1 = argread 1 g_arg1 0 4095 |
| 2200 | if lt n1 0 |
| 2201 | wr_fd_txt 2 t_usage |
| 2202 | ret 2 |
| 2203 | end |
| 2204 | un1 = ulong n1 |
| 2205 | if eq_arg1 t_f_check un1 |
| 2206 | mode = 1 |
| 2207 | else |
| 2208 | if eq_arg1 t_f_pretty un1 |
| 2209 | mode = 2 |
| 2210 | else |
| 2211 | if eq_arg1 t_f_sha un1 |
| 2212 | mode = 3 |
| 2213 | else |
| 2214 | if eq_arg1 t_f_lines un1 |
| 2215 | ret do_check_lines |
| 2216 | else |
| 2217 | wr_fd_txt 2 t_usage |
| 2218 | ret 2 |
| 2219 | end |
| 2220 | end |
| 2221 | end |
| 2222 | end |
| 2223 | else |
| 2224 | if ne ac 1 |
| 2225 | wr_fd_txt 2 t_usage |
| 2226 | ret 2 |
| 2227 | end |
| 2228 | end |
| 2229 | read_all_stdin |
| 2230 | n = get g_p 0 |
| 2231 | put g_st 0 0 |
| 2232 | put g_st 1 n |
| 2233 | put g_st 2 0 |
| 2234 | put g_st 3 0 |
| 2235 | put g_st 4 0 |
| 2236 | put g_st 5 0 |
| 2237 | root = parse_value 0 |
| 2238 | if lt root 0 |
| 2239 | ret 1 |
| 2240 | end |
| 2241 | skip_ws |
| 2242 | if ne get g_st 2 n |
| 2243 | fail_msg t_eextra |
| 2244 | end |
| 2245 | if eq mode 1 |
| 2246 | ret 0 |
| 2247 | end |
| 2248 | if and ne mode 2 ne get g_st 3 0 |
| 2249 | wr_fd_txt 2 t_esurr |
| 2250 | ret 1 |
| 2251 | end |
| 2252 | put g_p 2 0 |
| 2253 | if eq mode 2 |
| 2254 | emit_node ulong root 2 1 0 |
| 2255 | out_c 10 |
| 2256 | else |
| 2257 | emit_node ulong root 0 0 0 |
| 2258 | end |
| 2259 | if eq mode 3 |
| 2260 | ret emit_sha256_stdout |
| 2261 | end |
| 2262 | if not write_gout_all 1 get g_p 2 |
| 2263 | wr_fd_txt 2 t_ewrite |
| 2264 | ret 1 |
| 2265 | end |
| 2266 | ret 0 |
| 2267 | end |