aien-train
AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.
Brought by drakestapleton
aien-train / tools / omegallm / wscore / wscore.om
| 1 | om2 |
| 2 | note wscore: score `wprobe gen` output against a split (pure Omega port of DRA-187 EV/score.py). |
| 3 | note usage: wscore SPLIT.rec SPLIT.meta GEN.out |
| 4 | note SPLIT.rec: records PROMPT 0x1f ANSWER 0x00, or a G prompt alone (answer derived, see prec). |
| 5 | note SPLIT.meta: lines `idx role length kind`, role |
| 6 | note target|control, kind path|word, idx equal to the line number from 0. GEN.out: `G idx<TAB>text` |
| 7 | note lines, text escaped as \t \n \r \" \\ \xHH. A path row passes when its text, cut at the first |
| 8 | note `\nDone.` and stripped of Python whitespace, equals the answer as JSON (both parse) or as a |
| 9 | note string. A word row passes when the stripped first line equals the answer. |
| 10 | note Prints `target P/T L<n> p/t ...`, `control P/T ...`, then `P idx 0|1` per row. |
| 11 | note Exit: 0 ok, 2 usage, 3 open/read, 4 capacity, 5 malformed rec/meta/G key, 6 G rows do not |
| 12 | note cover the split exactly, 7 bad escape in GEN text, 8 JSON nesting over 9990 (inside the scorer's |
| 13 | note check_answer, CPython 3.12.3 raises RecursionError on some inputs from 9991 levels), 9 write failure. |
| 14 | |
| 15 | global ab uchar 4096 |
| 16 | global rb uchar 16777216 |
| 17 | global mb uchar 4194304 |
| 18 | global gb uchar 16777216 |
| 19 | global fz ulong 3 |
| 20 | global rn ulong 1 |
| 21 | global ra ulong 262144 |
| 22 | global rl ulong 262144 |
| 23 | global rr ulong 262144 |
| 24 | global rw ulong 262144 |
| 25 | global rg ulong 262144 |
| 26 | global rk ulong 262144 |
| 27 | global gs ulong 262144 |
| 28 | global gl ulong 262144 |
| 29 | global gq ulong 262144 |
| 30 | global pv ulong 262144 |
| 31 | global mp ulong 1 |
| 32 | global ca ulong 1048576 |
| 33 | global cb ulong 1048576 |
| 34 | global sr ulong 2 |
| 35 | global pw ulong 1 |
| 36 | global pp ulong 1 |
| 37 | global px ulong 1 |
| 38 | global pd ulong 1 |
| 39 | global pf ulong 1 |
| 40 | global nt ulong 2097152 |
| 41 | global n1 ulong 2097152 |
| 42 | global n2 ulong 2097152 |
| 43 | global nx ulong 2097152 |
| 44 | global nq ulong 2097152 |
| 45 | global nv long 2097152 |
| 46 | global nf ulong 2097152 |
| 47 | global bg ulong 1536 |
| 48 | global bgl ulong 4 |
| 49 | global fr ulong 4 |
| 50 | global nn ulong 1 |
| 51 | global pl ulong 2097152 |
| 52 | global pn ulong 1 |
| 53 | global js long 4 |
| 54 | global tp ulong 8192 |
| 55 | global tt ulong 8192 |
| 56 | global ob uchar 65536 |
| 57 | global obn ulong 1 |
| 58 | text ttarget "target" |
| 59 | text tcontr "control" |
| 60 | text tpath "path" |
| 61 | text tword "word" |
| 62 | text tnull "null" |
| 63 | text ttrue "true" |
| 64 | text tfalse "false" |
| 65 | text tnan "NaN" |
| 66 | text tinf "Infinity" |
| 67 | text tdone "\nDone." |
| 68 | text tgpre "Task: Write Omega that prints " |
| 69 | text tgpost " followed by a newline.\nAnswer:\n" |
| 70 | text tmsg "text msg \"" |
| 71 | |
| 72 | fn main to int |
| 73 | if ne argcnt 4 |
| 74 | exit 2 |
| 75 | end |
| 76 | ldfile 1 0 |
| 77 | ldfile 2 1 |
| 78 | ldfile 3 2 |
| 79 | prec |
| 80 | pmeta |
| 81 | pgen |
| 82 | tally |
| 83 | pline 0 |
| 84 | pline 1 |
| 85 | prows |
| 86 | oflush |
| 87 | ret 0 |
| 88 | end |
| 89 | |
| 90 | note ---- output ---- |
| 91 | fn oflush to void |
| 92 | n = get obn 0 |
| 93 | o = ulong 0 |
| 94 | while lt o n bound 65537 |
| 95 | k = write 1 ob o sub n o |
| 96 | if le k 0 |
| 97 | exit 9 |
| 98 | end |
| 99 | o = add o ulong k |
| 100 | end |
| 101 | put obn 0 0 |
| 102 | end |
| 103 | |
| 104 | fn oput c uchar to void |
| 105 | n = get obn 0 |
| 106 | if ge n 65536 |
| 107 | oflush |
| 108 | n = ulong 0 |
| 109 | end |
| 110 | put ob n c |
| 111 | put obn 0 add n 1 |
| 112 | end |
| 113 | |
| 114 | fn pdec v ulong to void |
| 115 | d = new uchar 24 0 |
| 116 | k = ulong 0 |
| 117 | x = v |
| 118 | c = true |
| 119 | while c bound 25 |
| 120 | put d k uchar add 48 rem x 10 |
| 121 | k = add k 1 |
| 122 | x = div x 10 |
| 123 | if eq x 0 |
| 124 | c = false |
| 125 | end |
| 126 | end |
| 127 | while gt k 0 bound 25 |
| 128 | k = sub k 1 |
| 129 | oput get d k |
| 130 | end |
| 131 | end |
| 132 | |
| 133 | fn pstr t view uchar * to void |
| 134 | for i ulong len t |
| 135 | oput get t i |
| 136 | end |
| 137 | end |
| 138 | |
| 139 | fn pline r ulong to void |
| 140 | if eq r 0 |
| 141 | pstr ttarget |
| 142 | else |
| 143 | pstr tcontr |
| 144 | end |
| 145 | b = mul r 4096 |
| 146 | p = ulong 0 |
| 147 | t = ulong 0 |
| 148 | for l ulong 4096 |
| 149 | p = add p get tp add b l |
| 150 | t = add t get tt add b l |
| 151 | end |
| 152 | oput 32 |
| 153 | pdec p |
| 154 | oput 47 |
| 155 | pdec t |
| 156 | oput 32 |
| 157 | oput 32 |
| 158 | pbylen b |
| 159 | oput 10 |
| 160 | end |
| 161 | |
| 162 | fn pbylen b ulong to void |
| 163 | c = true |
| 164 | for l ulong 4096 |
| 165 | x = add b l |
| 166 | if gt get tt x 0 |
| 167 | if not c |
| 168 | oput 32 |
| 169 | end |
| 170 | c = false |
| 171 | oput 76 |
| 172 | pdec l |
| 173 | oput 32 |
| 174 | pdec get tp x |
| 175 | oput 47 |
| 176 | pdec get tt x |
| 177 | end |
| 178 | end |
| 179 | end |
| 180 | |
| 181 | fn prows to void |
| 182 | nr = get rn 0 |
| 183 | i = ulong 0 |
| 184 | while lt i nr bound 999999 |
| 185 | oput 80 |
| 186 | oput 32 |
| 187 | pdec i |
| 188 | oput 32 |
| 189 | pdec get pv i |
| 190 | oput 10 |
| 191 | i = add i 1 |
| 192 | end |
| 193 | end |
| 194 | |
| 195 | note ---- input files ---- |
| 196 | fn capof w ulong to ulong |
| 197 | if eq w 0 |
| 198 | ret ulong 16777216 |
| 199 | end |
| 200 | if eq w 1 |
| 201 | ret ulong 4194304 |
| 202 | end |
| 203 | ret ulong 16777216 |
| 204 | end |
| 205 | |
| 206 | fn rdbuf w ulong fd int off ulong to long |
| 207 | if eq w 0 |
| 208 | ret read fd rb off sub 16777216 off |
| 209 | end |
| 210 | if eq w 1 |
| 211 | ret read fd mb off sub 4194304 off |
| 212 | end |
| 213 | ret read fd gb off sub 16777216 off |
| 214 | end |
| 215 | |
| 216 | fn ldfile ai ulong w ulong to void |
| 217 | k = argread ai ab 0 4096 |
| 218 | if lt k 0 |
| 219 | exit 3 |
| 220 | end |
| 221 | if ge k 4096 |
| 222 | exit 3 |
| 223 | end |
| 224 | fd = int fopen ab 0 ulong k 0 0 |
| 225 | if lt fd 0 |
| 226 | exit 3 |
| 227 | end |
| 228 | cap = capof w |
| 229 | off = ulong 0 |
| 230 | go = true |
| 231 | while go bound 999999 |
| 232 | if ge off cap |
| 233 | exit 4 |
| 234 | end |
| 235 | r = rdbuf w fd off |
| 236 | if lt r 0 |
| 237 | exit 3 |
| 238 | end |
| 239 | if eq r 0 |
| 240 | go = false |
| 241 | else |
| 242 | off = add off ulong r |
| 243 | end |
| 244 | end |
| 245 | x = fclose fd |
| 246 | put fz w off |
| 247 | end |
| 248 | |
| 249 | note SPLIT.rec: record i ends at its 0x00; its answer is the bytes after the first 0x1f in it. |
| 250 | note A record without 0x1f is a G prompt (make_splits.py gsplit, wsplit): the answer is |
| 251 | note `text msg "WORD\n"` with WORD cut from `Task: Write Omega that prints WORD followed by a |
| 252 | note newline.\nAnswer:\n`; ra/rl then hold WORD and rw is 1. Any other record is malformed. |
| 253 | fn prec to void |
| 254 | n = get fz 0 |
| 255 | p = ulong 0 |
| 256 | i = ulong 0 |
| 257 | while lt p n bound 999999 |
| 258 | e = recsep p n 0 |
| 259 | if ge e n |
| 260 | exit 5 |
| 261 | end |
| 262 | if ge i 262144 |
| 263 | exit 4 |
| 264 | end |
| 265 | q = recsep p e 31 |
| 266 | if lt q e |
| 267 | a = add q 1 |
| 268 | put ra i a |
| 269 | put rl i sub e a |
| 270 | put rw i 0 |
| 271 | else |
| 272 | gword i p e |
| 273 | end |
| 274 | i = add i 1 |
| 275 | p = add e 1 |
| 276 | end |
| 277 | put rn 0 i |
| 278 | end |
| 279 | |
| 280 | fn rbeq p ulong t view uchar * to bool |
| 281 | for k ulong len t |
| 282 | if ne get rb add p k get t k |
| 283 | ret false |
| 284 | end |
| 285 | end |
| 286 | ret true |
| 287 | end |
| 288 | |
| 289 | fn gword i ulong p ulong e ulong to void |
| 290 | a = add p len tgpre |
| 291 | b = sub e len tgpost |
| 292 | if or lt e add a len tgpost ge a b |
| 293 | exit 5 |
| 294 | end |
| 295 | if or not rbeq p tgpre not rbeq b tgpost |
| 296 | exit 5 |
| 297 | end |
| 298 | put ra i a |
| 299 | put rl i sub b a |
| 300 | put rw i 1 |
| 301 | end |
| 302 | |
| 303 | fn recsep p ulong n ulong b uchar to ulong |
| 304 | q = p |
| 305 | while lt q n bound 999999 |
| 306 | if ge sub q p 999990 |
| 307 | exit 4 |
| 308 | end |
| 309 | if eq get rb q b |
| 310 | ret q |
| 311 | end |
| 312 | q = add q 1 |
| 313 | end |
| 314 | ret n |
| 315 | end |
| 316 | |
| 317 | note SPLIT.meta |
| 318 | fn pmeta to void |
| 319 | n = get fz 1 |
| 320 | nr = get rn 0 |
| 321 | put mp 0 0 |
| 322 | i = ulong 0 |
| 323 | while lt get mp 0 n bound 999999 |
| 324 | if ge i nr |
| 325 | exit 5 |
| 326 | end |
| 327 | if ne mnum i |
| 328 | exit 5 |
| 329 | end |
| 330 | msp |
| 331 | put rr i mrole |
| 332 | msp |
| 333 | x = mnum |
| 334 | if ge x 4096 |
| 335 | exit 5 |
| 336 | end |
| 337 | put rg i x |
| 338 | msp |
| 339 | put rk i mkind |
| 340 | meol |
| 341 | i = add i 1 |
| 342 | end |
| 343 | if ne i nr |
| 344 | exit 5 |
| 345 | end |
| 346 | end |
| 347 | |
| 348 | fn mnum to ulong |
| 349 | p = get mp 0 |
| 350 | n = get fz 1 |
| 351 | v = ulong 0 |
| 352 | s = p |
| 353 | while lt p n bound 999999 |
| 354 | if ge sub p s 999990 |
| 355 | exit 5 |
| 356 | end |
| 357 | c = ulong get mb p |
| 358 | if or lt c 48 gt c 57 |
| 359 | break |
| 360 | end |
| 361 | if lt v 100000000000 |
| 362 | v = add mul v 10 sub c 48 |
| 363 | end |
| 364 | p = add p 1 |
| 365 | end |
| 366 | if eq p s |
| 367 | exit 5 |
| 368 | end |
| 369 | put mp 0 p |
| 370 | ret v |
| 371 | end |
| 372 | |
| 373 | fn msp to void |
| 374 | p = get mp 0 |
| 375 | if ge p get fz 1 |
| 376 | exit 5 |
| 377 | end |
| 378 | if ne get mb p 32 |
| 379 | exit 5 |
| 380 | end |
| 381 | put mp 0 add p 1 |
| 382 | end |
| 383 | |
| 384 | fn meol to void |
| 385 | p = get mp 0 |
| 386 | if lt p get fz 1 |
| 387 | if ne get mb p 10 |
| 388 | exit 5 |
| 389 | end |
| 390 | put mp 0 add p 1 |
| 391 | end |
| 392 | end |
| 393 | |
| 394 | fn mtok to ulong |
| 395 | p = get mp 0 |
| 396 | n = get fz 1 |
| 397 | while lt p n bound 999999 |
| 398 | if ge sub p get mp 0 999990 |
| 399 | exit 5 |
| 400 | end |
| 401 | c = get mb p |
| 402 | if or eq c 32 eq c 10 |
| 403 | break |
| 404 | end |
| 405 | p = add p 1 |
| 406 | end |
| 407 | ret p |
| 408 | end |
| 409 | |
| 410 | fn mteq s ulong e ulong t view uchar * to bool |
| 411 | if ne sub e s len t |
| 412 | ret false |
| 413 | end |
| 414 | for i ulong len t |
| 415 | if ne get mb add s i get t i |
| 416 | ret false |
| 417 | end |
| 418 | end |
| 419 | ret true |
| 420 | end |
| 421 | |
| 422 | fn mrole to ulong |
| 423 | s = get mp 0 |
| 424 | e = mtok |
| 425 | put mp 0 e |
| 426 | if mteq s e ttarget |
| 427 | ret ulong 0 |
| 428 | end |
| 429 | if mteq s e tcontr |
| 430 | ret ulong 1 |
| 431 | end |
| 432 | exit 5 |
| 433 | end |
| 434 | |
| 435 | fn mkind to ulong |
| 436 | s = get mp 0 |
| 437 | e = mtok |
| 438 | put mp 0 e |
| 439 | if mteq s e tpath |
| 440 | ret ulong 0 |
| 441 | end |
| 442 | if mteq s e tword |
| 443 | ret ulong 1 |
| 444 | end |
| 445 | exit 5 |
| 446 | end |
| 447 | |
| 448 | note GEN.out: lines end at \n, \r\n or \r (Python universal newlines); `G ` lines carry rows |
| 449 | fn pgen to void |
| 450 | n = get fz 2 |
| 451 | p = ulong 0 |
| 452 | c = ulong 0 |
| 453 | ln = ulong 0 |
| 454 | while lt p n bound 999999 |
| 455 | ln = add ln 1 |
| 456 | if ge ln 999990 |
| 457 | exit 4 |
| 458 | end |
| 459 | e = lnend p n |
| 460 | if isg p e |
| 461 | c = add c gline p e |
| 462 | end |
| 463 | p = lnnext e n |
| 464 | end |
| 465 | if ne c get rn 0 |
| 466 | exit 6 |
| 467 | end |
| 468 | end |
| 469 | |
| 470 | fn lnend p ulong n ulong to ulong |
| 471 | q = p |
| 472 | while lt q n bound 999999 |
| 473 | if ge sub q p 999990 |
| 474 | exit 4 |
| 475 | end |
| 476 | c = get gb q |
| 477 | if or eq c 10 eq c 13 |
| 478 | ret q |
| 479 | end |
| 480 | q = add q 1 |
| 481 | end |
| 482 | ret n |
| 483 | end |
| 484 | |
| 485 | fn lnnext e ulong n ulong to ulong |
| 486 | if ge e n |
| 487 | ret n |
| 488 | end |
| 489 | if eq get gb e 13 |
| 490 | q = add e 1 |
| 491 | if lt q n |
| 492 | if eq get gb q 10 |
| 493 | ret add e 2 |
| 494 | end |
| 495 | end |
| 496 | end |
| 497 | ret add e 1 |
| 498 | end |
| 499 | |
| 500 | fn isg p ulong e ulong to bool |
| 501 | if ge add p 1 e |
| 502 | ret false |
| 503 | end |
| 504 | ret and eq get gb p 71 eq get gb add p 1 32 |
| 505 | end |
| 506 | |
| 507 | note a G line: decimal key, TAB, text; returns 1 when the key is new (later lines replace earlier) |
| 508 | fn gline p ulong e ulong to ulong |
| 509 | q = add p 2 |
| 510 | v = ulong 0 |
| 511 | s = q |
| 512 | while lt q e bound 999999 |
| 513 | c = ulong get gb q |
| 514 | if eq c 9 |
| 515 | break |
| 516 | end |
| 517 | if or lt c 48 gt c 57 |
| 518 | exit 5 |
| 519 | end |
| 520 | if lt v 100000000000 |
| 521 | v = add mul v 10 sub c 48 |
| 522 | end |
| 523 | q = add q 1 |
| 524 | end |
| 525 | if eq q s |
| 526 | exit 5 |
| 527 | end |
| 528 | if ge v get rn 0 |
| 529 | exit 6 |
| 530 | end |
| 531 | t = q |
| 532 | if lt q e |
| 533 | t = add q 1 |
| 534 | end |
| 535 | gchk t e |
| 536 | put gs v t |
| 537 | put gl v sub e t |
| 538 | if eq get gq v 1 |
| 539 | ret ulong 0 |
| 540 | end |
| 541 | put gq v 1 |
| 542 | ret ulong 1 |
| 543 | end |
| 544 | |
| 545 | note ---- decoding to code points: w 0 is GEN text into ca, w 1 is the answer into cb ---- |
| 546 | fn bat w ulong p ulong to ulong |
| 547 | if eq w 0 |
| 548 | ret ulong get gb p |
| 549 | end |
| 550 | ret ulong get rb p |
| 551 | end |
| 552 | |
| 553 | fn cput w ulong i ulong v ulong to void |
| 554 | if ge i 999990 |
| 555 | exit 4 |
| 556 | end |
| 557 | if eq w 0 |
| 558 | put ca i v |
| 559 | else |
| 560 | put cb i v |
| 561 | end |
| 562 | end |
| 563 | |
| 564 | fn u8n b ulong to ulong |
| 565 | if and ge b 194 le b 223 |
| 566 | ret ulong 1 |
| 567 | end |
| 568 | if and ge b 224 le b 239 |
| 569 | ret ulong 2 |
| 570 | end |
| 571 | if and ge b 240 le b 244 |
| 572 | ret ulong 3 |
| 573 | end |
| 574 | ret ulong 0 |
| 575 | end |
| 576 | |
| 577 | fn u8ok b ulong i ulong c ulong to bool |
| 578 | lo = ulong 128 |
| 579 | hi = ulong 191 |
| 580 | if eq i 0 |
| 581 | if eq b 224 |
| 582 | lo = ulong 160 |
| 583 | end |
| 584 | if eq b 240 |
| 585 | lo = ulong 144 |
| 586 | end |
| 587 | if eq b 237 |
| 588 | hi = ulong 159 |
| 589 | end |
| 590 | if eq b 244 |
| 591 | hi = ulong 143 |
| 592 | end |
| 593 | end |
| 594 | ret and ge c lo le c hi |
| 595 | end |
| 596 | |
| 597 | note one UTF-8 character at p, maximal ill-formed subparts become U+FFFD (Python errors=replace) |
| 598 | fn dutf w ulong p ulong e ulong o ulong to ulong |
| 599 | b = bat w p |
| 600 | if lt b 128 |
| 601 | cput w o b |
| 602 | ret add p 1 |
| 603 | end |
| 604 | k = u8n b |
| 605 | if eq k 0 |
| 606 | cput w o 65533 |
| 607 | ret add p 1 |
| 608 | end |
| 609 | v = and b shr 127 add k 1 |
| 610 | q = add p 1 |
| 611 | for i ulong k |
| 612 | if ge q e |
| 613 | cput w o 65533 |
| 614 | ret q |
| 615 | end |
| 616 | c = bat w q |
| 617 | if not u8ok b i c |
| 618 | cput w o 65533 |
| 619 | ret q |
| 620 | end |
| 621 | v = or shl v 6 and c 63 |
| 622 | q = add q 1 |
| 623 | end |
| 624 | cput w o v |
| 625 | ret q |
| 626 | end |
| 627 | |
| 628 | fn hexv c ulong to ulong |
| 629 | if and ge c 48 le c 57 |
| 630 | ret sub c 48 |
| 631 | end |
| 632 | if and ge c 97 le c 102 |
| 633 | ret sub c 87 |
| 634 | end |
| 635 | if and ge c 65 le c 70 |
| 636 | ret sub c 55 |
| 637 | end |
| 638 | ret ulong 16 |
| 639 | end |
| 640 | |
| 641 | fn gsimp c ulong to ulong |
| 642 | if eq c 116 |
| 643 | ret ulong 9 |
| 644 | end |
| 645 | if eq c 110 |
| 646 | ret ulong 10 |
| 647 | end |
| 648 | if eq c 114 |
| 649 | ret ulong 13 |
| 650 | end |
| 651 | if or eq c 34 eq c 92 |
| 652 | ret c |
| 653 | end |
| 654 | exit 7 |
| 655 | end |
| 656 | |
| 657 | note value of the escape at p (a backslash); exits 7 when it is not one score.py's unescape takes |
| 658 | fn escv p ulong e ulong to ulong |
| 659 | if ge add p 1 e |
| 660 | exit 7 |
| 661 | end |
| 662 | c = ulong get gb add p 1 |
| 663 | if ne c 120 |
| 664 | ret gsimp c |
| 665 | end |
| 666 | if ge add p 3 e |
| 667 | exit 7 |
| 668 | end |
| 669 | h = hexv ulong get gb add p 2 |
| 670 | l = hexv ulong get gb add p 3 |
| 671 | if or gt h 15 gt l 15 |
| 672 | exit 7 |
| 673 | end |
| 674 | ret add shl h 4 l |
| 675 | end |
| 676 | |
| 677 | fn esclen p ulong to ulong |
| 678 | if eq get gb add p 1 120 |
| 679 | ret ulong 4 |
| 680 | end |
| 681 | ret ulong 2 |
| 682 | end |
| 683 | |
| 684 | fn desc p ulong e ulong o ulong to ulong |
| 685 | cput 0 o escv p e |
| 686 | ret add p esclen p |
| 687 | end |
| 688 | |
| 689 | note score.py unescapes every G line as it reads it, so a bad escape fails even on a replaced line |
| 690 | fn gchk p ulong e ulong to void |
| 691 | q = p |
| 692 | while lt q e bound 999999 |
| 693 | if eq get gb q 92 |
| 694 | v = escv q e |
| 695 | q = add q esclen q |
| 696 | else |
| 697 | q = add q 1 |
| 698 | end |
| 699 | end |
| 700 | end |
| 701 | |
| 702 | fn dgen i ulong to ulong |
| 703 | p = get gs i |
| 704 | e = add p get gl i |
| 705 | o = ulong 0 |
| 706 | while lt p e bound 999999 |
| 707 | if eq get gb p 92 |
| 708 | p = desc p e o |
| 709 | else |
| 710 | p = dutf 0 p e o |
| 711 | end |
| 712 | o = add o 1 |
| 713 | end |
| 714 | ret o |
| 715 | end |
| 716 | |
| 717 | fn dans i ulong to ulong |
| 718 | p = get ra i |
| 719 | e = add p get rl i |
| 720 | o = ulong 0 |
| 721 | w = eq get rw i 1 |
| 722 | if w |
| 723 | if ne get rk i 1 |
| 724 | exit 5 |
| 725 | end |
| 726 | for k ulong len tmsg |
| 727 | cput 1 o ulong get tmsg k |
| 728 | o = add o 1 |
| 729 | end |
| 730 | end |
| 731 | while lt p e bound 999999 |
| 732 | p = dutf 1 p e o |
| 733 | o = add o 1 |
| 734 | end |
| 735 | if w |
| 736 | cput 1 o 92 |
| 737 | cput 1 add o 1 110 |
| 738 | cput 1 add o 2 34 |
| 739 | o = add o 3 |
| 740 | end |
| 741 | ret o |
| 742 | end |
| 743 | |
| 744 | note ---- text cuts on ca ---- |
| 745 | note Python str.isspace |
| 746 | fn isws c ulong to bool |
| 747 | if and ge c 9 le c 13 |
| 748 | ret true |
| 749 | end |
| 750 | if and ge c 28 le c 32 |
| 751 | ret true |
| 752 | end |
| 753 | if or eq c 133 eq c 160 |
| 754 | ret true |
| 755 | end |
| 756 | if or eq c 5760 and ge c 8192 le c 8202 |
| 757 | ret true |
| 758 | end |
| 759 | if or or eq c 8232 eq c 8233 or eq c 8239 eq c 8287 |
| 760 | ret true |
| 761 | end |
| 762 | ret eq c 12288 |
| 763 | end |
| 764 | |
| 765 | fn strip e ulong to void |
| 766 | s = ulong 0 |
| 767 | t = e |
| 768 | while lt s t bound 999999 |
| 769 | if not isws get ca s |
| 770 | break |
| 771 | end |
| 772 | s = add s 1 |
| 773 | end |
| 774 | while gt t s bound 999999 |
| 775 | if not isws get ca sub t 1 |
| 776 | break |
| 777 | end |
| 778 | t = sub t 1 |
| 779 | end |
| 780 | put sr 0 s |
| 781 | put sr 1 t |
| 782 | end |
| 783 | |
| 784 | fn fnl n ulong to ulong |
| 785 | i = ulong 0 |
| 786 | while lt i n bound 999999 |
| 787 | if eq get ca i 10 |
| 788 | ret i |
| 789 | end |
| 790 | i = add i 1 |
| 791 | end |
| 792 | ret n |
| 793 | end |
| 794 | |
| 795 | fn isdone i ulong to bool |
| 796 | for k ulong 6 |
| 797 | if ne get ca add i k ulong get tdone k |
| 798 | ret false |
| 799 | end |
| 800 | end |
| 801 | ret true |
| 802 | end |
| 803 | |
| 804 | fn fdone n ulong to ulong |
| 805 | i = ulong 0 |
| 806 | while le add i 6 n bound 999999 |
| 807 | if isdone i |
| 808 | ret i |
| 809 | end |
| 810 | i = add i 1 |
| 811 | end |
| 812 | ret n |
| 813 | end |
| 814 | |
| 815 | fn cmpab s ulong e ulong m ulong to bool |
| 816 | if ne sub e s m |
| 817 | ret false |
| 818 | end |
| 819 | k = ulong 0 |
| 820 | while lt k m bound 999999 |
| 821 | if ne get ca add s k get cb k |
| 822 | ret false |
| 823 | end |
| 824 | k = add k 1 |
| 825 | end |
| 826 | ret true |
| 827 | end |
| 828 | |
| 829 | note ---- JSON (Python json.loads) ---- |
| 830 | note node types: 0 null, 1 false, 2 true, 3 number, 4 string, 5 array, 6 object, 7 NaN, |
| 831 | note 8 Infinity, 9 -Infinity. Strings: n1 pool start, n2 count of code points. Numbers and bools: |
| 832 | note value = (pool digits n1, count n2, no leading or trailing zero) * 10^nv, nq 1 when negative, |
| 833 | note n2 0 for zero. Arrays and objects: n1 first child, n2 child count, nx next sibling; an object's |
| 834 | note children alternate key, value. |
| 835 | fn pc i ulong to ulong |
| 836 | if eq get pw 0 0 |
| 837 | ret get ca i |
| 838 | end |
| 839 | ret get cb i |
| 840 | end |
| 841 | |
| 842 | fn pat k ulong to ulong |
| 843 | p = add get pp 0 k |
| 844 | if lt p get px 0 |
| 845 | ret pc p |
| 846 | end |
| 847 | ret ulong 4294967295 |
| 848 | end |
| 849 | |
| 850 | fn pcur to ulong |
| 851 | ret pat 0 |
| 852 | end |
| 853 | |
| 854 | fn padv k ulong to void |
| 855 | put pp 0 add get pp 0 k |
| 856 | end |
| 857 | |
| 858 | fn jfail to ulong |
| 859 | put pf 0 1 |
| 860 | ret ulong 0 |
| 861 | end |
| 862 | |
| 863 | fn bad to bool |
| 864 | ret ne get pf 0 0 |
| 865 | end |
| 866 | |
| 867 | fn isdig c ulong to bool |
| 868 | ret and ge c 48 le c 57 |
| 869 | end |
| 870 | |
| 871 | fn nnew t ulong to ulong |
| 872 | i = get nn 0 |
| 873 | if ge i 2097152 |
| 874 | exit 4 |
| 875 | end |
| 876 | put nt i t |
| 877 | put n1 i 0 |
| 878 | put n2 i 0 |
| 879 | put nx i 0 |
| 880 | put nq i 0 |
| 881 | put nv i 0 |
| 882 | put nf i 0 |
| 883 | put nn 0 add i 1 |
| 884 | ret i |
| 885 | end |
| 886 | |
| 887 | fn ppush v ulong to void |
| 888 | i = get pn 0 |
| 889 | if ge i 2097152 |
| 890 | exit 4 |
| 891 | end |
| 892 | put pl i v |
| 893 | put pn 0 add i 1 |
| 894 | end |
| 895 | |
| 896 | fn jlink nd ulong last ulong v ulong to void |
| 897 | if eq get n2 nd 0 |
| 898 | put n1 nd v |
| 899 | else |
| 900 | put nx last v |
| 901 | end |
| 902 | put n2 nd add get n2 nd 1 |
| 903 | end |
| 904 | |
| 905 | fn jdeep to void |
| 906 | d = add get pd 0 1 |
| 907 | if gt d 9990 |
| 908 | exit 8 |
| 909 | end |
| 910 | put pd 0 d |
| 911 | end |
| 912 | |
| 913 | fn jup to void |
| 914 | put pd 0 sub get pd 0 1 |
| 915 | end |
| 916 | |
| 917 | fn jws to void |
| 918 | c = true |
| 919 | while c bound 999999 |
| 920 | x = pcur |
| 921 | if or or eq x 32 eq x 9 or eq x 10 eq x 13 |
| 922 | padv 1 |
| 923 | else |
| 924 | c = false |
| 925 | end |
| 926 | end |
| 927 | end |
| 928 | |
| 929 | fn jlit t view uchar * to bool |
| 930 | for i ulong len t |
| 931 | if ne pat i ulong get t i |
| 932 | ret false |
| 933 | end |
| 934 | end |
| 935 | padv len t |
| 936 | ret true |
| 937 | end |
| 938 | |
| 939 | fn jkw t view uchar * ty ulong to ulong |
| 940 | if not jlit t |
| 941 | ret jfail |
| 942 | end |
| 943 | nd = nnew ty |
| 944 | if eq ty 2 |
| 945 | put n1 nd get pn 0 |
| 946 | put n2 nd 1 |
| 947 | ppush 1 |
| 948 | end |
| 949 | ret nd |
| 950 | end |
| 951 | |
| 952 | fn jtop w ulong s ulong e ulong to ulong |
| 953 | put pw 0 w |
| 954 | put pp 0 s |
| 955 | put px 0 e |
| 956 | put pf 0 0 |
| 957 | put pd 0 0 |
| 958 | jws |
| 959 | v = jval |
| 960 | if bad |
| 961 | ret ulong 0 |
| 962 | end |
| 963 | jws |
| 964 | if ne get pp 0 e |
| 965 | ret jfail |
| 966 | end |
| 967 | ret v |
| 968 | end |
| 969 | |
| 970 | fn jval to ulong |
| 971 | c = pcur |
| 972 | if eq c 34 |
| 973 | ret jstr |
| 974 | end |
| 975 | if eq c 123 |
| 976 | ret jobj |
| 977 | end |
| 978 | if eq c 91 |
| 979 | ret jarr |
| 980 | end |
| 981 | if eq c 110 |
| 982 | ret jkw tnull 0 |
| 983 | end |
| 984 | if eq c 116 |
| 985 | ret jkw ttrue 2 |
| 986 | end |
| 987 | if eq c 102 |
| 988 | ret jkw tfalse 1 |
| 989 | end |
| 990 | if eq c 78 |
| 991 | ret jkw tnan 7 |
| 992 | end |
| 993 | if eq c 73 |
| 994 | ret jkw tinf 8 |
| 995 | end |
| 996 | if eq c 45 |
| 997 | if eq pat 1 73 |
| 998 | padv 1 |
| 999 | ret jkw tinf 9 |
| 1000 | end |
| 1001 | end |
| 1002 | ret jnum |
| 1003 | end |
| 1004 | |
| 1005 | fn jdigs to void |
| 1006 | c = true |
| 1007 | while c bound 999999 |
| 1008 | if isdig pcur |
| 1009 | padv 1 |
| 1010 | else |
| 1011 | c = false |
| 1012 | end |
| 1013 | end |
| 1014 | end |
| 1015 | |
| 1016 | note exponent after the number body; js 0 is 1 when present; saturates near 1e15 |
| 1017 | fn jexp to long |
| 1018 | put js 0 0 |
| 1019 | c = pcur |
| 1020 | if and ne c 101 ne c 69 |
| 1021 | ret long 0 |
| 1022 | end |
| 1023 | k = ulong 1 |
| 1024 | s = long 1 |
| 1025 | c2 = pat 1 |
| 1026 | if or eq c2 43 eq c2 45 |
| 1027 | if eq c2 45 |
| 1028 | s = long -1 |
| 1029 | end |
| 1030 | k = ulong 2 |
| 1031 | end |
| 1032 | if not isdig pat k |
| 1033 | ret long 0 |
| 1034 | end |
| 1035 | padv k |
| 1036 | put js 0 1 |
| 1037 | v = long 0 |
| 1038 | while isdig pcur bound 999999 |
| 1039 | if lt v 1000000000000000 |
| 1040 | v = add mul v 10 long sub pcur 48 |
| 1041 | end |
| 1042 | padv 1 |
| 1043 | end |
| 1044 | ret mul s v |
| 1045 | end |
| 1046 | |
| 1047 | fn jnum to ulong |
| 1048 | put js 1 0 |
| 1049 | if eq pcur 45 |
| 1050 | put js 1 1 |
| 1051 | padv 1 |
| 1052 | end |
| 1053 | if not isdig pcur |
| 1054 | ret jfail |
| 1055 | end |
| 1056 | i0 = get pp 0 |
| 1057 | if eq pcur 48 |
| 1058 | padv 1 |
| 1059 | else |
| 1060 | jdigs |
| 1061 | end |
| 1062 | i1 = get pp 0 |
| 1063 | f0 = i1 |
| 1064 | f1 = i1 |
| 1065 | if eq pcur 46 |
| 1066 | if isdig pat 1 |
| 1067 | padv 1 |
| 1068 | f0 = get pp 0 |
| 1069 | jdigs |
| 1070 | f1 = get pp 0 |
| 1071 | end |
| 1072 | end |
| 1073 | put js 2 jexp |
| 1074 | put js 3 get js 0 |
| 1075 | if ne f0 f1 |
| 1076 | put js 3 1 |
| 1077 | end |
| 1078 | if eq get js 3 0 |
| 1079 | if gt sub i1 i0 4300 |
| 1080 | ret jfail |
| 1081 | end |
| 1082 | end |
| 1083 | ret jmkn i0 i1 f0 f1 |
| 1084 | end |
| 1085 | |
| 1086 | fn jpush a ulong b ulong st ulong to void |
| 1087 | k = a |
| 1088 | while lt k b bound 999999 |
| 1089 | d = sub pc k 48 |
| 1090 | if or ne d 0 gt get pn 0 st |
| 1091 | ppush d |
| 1092 | end |
| 1093 | k = add k 1 |
| 1094 | end |
| 1095 | end |
| 1096 | |
| 1097 | fn jmkn i0 ulong i1 ulong f0 ulong f1 ulong to ulong |
| 1098 | nd = nnew 3 |
| 1099 | st = get pn 0 |
| 1100 | put n1 nd st |
| 1101 | jpush i0 i1 st |
| 1102 | jpush f0 f1 st |
| 1103 | t = ulong 0 |
| 1104 | e = get pn 0 |
| 1105 | while gt e st bound 999999 |
| 1106 | if ne get pl sub e 1 0 |
| 1107 | break |
| 1108 | end |
| 1109 | e = sub e 1 |
| 1110 | t = add t 1 |
| 1111 | end |
| 1112 | put pn 0 e |
| 1113 | put n2 nd sub e st |
| 1114 | put nf nd ulong get js 3 |
| 1115 | if gt e st |
| 1116 | put nq nd ulong get js 1 |
| 1117 | x = sub long t long sub f1 f0 |
| 1118 | put nv nd add get js 2 x |
| 1119 | end |
| 1120 | ret nd |
| 1121 | end |
| 1122 | |
| 1123 | fn jsimp c ulong to ulong |
| 1124 | if or or eq c 34 eq c 92 eq c 47 |
| 1125 | ret c |
| 1126 | end |
| 1127 | if eq c 98 |
| 1128 | ret ulong 8 |
| 1129 | end |
| 1130 | if eq c 102 |
| 1131 | ret ulong 12 |
| 1132 | end |
| 1133 | if eq c 110 |
| 1134 | ret ulong 10 |
| 1135 | end |
| 1136 | if eq c 114 |
| 1137 | ret ulong 13 |
| 1138 | end |
| 1139 | if eq c 116 |
| 1140 | ret ulong 9 |
| 1141 | end |
| 1142 | ret ulong 4294967295 |
| 1143 | end |
| 1144 | |
| 1145 | fn hex4 k ulong to ulong |
| 1146 | v = ulong 0 |
| 1147 | for i ulong 4 |
| 1148 | h = hexv pat add k i |
| 1149 | if gt h 15 |
| 1150 | ret ulong 65536 |
| 1151 | end |
| 1152 | v = add shl v 4 h |
| 1153 | end |
| 1154 | ret v |
| 1155 | end |
| 1156 | |
| 1157 | note one escape at pp (a backslash); surrogate pairs combine, lone surrogates stay |
| 1158 | fn jesc to bool |
| 1159 | c = pat 1 |
| 1160 | v = jsimp c |
| 1161 | if ne v 4294967295 |
| 1162 | ppush v |
| 1163 | padv 2 |
| 1164 | ret true |
| 1165 | end |
| 1166 | if ne c 117 |
| 1167 | ret false |
| 1168 | end |
| 1169 | u = hex4 2 |
| 1170 | if gt u 65535 |
| 1171 | ret false |
| 1172 | end |
| 1173 | padv 6 |
| 1174 | if and ge u 55296 le u 56319 |
| 1175 | if and eq pcur 92 eq pat 1 117 |
| 1176 | u2 = hex4 2 |
| 1177 | if gt u2 65535 |
| 1178 | ret false |
| 1179 | end |
| 1180 | if and ge u2 56320 le u2 57343 |
| 1181 | u = add add 65536 shl sub u 55296 10 sub u2 56320 |
| 1182 | padv 6 |
| 1183 | end |
| 1184 | end |
| 1185 | end |
| 1186 | ppush u |
| 1187 | ret true |
| 1188 | end |
| 1189 | |
| 1190 | fn jstr to ulong |
| 1191 | padv 1 |
| 1192 | nd = nnew 4 |
| 1193 | st = get pn 0 |
| 1194 | put n1 nd st |
| 1195 | go = true |
| 1196 | while go bound 999999 |
| 1197 | c = pcur |
| 1198 | if eq c 34 |
| 1199 | padv 1 |
| 1200 | go = false |
| 1201 | else |
| 1202 | if lt c 32 |
| 1203 | ret jfail |
| 1204 | end |
| 1205 | if eq c 4294967295 |
| 1206 | ret jfail |
| 1207 | end |
| 1208 | if eq c 92 |
| 1209 | if not jesc |
| 1210 | ret jfail |
| 1211 | end |
| 1212 | else |
| 1213 | ppush c |
| 1214 | padv 1 |
| 1215 | end |
| 1216 | end |
| 1217 | end |
| 1218 | put n2 nd sub get pn 0 st |
| 1219 | ret nd |
| 1220 | end |
| 1221 | |
| 1222 | fn jarr to ulong |
| 1223 | padv 1 |
| 1224 | nd = nnew 5 |
| 1225 | jdeep |
| 1226 | jws |
| 1227 | if eq pcur 93 |
| 1228 | padv 1 |
| 1229 | jup |
| 1230 | ret nd |
| 1231 | end |
| 1232 | last = nd |
| 1233 | go = true |
| 1234 | while go bound 999999 |
| 1235 | v = jval |
| 1236 | if bad |
| 1237 | ret ulong 0 |
| 1238 | end |
| 1239 | jlink nd last v |
| 1240 | last = v |
| 1241 | jws |
| 1242 | c = pcur |
| 1243 | if eq c 93 |
| 1244 | padv 1 |
| 1245 | go = false |
| 1246 | else |
| 1247 | if ne c 44 |
| 1248 | ret jfail |
| 1249 | end |
| 1250 | padv 1 |
| 1251 | jws |
| 1252 | end |
| 1253 | end |
| 1254 | jup |
| 1255 | ret nd |
| 1256 | end |
| 1257 | |
| 1258 | fn jobj to ulong |
| 1259 | padv 1 |
| 1260 | nd = nnew 6 |
| 1261 | jdeep |
| 1262 | jws |
| 1263 | if eq pcur 125 |
| 1264 | padv 1 |
| 1265 | jup |
| 1266 | ret nd |
| 1267 | end |
| 1268 | last = nd |
| 1269 | go = true |
| 1270 | while go bound 999999 |
| 1271 | if ne pcur 34 |
| 1272 | ret jfail |
| 1273 | end |
| 1274 | k = jstr |
| 1275 | if bad |
| 1276 | ret ulong 0 |
| 1277 | end |
| 1278 | jlink nd last k |
| 1279 | jws |
| 1280 | if ne pcur 58 |
| 1281 | ret jfail |
| 1282 | end |
| 1283 | padv 1 |
| 1284 | jws |
| 1285 | v = jval |
| 1286 | if bad |
| 1287 | ret ulong 0 |
| 1288 | end |
| 1289 | jlink nd k v |
| 1290 | last = v |
| 1291 | jws |
| 1292 | c = pcur |
| 1293 | if eq c 125 |
| 1294 | padv 1 |
| 1295 | go = false |
| 1296 | else |
| 1297 | if ne c 44 |
| 1298 | ret jfail |
| 1299 | end |
| 1300 | padv 1 |
| 1301 | jws |
| 1302 | end |
| 1303 | end |
| 1304 | jup |
| 1305 | ret nd |
| 1306 | end |
| 1307 | |
| 1308 | note ---- Python == on the parsed values: bool equals 0/1, ints compare exactly, a float equals |
| 1309 | note what its literal rounds to in binary64 (ties to even), -0 equals 0, NaN equals nothing at the |
| 1310 | note top level but equals NaN inside a list or dict (json shares one NaN object, and Python |
| 1311 | note containers compare identical items as equal), dicts compare as key sets with the last |
| 1312 | note duplicate key winning ---- |
| 1313 | fn pooleq x ulong y ulong k ulong to bool |
| 1314 | i = ulong 0 |
| 1315 | while lt i k bound 999999 |
| 1316 | if ne get pl add x i get pl add y i |
| 1317 | ret false |
| 1318 | end |
| 1319 | i = add i 1 |
| 1320 | end |
| 1321 | ret true |
| 1322 | end |
| 1323 | |
| 1324 | fn isnum t ulong to bool |
| 1325 | if and ge t 1 le t 3 |
| 1326 | ret true |
| 1327 | end |
| 1328 | ret or eq t 8 eq t 9 |
| 1329 | end |
| 1330 | |
| 1331 | fn isint a ulong to bool |
| 1332 | t = get nt a |
| 1333 | if or eq t 1 eq t 2 |
| 1334 | ret true |
| 1335 | end |
| 1336 | ret and eq t 3 eq get nf a 0 |
| 1337 | end |
| 1338 | |
| 1339 | fn nsign a ulong to ulong |
| 1340 | t = get nt a |
| 1341 | if eq t 9 |
| 1342 | ret ulong 1 |
| 1343 | end |
| 1344 | if eq t 3 |
| 1345 | ret get nq a |
| 1346 | end |
| 1347 | ret ulong 0 |
| 1348 | end |
| 1349 | |
| 1350 | note two exact decimals |
| 1351 | fn deq a ulong b ulong to bool |
| 1352 | k = get n2 a |
| 1353 | if ne k get n2 b |
| 1354 | ret false |
| 1355 | end |
| 1356 | if eq k 0 |
| 1357 | ret true |
| 1358 | end |
| 1359 | if ne get nq a get nq b |
| 1360 | ret false |
| 1361 | end |
| 1362 | if ne get nv a get nv b |
| 1363 | ret false |
| 1364 | end |
| 1365 | ret pooleq get n1 a get n1 b k |
| 1366 | end |
| 1367 | |
| 1368 | note bignums: slot d (0..3) is limbs bg[d*384 ..], 32 bits per limb, low limb first |
| 1369 | fn bclr d ulong to void |
| 1370 | put bgl d 0 |
| 1371 | end |
| 1372 | |
| 1373 | fn nrm d ulong m ulong to void |
| 1374 | b = mul d 384 |
| 1375 | n = m |
| 1376 | while gt n 0 bound 999999 |
| 1377 | if ne get bg add b sub n 1 0 |
| 1378 | break |
| 1379 | end |
| 1380 | n = sub n 1 |
| 1381 | end |
| 1382 | put bgl d n |
| 1383 | end |
| 1384 | |
| 1385 | fn bmuladd d ulong m ulong a ulong to void |
| 1386 | b = mul d 384 |
| 1387 | n = get bgl d |
| 1388 | c = a |
| 1389 | i = ulong 0 |
| 1390 | while lt i n bound 999999 |
| 1391 | t = add mul get bg add b i m c |
| 1392 | put bg add b i and t 4294967295 |
| 1393 | c = shr t 32 |
| 1394 | i = add i 1 |
| 1395 | end |
| 1396 | if ne c 0 |
| 1397 | if ge n 384 |
| 1398 | exit 4 |
| 1399 | end |
| 1400 | put bg add b n c |
| 1401 | put bgl d add n 1 |
| 1402 | end |
| 1403 | end |
| 1404 | |
| 1405 | fn bsv d ulong j ulong w ulong r ulong to ulong |
| 1406 | b = mul d 384 |
| 1407 | n = get bgl d |
| 1408 | hi = ulong 0 |
| 1409 | lo = ulong 0 |
| 1410 | if ge j w |
| 1411 | x = sub j w |
| 1412 | if lt x n |
| 1413 | hi = get bg add b x |
| 1414 | end |
| 1415 | end |
| 1416 | if gt j w |
| 1417 | x = sub sub j w 1 |
| 1418 | if lt x n |
| 1419 | lo = get bg add b x |
| 1420 | end |
| 1421 | end |
| 1422 | v = shl hi r |
| 1423 | if gt r 0 |
| 1424 | v = or v shr lo sub 32 r |
| 1425 | end |
| 1426 | ret and v 4294967295 |
| 1427 | end |
| 1428 | |
| 1429 | fn bshl d ulong k ulong to void |
| 1430 | n = get bgl d |
| 1431 | if eq n 0 |
| 1432 | ret |
| 1433 | end |
| 1434 | b = mul d 384 |
| 1435 | w = shr k 5 |
| 1436 | r = and k 31 |
| 1437 | m = add n add w 1 |
| 1438 | if gt m 384 |
| 1439 | exit 4 |
| 1440 | end |
| 1441 | j = m |
| 1442 | while gt j 0 bound 999999 |
| 1443 | j = sub j 1 |
| 1444 | put bg add b j bsv d j w r |
| 1445 | end |
| 1446 | nrm d m |
| 1447 | end |
| 1448 | |
| 1449 | fn bshr1 d ulong to void |
| 1450 | b = mul d 384 |
| 1451 | n = get bgl d |
| 1452 | i = ulong 0 |
| 1453 | while lt i n bound 999999 |
| 1454 | v = shr get bg add b i 1 |
| 1455 | if lt add i 1 n |
| 1456 | v = or v and shl get bg add b add i 1 31 4294967295 |
| 1457 | end |
| 1458 | put bg add b i v |
| 1459 | i = add i 1 |
| 1460 | end |
| 1461 | nrm d n |
| 1462 | end |
| 1463 | |
| 1464 | note 0 when slot x is less than slot y, 1 equal, 2 greater |
| 1465 | fn bcmp x ulong y ulong to ulong |
| 1466 | na = get bgl x |
| 1467 | nb = get bgl y |
| 1468 | if ne na nb |
| 1469 | if gt na nb |
| 1470 | ret ulong 2 |
| 1471 | end |
| 1472 | ret ulong 0 |
| 1473 | end |
| 1474 | i = na |
| 1475 | while gt i 0 bound 999999 |
| 1476 | i = sub i 1 |
| 1477 | u = get bg add mul x 384 i |
| 1478 | v = get bg add mul y 384 i |
| 1479 | if ne u v |
| 1480 | if gt u v |
| 1481 | ret ulong 2 |
| 1482 | end |
| 1483 | ret ulong 0 |
| 1484 | end |
| 1485 | end |
| 1486 | ret ulong 1 |
| 1487 | end |
| 1488 | |
| 1489 | note slot x minus slot y, x not less than y |
| 1490 | fn bsub x ulong y ulong to void |
| 1491 | na = get bgl x |
| 1492 | nb = get bgl y |
| 1493 | bo = ulong 0 |
| 1494 | i = ulong 0 |
| 1495 | while lt i na bound 999999 |
| 1496 | u = get bg add mul x 384 i |
| 1497 | v = bo |
| 1498 | if lt i nb |
| 1499 | v = add v get bg add mul y 384 i |
| 1500 | end |
| 1501 | if ge u v |
| 1502 | put bg add mul x 384 i sub u v |
| 1503 | bo = ulong 0 |
| 1504 | else |
| 1505 | put bg add mul x 384 i sub add u 4294967296 v |
| 1506 | bo = ulong 1 |
| 1507 | end |
| 1508 | i = add i 1 |
| 1509 | end |
| 1510 | nrm x na |
| 1511 | end |
| 1512 | |
| 1513 | fn bcopy d ulong s ulong to void |
| 1514 | n = get bgl s |
| 1515 | for i ulong n |
| 1516 | put bg add mul d 384 i get bg add mul s 384 i |
| 1517 | end |
| 1518 | put bgl d n |
| 1519 | end |
| 1520 | |
| 1521 | fn bbits d ulong to ulong |
| 1522 | n = get bgl d |
| 1523 | if eq n 0 |
| 1524 | ret ulong 0 |
| 1525 | end |
| 1526 | t = get bg add mul d 384 sub n 1 |
| 1527 | k = mul sub n 1 32 |
| 1528 | while gt t 0 bound 64 |
| 1529 | t = shr t 1 |
| 1530 | k = add k 1 |
| 1531 | end |
| 1532 | ret k |
| 1533 | end |
| 1534 | |
| 1535 | note fr 2 = floor(num * 2^s / den) (num slot 0, den slot 1), fr 3 = 1 when a remainder is left |
| 1536 | fn fqs s long to void |
| 1537 | bcopy 2 0 |
| 1538 | bcopy 3 1 |
| 1539 | if ge s 0 |
| 1540 | bshl 2 ulong s |
| 1541 | else |
| 1542 | bshl 3 ulong sub long 0 s |
| 1543 | end |
| 1544 | bshl 3 55 |
| 1545 | q = ulong 0 |
| 1546 | for i ulong 56 |
| 1547 | q = shl q 1 |
| 1548 | if ne bcmp 2 3 0 |
| 1549 | bsub 2 3 |
| 1550 | q = or q 1 |
| 1551 | end |
| 1552 | bshr1 3 |
| 1553 | end |
| 1554 | put fr 2 q |
| 1555 | put fr 3 0 |
| 1556 | if gt get bgl 2 0 |
| 1557 | put fr 3 1 |
| 1558 | end |
| 1559 | end |
| 1560 | |
| 1561 | note num/den into binary64 bits (fr 0) and an exact flag (fr 1); st 1 when digits were dropped |
| 1562 | fn fround st ulong to void |
| 1563 | d = sub long bbits 0 long bbits 1 |
| 1564 | s = sub long 54 d |
| 1565 | fqs s |
| 1566 | if ge get fr 2 18014398509481984 |
| 1567 | s = sub s 1 |
| 1568 | fqs s |
| 1569 | end |
| 1570 | e2 = sub long 53 s |
| 1571 | if lt e2 -1022 |
| 1572 | s = long 1075 |
| 1573 | fqs s |
| 1574 | e2 = long -1022 |
| 1575 | end |
| 1576 | q = get fr 2 |
| 1577 | m = shr q 1 |
| 1578 | r = and q 1 |
| 1579 | sk = or get fr 3 st |
| 1580 | if ne r 0 |
| 1581 | if or ne sk 0 ne and m 1 0 |
| 1582 | m = add m 1 |
| 1583 | end |
| 1584 | end |
| 1585 | ex = ulong 0 |
| 1586 | if and eq r 0 eq sk 0 |
| 1587 | ex = ulong 1 |
| 1588 | end |
| 1589 | bits = add shl ulong add e2 1022 52 m |
| 1590 | if ge bits 9218868437227405312 |
| 1591 | bits = ulong 9218868437227405312 |
| 1592 | ex = ulong 0 |
| 1593 | end |
| 1594 | put fr 0 bits |
| 1595 | put fr 1 ex |
| 1596 | end |
| 1597 | |
| 1598 | note magnitude of number node a in binary64: fr 0 bits, fr 1 = 1 when exact |
| 1599 | fn f64of a ulong to void |
| 1600 | put fr 0 0 |
| 1601 | put fr 1 1 |
| 1602 | t = get nt a |
| 1603 | if or eq t 8 eq t 9 |
| 1604 | put fr 0 9218868437227405312 |
| 1605 | put fr 1 0 |
| 1606 | ret |
| 1607 | end |
| 1608 | n = get n2 a |
| 1609 | if eq n 0 |
| 1610 | ret |
| 1611 | end |
| 1612 | e = get nv a |
| 1613 | p = add long n e |
| 1614 | if gt p 310 |
| 1615 | put fr 0 9218868437227405312 |
| 1616 | put fr 1 0 |
| 1617 | ret |
| 1618 | end |
| 1619 | if lt p -330 |
| 1620 | put fr 0 0 |
| 1621 | put fr 1 0 |
| 1622 | ret |
| 1623 | end |
| 1624 | k = n |
| 1625 | st = ulong 0 |
| 1626 | if gt n 800 |
| 1627 | k = ulong 800 |
| 1628 | st = ulong 1 |
| 1629 | e = add e long sub n 800 |
| 1630 | end |
| 1631 | bclr 0 |
| 1632 | b = get n1 a |
| 1633 | for i ulong k |
| 1634 | bmuladd 0 10 get pl add b i |
| 1635 | end |
| 1636 | bclr 1 |
| 1637 | bmuladd 1 1 1 |
| 1638 | if ge e 0 |
| 1639 | for i ulong ulong e |
| 1640 | bmuladd 0 10 0 |
| 1641 | end |
| 1642 | else |
| 1643 | for i ulong ulong sub long 0 e |
| 1644 | bmuladd 1 10 0 |
| 1645 | end |
| 1646 | end |
| 1647 | fround st |
| 1648 | end |
| 1649 | |
| 1650 | fn numeq a ulong b ulong to bool |
| 1651 | if and isint a isint b |
| 1652 | ret deq a b |
| 1653 | end |
| 1654 | f64of a |
| 1655 | x = get fr 0 |
| 1656 | xe = get fr 1 |
| 1657 | f64of b |
| 1658 | y = get fr 0 |
| 1659 | ye = get fr 1 |
| 1660 | if and eq x 0 eq y 0 |
| 1661 | ret true |
| 1662 | end |
| 1663 | if ne x y |
| 1664 | ret false |
| 1665 | end |
| 1666 | if ne nsign a nsign b |
| 1667 | ret false |
| 1668 | end |
| 1669 | if and isint a eq xe 0 |
| 1670 | ret false |
| 1671 | end |
| 1672 | if and isint b eq ye 0 |
| 1673 | ret false |
| 1674 | end |
| 1675 | ret true |
| 1676 | end |
| 1677 | |
| 1678 | fn streq a ulong b ulong to bool |
| 1679 | k = get n2 a |
| 1680 | if ne k get n2 b |
| 1681 | ret false |
| 1682 | end |
| 1683 | ret pooleq get n1 a get n1 b k |
| 1684 | end |
| 1685 | |
| 1686 | fn arreq a ulong b ulong to bool |
| 1687 | k = get n2 a |
| 1688 | if ne k get n2 b |
| 1689 | ret false |
| 1690 | end |
| 1691 | x = get n1 a |
| 1692 | y = get n1 b |
| 1693 | i = ulong 0 |
| 1694 | while lt i k bound 999999 |
| 1695 | if not jeq x y 0 |
| 1696 | ret false |
| 1697 | end |
| 1698 | x = get nx x |
| 1699 | y = get nx y |
| 1700 | i = add i 1 |
| 1701 | end |
| 1702 | ret true |
| 1703 | end |
| 1704 | |
| 1705 | note key x has no equal key among the r keys after it |
| 1706 | fn klast x ulong r ulong to bool |
| 1707 | y = x |
| 1708 | i = ulong 0 |
| 1709 | while lt i r bound 999999 |
| 1710 | y = get nx get nx y |
| 1711 | if streq x y |
| 1712 | ret false |
| 1713 | end |
| 1714 | i = add i 1 |
| 1715 | end |
| 1716 | ret true |
| 1717 | end |
| 1718 | |
| 1719 | fn okeys a ulong to ulong |
| 1720 | m = shr get n2 a 1 |
| 1721 | x = get n1 a |
| 1722 | c = ulong 0 |
| 1723 | i = ulong 0 |
| 1724 | while lt i m bound 999999 |
| 1725 | if klast x sub sub m i 1 |
| 1726 | c = add c 1 |
| 1727 | end |
| 1728 | x = get nx get nx x |
| 1729 | i = add i 1 |
| 1730 | end |
| 1731 | ret c |
| 1732 | end |
| 1733 | |
| 1734 | note value of the last key in object b equal to key x, else 4294967295 |
| 1735 | fn ofind b ulong x ulong to ulong |
| 1736 | m = shr get n2 b 1 |
| 1737 | y = get n1 b |
| 1738 | v = ulong 4294967295 |
| 1739 | i = ulong 0 |
| 1740 | while lt i m bound 999999 |
| 1741 | if streq x y |
| 1742 | v = get nx y |
| 1743 | end |
| 1744 | y = get nx get nx y |
| 1745 | i = add i 1 |
| 1746 | end |
| 1747 | ret v |
| 1748 | end |
| 1749 | |
| 1750 | fn objeq a ulong b ulong to bool |
| 1751 | if ne okeys a okeys b |
| 1752 | ret false |
| 1753 | end |
| 1754 | m = shr get n2 a 1 |
| 1755 | x = get n1 a |
| 1756 | i = ulong 0 |
| 1757 | while lt i m bound 999999 |
| 1758 | if klast x sub sub m i 1 |
| 1759 | v = ofind b x |
| 1760 | if eq v 4294967295 |
| 1761 | ret false |
| 1762 | end |
| 1763 | if not jeq get nx x v 0 |
| 1764 | ret false |
| 1765 | end |
| 1766 | end |
| 1767 | x = get nx get nx x |
| 1768 | i = add i 1 |
| 1769 | end |
| 1770 | ret true |
| 1771 | end |
| 1772 | |
| 1773 | note top 1 for the two whole documents, 0 for items inside a list or dict |
| 1774 | fn jeq a ulong b ulong top ulong to bool |
| 1775 | ta = get nt a |
| 1776 | tb = get nt b |
| 1777 | if or eq ta 7 eq tb 7 |
| 1778 | ret and and eq ta 7 eq tb 7 eq top 0 |
| 1779 | end |
| 1780 | if and isnum ta isnum tb |
| 1781 | ret numeq a b |
| 1782 | end |
| 1783 | if ne ta tb |
| 1784 | ret false |
| 1785 | end |
| 1786 | if eq ta 4 |
| 1787 | ret streq a b |
| 1788 | end |
| 1789 | if eq ta 5 |
| 1790 | ret arreq a b |
| 1791 | end |
| 1792 | if eq ta 6 |
| 1793 | ret objeq a b |
| 1794 | end |
| 1795 | ret true |
| 1796 | end |
| 1797 | |
| 1798 | note ---- rows ---- |
| 1799 | fn srow i ulong to bool |
| 1800 | n = dgen i |
| 1801 | m = dans i |
| 1802 | if eq get rk i 1 |
| 1803 | strip fnl n |
| 1804 | ret cmpab get sr 0 get sr 1 m |
| 1805 | end |
| 1806 | strip fdone n |
| 1807 | s = get sr 0 |
| 1808 | t = get sr 1 |
| 1809 | put nn 0 0 |
| 1810 | put pn 0 0 |
| 1811 | a = jtop 0 s t |
| 1812 | if not bad |
| 1813 | b = jtop 1 0 m |
| 1814 | if not bad |
| 1815 | ret jeq a b 1 |
| 1816 | end |
| 1817 | end |
| 1818 | ret cmpab s t m |
| 1819 | end |
| 1820 | |
| 1821 | fn tally to void |
| 1822 | nr = get rn 0 |
| 1823 | i = ulong 0 |
| 1824 | while lt i nr bound 999999 |
| 1825 | ok = srow i |
| 1826 | x = add mul get rr i 4096 get rg i |
| 1827 | put tt x add get tt x 1 |
| 1828 | if ok |
| 1829 | put tp x add get tp x 1 |
| 1830 | put pv i 1 |
| 1831 | end |
| 1832 | i = add i 1 |
| 1833 | end |
| 1834 | end |