omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / seed / node / tool.om
| 1 | note Omega Node tool adapter (DRA-290): the typed coding tools of the |
| 2 | note round40 repair protocol, one bounded process per granted tool. OM-2: |
| 3 | note { printf 'om2\n'; cat seed/agent/path.om seed/node/gram.om seed/node/tool.om; } | omc |
| 4 | note The Node's authority runs it in its own process group (auth_wtool), so |
| 5 | note cancellation kills it and every compiler or test run it started. |
| 6 | note Usage: |
| 7 | note omnode-tool read ROOT PATH |
| 8 | note omnode-tool replace ROOT PATH OLD NEW |
| 9 | note omnode-tool check ROOT PATH WORK OMC KIND K N A |
| 10 | note ROOT is the workspace; PATH must pass omagent's path rule (agpathok, |
| 11 | note seed/agent/path.om) inside it, and the file is opened without following |
| 12 | note a final symbolic link. Files are at most 3800 bytes. |
| 13 | note Record on stdout, exit 0: one line OMNT1 status olen clen, then the |
| 14 | note observation and the file content back to back. Status: 0 done (Read |
| 15 | note succeeded., Edit saved., Tests passed.), 1 File not found., 2 path |
| 16 | note refused, 3 file over 3800 bytes, 4 I/O error, 5 replace refused (old |
| 17 | note must occur exactly once, 1..40 bytes; new 0..40), 6 check failed (the |
| 18 | note observation says why). Exit 2: usage. |
| 19 | note replace writes the new content to PATH.omnode-new, syncs it, renames it |
| 20 | note over PATH and syncs the directory: Edit saved. means durable. |
| 21 | note check follows round40 repair.py compile_test: a source that is empty |
| 22 | note or over 600 bytes is refused; it is compiled with OMC (compile only); |
| 23 | note a compiler error is returned as omc printed it, trimmed; then every |
| 24 | note line must be in the bounded repair grammar, calc and main must appear |
| 25 | note once each, main must keep ret calc int A, and a loop goal needs its |
| 26 | note for loop. Then for each input of A, 0, 3, 7 (distinct, in order) the |
| 27 | note candidate with ret calc int replaced is compiled in WORK and run with |
| 28 | note execpure (Landlock, no new privileges, CPU, file, process and memory |
| 29 | note ceilings, seccomp; at most 2 s wall clock, then SIGKILL), and its exit |
| 30 | note status must equal the goal (KIND 1 add x+K, 2 mul x*K, 3 loop x+K*N) |
| 31 | note with no output. The content returned is the file as checked. |
| 32 | text tnew ".omnode-new" |
| 33 | text trec "OMNT1 " |
| 34 | text oread "Read succeeded." |
| 35 | text onf "File not found." |
| 36 | text oedit "Edit saved." |
| 37 | text opass "Tests passed." |
| 38 | text orefu "Refused unsupported source." |
| 39 | text ofns "Required functions missing." |
| 40 | text omain "main must preserve the requested input." |
| 41 | text oloop "Required loop missing." |
| 42 | text otf1 "Test failed: calc(" |
| 43 | text otf2 ") expected " |
| 44 | text otf3 ", got " |
| 45 | text otf4 "." |
| 46 | text orepl "Old text must occur exactly once." |
| 47 | text opath "Path refused." |
| 48 | text obig "File is over 3800 bytes." |
| 49 | text oio "I/O error." |
| 50 | text cread "read" |
| 51 | text crepl "replace" |
| 52 | text ccheck "check" |
| 53 | text fsrc "c.om" |
| 54 | text fbin "c.bin" |
| 55 | text ferr "c.err" |
| 56 | text fout "c.out" |
| 57 | text fnull "/dev/null" |
| 58 | |
| 59 | note ======== records |
| 60 | |
| 61 | fn tl_out o view uchar * n ulong to void |
| 62 | p = ulong 0 |
| 63 | for it ulong 4096 |
| 64 | if ge p n |
| 65 | ret |
| 66 | end |
| 67 | r = write 1 o p sub n p |
| 68 | if le r 0 |
| 69 | ret |
| 70 | end |
| 71 | p = add p ulong r |
| 72 | end |
| 73 | end |
| 74 | |
| 75 | note The number of decimal digits of v. |
| 76 | fn tl_nd v ulong to ulong |
| 77 | if lt v 10 |
| 78 | ret 1 |
| 79 | end |
| 80 | ret add 1 tl_nd div v 10 |
| 81 | end |
| 82 | |
| 83 | note Append the decimal v to o at p; the new end. |
| 84 | fn tl_dec o edit uchar * p ulong v ulong to ulong |
| 85 | d = new uchar 24 0 |
| 86 | k = ulong 0 |
| 87 | x = v |
| 88 | for i ulong 24 |
| 89 | put d k uchar add 48 rem x 10 |
| 90 | k = add k 1 |
| 91 | x = div x 10 |
| 92 | if eq x 0 |
| 93 | break |
| 94 | end |
| 95 | end |
| 96 | for j ulong k |
| 97 | put o add p j get d sub sub k j 1 |
| 98 | end |
| 99 | ret add p k |
| 100 | end |
| 101 | |
| 102 | fn tl_cat o edit uchar * p ulong a view uchar * n ulong to ulong |
| 103 | for i ulong n |
| 104 | put o add p i get a i |
| 105 | end |
| 106 | ret add p n |
| 107 | end |
| 108 | |
| 109 | note The record: status st, observation ob[0, on), content cb[0, cn). |
| 110 | fn tl_rec st ulong ob view uchar * cb view uchar * cn ulong to int |
| 111 | o = new uchar 8192 0 |
| 112 | p = tl_cat o 0 trec len trec |
| 113 | p = tl_dec o p st |
| 114 | put o p 32 |
| 115 | p = tl_dec o add p 1 len ob |
| 116 | put o p 32 |
| 117 | p = tl_dec o add p 1 cn |
| 118 | put o p 10 |
| 119 | p = tl_cat o add p 1 ob len ob |
| 120 | p = tl_cat o p cb cn |
| 121 | tl_out o p |
| 122 | ret 0 |
| 123 | end |
| 124 | |
| 125 | note ======== files |
| 126 | |
| 127 | note Read path pa into b; the length, -2 not found, -3 over 3800 bytes, |
| 128 | note -4 another error (not a regular file included). |
| 129 | fn tl_slurp pa view uchar * b edit uchar 4096 to long |
| 130 | st = new uchar 128 0 |
| 131 | fd = fopen pa 0 len pa 557056 0 |
| 132 | if eq fd -2 |
| 133 | ret neg long 2 |
| 134 | end |
| 135 | if lt fd 0 |
| 136 | ret neg long 4 |
| 137 | end |
| 138 | r = fstat int fd st 0 128 |
| 139 | md = or ulong get st 16 shl ulong get st 17 8 |
| 140 | if or lt r 0 ne and md 61440 32768 |
| 141 | x = fclose int fd |
| 142 | ret neg long 4 |
| 143 | end |
| 144 | n = ulong 0 |
| 145 | for it ulong 4096 |
| 146 | if ge n 4096 |
| 147 | break |
| 148 | end |
| 149 | k = read int fd b n sub 4096 n |
| 150 | if le k 0 |
| 151 | break |
| 152 | end |
| 153 | n = add n ulong k |
| 154 | end |
| 155 | x = fclose int fd |
| 156 | if gt n 3800 |
| 157 | ret neg long 3 |
| 158 | end |
| 159 | ret long n |
| 160 | end |
| 161 | |
| 162 | note Write b[0, n) to the new file pa (truncated, no final symbolic link) |
| 163 | note and sync it. 0 or negative. |
| 164 | fn tl_spit pa view uchar * b view uchar * n ulong to long |
| 165 | fd = fopen pa 0 len pa 557633 420 |
| 166 | if lt fd 0 |
| 167 | ret fd |
| 168 | end |
| 169 | p = ulong 0 |
| 170 | for it ulong 4096 |
| 171 | if ge p n |
| 172 | break |
| 173 | end |
| 174 | k = write int fd b p sub n p |
| 175 | if le k 0 |
| 176 | x = fclose int fd |
| 177 | ret neg long 5 |
| 178 | end |
| 179 | p = add p ulong k |
| 180 | end |
| 181 | r = fsync int fd |
| 182 | x = fclose int fd |
| 183 | ret r |
| 184 | end |
| 185 | |
| 186 | note Rename a over b (packed list), then sync the directory of b. |
| 187 | fn tl_mv a view uchar * b view uchar * to long |
| 188 | pk = new uchar 4096 0 |
| 189 | na = len a |
| 190 | nb = len b |
| 191 | put pk 0 2 |
| 192 | put pk 2 uchar and na 255 |
| 193 | put pk 3 uchar shr na 8 |
| 194 | for i ulong na |
| 195 | put pk add 4 i get a i |
| 196 | end |
| 197 | o = add 4 na |
| 198 | put pk o uchar and nb 255 |
| 199 | put pk add o 1 uchar shr nb 8 |
| 200 | for j ulong nb |
| 201 | put pk add add o 2 j get b j |
| 202 | end |
| 203 | r = renames pk 0 add add o 2 nb 0 |
| 204 | if lt r 0 |
| 205 | ret r |
| 206 | end |
| 207 | s = ulong 0 |
| 208 | for k ulong nb |
| 209 | if eq get b k 47 |
| 210 | s = k |
| 211 | end |
| 212 | end |
| 213 | dn = new uchar 4096 0 |
| 214 | dl = ulong 1 |
| 215 | put dn 0 46 |
| 216 | if gt s 0 |
| 217 | for m ulong s |
| 218 | put dn m get b m |
| 219 | end |
| 220 | dl = s |
| 221 | end |
| 222 | fd = fopen dn 0 dl 540672 0 |
| 223 | if lt fd 0 |
| 224 | ret fd |
| 225 | end |
| 226 | r = fsync int fd |
| 227 | x = fclose int fd |
| 228 | ret r |
| 229 | end |
| 230 | |
| 231 | note ======== read and replace |
| 232 | |
| 233 | fn tl_read pa view uchar * to int |
| 234 | b = new uchar 4096 0 |
| 235 | if not agpathok pa |
| 236 | ret tl_rec 2 opath b 0 |
| 237 | end |
| 238 | n = tl_slurp pa b |
| 239 | if eq n -2 |
| 240 | ret tl_rec 1 onf b 0 |
| 241 | end |
| 242 | if eq n -3 |
| 243 | ret tl_rec 3 obig b 0 |
| 244 | end |
| 245 | if lt n 0 |
| 246 | ret tl_rec 4 oio b 0 |
| 247 | end |
| 248 | ret tl_rec 0 oread b ulong n |
| 249 | end |
| 250 | |
| 251 | note true when a occurs in b at offset i (b holds at least i + len a bytes). |
| 252 | fn tl_at b view uchar 4096 i ulong a view uchar * to bool |
| 253 | for j ulong len a |
| 254 | if ne get b add i j get a j |
| 255 | ret false |
| 256 | end |
| 257 | end |
| 258 | ret true |
| 259 | end |
| 260 | |
| 261 | note Occurrences of a in b[0, bn), counted without overlap. |
| 262 | fn tl_count b view uchar 4096 bn ulong a view uchar * to ulong |
| 263 | an = len a |
| 264 | c = ulong 0 |
| 265 | i = ulong 0 |
| 266 | for it ulong add bn 1 |
| 267 | if gt add i an bn |
| 268 | ret c |
| 269 | end |
| 270 | if tl_at b i a |
| 271 | c = add c 1 |
| 272 | i = add i an |
| 273 | else |
| 274 | i = add i 1 |
| 275 | end |
| 276 | end |
| 277 | ret c |
| 278 | end |
| 279 | |
| 280 | note The offset of the first occurrence of a in b[0, bn) (one is known). |
| 281 | fn tl_find b view uchar 4096 bn ulong a view uchar * to ulong |
| 282 | for i ulong bn |
| 283 | if tl_at b i a |
| 284 | ret i |
| 285 | end |
| 286 | end |
| 287 | ret bn |
| 288 | end |
| 289 | |
| 290 | note Copy b[s, n) to d at p; the new end. |
| 291 | fn tl_tail d edit uchar 4096 p ulong b view uchar 4096 s ulong n ulong to ulong |
| 292 | for i ulong sub n s |
| 293 | put d add p i get b add s i |
| 294 | end |
| 295 | ret add p sub n s |
| 296 | end |
| 297 | |
| 298 | note Durably replace pa with d[0, dn): PATH.omnode-new written and synced, |
| 299 | note renamed over pa, directory synced. 0 or negative. |
| 300 | fn tl_save pa view uchar * d view uchar 4096 dn ulong to long |
| 301 | tn = new uchar 4096 0 |
| 302 | q = tl_cat tn 0 pa len pa |
| 303 | q = tl_cat tn q tnew len tnew |
| 304 | r = tl_spit view part tn 0 q d dn |
| 305 | if eq r 0 |
| 306 | r = tl_mv view part tn 0 q pa |
| 307 | end |
| 308 | if lt r 0 |
| 309 | x = unlink tn 0 q |
| 310 | end |
| 311 | ret r |
| 312 | end |
| 313 | |
| 314 | note replace with PATH at d[0, L0), OLD at aw[0, L1), NEW at aw[1024, L2). |
| 315 | fn tl_repl d view uchar 8192 aw view uchar 2048 l view ulong 4 to int |
| 316 | b = new uchar 4096 0 |
| 317 | o = new uchar 4096 0 |
| 318 | if not agpathok view part d 0 get l 0 |
| 319 | ret tl_rec 2 opath b 0 |
| 320 | end |
| 321 | if or or eq get l 1 0 gt get l 1 40 gt get l 2 40 |
| 322 | ret tl_rec 5 orepl b 0 |
| 323 | end |
| 324 | n = tl_slurp view part d 0 get l 0 b |
| 325 | if eq n -2 |
| 326 | ret tl_rec 1 onf b 0 |
| 327 | end |
| 328 | if eq n -3 |
| 329 | ret tl_rec 3 obig b 0 |
| 330 | end |
| 331 | if lt n 0 |
| 332 | ret tl_rec 4 oio b 0 |
| 333 | end |
| 334 | if ne tl_count b ulong n view part aw 0 get l 1 1 |
| 335 | ret tl_rec 5 orepl b ulong n |
| 336 | end |
| 337 | p = tl_edit b ulong n aw l o |
| 338 | if gt p 3800 |
| 339 | ret tl_rec 3 obig b ulong n |
| 340 | end |
| 341 | if lt tl_save view part d 0 get l 0 o p 0 |
| 342 | ret tl_rec 4 oio b ulong n |
| 343 | end |
| 344 | ret tl_rec 0 oedit o p |
| 345 | end |
| 346 | |
| 347 | note b[0, n) with its one OLD replaced by NEW into o; the length. |
| 348 | fn tl_edit b view uchar 4096 n ulong aw view uchar 2048 l view ulong 4 o edit uchar 4096 to ulong |
| 349 | i = tl_find b n view part aw 0 get l 1 |
| 350 | p = tl_cat o 0 b i |
| 351 | p = tl_cat o p view part aw 1024 get l 2 get l 2 |
| 352 | ret tl_tail o p b add i get l 1 n |
| 353 | end |
| 354 | |
| 355 | note ======== check |
| 356 | |
| 357 | note The trimmed span of b[0, n) (ASCII white space) into m[0], m[1]. |
| 358 | fn tl_trim b view uchar 4096 n ulong m edit ulong 16 to void |
| 359 | s = ulong 0 |
| 360 | for i ulong n |
| 361 | if gt get b s 32 |
| 362 | break |
| 363 | end |
| 364 | s = add s 1 |
| 365 | end |
| 366 | e = n |
| 367 | for j ulong sub n s |
| 368 | if gt get b sub e 1 32 |
| 369 | break |
| 370 | end |
| 371 | e = sub e 1 |
| 372 | end |
| 373 | put m 0 s |
| 374 | put m 1 e |
| 375 | end |
| 376 | |
| 377 | note Grammar and structure of b[0, n): 0 good, else the refusal (1 |
| 378 | note unsupported, 2 functions, 3 main input, 4 loop). The value of the one |
| 379 | note ret calc int line goes to m[2], its line start to m[3], end to m[4]. |
| 380 | fn tl_shape b view uchar 4096 n ulong kind ulong m edit ulong 16 to ulong |
| 381 | tl_trim b n m |
| 382 | for q ulong 4 |
| 383 | put m add 10 q 0 |
| 384 | end |
| 385 | a = get m 0 |
| 386 | for it ulong add n 1 |
| 387 | if gt a get m 1 |
| 388 | break |
| 389 | end |
| 390 | z = gr_eol b a get m 1 |
| 391 | if not tl_tally b a z m |
| 392 | ret 1 |
| 393 | end |
| 394 | a = add z 1 |
| 395 | end |
| 396 | if or ne get m 10 1 ne get m 11 1 |
| 397 | ret 2 |
| 398 | end |
| 399 | if ne get m 12 1 |
| 400 | ret 3 |
| 401 | end |
| 402 | if and eq kind 3 eq get m 13 0 |
| 403 | ret 4 |
| 404 | end |
| 405 | ret 0 |
| 406 | end |
| 407 | |
| 408 | note Count line b[a, z) into m: 10 calc, 11 main, 12 ret calc int (value |
| 409 | note to m[2], span to m[3], m[4]), 13 for loops. false outside the grammar. |
| 410 | fn tl_tally b view uchar 4096 a ulong z ulong m edit ulong 16 to bool |
| 411 | if not gr_gram b a z |
| 412 | ret false |
| 413 | end |
| 414 | if gr_lin b a z 0 0 |
| 415 | put m 10 add get m 10 1 |
| 416 | end |
| 417 | if gr_lin b a z 1 0 |
| 418 | put m 11 add get m 11 1 |
| 419 | end |
| 420 | if gr_lin b a z 9 1 |
| 421 | put m 13 add get m 13 1 |
| 422 | end |
| 423 | if gr_lin b a z 5 2 |
| 424 | put m 12 add get m 12 1 |
| 425 | put m 2 tl_num b add a len grcal z |
| 426 | put m 3 a |
| 427 | put m 4 z |
| 428 | end |
| 429 | ret true |
| 430 | end |
| 431 | |
| 432 | fn tl_num b view uchar 4096 a ulong e ulong to ulong |
| 433 | v = ulong 0 |
| 434 | for i ulong sub e a |
| 435 | v = add mul v 10 sub ulong get b add a i 48 |
| 436 | end |
| 437 | ret v |
| 438 | end |
| 439 | |
| 440 | note ======== processes |
| 441 | |
| 442 | note Wait for pid up to dl ns; kill its group at the deadline. The wait |
| 443 | note status word (exit code in byte 1, signal in byte 0's low 7 bits). |
| 444 | fn tl_wait pid long dl ulong to ulong |
| 445 | ws = new uchar 4 0 |
| 446 | t0 = ulong monons |
| 447 | for it ulong 100000 |
| 448 | g = waitpid int pid ws 0 4 1 |
| 449 | if gt g 0 |
| 450 | ret or ulong get ws 0 shl ulong get ws 1 8 |
| 451 | end |
| 452 | if lt g 0 |
| 453 | ret 65535 |
| 454 | end |
| 455 | if ge sub ulong monons t0 dl |
| 456 | x = killpid neg int pid 9 |
| 457 | g = waitpid int pid ws 0 4 0 |
| 458 | ret 65535 |
| 459 | end |
| 460 | x = sleepns 1000000 |
| 461 | end |
| 462 | ret 65535 |
| 463 | end |
| 464 | |
| 465 | note The child's stdin (k 0) or stdout (k 1) for mode md, opened. |
| 466 | fn tl_fd md ulong k ulong to long |
| 467 | if eq md 0 |
| 468 | if eq k 0 |
| 469 | ret fopen fsrc 0 len fsrc 524288 0 |
| 470 | end |
| 471 | ret fopen fbin 0 len fbin 524865 448 |
| 472 | end |
| 473 | if eq k 0 |
| 474 | ret fopen fnull 0 len fnull 524288 0 |
| 475 | end |
| 476 | ret fopen fout 0 len fout 524865 448 |
| 477 | end |
| 478 | |
| 479 | note Run the packed argv pk[0, pn) in its own process group. md 0 (the |
| 480 | note compiler): stdin c.om, stdout c.bin, execprog, 10 s. md 1 (a test): |
| 481 | note stdin /dev/null, stdout c.out, execpure, 2 s (round40's timeout). |
| 482 | note stderr to c.err. The wait status, or 65535 when it did not finish |
| 483 | note (its group is killed). |
| 484 | fn tl_run pk view uchar 4096 pn ulong md ulong to ulong |
| 485 | pid = fork |
| 486 | if eq pid 0 |
| 487 | r = setpgid 0 0 |
| 488 | fi = tl_fd md 0 |
| 489 | fo = tl_fd md 1 |
| 490 | fe = fopen ferr 0 len ferr 524865 448 |
| 491 | if or or lt fi 0 lt fo 0 lt fe 0 |
| 492 | exit 120 |
| 493 | end |
| 494 | r = dupfd int fi 0 0 |
| 495 | r = dupfd int fo 1 0 |
| 496 | r = dupfd int fe 2 0 |
| 497 | if eq md 1 |
| 498 | r = execpure pk 0 pn |
| 499 | else |
| 500 | r = execprog pk 0 pn |
| 501 | end |
| 502 | exit 121 |
| 503 | end |
| 504 | if lt pid 0 |
| 505 | ret 65535 |
| 506 | end |
| 507 | r = setpgid int pid int pid |
| 508 | if eq md 1 |
| 509 | ret tl_wait pid 2000000000 |
| 510 | end |
| 511 | ret tl_wait pid 10000000000 |
| 512 | end |
| 513 | |
| 514 | note Pack one or two arguments into pk; the length. |
| 515 | fn tl_pack pk edit uchar 4096 a view uchar * b view uchar * to ulong |
| 516 | na = len a |
| 517 | nb = len b |
| 518 | c = ulong 1 |
| 519 | if gt nb 0 |
| 520 | c = 2 |
| 521 | end |
| 522 | put pk 0 uchar c |
| 523 | put pk 1 0 |
| 524 | put pk 2 uchar and na 255 |
| 525 | put pk 3 uchar shr na 8 |
| 526 | for i ulong na |
| 527 | put pk add 4 i get a i |
| 528 | end |
| 529 | o = add 4 na |
| 530 | if eq c 1 |
| 531 | ret o |
| 532 | end |
| 533 | put pk o uchar and nb 255 |
| 534 | put pk add o 1 uchar shr nb 8 |
| 535 | for j ulong nb |
| 536 | put pk add add o 2 j get b j |
| 537 | end |
| 538 | ret add add o 2 nb |
| 539 | end |
| 540 | |
| 541 | note The trimmed compiler message of file c.err into ob; its length. |
| 542 | fn tl_cerr ob edit uchar 4096 to ulong |
| 543 | b = new uchar 4096 0 |
| 544 | m = new ulong 16 0 |
| 545 | n = tl_slurp ferr b |
| 546 | if lt n 0 |
| 547 | ret 0 |
| 548 | end |
| 549 | tl_trim b ulong n m |
| 550 | k = sub get m 1 get m 0 |
| 551 | for i ulong k |
| 552 | put ob i get b add get m 0 i |
| 553 | end |
| 554 | ret k |
| 555 | end |
| 556 | |
| 557 | note Compile src[0, n) to c.bin in the current (WORK) directory with omc |
| 558 | note path oc. 0 compiled, 1 refused by omc (message in c.err), 2 failed. |
| 559 | fn tl_omc oc view uchar * src view uchar * n ulong to ulong |
| 560 | pk = new uchar 4096 0 |
| 561 | none = new uchar 1 0 |
| 562 | if ne tl_spit fsrc src n 0 |
| 563 | ret 2 |
| 564 | end |
| 565 | pn = tl_pack pk oc view part none 0 0 |
| 566 | ws = tl_run pk pn 0 |
| 567 | if eq ws 0 |
| 568 | ret 0 |
| 569 | end |
| 570 | if ne and ws 127 0 |
| 571 | ret 2 |
| 572 | end |
| 573 | c = and shr ws 8 255 |
| 574 | if or eq c 120 eq c 121 |
| 575 | ret 2 |
| 576 | end |
| 577 | ret 1 |
| 578 | end |
| 579 | |
| 580 | note The wait status ws of a test run as the round40 return code: the exit |
| 581 | note code, or minus the signal. |
| 582 | fn tl_code ws ulong to long |
| 583 | if eq ws 65535 |
| 584 | ret neg long 9 |
| 585 | end |
| 586 | if ne and ws 127 0 |
| 587 | ret neg long and ws 127 |
| 588 | end |
| 589 | ret long and shr ws 8 255 |
| 590 | end |
| 591 | |
| 592 | note Test failed: calc(x) expected w, got g. into ob; its length. |
| 593 | fn tl_tfail ob edit uchar 4096 x ulong w ulong g long to ulong |
| 594 | p = tl_cat ob 0 otf1 len otf1 |
| 595 | p = tl_dec ob p x |
| 596 | p = tl_cat ob p otf2 len otf2 |
| 597 | p = tl_dec ob p w |
| 598 | p = tl_cat ob p otf3 len otf3 |
| 599 | v = ulong 0 |
| 600 | if lt g 0 |
| 601 | put ob p 45 |
| 602 | p = add p 1 |
| 603 | v = ulong neg g |
| 604 | else |
| 605 | v = ulong g |
| 606 | end |
| 607 | p = tl_dec ob p v |
| 608 | ret tl_cat ob p otf4 len otf4 |
| 609 | end |
| 610 | |
| 611 | note The goal for input x of kind m[5] (1 add, 2 mul, 3 loop), K m[6], N m[7]. |
| 612 | fn tl_want m view ulong 16 x ulong to ulong |
| 613 | if eq get m 5 1 |
| 614 | ret add x get m 6 |
| 615 | end |
| 616 | if eq get m 5 2 |
| 617 | ret mul x get m 6 |
| 618 | end |
| 619 | ret add x mul get m 6 get m 7 |
| 620 | end |
| 621 | |
| 622 | note true when the file c.out is empty. |
| 623 | fn tl_quiet to bool |
| 624 | st = new uchar 128 0 |
| 625 | r = stat fout 0 len fout st 0 128 |
| 626 | if lt r 0 |
| 627 | ret false |
| 628 | end |
| 629 | for i ulong 8 |
| 630 | if ne get st add 48 i 0 |
| 631 | ret false |
| 632 | end |
| 633 | end |
| 634 | ret true |
| 635 | end |
| 636 | |
| 637 | note Run c.bin for input m[9]: 0 passed, else the observation's length in ob. |
| 638 | fn tl_exec m view ulong 16 ob edit uchar 4096 to ulong |
| 639 | pk = new uchar 4096 0 |
| 640 | none = new uchar 1 0 |
| 641 | pn = tl_pack pk fbin view part none 0 0 |
| 642 | g = tl_code tl_run pk pn 1 |
| 643 | x = get m 9 |
| 644 | w = tl_want m x |
| 645 | if and eq g long w tl_quiet |
| 646 | ret 0 |
| 647 | end |
| 648 | ret tl_tfail ob x w g |
| 649 | end |
| 650 | |
| 651 | note One test: the candidate b with input x = m[9] (ret calc int line |
| 652 | note m[3]..m[4]) compiled and run. 0 passed, else the length of the |
| 653 | note observation in ob, or 4096 for an I/O failure. |
| 654 | fn tl_test oc view uchar * b view uchar 4096 n ulong m view ulong 16 ob edit uchar 4096 to ulong |
| 655 | c = new uchar 4096 0 |
| 656 | p = tl_cat c 0 b get m 3 |
| 657 | p = tl_cat c p grcal len grcal |
| 658 | p = tl_dec c p get m 9 |
| 659 | p = tl_tail c p b get m 4 n |
| 660 | r = tl_omc oc view part c 0 p p |
| 661 | if eq r 1 |
| 662 | ret tl_cerr ob |
| 663 | end |
| 664 | if ne r 0 |
| 665 | ret 4096 |
| 666 | end |
| 667 | ret tl_exec m ob |
| 668 | end |
| 669 | |
| 670 | note Inputs A, 0, 3, 7 without repeats, in that order: the i-th, or 999. |
| 671 | fn tl_input a ulong i ulong to ulong |
| 672 | v = new ulong 4 0 |
| 673 | put v 0 a |
| 674 | put v 1 0 |
| 675 | put v 2 3 |
| 676 | put v 3 7 |
| 677 | k = ulong 0 |
| 678 | for j ulong 4 |
| 679 | dup = false |
| 680 | for q ulong j |
| 681 | if eq get v q get v j |
| 682 | dup = true |
| 683 | end |
| 684 | end |
| 685 | if not dup |
| 686 | if eq k i |
| 687 | ret get v j |
| 688 | end |
| 689 | k = add k 1 |
| 690 | end |
| 691 | end |
| 692 | ret 999 |
| 693 | end |
| 694 | |
| 695 | note The check of b[0, n) (m[5..8] = kind, K, N, A) in WORK; the record. |
| 696 | fn tl_check2 oc view uchar * b view uchar 4096 n ulong m edit ulong 16 to int |
| 697 | ob = new uchar 4096 0 |
| 698 | if or eq n 0 gt n 600 |
| 699 | ret tl_rec 6 orefu b n |
| 700 | end |
| 701 | r = tl_omc oc b n |
| 702 | if eq r 1 |
| 703 | k = tl_cerr ob |
| 704 | ret tl_rec 6 view part ob 0 k b n |
| 705 | end |
| 706 | if ne r 0 |
| 707 | ret tl_rec 4 oio b n |
| 708 | end |
| 709 | sh = tl_shape b n get m 5 m |
| 710 | if eq sh 1 |
| 711 | ret tl_rec 6 orefu b n |
| 712 | end |
| 713 | if eq sh 2 |
| 714 | ret tl_rec 6 ofns b n |
| 715 | end |
| 716 | if or eq sh 3 ne get m 2 get m 8 |
| 717 | ret tl_rec 6 omain b n |
| 718 | end |
| 719 | note round40 wants the line to read exactly ret calc int A (no leading 0). |
| 720 | if ne sub get m 4 get m 3 add len grcal tl_nd get m 8 |
| 721 | ret tl_rec 6 omain b n |
| 722 | end |
| 723 | if eq sh 4 |
| 724 | ret tl_rec 6 oloop b n |
| 725 | end |
| 726 | for i ulong 4 |
| 727 | x = tl_input get m 8 i |
| 728 | if eq x 999 |
| 729 | break |
| 730 | end |
| 731 | put m 9 x |
| 732 | t = tl_test oc b n m ob |
| 733 | if eq t 4096 |
| 734 | ret tl_rec 4 oio b n |
| 735 | end |
| 736 | if ne t 0 |
| 737 | ret tl_rec 6 view part ob 0 t b n |
| 738 | end |
| 739 | end |
| 740 | ret tl_rec 0 opass b n |
| 741 | end |
| 742 | |
| 743 | note Argument i into a fresh slice of d at offset o (at most 1024 bytes); |
| 744 | note its length, or -1. |
| 745 | fn tl_arg i ulong d edit uchar 8192 o ulong to long |
| 746 | ret argread i d o 1024 |
| 747 | end |
| 748 | |
| 749 | fn tl_cnum i ulong to ulong |
| 750 | b = new uchar 16 0 |
| 751 | n = argread i b 0 16 |
| 752 | if or lt n 1 gt n 3 |
| 753 | ret 999 |
| 754 | end |
| 755 | v = ulong 0 |
| 756 | for j ulong ulong n |
| 757 | c = ulong get b j |
| 758 | if or lt c 48 gt c 57 |
| 759 | ret 999 |
| 760 | end |
| 761 | v = add mul v 10 sub c 48 |
| 762 | end |
| 763 | ret v |
| 764 | end |
| 765 | |
| 766 | note check ROOT PATH WORK OMC KIND K N A, after chdir ROOT. |
| 767 | fn tl_check d edit uchar 8192 to int |
| 768 | b = new uchar 4096 0 |
| 769 | m = new ulong 16 0 |
| 770 | put m 5 tl_cnum 6 |
| 771 | put m 6 tl_cnum 7 |
| 772 | put m 7 tl_cnum 8 |
| 773 | put m 8 tl_cnum 9 |
| 774 | if or or lt get m 5 1 gt get m 5 3 or gt get m 6 99 gt get m 7 99 |
| 775 | ret 2 |
| 776 | end |
| 777 | if gt get m 8 99 |
| 778 | ret 2 |
| 779 | end |
| 780 | on = tl_arg 5 d 0 |
| 781 | pn = tl_arg 3 d 1024 |
| 782 | wn = tl_arg 4 d 2048 |
| 783 | if or or lt on 1 lt pn 1 lt wn 1 |
| 784 | ret 2 |
| 785 | end |
| 786 | put m 14 ulong on |
| 787 | put m 15 ulong pn |
| 788 | ret tl_chk1 d ulong wn m b |
| 789 | end |
| 790 | |
| 791 | note check, with OMC at d[0, m14), PATH at d[1024, m15), WORK at d[2048, wn). |
| 792 | fn tl_chk1 d edit uchar 8192 wn ulong m edit ulong 16 b edit uchar 4096 to int |
| 793 | if not agpathok view part d 1024 get m 15 |
| 794 | ret tl_rec 2 opath b 0 |
| 795 | end |
| 796 | n = tl_slurp view part d 1024 get m 15 b |
| 797 | if eq n -2 |
| 798 | ret tl_rec 1 onf b 0 |
| 799 | end |
| 800 | if eq n -3 |
| 801 | ret tl_rec 3 obig b 0 |
| 802 | end |
| 803 | if lt n 0 |
| 804 | ret tl_rec 4 oio b 0 |
| 805 | end |
| 806 | r = chdir d 2048 wn |
| 807 | if lt r 0 |
| 808 | ret tl_rec 4 oio b 0 |
| 809 | end |
| 810 | ret tl_check2 view part d 0 get m 14 b ulong n m |
| 811 | end |
| 812 | |
| 813 | fn tl_main to int |
| 814 | d = new uchar 8192 0 |
| 815 | c = argcnt |
| 816 | if lt c 4 |
| 817 | ret 2 |
| 818 | end |
| 819 | cn = tl_arg 1 d 4096 |
| 820 | if lt cn 1 |
| 821 | ret 2 |
| 822 | end |
| 823 | rn = tl_arg 2 d 5120 |
| 824 | if lt rn 1 |
| 825 | ret 2 |
| 826 | end |
| 827 | if lt chdir d 5120 ulong rn 0 |
| 828 | ret 2 |
| 829 | end |
| 830 | if and heq2 view part d 4096 ulong cn ccheck eq c 10 |
| 831 | ret tl_check d |
| 832 | end |
| 833 | pn = tl_arg 3 d 0 |
| 834 | if lt pn 0 |
| 835 | ret 2 |
| 836 | end |
| 837 | if and heq2 view part d 4096 ulong cn cread eq c 4 |
| 838 | ret tl_read view part d 0 ulong pn |
| 839 | end |
| 840 | if and heq2 view part d 4096 ulong cn crepl eq c 6 |
| 841 | ret tl_rmain d ulong pn |
| 842 | end |
| 843 | ret 2 |
| 844 | end |
| 845 | |
| 846 | note replace ROOT PATH OLD NEW, with PATH at d[0, pn). |
| 847 | fn tl_rmain d edit uchar 8192 pn ulong to int |
| 848 | aw = new uchar 2048 0 |
| 849 | l = new ulong 4 0 |
| 850 | an = argread 4 aw 0 1024 |
| 851 | wn = argread 5 aw 1024 1024 |
| 852 | if or lt an 0 lt wn 0 |
| 853 | ret 2 |
| 854 | end |
| 855 | put l 0 pn |
| 856 | put l 1 ulong an |
| 857 | put l 2 ulong wn |
| 858 | ret tl_repl d aw l |
| 859 | end |
| 860 | |
| 861 | fn heq2 a view uchar * t view uchar * to bool |
| 862 | if ne len a len t |
| 863 | ret false |
| 864 | end |
| 865 | for i ulong len a |
| 866 | if ne get a i get t i |
| 867 | ret false |
| 868 | end |
| 869 | end |
| 870 | ret true |
| 871 | end |
| 872 | |
| 873 | fn main to int |
| 874 | ret tl_main |
| 875 | end |