omega-harness
Agent skills for operating, encoding and communicating in Omega (.om) and the AIEN repositories
Brought by drakestapleton
omega-harness / tools / guides.om
| 1 | om2 |
| 2 | note guides: the generated blocks of this repository's guides, and the check that keeps them true. |
| 3 | note |
| 4 | note git ls-files | build/guides check report stale blocks and typed pins or counts; change nothing |
| 5 | note git ls-files | build/guides gen rewrite every block from its sources, then report the rest |
| 6 | note build/guides pinof < FILE exit 0 when FILE (tools/jflat output) is reference data for PIN |
| 7 | note |
| 8 | note Run in the repository root. Sources: PIN (the omega-mv commit and the omc sha256, written once), |
| 9 | note data/reference.tsv (omega-mcp's data/reference.json for that pin, flattened by tools/jflat; its first |
| 10 | note two lines must equal PIN) and the tracked file list on stdin (example and skill counts, example names, |
| 11 | note each skill's frontmatter description). A block is the lines between `<!-- gen:NAME -->` and |
| 12 | note `<!-- /gen:NAME -->`; NAME is pin, errors, traps, words, hostwords, examples, gateline or skills. |
| 13 | note Text outside blocks is written by people and copied byte for byte; it is never reworded. |
| 14 | note |
| 15 | note Findings (exit 1): a stale block (check only), an unknown block or one without its end marker, and, |
| 16 | note outside blocks, a word of 7 or more characters that, with A-F read as a-f, begins the PIN revision or |
| 17 | note omc sha256 (it restates the pin; compared against the values in PIN, so an all-digit short revision |
| 18 | note is found and a number that is not a prefix of the pin is not), or a typed count: a number (not after `-`) before examples, example programs, worked |
| 19 | note examples, programs, skills, words, host words, keywords, entries, error codes or topics, a number word |
| 20 | note (one .. twelve) before skills, programs |
| 21 | note or examples, or `examples=N`. A dated observation may name the compiler it ran on and the counts it |
| 22 | note saw: a paragraph that holds an ISO date (YYYY-MM-DD), or, in a .md file, the lines under a heading that |
| 23 | note holds one (up to the next heading). Alarm (also a finding): an input over half of its buffer, so the |
| 24 | note buffer is grown long before an input is refused. |
| 25 | note Not read: PIN, data/, README.zh-CN.md (translated copies are retired, not maintained). |
| 26 | note Exit 0 clean, 1 findings, 2 usage, PIN, data or I/O error, or an input that fills its buffer. |
| 27 | |
| 28 | const capb ulong 16777216 |
| 29 | const halfb ulong 8388608 |
| 30 | |
| 31 | global pin uchar 128 |
| 32 | global dat uchar 16777216 |
| 33 | global fl uchar 16777216 |
| 34 | global inb uchar 16777216 |
| 35 | global obuf uchar 16777216 |
| 36 | global aux uchar 65536 |
| 37 | global key uchar 4096 |
| 38 | global pfx uchar 4096 |
| 39 | global pth uchar 4096 |
| 40 | global sp uchar 4096 |
| 41 | global pk uchar 8448 |
| 42 | global nb uchar 32 |
| 43 | global ab uchar 16 |
| 44 | global cs ulong 256 |
| 45 | global ce ulong 256 |
| 46 | global st ulong 32 |
| 47 | |
| 48 | note st cells |
| 49 | const s_dn ulong 0 |
| 50 | const s_fn ulong 1 |
| 51 | const s_in ulong 2 |
| 52 | const s_on ulong 3 |
| 53 | const s_pl ulong 4 |
| 54 | const s_kl ulong 5 |
| 55 | const s_vs ulong 6 |
| 56 | const s_ve ulong 7 |
| 57 | const s_gen ulong 8 |
| 58 | const s_bad ulong 9 |
| 59 | const s_ln ulong 10 |
| 60 | const s_date ulong 11 |
| 61 | const s_xl ulong 12 |
| 62 | const s_nf ulong 13 |
| 63 | const s_ncat ulong 14 |
| 64 | const s_ls ulong 15 |
| 65 | const s_tp ulong 16 |
| 66 | const s_le ulong 17 |
| 67 | const s_ns ulong 18 |
| 68 | const s_ne ulong 19 |
| 69 | const s_blk ulong 20 |
| 70 | const s_rw ulong 21 |
| 71 | const s_max ulong 22 |
| 72 | const s_err ulong 23 |
| 73 | const s_ax ulong 24 |
| 74 | const s_md ulong 25 |
| 75 | const s_hd ulong 26 |
| 76 | |
| 77 | text kpinf "PIN" |
| 78 | text kdatf "data/reference.tsv" |
| 79 | text kmvw "omega-mv " |
| 80 | text kshaw "omc-sha256 " |
| 81 | text kpmv "/pin/omega_mv\t" |
| 82 | text kpsha "/pin/omc_sha256\t" |
| 83 | text a_check "check" |
| 84 | text a_gen "gen" |
| 85 | text a_pinof "pinof" |
| 86 | text m_use "usage: git ls-files | guides check|gen (in the repository root), or guides pinof < FLATTENED-REFERENCE\n" |
| 87 | text m_pin "guides: PIN must be exactly two lines, `omega-mv <40 hex>` and `omc-sha256 <64 hex>`\n" |
| 88 | text m_dat "guides: data/reference.tsv is missing or is not the reference data for PIN; run tools/regen.sh\n" |
| 89 | text m_full "guides: refused, an input filled its 16777216-byte buffer: " |
| 90 | text m_dalarm "guides: alarm: data/reference.tsv or the file list is over half of its 16777216-byte buffer; grow the buffer\n" |
| 91 | text m_open "guides: cannot read " |
| 92 | text m_wr "guides: cannot write " |
| 93 | text m_long "guides: a path of 4096 bytes or longer\n" |
| 94 | text m_cats "guides: more than 256 keyword kinds in the reference data\n" |
| 95 | text f_stale ": block is stale (run `git ls-files | build/guides gen`): " |
| 96 | text f_unk ": unknown block (pin, errors, traps, words, hostwords, examples, gateline or skills): " |
| 97 | text f_unt ": block has no end marker: " |
| 98 | text f_pin ": restates PIN outside a generated block: " |
| 99 | text f_cnt ": typed count outside a generated block: " |
| 100 | text f_alarm ": alarm: over half of the 16777216-byte buffer; grow the buffer" |
| 101 | text m_hint "guides: pins and counts are generated, never typed: use a block (`<!-- gen:NAME -->`), point at PIN, or date the observation\n" |
| 102 | text m_sum1 "guides: files " |
| 103 | text m_sum2 " blocks " |
| 104 | text m_sum3 " rewritten " |
| 105 | text m_sum4 " findings " |
| 106 | text m_sum5 " largest " |
| 107 | text nl "\n" |
| 108 | text colon ":" |
| 109 | text x_pin "PIN" |
| 110 | text x_data "data/" |
| 111 | text x_zh "README.zh-CN.md" |
| 112 | text x_tmp ".guides-tmp" |
| 113 | text mk_b "<!-- gen:" |
| 114 | text mk_e "<!-- /gen:" |
| 115 | text mk_t " -->" |
| 116 | text b_pin "pin" |
| 117 | text b_err "errors" |
| 118 | text b_trap "traps" |
| 119 | text b_words "words" |
| 120 | text b_host "hostwords" |
| 121 | text b_exam "examples" |
| 122 | text b_gate "gateline" |
| 123 | text b_skill "skills" |
| 124 | text k_wd "/words/" |
| 125 | text k_cat "/category" |
| 126 | text k_form "/form" |
| 127 | text k_er "/errors/" |
| 128 | text k_tr "/traps/" |
| 129 | text k_tp "/topics/" |
| 130 | text k_mean "/meaning" |
| 131 | text k_fix "/fix" |
| 132 | text k_name "/name" |
| 133 | text k_sum "/summary" |
| 134 | text k_host "host-" |
| 135 | text k_exd "examples/" |
| 136 | text k_skd "skills/" |
| 137 | text k_exs "/examples/" |
| 138 | text k_om ".om" |
| 139 | text k_skmd "/SKILL.md" |
| 140 | text k_xerr "examples/err" |
| 141 | text k_xtrap "examples/trap" |
| 142 | text k_fm "---" |
| 143 | text k_desc "description: " |
| 144 | text k_num "one two three four five six seven eight nine ten eleven twelve " |
| 145 | text n_ex "examples" |
| 146 | text n_pr "programs" |
| 147 | text n_sk "skills" |
| 148 | text n_wd "words" |
| 149 | text n_hw "host words" |
| 150 | text n_kw "keywords" |
| 151 | text n_en "entries" |
| 152 | text n_ec "error codes" |
| 153 | text n_tp "topics" |
| 154 | text n_ep "example programs" |
| 155 | text n_we "worked examples" |
| 156 | text t_bar "| " |
| 157 | text t_mid " | " |
| 158 | text t_end " |\n" |
| 159 | text t_sep ", " |
| 160 | text t_none "none" |
| 161 | text t_pinh "| Pin (`PIN`) | Value |\n|---|---|\n| omega-mv commit | `" |
| 162 | text t_pin2 "` |\n| `seed/out/omc` sha256 | `" |
| 163 | text t_pin3 "` |\n| reference data | `data/reference.tsv`: omega-mcp's `data/reference.json` for this pin, with " |
| 164 | text t_pin4 " keyword-table entries (" |
| 165 | text t_pin5 " host words), " |
| 166 | text t_pin6 " compiler error codes, " |
| 167 | text t_pin7 " traps and " |
| 168 | text t_pin8 " topics |\n" |
| 169 | text t_errh "| Code | Meaning | Fix | Example in `examples/` |\n|---|---|---|---|\n" |
| 170 | text t_trh "| Trap | Name | Cause | Exit status | Examples in `examples/` |\n|---|---|---|---|---|\n" |
| 171 | text k_cause "/cause" |
| 172 | text t_wd1 "The keyword table at the pin has " |
| 173 | text t_wd2 " entries; the " |
| 174 | text t_wd3 " host words are in the host-word table. The rest, by kind:\n\n| Kind | Words |\n|---|---|\n" |
| 175 | text t_hw1 "The keyword table at the pin has " |
| 176 | text t_hw2 " host words, each shown with its values:\n\n| Kind | Words |\n|---|---|\n" |
| 177 | text t_ex1 "`examples/gate.sh` reruns " |
| 178 | text t_ex2 " programs: " |
| 179 | text t_ex3 " in `examples/` and " |
| 180 | text t_ex4 " in `skills/*/examples/`.\n" |
| 181 | text t_gt1 "```text\nGATE PASS examples=" |
| 182 | text t_gt2 " pass=" |
| 183 | text t_gt3 " fail=0 omc=" |
| 184 | text t_gt4 "\n```\n" |
| 185 | text t_sk1 " skills, each `skills/<name>/SKILL.md`; the use column is the skill's own frontmatter `description`:\n\n| Skill | Use |\n|---|---|\n" |
| 186 | text t_nodesc "(no `description` in the frontmatter)" |
| 187 | |
| 188 | note ---------------------------------------------------------------- stdout and stderr |
| 189 | |
| 190 | fn wfd fd int t view uchar * to void |
| 191 | n = len t |
| 192 | p = ulong 0 |
| 193 | while lt p n bound 4096 |
| 194 | k = write fd t p sub n p |
| 195 | if le k 0 |
| 196 | exit 2 |
| 197 | end |
| 198 | p = add p ulong k |
| 199 | end |
| 200 | end |
| 201 | |
| 202 | fn wt t view uchar * to void |
| 203 | wfd 1 t |
| 204 | end |
| 205 | |
| 206 | fn wt2 t view uchar * to void |
| 207 | wfd 2 t |
| 208 | end |
| 209 | |
| 210 | fn wnum fd int v ulong to void |
| 211 | i = ulong 32 |
| 212 | n = v |
| 213 | while true bound 21 |
| 214 | i = sub i 1 |
| 215 | put nb i uchar add rem n 10 48 |
| 216 | n = div n 10 |
| 217 | if eq n 0 |
| 218 | break |
| 219 | end |
| 220 | end |
| 221 | while lt i 32 bound 32 |
| 222 | k = write fd nb i sub 32 i |
| 223 | if le k 0 |
| 224 | exit 2 |
| 225 | end |
| 226 | i = add i ulong k |
| 227 | end |
| 228 | end |
| 229 | |
| 230 | fn wpth fd int to void |
| 231 | n = get st s_pl |
| 232 | p = ulong 0 |
| 233 | while lt p n bound 4096 |
| 234 | k = write fd pth p sub n p |
| 235 | if le k 0 |
| 236 | exit 2 |
| 237 | end |
| 238 | p = add p ulong k |
| 239 | end |
| 240 | end |
| 241 | |
| 242 | fn wrng a ulong b ulong to void |
| 243 | p = a |
| 244 | while lt p b bound 16777216 |
| 245 | k = write 1 inb p sub b p |
| 246 | if le k 0 |
| 247 | exit 2 |
| 248 | end |
| 249 | p = add p ulong k |
| 250 | end |
| 251 | end |
| 252 | |
| 253 | note fnd: one finding about the current file and line, naming inb[a, b) |
| 254 | fn fnd m view uchar * a ulong b ulong to void |
| 255 | wpth 1 |
| 256 | wt colon |
| 257 | wnum 1 get st s_ln |
| 258 | wt m |
| 259 | wrng a b |
| 260 | wt nl |
| 261 | put st s_bad add get st s_bad 1 |
| 262 | end |
| 263 | |
| 264 | fn ferr m view uchar * to void |
| 265 | wt2 m |
| 266 | wpth 2 |
| 267 | wt2 nl |
| 268 | put st s_err 1 |
| 269 | end |
| 270 | |
| 271 | note ---------------------------------------------------------------- the rendered file (obuf) |
| 272 | |
| 273 | fn o c uchar to void |
| 274 | n = get st s_on |
| 275 | if eq n capb |
| 276 | wt2 m_full |
| 277 | wpth 2 |
| 278 | wt2 nl |
| 279 | exit 2 |
| 280 | end |
| 281 | put obuf n c |
| 282 | put st s_on add n 1 |
| 283 | end |
| 284 | |
| 285 | fn ot t view uchar * to void |
| 286 | for i ulong len t bound 4096 |
| 287 | o get t i |
| 288 | end |
| 289 | end |
| 290 | |
| 291 | fn onum v ulong to void |
| 292 | i = ulong 32 |
| 293 | n = v |
| 294 | while true bound 21 |
| 295 | i = sub i 1 |
| 296 | put nb i uchar add rem n 10 48 |
| 297 | n = div n 10 |
| 298 | if eq n 0 |
| 299 | break |
| 300 | end |
| 301 | end |
| 302 | while lt i 32 bound 32 |
| 303 | o get nb i |
| 304 | i = add i 1 |
| 305 | end |
| 306 | end |
| 307 | |
| 308 | note odat: a data value dat[a, b) as a table cell: jflat escapes decoded (\n and \t become a space), `|` escaped |
| 309 | fn odat a ulong b ulong to void |
| 310 | i = a |
| 311 | while lt i b bound 16777216 |
| 312 | c = get dat i |
| 313 | if eq c 92 |
| 314 | d = uchar 0 |
| 315 | if lt add i 1 b |
| 316 | d = get dat add i 1 |
| 317 | end |
| 318 | if or eq d 110 eq d 116 |
| 319 | o 32 |
| 320 | i = add i 2 |
| 321 | else |
| 322 | o 92 |
| 323 | if eq d 92 |
| 324 | i = add i 2 |
| 325 | else |
| 326 | i = add i 1 |
| 327 | end |
| 328 | end |
| 329 | else |
| 330 | if eq c 124 |
| 331 | o 92 |
| 332 | end |
| 333 | o c |
| 334 | i = add i 1 |
| 335 | end |
| 336 | end |
| 337 | end |
| 338 | |
| 339 | fn oflr a ulong b ulong to void |
| 340 | i = a |
| 341 | while lt i b bound 4096 |
| 342 | o get fl i |
| 343 | i = add i 1 |
| 344 | end |
| 345 | end |
| 346 | |
| 347 | fn opin a ulong n ulong to void |
| 348 | for i ulong n bound 64 |
| 349 | o get pin add a i |
| 350 | end |
| 351 | end |
| 352 | |
| 353 | note oaux: aux[a, b) as a table cell |
| 354 | fn oaux a ulong b ulong to void |
| 355 | i = a |
| 356 | while lt i b bound 65536 |
| 357 | c = get aux i |
| 358 | if eq c 124 |
| 359 | o 92 |
| 360 | end |
| 361 | o c |
| 362 | i = add i 1 |
| 363 | end |
| 364 | end |
| 365 | |
| 366 | fn cpraw a ulong b ulong to void |
| 367 | i = a |
| 368 | while lt i b bound 16777216 |
| 369 | o get inb i |
| 370 | i = add i 1 |
| 371 | end |
| 372 | end |
| 373 | |
| 374 | note cpin: inb[a, b) and the newline at b, when b is inside the file |
| 375 | fn cpin a ulong b ulong to void |
| 376 | cpraw a b |
| 377 | if lt b get st s_in |
| 378 | o 10 |
| 379 | end |
| 380 | end |
| 381 | |
| 382 | note ---------------------------------------------------------------- PIN and the reference data |
| 383 | |
| 384 | fn ishex c uchar to bool |
| 385 | if and ge c 48 le c 57 |
| 386 | ret true |
| 387 | end |
| 388 | ret and ge c 97 le c 102 |
| 389 | end |
| 390 | |
| 391 | fn isdig c uchar to bool |
| 392 | ret and ge c 48 le c 57 |
| 393 | end |
| 394 | |
| 395 | fn islow c uchar to bool |
| 396 | ret and ge c 97 le c 122 |
| 397 | end |
| 398 | |
| 399 | fn isw c uchar to bool |
| 400 | if isdig c |
| 401 | ret true |
| 402 | end |
| 403 | if and ge c 65 le c 90 |
| 404 | ret true |
| 405 | end |
| 406 | if islow c |
| 407 | ret true |
| 408 | end |
| 409 | ret eq c 95 |
| 410 | end |
| 411 | |
| 412 | note ldpin: PIN into pin; true when it is exactly the two lines |
| 413 | fn ldpin to bool |
| 414 | f = fopen kpinf 0 len kpinf 0 0 |
| 415 | if lt f 0 |
| 416 | ret false |
| 417 | end |
| 418 | n = read int f pin 0 128 |
| 419 | z = fclose int f |
| 420 | if ne n 126 |
| 421 | ret false |
| 422 | end |
| 423 | for i ulong 9 |
| 424 | if ne get pin i get kmvw i |
| 425 | ret false |
| 426 | end |
| 427 | end |
| 428 | for j ulong 11 |
| 429 | if ne get pin add 50 j get kshaw j |
| 430 | ret false |
| 431 | end |
| 432 | end |
| 433 | if or ne get pin 49 10 ne get pin 125 10 |
| 434 | ret false |
| 435 | end |
| 436 | for k ulong 40 |
| 437 | if not ishex get pin add 9 k |
| 438 | ret false |
| 439 | end |
| 440 | end |
| 441 | for m ulong 64 |
| 442 | if not ishex get pin add 61 m |
| 443 | ret false |
| 444 | end |
| 445 | end |
| 446 | ret true |
| 447 | end |
| 448 | |
| 449 | note rddat: fd into dat; 0 read, 1 read error, 2 the input fills the buffer |
| 450 | fn rddat f int to int |
| 451 | n = ulong 0 |
| 452 | while true bound 16777217 |
| 453 | if eq n capb |
| 454 | ret 2 |
| 455 | end |
| 456 | k = read f dat n sub capb n |
| 457 | if lt k 0 |
| 458 | ret 1 |
| 459 | end |
| 460 | if eq k 0 |
| 461 | break |
| 462 | end |
| 463 | n = add n ulong k |
| 464 | end |
| 465 | put st s_dn n |
| 466 | ret 0 |
| 467 | end |
| 468 | |
| 469 | fn rdfl f int to int |
| 470 | n = ulong 0 |
| 471 | while true bound 16777217 |
| 472 | if eq n capb |
| 473 | ret 2 |
| 474 | end |
| 475 | k = read f fl n sub capb n |
| 476 | if lt k 0 |
| 477 | ret 1 |
| 478 | end |
| 479 | if eq k 0 |
| 480 | break |
| 481 | end |
| 482 | n = add n ulong k |
| 483 | end |
| 484 | put st s_fn n |
| 485 | ret 0 |
| 486 | end |
| 487 | |
| 488 | fn rdin f int to int |
| 489 | n = ulong 0 |
| 490 | while true bound 16777217 |
| 491 | if eq n capb |
| 492 | ret 2 |
| 493 | end |
| 494 | k = read f inb n sub capb n |
| 495 | if lt k 0 |
| 496 | ret 1 |
| 497 | end |
| 498 | if eq k 0 |
| 499 | break |
| 500 | end |
| 501 | n = add n ulong k |
| 502 | end |
| 503 | put st s_in n |
| 504 | ret 0 |
| 505 | end |
| 506 | |
| 507 | note rdaux: the first bytes of fd (a skill's frontmatter) into aux |
| 508 | fn rdaux f int to void |
| 509 | n = ulong 0 |
| 510 | while lt n 65536 bound 65537 |
| 511 | k = read f aux n sub 65536 n |
| 512 | if le k 0 |
| 513 | break |
| 514 | end |
| 515 | n = add n ulong k |
| 516 | end |
| 517 | put st s_ax n |
| 518 | end |
| 519 | |
| 520 | note datpin: dat starts with the two pin lines of PIN, as tools/jflat writes them |
| 521 | fn datpin to bool |
| 522 | a = len kpmv |
| 523 | b = len kpsha |
| 524 | w = add add add a 41 b 65 |
| 525 | if lt get st s_dn w |
| 526 | ret false |
| 527 | end |
| 528 | for i ulong a bound 64 |
| 529 | if ne get dat i get kpmv i |
| 530 | ret false |
| 531 | end |
| 532 | end |
| 533 | for j ulong 40 |
| 534 | if ne get dat add a j get pin add 9 j |
| 535 | ret false |
| 536 | end |
| 537 | end |
| 538 | if ne get dat add a 40 10 |
| 539 | ret false |
| 540 | end |
| 541 | q = add a 41 |
| 542 | for k ulong b bound 64 |
| 543 | if ne get dat add q k get kpsha k |
| 544 | ret false |
| 545 | end |
| 546 | end |
| 547 | r = add q b |
| 548 | for m ulong 64 |
| 549 | if ne get dat add r m get pin add 61 m |
| 550 | ret false |
| 551 | end |
| 552 | end |
| 553 | ret eq get dat add r 64 10 |
| 554 | end |
| 555 | |
| 556 | note ---------------------------------------------------------------- data lines and lookups |
| 557 | |
| 558 | note dline: the data line at p; cells s_ls (start), s_tp (its tab, or its end) and s_le (its end) |
| 559 | fn dline p ulong to void |
| 560 | n = get st s_dn |
| 561 | put st s_ls p |
| 562 | q = p |
| 563 | t = n |
| 564 | while lt q n bound 16777216 |
| 565 | c = get dat q |
| 566 | if eq c 10 |
| 567 | break |
| 568 | end |
| 569 | if and eq c 9 eq t n |
| 570 | t = q |
| 571 | end |
| 572 | q = add q 1 |
| 573 | end |
| 574 | if gt t q |
| 575 | t = q |
| 576 | end |
| 577 | put st s_tp t |
| 578 | put st s_le q |
| 579 | end |
| 580 | |
| 581 | note dkid: the current line's path is pre, then one name without `/`, then suf |
| 582 | fn dkid pre view uchar * suf view uchar * to bool |
| 583 | ls = get st s_ls |
| 584 | tp = get st s_tp |
| 585 | a = len pre |
| 586 | b = len suf |
| 587 | if lt sub tp ls add add a b 1 |
| 588 | ret false |
| 589 | end |
| 590 | for i ulong a bound 4096 |
| 591 | if ne get dat add ls i get pre i |
| 592 | ret false |
| 593 | end |
| 594 | end |
| 595 | s = sub tp b |
| 596 | for j ulong b bound 4096 |
| 597 | if ne get dat add s j get suf j |
| 598 | ret false |
| 599 | end |
| 600 | end |
| 601 | k = add ls a |
| 602 | while lt k s bound 16777216 |
| 603 | if eq get dat k 47 |
| 604 | ret false |
| 605 | end |
| 606 | k = add k 1 |
| 607 | end |
| 608 | ret true |
| 609 | end |
| 610 | |
| 611 | fn dcount pre view uchar * suf view uchar * to ulong |
| 612 | n = get st s_dn |
| 613 | p = ulong 0 |
| 614 | c = ulong 0 |
| 615 | while lt p n bound 16777216 |
| 616 | dline p |
| 617 | if dkid pre suf |
| 618 | c = add c 1 |
| 619 | end |
| 620 | p = add get st s_le 1 |
| 621 | end |
| 622 | ret c |
| 623 | end |
| 624 | |
| 625 | fn deq a ulong b ulong c ulong d ulong to bool |
| 626 | if ne sub b a sub d c |
| 627 | ret false |
| 628 | end |
| 629 | k = sub b a |
| 630 | for i ulong k bound 16777216 |
| 631 | if ne get dat add a i get dat add c i |
| 632 | ret false |
| 633 | end |
| 634 | end |
| 635 | ret true |
| 636 | end |
| 637 | |
| 638 | fn ishost a ulong b ulong to bool |
| 639 | n = len k_host |
| 640 | if lt sub b a n |
| 641 | ret false |
| 642 | end |
| 643 | for i ulong n bound 16 |
| 644 | if ne get dat add a i get k_host i |
| 645 | ret false |
| 646 | end |
| 647 | end |
| 648 | ret true |
| 649 | end |
| 650 | |
| 651 | note hostn: the number of words whose category starts with host- |
| 652 | fn hostn to ulong |
| 653 | n = get st s_dn |
| 654 | p = ulong 0 |
| 655 | c = ulong 0 |
| 656 | while lt p n bound 16777216 |
| 657 | dline p |
| 658 | if dkid k_wd k_cat |
| 659 | if ishost add get st s_tp 1 get st s_le |
| 660 | c = add c 1 |
| 661 | end |
| 662 | end |
| 663 | p = add get st s_le 1 |
| 664 | end |
| 665 | ret c |
| 666 | end |
| 667 | |
| 668 | fn kclr to void |
| 669 | put st s_kl 0 |
| 670 | end |
| 671 | |
| 672 | fn kc c uchar to void |
| 673 | n = get st s_kl |
| 674 | if eq n 4096 |
| 675 | wt2 m_long |
| 676 | exit 2 |
| 677 | end |
| 678 | put key n c |
| 679 | put st s_kl add n 1 |
| 680 | end |
| 681 | |
| 682 | fn kt t view uchar * to void |
| 683 | for i ulong len t bound 4096 |
| 684 | kc get t i |
| 685 | end |
| 686 | end |
| 687 | |
| 688 | fn kdat a ulong b ulong to void |
| 689 | i = a |
| 690 | while lt i b bound 4096 |
| 691 | kc get dat i |
| 692 | i = add i 1 |
| 693 | end |
| 694 | end |
| 695 | |
| 696 | fn dend p ulong to ulong |
| 697 | n = get st s_dn |
| 698 | q = p |
| 699 | while lt q n bound 16777216 |
| 700 | if eq get dat q 10 |
| 701 | ret q |
| 702 | end |
| 703 | q = add q 1 |
| 704 | end |
| 705 | ret n |
| 706 | end |
| 707 | |
| 708 | fn keyat p ulong kl ulong to bool |
| 709 | if gt add add p kl 1 get st s_dn |
| 710 | ret false |
| 711 | end |
| 712 | for i ulong kl bound 4096 |
| 713 | if ne get dat add p i get key i |
| 714 | ret false |
| 715 | end |
| 716 | end |
| 717 | ret eq get dat add p kl 9 |
| 718 | end |
| 719 | |
| 720 | note look: the value of the line whose path is key, into s_vs and s_ve |
| 721 | fn look to bool |
| 722 | kl = get st s_kl |
| 723 | n = get st s_dn |
| 724 | p = ulong 0 |
| 725 | while lt p n bound 16777216 |
| 726 | if keyat p kl |
| 727 | v = add add p kl 1 |
| 728 | put st s_vs v |
| 729 | put st s_ve dend v |
| 730 | ret true |
| 731 | end |
| 732 | p = add dend p 1 |
| 733 | end |
| 734 | ret false |
| 735 | end |
| 736 | |
| 737 | note ---------------------------------------------------------------- the tracked file list |
| 738 | |
| 739 | fn fend p ulong to ulong |
| 740 | n = get st s_fn |
| 741 | q = p |
| 742 | while lt q n bound 16777216 |
| 743 | if eq get fl q 10 |
| 744 | ret q |
| 745 | end |
| 746 | q = add q 1 |
| 747 | end |
| 748 | ret n |
| 749 | end |
| 750 | |
| 751 | fn flpre p ulong e ulong t view uchar * to bool |
| 752 | n = len t |
| 753 | if lt sub e p n |
| 754 | ret false |
| 755 | end |
| 756 | for i ulong n bound 4096 |
| 757 | if ne get fl add p i get t i |
| 758 | ret false |
| 759 | end |
| 760 | end |
| 761 | ret true |
| 762 | end |
| 763 | |
| 764 | fn flsuf p ulong e ulong t view uchar * to bool |
| 765 | n = len t |
| 766 | if lt sub e p n |
| 767 | ret false |
| 768 | end |
| 769 | s = sub e n |
| 770 | for i ulong n bound 4096 |
| 771 | if ne get fl add s i get t i |
| 772 | ret false |
| 773 | end |
| 774 | end |
| 775 | ret true |
| 776 | end |
| 777 | |
| 778 | fn flnos a ulong b ulong to bool |
| 779 | i = a |
| 780 | while lt i b bound 16777216 |
| 781 | if eq get fl i 47 |
| 782 | ret false |
| 783 | end |
| 784 | i = add i 1 |
| 785 | end |
| 786 | ret true |
| 787 | end |
| 788 | |
| 789 | note isexa: examples/NAME.om |
| 790 | fn isexa p ulong e ulong to bool |
| 791 | if lt sub e p 13 |
| 792 | ret false |
| 793 | end |
| 794 | if not flpre p e k_exd |
| 795 | ret false |
| 796 | end |
| 797 | if not flsuf p e k_om |
| 798 | ret false |
| 799 | end |
| 800 | ret flnos add p 9 e |
| 801 | end |
| 802 | |
| 803 | note isskex: skills/S/examples/NAME.om |
| 804 | fn isskex p ulong e ulong to bool |
| 805 | if not flpre p e k_skd |
| 806 | ret false |
| 807 | end |
| 808 | if not flsuf p e k_om |
| 809 | ret false |
| 810 | end |
| 811 | q = add p 7 |
| 812 | while lt q e bound 4096 |
| 813 | if eq get fl q 47 |
| 814 | break |
| 815 | end |
| 816 | q = add q 1 |
| 817 | end |
| 818 | if or eq q add p 7 ge q e |
| 819 | ret false |
| 820 | end |
| 821 | if not flpre q e k_exs |
| 822 | ret false |
| 823 | end |
| 824 | if lt sub e q 14 |
| 825 | ret false |
| 826 | end |
| 827 | ret flnos add q 10 e |
| 828 | end |
| 829 | |
| 830 | note isskill: skills/S/SKILL.md |
| 831 | fn isskill p ulong e ulong to bool |
| 832 | if lt sub e p 17 |
| 833 | ret false |
| 834 | end |
| 835 | if not flpre p e k_skd |
| 836 | ret false |
| 837 | end |
| 838 | if not flsuf p e k_skmd |
| 839 | ret false |
| 840 | end |
| 841 | ret flnos add p 7 sub e 9 |
| 842 | end |
| 843 | |
| 844 | fn pclr to void |
| 845 | put st s_xl 0 |
| 846 | end |
| 847 | |
| 848 | fn pc c uchar to void |
| 849 | n = get st s_xl |
| 850 | put pfx n c |
| 851 | put st s_xl add n 1 |
| 852 | end |
| 853 | |
| 854 | fn pt t view uchar * to void |
| 855 | for i ulong len t bound 64 |
| 856 | pc get t i |
| 857 | end |
| 858 | end |
| 859 | |
| 860 | fn pdat a ulong b ulong to void |
| 861 | i = a |
| 862 | while lt i b bound 64 |
| 863 | pc get dat i |
| 864 | i = add i 1 |
| 865 | end |
| 866 | end |
| 867 | |
| 868 | note isex: the path begins with pfx (an examples/ prefix) and is examples/NAME.om |
| 869 | fn isex p ulong e ulong to bool |
| 870 | xl = get st s_xl |
| 871 | if lt sub e p add xl 3 |
| 872 | ret false |
| 873 | end |
| 874 | for i ulong xl bound 4096 |
| 875 | if ne get fl add p i get pfx i |
| 876 | ret false |
| 877 | end |
| 878 | end |
| 879 | if not flsuf p e k_om |
| 880 | ret false |
| 881 | end |
| 882 | ret flnos add p 9 e |
| 883 | end |
| 884 | |
| 885 | note exlist: every examples/ program whose path begins with pfx, by name |
| 886 | fn exlist to void |
| 887 | n = get st s_fn |
| 888 | p = ulong 0 |
| 889 | c = ulong 0 |
| 890 | while lt p n bound 16777216 |
| 891 | e = fend p |
| 892 | if isex p e |
| 893 | if gt c 0 |
| 894 | ot t_sep |
| 895 | end |
| 896 | o 96 |
| 897 | oflr add p 9 sub e 3 |
| 898 | o 96 |
| 899 | c = add c 1 |
| 900 | end |
| 901 | p = add e 1 |
| 902 | end |
| 903 | if eq c 0 |
| 904 | ot t_none |
| 905 | end |
| 906 | end |
| 907 | |
| 908 | fn nexa to ulong |
| 909 | n = get st s_fn |
| 910 | p = ulong 0 |
| 911 | c = ulong 0 |
| 912 | while lt p n bound 16777216 |
| 913 | e = fend p |
| 914 | if isexa p e |
| 915 | c = add c 1 |
| 916 | end |
| 917 | p = add e 1 |
| 918 | end |
| 919 | ret c |
| 920 | end |
| 921 | |
| 922 | fn nexb to ulong |
| 923 | n = get st s_fn |
| 924 | p = ulong 0 |
| 925 | c = ulong 0 |
| 926 | while lt p n bound 16777216 |
| 927 | e = fend p |
| 928 | if isskex p e |
| 929 | c = add c 1 |
| 930 | end |
| 931 | p = add e 1 |
| 932 | end |
| 933 | ret c |
| 934 | end |
| 935 | |
| 936 | note ---------------------------------------------------------------- block renderers |
| 937 | |
| 938 | fn rpin to void |
| 939 | ot t_pinh |
| 940 | opin 9 40 |
| 941 | ot t_pin2 |
| 942 | opin 61 64 |
| 943 | ot t_pin3 |
| 944 | onum dcount k_wd k_cat |
| 945 | ot t_pin4 |
| 946 | onum hostn |
| 947 | ot t_pin5 |
| 948 | onum dcount k_er k_mean |
| 949 | ot t_pin6 |
| 950 | onum dcount k_tr k_mean |
| 951 | ot t_pin7 |
| 952 | onum dcount k_tp k_sum |
| 953 | ot t_pin8 |
| 954 | end |
| 955 | |
| 956 | note erow: the row of the error whose /errors/X/meaning line is current |
| 957 | fn erow to void |
| 958 | xs = add get st s_ls 8 |
| 959 | xe = sub get st s_tp 8 |
| 960 | vs = add get st s_tp 1 |
| 961 | ve = get st s_le |
| 962 | ot t_bar |
| 963 | odat xs xe |
| 964 | ot t_mid |
| 965 | odat vs ve |
| 966 | ot t_mid |
| 967 | kclr |
| 968 | kt k_er |
| 969 | kdat xs xe |
| 970 | kt k_fix |
| 971 | if look |
| 972 | odat get st s_vs get st s_ve |
| 973 | end |
| 974 | ot t_mid |
| 975 | pclr |
| 976 | pt k_xerr |
| 977 | if eq sub xe xs 1 |
| 978 | pc 48 |
| 979 | end |
| 980 | pdat xs xe |
| 981 | pc 95 |
| 982 | exlist |
| 983 | ot t_end |
| 984 | end |
| 985 | |
| 986 | fn rerr to void |
| 987 | ot t_errh |
| 988 | n = get st s_dn |
| 989 | p = ulong 0 |
| 990 | while lt p n bound 16777216 |
| 991 | dline p |
| 992 | lz = get st s_le |
| 993 | if dkid k_er k_mean |
| 994 | erow |
| 995 | end |
| 996 | p = add lz 1 |
| 997 | end |
| 998 | end |
| 999 | |
| 1000 | fn dnum a ulong b ulong to ulong |
| 1001 | v = ulong 0 |
| 1002 | i = a |
| 1003 | while lt i b bound 20 |
| 1004 | v = add mul v 10 ulong sub get dat i 48 |
| 1005 | i = add i 1 |
| 1006 | end |
| 1007 | ret v |
| 1008 | end |
| 1009 | |
| 1010 | fn trow to void |
| 1011 | xs = add get st s_ls 7 |
| 1012 | xe = sub get st s_tp 8 |
| 1013 | vs = add get st s_tp 1 |
| 1014 | ve = get st s_le |
| 1015 | ot t_bar |
| 1016 | odat xs xe |
| 1017 | ot t_mid |
| 1018 | kclr |
| 1019 | kt k_tr |
| 1020 | kdat xs xe |
| 1021 | kt k_name |
| 1022 | if look |
| 1023 | odat get st s_vs get st s_ve |
| 1024 | end |
| 1025 | ot t_mid |
| 1026 | kclr |
| 1027 | kt k_tr |
| 1028 | kdat xs xe |
| 1029 | kt k_cause |
| 1030 | if look |
| 1031 | odat get st s_vs get st s_ve |
| 1032 | end |
| 1033 | ot t_mid |
| 1034 | onum add 100 dnum xs xe |
| 1035 | ot t_mid |
| 1036 | pclr |
| 1037 | pt k_xtrap |
| 1038 | pdat xs xe |
| 1039 | pc 95 |
| 1040 | exlist |
| 1041 | ot t_end |
| 1042 | end |
| 1043 | |
| 1044 | fn rtrap to void |
| 1045 | ot t_trh |
| 1046 | n = get st s_dn |
| 1047 | p = ulong 0 |
| 1048 | while lt p n bound 16777216 |
| 1049 | dline p |
| 1050 | lz = get st s_le |
| 1051 | if dkid k_tr k_mean |
| 1052 | trow |
| 1053 | end |
| 1054 | p = add lz 1 |
| 1055 | end |
| 1056 | end |
| 1057 | |
| 1058 | note cats: the keyword kinds in order of first use, as data ranges in cs and ce |
| 1059 | fn addcat to void |
| 1060 | vs = add get st s_tp 1 |
| 1061 | ve = get st s_le |
| 1062 | m = get st s_ncat |
| 1063 | for j ulong m bound 256 |
| 1064 | if deq get cs j get ce j vs ve |
| 1065 | ret |
| 1066 | end |
| 1067 | end |
| 1068 | if eq m 256 |
| 1069 | wt2 m_cats |
| 1070 | exit 2 |
| 1071 | end |
| 1072 | put cs m vs |
| 1073 | put ce m ve |
| 1074 | put st s_ncat add m 1 |
| 1075 | end |
| 1076 | |
| 1077 | fn cats to void |
| 1078 | put st s_ncat 0 |
| 1079 | n = get st s_dn |
| 1080 | p = ulong 0 |
| 1081 | while lt p n bound 16777216 |
| 1082 | dline p |
| 1083 | lz = get st s_le |
| 1084 | if dkid k_wd k_cat |
| 1085 | addcat |
| 1086 | end |
| 1087 | p = add lz 1 |
| 1088 | end |
| 1089 | end |
| 1090 | |
| 1091 | note wform: the form of the word whose category line is current (its name when it has none) |
| 1092 | fn wform to void |
| 1093 | a = add get st s_ls 7 |
| 1094 | b = sub get st s_tp 9 |
| 1095 | kclr |
| 1096 | kt k_wd |
| 1097 | kdat a b |
| 1098 | kt k_form |
| 1099 | if look |
| 1100 | odat get st s_vs get st s_ve |
| 1101 | else |
| 1102 | odat a b |
| 1103 | end |
| 1104 | end |
| 1105 | |
| 1106 | fn wrow j ulong host bool to void |
| 1107 | ot t_bar |
| 1108 | odat get cs j get ce j |
| 1109 | ot t_mid |
| 1110 | first = true |
| 1111 | n = get st s_dn |
| 1112 | p = ulong 0 |
| 1113 | while lt p n bound 16777216 |
| 1114 | dline p |
| 1115 | lz = get st s_le |
| 1116 | if dkid k_wd k_cat |
| 1117 | if deq add get st s_tp 1 lz get cs j get ce j |
| 1118 | if not first |
| 1119 | ot t_sep |
| 1120 | end |
| 1121 | first = false |
| 1122 | o 96 |
| 1123 | wform |
| 1124 | o 96 |
| 1125 | end |
| 1126 | end |
| 1127 | p = add lz 1 |
| 1128 | end |
| 1129 | ot t_end |
| 1130 | end |
| 1131 | |
| 1132 | fn rwords host bool to void |
| 1133 | h = hostn |
| 1134 | if host |
| 1135 | ot t_hw1 |
| 1136 | onum h |
| 1137 | ot t_hw2 |
| 1138 | else |
| 1139 | ot t_wd1 |
| 1140 | onum dcount k_wd k_cat |
| 1141 | ot t_wd2 |
| 1142 | onum h |
| 1143 | ot t_wd3 |
| 1144 | end |
| 1145 | cats |
| 1146 | m = get st s_ncat |
| 1147 | for j ulong m bound 256 |
| 1148 | if eq ishost get cs j get ce j host |
| 1149 | wrow j host |
| 1150 | end |
| 1151 | end |
| 1152 | end |
| 1153 | |
| 1154 | fn rexam to void |
| 1155 | a = nexa |
| 1156 | b = nexb |
| 1157 | ot t_ex1 |
| 1158 | onum add a b |
| 1159 | ot t_ex2 |
| 1160 | onum a |
| 1161 | ot t_ex3 |
| 1162 | onum b |
| 1163 | ot t_ex4 |
| 1164 | end |
| 1165 | |
| 1166 | fn rgate to void |
| 1167 | n = add nexa nexb |
| 1168 | ot t_gt1 |
| 1169 | onum n |
| 1170 | ot t_gt2 |
| 1171 | onum n |
| 1172 | ot t_gt3 |
| 1173 | opin 61 64 |
| 1174 | ot t_gt4 |
| 1175 | end |
| 1176 | |
| 1177 | note auxpre: aux[p, e) begins with t |
| 1178 | fn auxpre p ulong e ulong t view uchar * to bool |
| 1179 | n = len t |
| 1180 | if lt sub e p n |
| 1181 | ret false |
| 1182 | end |
| 1183 | for i ulong n bound 64 |
| 1184 | if ne get aux add p i get t i |
| 1185 | ret false |
| 1186 | end |
| 1187 | end |
| 1188 | ret true |
| 1189 | end |
| 1190 | |
| 1191 | fn auxend p ulong to ulong |
| 1192 | n = get st s_ax |
| 1193 | q = p |
| 1194 | while lt q n bound 65536 |
| 1195 | if eq get aux q 10 |
| 1196 | ret q |
| 1197 | end |
| 1198 | q = add q 1 |
| 1199 | end |
| 1200 | ret n |
| 1201 | end |
| 1202 | |
| 1203 | note odesc: the description line of the frontmatter in aux |
| 1204 | fn odesc to void |
| 1205 | n = get st s_ax |
| 1206 | e = auxend 0 |
| 1207 | if or ne e 3 not auxpre 0 e k_fm |
| 1208 | ot t_nodesc |
| 1209 | ret |
| 1210 | end |
| 1211 | p = add e 1 |
| 1212 | while lt p n bound 65536 |
| 1213 | q = auxend p |
| 1214 | if and eq sub q p 3 auxpre p q k_fm |
| 1215 | break |
| 1216 | end |
| 1217 | if auxpre p q k_desc |
| 1218 | a = add p len k_desc |
| 1219 | if gt sub q a 1 |
| 1220 | if and eq get aux a 34 eq get aux sub q 1 34 |
| 1221 | a = add a 1 |
| 1222 | q = sub q 1 |
| 1223 | end |
| 1224 | end |
| 1225 | oaux a q |
| 1226 | ret |
| 1227 | end |
| 1228 | p = add q 1 |
| 1229 | end |
| 1230 | ot t_nodesc |
| 1231 | end |
| 1232 | |
| 1233 | fn skrow p ulong e ulong to void |
| 1234 | ot t_bar |
| 1235 | o 96 |
| 1236 | oflr p e |
| 1237 | o 96 |
| 1238 | ot t_mid |
| 1239 | m = sub e p |
| 1240 | for i ulong m bound 4096 |
| 1241 | put sp i get fl add p i |
| 1242 | end |
| 1243 | f = fopen sp 0 m 0 0 |
| 1244 | if lt f 0 |
| 1245 | put st s_ax 0 |
| 1246 | wt2 m_open |
| 1247 | k = write 2 sp 0 m |
| 1248 | wt2 nl |
| 1249 | put st s_err 1 |
| 1250 | else |
| 1251 | rdaux int f |
| 1252 | z = fclose int f |
| 1253 | end |
| 1254 | odesc |
| 1255 | ot t_end |
| 1256 | end |
| 1257 | |
| 1258 | fn rskill to void |
| 1259 | n = get st s_fn |
| 1260 | c = ulong 0 |
| 1261 | p = ulong 0 |
| 1262 | while lt p n bound 16777216 |
| 1263 | e = fend p |
| 1264 | if isskill p e |
| 1265 | c = add c 1 |
| 1266 | end |
| 1267 | p = add e 1 |
| 1268 | end |
| 1269 | onum c |
| 1270 | ot t_sk1 |
| 1271 | q = ulong 0 |
| 1272 | while lt q n bound 16777216 |
| 1273 | f = fend q |
| 1274 | if isskill q f |
| 1275 | skrow q f |
| 1276 | end |
| 1277 | q = add f 1 |
| 1278 | end |
| 1279 | end |
| 1280 | |
| 1281 | note ---------------------------------------------------------------- one file |
| 1282 | |
| 1283 | fn iend p ulong to ulong |
| 1284 | n = get st s_in |
| 1285 | q = p |
| 1286 | while lt q n bound 16777216 |
| 1287 | if eq get inb q 10 |
| 1288 | ret q |
| 1289 | end |
| 1290 | q = add q 1 |
| 1291 | end |
| 1292 | ret n |
| 1293 | end |
| 1294 | |
| 1295 | note isbeg: the line inb[p, e) is `<!-- gen:NAME -->`; the name goes to s_ns and s_ne |
| 1296 | fn isbeg p ulong e ulong to bool |
| 1297 | a = len mk_b |
| 1298 | b = len mk_t |
| 1299 | if le sub e p add a b |
| 1300 | ret false |
| 1301 | end |
| 1302 | for i ulong a bound 16 |
| 1303 | if ne get inb add p i get mk_b i |
| 1304 | ret false |
| 1305 | end |
| 1306 | end |
| 1307 | s = sub e b |
| 1308 | for j ulong b bound 16 |
| 1309 | if ne get inb add s j get mk_t j |
| 1310 | ret false |
| 1311 | end |
| 1312 | end |
| 1313 | k = add p a |
| 1314 | while lt k s bound 4096 |
| 1315 | if not islow get inb k |
| 1316 | ret false |
| 1317 | end |
| 1318 | k = add k 1 |
| 1319 | end |
| 1320 | put st s_ns add p a |
| 1321 | put st s_ne s |
| 1322 | ret true |
| 1323 | end |
| 1324 | |
| 1325 | fn startsb p ulong e ulong to bool |
| 1326 | a = len mk_b |
| 1327 | if lt sub e p a |
| 1328 | ret false |
| 1329 | end |
| 1330 | for i ulong a bound 16 |
| 1331 | if ne get inb add p i get mk_b i |
| 1332 | ret false |
| 1333 | end |
| 1334 | end |
| 1335 | ret true |
| 1336 | end |
| 1337 | |
| 1338 | note iat: inb at p holds the text t |
| 1339 | fn iat p ulong t view uchar * to bool |
| 1340 | for i ulong len t bound 64 |
| 1341 | if ne get inb add p i get t i |
| 1342 | ret false |
| 1343 | end |
| 1344 | end |
| 1345 | ret true |
| 1346 | end |
| 1347 | |
| 1348 | note ieq: inb[a, a+n) equals inb[b, b+n) |
| 1349 | fn ieq a ulong b ulong n ulong to bool |
| 1350 | for i ulong n bound 4096 |
| 1351 | if ne get inb add a i get inb add b i |
| 1352 | ret false |
| 1353 | end |
| 1354 | end |
| 1355 | ret true |
| 1356 | end |
| 1357 | |
| 1358 | note isend: the line inb[q, f) is the end marker of the current block |
| 1359 | fn isend q ulong f ulong to bool |
| 1360 | a = len mk_e |
| 1361 | ns = get st s_ns |
| 1362 | m = sub get st s_ne ns |
| 1363 | if ne sub f q add add a m len mk_t |
| 1364 | ret false |
| 1365 | end |
| 1366 | if not iat q mk_e |
| 1367 | ret false |
| 1368 | end |
| 1369 | if not ieq add q a ns m |
| 1370 | ret false |
| 1371 | end |
| 1372 | ret iat add add q a m mk_t |
| 1373 | end |
| 1374 | |
| 1375 | fn nmis t view uchar * to bool |
| 1376 | a = get st s_ns |
| 1377 | n = sub get st s_ne a |
| 1378 | if ne n len t |
| 1379 | ret false |
| 1380 | end |
| 1381 | for i ulong n bound 64 |
| 1382 | if ne get inb add a i get t i |
| 1383 | ret false |
| 1384 | end |
| 1385 | end |
| 1386 | ret true |
| 1387 | end |
| 1388 | |
| 1389 | fn render to bool |
| 1390 | if nmis b_pin |
| 1391 | rpin |
| 1392 | ret true |
| 1393 | end |
| 1394 | if nmis b_err |
| 1395 | rerr |
| 1396 | ret true |
| 1397 | end |
| 1398 | if nmis b_trap |
| 1399 | rtrap |
| 1400 | ret true |
| 1401 | end |
| 1402 | if nmis b_words |
| 1403 | rwords false |
| 1404 | ret true |
| 1405 | end |
| 1406 | if nmis b_host |
| 1407 | rwords true |
| 1408 | ret true |
| 1409 | end |
| 1410 | if nmis b_exam |
| 1411 | rexam |
| 1412 | ret true |
| 1413 | end |
| 1414 | if nmis b_gate |
| 1415 | rgate |
| 1416 | ret true |
| 1417 | end |
| 1418 | if nmis b_skill |
| 1419 | rskill |
| 1420 | ret true |
| 1421 | end |
| 1422 | ret false |
| 1423 | end |
| 1424 | |
| 1425 | note osame: obuf[ob, end) equals inb[a, b) |
| 1426 | fn osame ob ulong a ulong b ulong to bool |
| 1427 | on = get st s_on |
| 1428 | if ne sub on ob sub b a |
| 1429 | ret false |
| 1430 | end |
| 1431 | n = sub b a |
| 1432 | for i ulong n bound 16777216 |
| 1433 | if ne get obuf add ob i get inb add a i |
| 1434 | ret false |
| 1435 | end |
| 1436 | end |
| 1437 | ret true |
| 1438 | end |
| 1439 | |
| 1440 | note doblk: the block whose begin marker is the line inb[p, e); returns where the next line starts |
| 1441 | fn doblk p ulong e ulong to ulong |
| 1442 | n = get st s_in |
| 1443 | cpin p e |
| 1444 | ob = get st s_on |
| 1445 | ib = add e 1 |
| 1446 | q = ib |
| 1447 | f = n |
| 1448 | lines = ulong 0 |
| 1449 | found = false |
| 1450 | while lt q n bound 16777216 |
| 1451 | f = iend q |
| 1452 | if isend q f |
| 1453 | found = true |
| 1454 | break |
| 1455 | end |
| 1456 | if startsb q f |
| 1457 | break |
| 1458 | end |
| 1459 | lines = add lines 1 |
| 1460 | q = add f 1 |
| 1461 | end |
| 1462 | put st s_blk add get st s_blk 1 |
| 1463 | if not found |
| 1464 | fnd f_unt get st s_ns get st s_ne |
| 1465 | if gt q n |
| 1466 | q = n |
| 1467 | end |
| 1468 | cpraw ib q |
| 1469 | put st s_ln add get st s_ln lines |
| 1470 | ret q |
| 1471 | end |
| 1472 | if render |
| 1473 | if not osame ob ib q |
| 1474 | if eq get st s_gen 0 |
| 1475 | fnd f_stale get st s_ns get st s_ne |
| 1476 | end |
| 1477 | end |
| 1478 | else |
| 1479 | fnd f_unk get st s_ns get st s_ne |
| 1480 | put st s_on ob |
| 1481 | cpraw ib q |
| 1482 | end |
| 1483 | put st s_ln add get st s_ln add lines 1 |
| 1484 | cpin q f |
| 1485 | ret add f 1 |
| 1486 | end |
| 1487 | |
| 1488 | note isdate: inb at k is 20DD-DD-DD |
| 1489 | fn isdate k ulong to bool |
| 1490 | if or ne get inb k 50 ne get inb add k 1 48 |
| 1491 | ret false |
| 1492 | end |
| 1493 | if or not isdig get inb add k 2 not isdig get inb add k 3 |
| 1494 | ret false |
| 1495 | end |
| 1496 | if or ne get inb add k 4 45 ne get inb add k 7 45 |
| 1497 | ret false |
| 1498 | end |
| 1499 | if or not isdig get inb add k 5 not isdig get inb add k 6 |
| 1500 | ret false |
| 1501 | end |
| 1502 | ret and isdig get inb add k 8 isdig get inb add k 9 |
| 1503 | end |
| 1504 | |
| 1505 | fn blank p ulong e ulong to bool |
| 1506 | i = p |
| 1507 | while lt i e bound 16777216 |
| 1508 | c = get inb i |
| 1509 | if not or eq c 32 eq c 9 |
| 1510 | ret false |
| 1511 | end |
| 1512 | i = add i 1 |
| 1513 | end |
| 1514 | ret true |
| 1515 | end |
| 1516 | |
| 1517 | note ldate: the line inb[q, e) holds an ISO date |
| 1518 | fn ldate q ulong e ulong to bool |
| 1519 | if lt sub e q 10 |
| 1520 | ret false |
| 1521 | end |
| 1522 | k = q |
| 1523 | m = sub e 10 |
| 1524 | while le k m bound 16777216 |
| 1525 | if isdate k |
| 1526 | ret true |
| 1527 | end |
| 1528 | k = add k 1 |
| 1529 | end |
| 1530 | ret false |
| 1531 | end |
| 1532 | |
| 1533 | note pdate: the paragraph that starts at p (up to a blank line) holds an ISO date |
| 1534 | fn pdate p ulong to bool |
| 1535 | n = get st s_in |
| 1536 | q = p |
| 1537 | while lt q n bound 16777216 |
| 1538 | e = iend q |
| 1539 | if blank q e |
| 1540 | ret false |
| 1541 | end |
| 1542 | if ldate q e |
| 1543 | ret true |
| 1544 | end |
| 1545 | q = add e 1 |
| 1546 | end |
| 1547 | ret false |
| 1548 | end |
| 1549 | |
| 1550 | fn ishexw s ulong t ulong to bool |
| 1551 | d = false |
| 1552 | l = false |
| 1553 | i = s |
| 1554 | while lt i t bound 16777216 |
| 1555 | c = get inb i |
| 1556 | if not ishex c |
| 1557 | ret false |
| 1558 | end |
| 1559 | if isdig c |
| 1560 | d = true |
| 1561 | else |
| 1562 | l = true |
| 1563 | end |
| 1564 | i = add i 1 |
| 1565 | end |
| 1566 | ret and d l |
| 1567 | end |
| 1568 | |
| 1569 | note fold: c with A-F read as a-f |
| 1570 | fn fold c uchar to uchar |
| 1571 | if and ge c 65 le c 70 |
| 1572 | ret add c 32 |
| 1573 | end |
| 1574 | ret c |
| 1575 | end |
| 1576 | |
| 1577 | note pinpre: inb[s, t), with A-F read as a-f, begins the PIN revision or the omc sha256 |
| 1578 | fn pinpre s ulong t ulong to bool |
| 1579 | n = sub t s |
| 1580 | r = le n 40 |
| 1581 | if r |
| 1582 | for i ulong n bound 40 |
| 1583 | c = fold get inb add s i |
| 1584 | if ne c get pin add 9 i |
| 1585 | r = false |
| 1586 | end |
| 1587 | end |
| 1588 | end |
| 1589 | if r |
| 1590 | ret true |
| 1591 | end |
| 1592 | if gt n 64 |
| 1593 | ret false |
| 1594 | end |
| 1595 | for j ulong n bound 64 |
| 1596 | c = fold get inb add s j |
| 1597 | if ne c get pin add 61 j |
| 1598 | ret false |
| 1599 | end |
| 1600 | end |
| 1601 | ret true |
| 1602 | end |
| 1603 | |
| 1604 | fn alldig s ulong t ulong to bool |
| 1605 | i = s |
| 1606 | while lt i t bound 16777216 |
| 1607 | if not isdig get inb i |
| 1608 | ret false |
| 1609 | end |
| 1610 | i = add i 1 |
| 1611 | end |
| 1612 | ret true |
| 1613 | end |
| 1614 | |
| 1615 | fn lc c uchar to uchar |
| 1616 | if and ge c 65 le c 90 |
| 1617 | ret add c 32 |
| 1618 | end |
| 1619 | ret c |
| 1620 | end |
| 1621 | |
| 1622 | note spelled: inb[s, t) is one of the number words one .. twelve, in any case |
| 1623 | fn spelled s ulong t ulong to bool |
| 1624 | n = len k_num |
| 1625 | w = sub t s |
| 1626 | a = ulong 0 |
| 1627 | while lt a n bound 4096 |
| 1628 | b = a |
| 1629 | while lt b n bound 4096 |
| 1630 | if eq get k_num b 32 |
| 1631 | break |
| 1632 | end |
| 1633 | b = add b 1 |
| 1634 | end |
| 1635 | if eq sub b a w |
| 1636 | same = true |
| 1637 | for i ulong w bound 16 |
| 1638 | if ne lc get inb add s i get k_num add a i |
| 1639 | same = false |
| 1640 | end |
| 1641 | end |
| 1642 | if same |
| 1643 | ret true |
| 1644 | end |
| 1645 | end |
| 1646 | a = add b 1 |
| 1647 | end |
| 1648 | ret false |
| 1649 | end |
| 1650 | |
| 1651 | note nounat: inb at q is the word t, ended by a non-word byte or the line end; its length, else 0 |
| 1652 | fn nounat q ulong e ulong t view uchar * to ulong |
| 1653 | n = len t |
| 1654 | if gt add q n e |
| 1655 | ret 0 |
| 1656 | end |
| 1657 | for i ulong n bound 64 |
| 1658 | if ne get inb add q i get t i |
| 1659 | ret 0 |
| 1660 | end |
| 1661 | end |
| 1662 | r = add q n |
| 1663 | if lt r e |
| 1664 | if isw get inb r |
| 1665 | ret 0 |
| 1666 | end |
| 1667 | end |
| 1668 | ret n |
| 1669 | end |
| 1670 | |
| 1671 | fn nounsp q ulong e ulong to ulong |
| 1672 | k = nounat q e n_sk |
| 1673 | if gt k 0 |
| 1674 | ret k |
| 1675 | end |
| 1676 | k = nounat q e n_pr |
| 1677 | if gt k 0 |
| 1678 | ret k |
| 1679 | end |
| 1680 | ret nounat q e n_ex |
| 1681 | end |
| 1682 | |
| 1683 | fn nounall q ulong e ulong to ulong |
| 1684 | k = nounsp q e |
| 1685 | if gt k 0 |
| 1686 | ret k |
| 1687 | end |
| 1688 | k = nounat q e n_ep |
| 1689 | if gt k 0 |
| 1690 | ret k |
| 1691 | end |
| 1692 | k = nounat q e n_we |
| 1693 | if gt k 0 |
| 1694 | ret k |
| 1695 | end |
| 1696 | k = nounat q e n_hw |
| 1697 | if gt k 0 |
| 1698 | ret k |
| 1699 | end |
| 1700 | k = nounat q e n_wd |
| 1701 | if gt k 0 |
| 1702 | ret k |
| 1703 | end |
| 1704 | k = nounat q e n_kw |
| 1705 | if gt k 0 |
| 1706 | ret k |
| 1707 | end |
| 1708 | k = nounat q e n_en |
| 1709 | if gt k 0 |
| 1710 | ret k |
| 1711 | end |
| 1712 | k = nounat q e n_ec |
| 1713 | if gt k 0 |
| 1714 | ret k |
| 1715 | end |
| 1716 | ret nounat q e n_tp |
| 1717 | end |
| 1718 | |
| 1719 | fn tokis s ulong t ulong w view uchar * to bool |
| 1720 | if ne sub t s len w |
| 1721 | ret false |
| 1722 | end |
| 1723 | for i ulong len w bound 64 |
| 1724 | if ne get inb add s i get w i |
| 1725 | ret false |
| 1726 | end |
| 1727 | end |
| 1728 | ret true |
| 1729 | end |
| 1730 | |
| 1731 | note tok: judge the word inb[s, t) of a line that ends at e |
| 1732 | fn tok s ulong t ulong e ulong to void |
| 1733 | if or ne get st s_date 0 ne get st s_hd 0 |
| 1734 | ret |
| 1735 | end |
| 1736 | if ge sub t s 7 |
| 1737 | if pinpre s t |
| 1738 | fnd f_pin s t |
| 1739 | ret |
| 1740 | end |
| 1741 | if ishexw s t |
| 1742 | ret |
| 1743 | end |
| 1744 | end |
| 1745 | if ge t e |
| 1746 | ret |
| 1747 | end |
| 1748 | if gt s 0 |
| 1749 | if eq get inb sub s 1 45 |
| 1750 | ret |
| 1751 | end |
| 1752 | end |
| 1753 | c = get inb t |
| 1754 | q = add t 1 |
| 1755 | if eq c 32 |
| 1756 | k = ulong 0 |
| 1757 | if alldig s t |
| 1758 | k = nounall q e |
| 1759 | else |
| 1760 | if spelled s t |
| 1761 | k = nounsp q e |
| 1762 | end |
| 1763 | end |
| 1764 | if gt k 0 |
| 1765 | fnd f_cnt s add q k |
| 1766 | end |
| 1767 | ret |
| 1768 | end |
| 1769 | if and eq c 61 tokis s t n_ex |
| 1770 | if lt q e |
| 1771 | if isdig get inb q |
| 1772 | r = q |
| 1773 | while lt r e bound 64 |
| 1774 | if not isdig get inb r |
| 1775 | break |
| 1776 | end |
| 1777 | r = add r 1 |
| 1778 | end |
| 1779 | fnd f_cnt s r |
| 1780 | end |
| 1781 | end |
| 1782 | end |
| 1783 | end |
| 1784 | |
| 1785 | fn scanl p ulong e ulong to void |
| 1786 | i = p |
| 1787 | while lt i e bound 16777216 |
| 1788 | if isw get inb i |
| 1789 | j = i |
| 1790 | while lt j e bound 16777216 |
| 1791 | if not isw get inb j |
| 1792 | break |
| 1793 | end |
| 1794 | j = add j 1 |
| 1795 | end |
| 1796 | tok i j e |
| 1797 | i = j |
| 1798 | else |
| 1799 | i = add i 1 |
| 1800 | end |
| 1801 | end |
| 1802 | end |
| 1803 | |
| 1804 | fn dofile to void |
| 1805 | put st s_on 0 |
| 1806 | put st s_ln 0 |
| 1807 | put st s_date 0 |
| 1808 | put st s_hd 0 |
| 1809 | n = get st s_in |
| 1810 | para = true |
| 1811 | p = ulong 0 |
| 1812 | while lt p n bound 16777216 |
| 1813 | e = iend p |
| 1814 | put st s_ln add get st s_ln 1 |
| 1815 | if isbeg p e |
| 1816 | p = doblk p e |
| 1817 | para = true |
| 1818 | else |
| 1819 | if blank p e |
| 1820 | para = true |
| 1821 | else |
| 1822 | if and ne get st s_md 0 eq get inb p 35 |
| 1823 | put st s_hd 0 |
| 1824 | if ldate p e |
| 1825 | put st s_hd 1 |
| 1826 | end |
| 1827 | end |
| 1828 | if para |
| 1829 | put st s_date 0 |
| 1830 | if pdate p |
| 1831 | put st s_date 1 |
| 1832 | end |
| 1833 | para = false |
| 1834 | end |
| 1835 | scanl p e |
| 1836 | end |
| 1837 | cpin p e |
| 1838 | p = add e 1 |
| 1839 | end |
| 1840 | end |
| 1841 | end |
| 1842 | |
| 1843 | note ---------------------------------------------------------------- paths |
| 1844 | |
| 1845 | fn pathis t view uchar * to bool |
| 1846 | if ne get st s_pl len t |
| 1847 | ret false |
| 1848 | end |
| 1849 | for i ulong len t bound 64 |
| 1850 | if ne get pth i get t i |
| 1851 | ret false |
| 1852 | end |
| 1853 | end |
| 1854 | ret true |
| 1855 | end |
| 1856 | |
| 1857 | fn pathpre t view uchar * to bool |
| 1858 | if lt get st s_pl len t |
| 1859 | ret false |
| 1860 | end |
| 1861 | for i ulong len t bound 64 |
| 1862 | if ne get pth i get t i |
| 1863 | ret false |
| 1864 | end |
| 1865 | end |
| 1866 | ret true |
| 1867 | end |
| 1868 | |
| 1869 | fn wobuf f int to bool |
| 1870 | n = get st s_on |
| 1871 | p = ulong 0 |
| 1872 | while lt p n bound 16777216 |
| 1873 | k = write f obuf p sub n p |
| 1874 | if le k 0 |
| 1875 | ret false |
| 1876 | end |
| 1877 | p = add p ulong k |
| 1878 | end |
| 1879 | ret true |
| 1880 | end |
| 1881 | |
| 1882 | note writef: obuf to the current path, through a temporary file and a rename |
| 1883 | fn writef to bool |
| 1884 | pl = get st s_pl |
| 1885 | x = len x_tmp |
| 1886 | tl = add pl x |
| 1887 | put pk 0 2 |
| 1888 | put pk 1 0 |
| 1889 | put pk 2 uchar and tl 255 |
| 1890 | put pk 3 uchar shr tl 8 |
| 1891 | for i ulong pl bound 4096 |
| 1892 | put pk add 4 i get pth i |
| 1893 | end |
| 1894 | for j ulong x bound 16 |
| 1895 | put pk add add 4 pl j get x_tmp j |
| 1896 | end |
| 1897 | r = add 4 tl |
| 1898 | put pk r uchar and pl 255 |
| 1899 | put pk add r 1 uchar shr pl 8 |
| 1900 | for k ulong pl bound 4096 |
| 1901 | put pk add add r 2 k get pth k |
| 1902 | end |
| 1903 | f = fopen pk 4 tl 577 420 |
| 1904 | if lt f 0 |
| 1905 | ret false |
| 1906 | end |
| 1907 | ok = wobuf int f |
| 1908 | y = fsync int f |
| 1909 | z = fclose int f |
| 1910 | if or not ok lt y 0 |
| 1911 | ret false |
| 1912 | end |
| 1913 | v = renames pk 0 add add r 2 pl 0 |
| 1914 | ret ge v 0 |
| 1915 | end |
| 1916 | |
| 1917 | fn pathmd to bool |
| 1918 | m = get st s_pl |
| 1919 | if le m 3 |
| 1920 | ret false |
| 1921 | end |
| 1922 | if ne get pth sub m 3 46 |
| 1923 | ret false |
| 1924 | end |
| 1925 | if ne get pth sub m 2 109 |
| 1926 | ret false |
| 1927 | end |
| 1928 | ret eq get pth sub m 1 100 |
| 1929 | end |
| 1930 | |
| 1931 | fn dopath p ulong e ulong to void |
| 1932 | m = sub e p |
| 1933 | if ge m 4096 |
| 1934 | wt2 m_long |
| 1935 | put st s_err 1 |
| 1936 | ret |
| 1937 | end |
| 1938 | for i ulong m bound 4096 |
| 1939 | put pth i get fl add p i |
| 1940 | end |
| 1941 | put st s_pl m |
| 1942 | put st s_md 0 |
| 1943 | if pathmd |
| 1944 | put st s_md 1 |
| 1945 | end |
| 1946 | if or pathis x_pin or pathpre x_data pathis x_zh |
| 1947 | ret |
| 1948 | end |
| 1949 | f = fopen pth 0 m 0 0 |
| 1950 | if lt f 0 |
| 1951 | ferr m_open |
| 1952 | ret |
| 1953 | end |
| 1954 | r = rdin int f |
| 1955 | z = fclose int f |
| 1956 | if eq r 2 |
| 1957 | ferr m_full |
| 1958 | ret |
| 1959 | end |
| 1960 | if ne r 0 |
| 1961 | ferr m_open |
| 1962 | ret |
| 1963 | end |
| 1964 | n = get st s_in |
| 1965 | put st s_nf add get st s_nf 1 |
| 1966 | if gt n get st s_max |
| 1967 | put st s_max n |
| 1968 | end |
| 1969 | if gt n halfb |
| 1970 | put st s_ln 0 |
| 1971 | fnd f_alarm 0 0 |
| 1972 | end |
| 1973 | dofile |
| 1974 | if osame 0 0 n |
| 1975 | ret |
| 1976 | end |
| 1977 | if eq get st s_gen 1 |
| 1978 | if writef |
| 1979 | put st s_rw add get st s_rw 1 |
| 1980 | else |
| 1981 | ferr m_wr |
| 1982 | end |
| 1983 | end |
| 1984 | end |
| 1985 | |
| 1986 | fn argis t view uchar * k ulong to bool |
| 1987 | if ne k len t |
| 1988 | ret false |
| 1989 | end |
| 1990 | for i ulong k bound 16 |
| 1991 | if ne get ab i get t i |
| 1992 | ret false |
| 1993 | end |
| 1994 | end |
| 1995 | ret true |
| 1996 | end |
| 1997 | |
| 1998 | fn main to int |
| 1999 | if ne argcnt 2 |
| 2000 | wt2 m_use |
| 2001 | ret 2 |
| 2002 | end |
| 2003 | a = argread 1 ab 0 16 |
| 2004 | if lt a 0 |
| 2005 | wt2 m_use |
| 2006 | ret 2 |
| 2007 | end |
| 2008 | k = ulong a |
| 2009 | mode = ulong 3 |
| 2010 | if argis a_check k |
| 2011 | mode = ulong 0 |
| 2012 | end |
| 2013 | if argis a_gen k |
| 2014 | mode = ulong 1 |
| 2015 | end |
| 2016 | if argis a_pinof k |
| 2017 | mode = ulong 2 |
| 2018 | end |
| 2019 | if eq mode 3 |
| 2020 | wt2 m_use |
| 2021 | ret 2 |
| 2022 | end |
| 2023 | if not ldpin |
| 2024 | wt2 m_pin |
| 2025 | ret 2 |
| 2026 | end |
| 2027 | if eq mode 2 |
| 2028 | if ne rddat 0 0 |
| 2029 | wt2 m_dat |
| 2030 | ret 2 |
| 2031 | end |
| 2032 | if datpin |
| 2033 | ret 0 |
| 2034 | end |
| 2035 | ret 1 |
| 2036 | end |
| 2037 | put st s_gen mode |
| 2038 | f = fopen kdatf 0 len kdatf 0 0 |
| 2039 | if lt f 0 |
| 2040 | wt2 m_dat |
| 2041 | ret 2 |
| 2042 | end |
| 2043 | r = rddat int f |
| 2044 | z = fclose int f |
| 2045 | if or ne r 0 not datpin |
| 2046 | wt2 m_dat |
| 2047 | ret 2 |
| 2048 | end |
| 2049 | if ne rdfl 0 0 |
| 2050 | wt2 m_use |
| 2051 | ret 2 |
| 2052 | end |
| 2053 | if or gt get st s_dn halfb gt get st s_fn halfb |
| 2054 | wt m_dalarm |
| 2055 | put st s_bad add get st s_bad 1 |
| 2056 | end |
| 2057 | n = get st s_fn |
| 2058 | p = ulong 0 |
| 2059 | while lt p n bound 16777216 |
| 2060 | e = fend p |
| 2061 | if gt e p |
| 2062 | dopath p e |
| 2063 | end |
| 2064 | p = add e 1 |
| 2065 | end |
| 2066 | wt m_sum1 |
| 2067 | wnum 1 get st s_nf |
| 2068 | wt m_sum2 |
| 2069 | wnum 1 get st s_blk |
| 2070 | wt m_sum3 |
| 2071 | wnum 1 get st s_rw |
| 2072 | wt m_sum4 |
| 2073 | wnum 1 get st s_bad |
| 2074 | wt m_sum5 |
| 2075 | wnum 1 get st s_max |
| 2076 | wt nl |
| 2077 | if ne get st s_err 0 |
| 2078 | ret 2 |
| 2079 | end |
| 2080 | if ne get st s_bad 0 |
| 2081 | wt m_hint |
| 2082 | ret 1 |
| 2083 | end |
| 2084 | ret 0 |
| 2085 | end |