omega-feel
This project has not described itself yet.
Brought by drakestapleton
omega-feel / feel.om
| 1 | note feel: the decision core of SPEC.md. Per class of work it decides which arm (way of running a |
| 2 | note kernel) runs next. Rules: SPEC.md section 3. Program form (trace in, events out): section 4. |
| 3 | note |
| 4 | note What an engine calls (SPEC.md section 4 is the contract). Every entry checks its arguments and |
| 5 | note the order of calls before it changes anything. A refusal changes nothing and is a result, not a |
| 6 | note trap: setup, verdict and observe return 0 or an error code, choose returns ERR plus the code. |
| 7 | note setup c K declare class c with K arms, once. K from 2 to KMAX. |
| 8 | note r = choose c once per launch of class c, and only when the last launch of c is settled. |
| 9 | note The arm to run is (and r 65535). (and shr r 32 3) says what is owed: |
| 10 | note 0 nothing; 1 verdict c equal for arm (and shr r 16 65535), run on the same |
| 11 | note input into scratch memory; 2 observe c t, the time of this launch. |
| 12 | note verdict c equal equal is 1 when that arm's output equals the reference bit for bit, else 0. |
| 13 | note observe c t t is the time the chosen arm took, below TMAX. |
| 14 | note Every number is a non-negative ulong. A time is below 10^15, so that 256 pairs and the |
| 15 | note multiplication by 64 in the mean rule stay below 2^64. |
| 16 | note The constants of section 3. |
| 17 | const H ulong 12 |
| 18 | const NMIN ulong 32 |
| 19 | const NMAX ulong 256 |
| 20 | const V ulong 3 |
| 21 | const WARM ulong 64 |
| 22 | const REST0 ulong 1024 |
| 23 | const RESTMAX ulong 65536 |
| 24 | note Adopt by mean needs MDEN * challenger below MNUM * incumbent, in both halves of the duel. |
| 25 | const MNUM ulong 63 |
| 26 | const MDEN ulong 64 |
| 27 | note The order generator of 3.1: z = (z * LCGA + LCGC) mod LCGM. |
| 28 | const LCGA ulong 1103515245 |
| 29 | const LCGC ulong 12345 |
| 30 | const LCGM ulong 2147483648 |
| 31 | note The limits of the interface. Each is a width of the machine word, not a size of a table: no |
| 32 | note table here has a fixed size, every one grows with the classes and arms that are declared. |
| 33 | note TMAX a time is below 10^15 (256 pairs times 64 times a time stays below 2^64). |
| 34 | note KMAX arm numbers travel in 16 bits of the word choose returns. |
| 35 | note CTOP conservative class input bound. One table of 2^52 ulong entries would occupy 32 PiB. |
| 36 | note KTRACE the trace form writes the equal-output mask as a decimal below 10^15, which has room |
| 37 | note for 49 arms (2^49 - 1 is below 10^15). |
| 38 | note ERR choose returns ERR plus an error code when it refuses; a result is below 2^63. |
| 39 | const TMAX ulong 1000000000000000 |
| 40 | const KMAX ulong 65535 |
| 41 | const CTOP ulong 4503599627370496 |
| 42 | const KTRACE ulong 49 |
| 43 | const ERR ulong 9223372036854775808 |
| 44 | note Error codes: 1 class not declared, 2 call out of order, 3 argument out of range, 4 class |
| 45 | note already declared, 5 memory refused by the host. |
| 46 | const E_CLASS ulong 1 |
| 47 | const E_STATE ulong 2 |
| 48 | const E_ARG ulong 3 |
| 49 | const E_DUP ulong 4 |
| 50 | const E_MEM ulong 5 |
| 51 | note |
| 52 | note State of one class, index c, in run-time pools that grow when a class is declared. s_K is 0 |
| 53 | note until the class is declared. Modes: 0 rest, 1 shadow, 2 duel. s_owed: what the last choose |
| 54 | note left owing (0 nothing, 1 a verdict, 2 a time). Per arm, in s_status and s_counts at index |
| 55 | note s_base c + arm: status (0 untested, 1 admitted, 2 banned), count of launches. |
| 56 | global s_K ulong * |
| 57 | global s_inc ulong * |
| 58 | global s_mode ulong * |
| 59 | global s_rest ulong * |
| 60 | global s_restlen ulong * |
| 61 | global s_next ulong * |
| 62 | global s_ch ulong * |
| 63 | global s_vcount ulong * |
| 64 | global s_k ulong * |
| 65 | global s_w ulong * |
| 66 | global s_l ulong * |
| 67 | global s_n ulong * |
| 68 | global s_sI ulong * |
| 69 | global s_sC ulong * |
| 70 | global s_hI ulong * |
| 71 | global s_hC ulong * |
| 72 | global s_tfirst ulong * |
| 73 | global s_o ulong * |
| 74 | global s_z ulong * |
| 75 | global s_i ulong * |
| 76 | global s_owed ulong * |
| 77 | global s_base ulong * |
| 78 | global s_status ulong * |
| 79 | global s_counts ulong * |
| 80 | global g_arms ulong 1 |
| 81 | note |
| 82 | note The trace reader's own memory: input and output buffers, the times of one line, the byte |
| 83 | note that ended the last number, the line number. |
| 84 | global g_ib uchar 65536 |
| 85 | global g_ip ulong 1 |
| 86 | global g_il ulong 1 |
| 87 | global g_ob uchar 65536 |
| 88 | global g_op ulong 1 |
| 89 | global g_tm ulong * |
| 90 | global g_term ulong 1 |
| 91 | global g_line ulong 1 |
| 92 | text t_bad "feel: bad input, line " |
| 93 | text t_io "feel: cannot read standard input\n" |
| 94 | text t_mem "feel: out of memory, line " |
| 95 | note |
| 96 | note Output: bytes gather in g_ob; oflush writes them all to fd. A failed write ends with status 3. |
| 97 | fn oflush fd int to void |
| 98 | n = get g_op 0 |
| 99 | off = ulong 0 |
| 100 | while lt off n bound 9223372036854775807 |
| 101 | r = write fd g_ob off sub n off |
| 102 | if le r 0 |
| 103 | exit 3 |
| 104 | end |
| 105 | off = add off ulong r |
| 106 | end |
| 107 | put g_op 0 0 |
| 108 | end |
| 109 | fn ochr b ulong to void |
| 110 | p = get g_op 0 |
| 111 | if ge p 65536 |
| 112 | oflush 1 |
| 113 | p = 0 |
| 114 | end |
| 115 | put g_ob p uchar b |
| 116 | put g_op 0 add p 1 |
| 117 | end |
| 118 | note onum writes v in decimal; osn writes a space, then v. |
| 119 | fn onum v ulong to void |
| 120 | d = new uchar 20 0 |
| 121 | x = v |
| 122 | m = ulong 0 |
| 123 | put d 0 uchar add 48 rem x 10 |
| 124 | x = div x 10 |
| 125 | while gt x 0 bound 20 |
| 126 | m = add m 1 |
| 127 | put d m uchar add 48 rem x 10 |
| 128 | x = div x 10 |
| 129 | end |
| 130 | for j ulong add m 1 |
| 131 | ochr ulong get d sub m j |
| 132 | end |
| 133 | end |
| 134 | fn osn v ulong to void |
| 135 | ochr 32 |
| 136 | onum v |
| 137 | end |
| 138 | note |
| 139 | note A duel starts (3.1): the counters of the pairs go to zero. |
| 140 | fn start c ulong to void |
| 141 | put s_mode c 2 |
| 142 | put s_k c 0 |
| 143 | put s_w c 0 |
| 144 | put s_l c 0 |
| 145 | put s_sI c 0 |
| 146 | put s_sC c 0 |
| 147 | put s_hI c 0 |
| 148 | put s_hC c 0 |
| 149 | put s_n c 0 |
| 150 | end |
| 151 | note Growing the tables. growby n makes every per-class pool hold n classes: 0, or E_MEM when the host |
| 152 | note refuses. s_K goes last, so a class number below len s_K has every pool behind it. Growing by the |
| 153 | note one class asked for is enough: the run-time pool allocates ahead of its length, so a long run of |
| 154 | note declarations is not quadratic. tests/run.sh has an alarm on the memory a long run needs. |
| 155 | fn growby n ulong to ulong |
| 156 | r = enlarge s_inc n |
| 157 | if ne r 0 |
| 158 | ret E_MEM |
| 159 | end |
| 160 | r = enlarge s_mode n |
| 161 | if ne r 0 |
| 162 | ret E_MEM |
| 163 | end |
| 164 | r = enlarge s_rest n |
| 165 | if ne r 0 |
| 166 | ret E_MEM |
| 167 | end |
| 168 | r = enlarge s_restlen n |
| 169 | if ne r 0 |
| 170 | ret E_MEM |
| 171 | end |
| 172 | r = enlarge s_next n |
| 173 | if ne r 0 |
| 174 | ret E_MEM |
| 175 | end |
| 176 | r = enlarge s_ch n |
| 177 | if ne r 0 |
| 178 | ret E_MEM |
| 179 | end |
| 180 | r = enlarge s_vcount n |
| 181 | if ne r 0 |
| 182 | ret E_MEM |
| 183 | end |
| 184 | r = enlarge s_k n |
| 185 | if ne r 0 |
| 186 | ret E_MEM |
| 187 | end |
| 188 | r = enlarge s_w n |
| 189 | if ne r 0 |
| 190 | ret E_MEM |
| 191 | end |
| 192 | r = enlarge s_l n |
| 193 | if ne r 0 |
| 194 | ret E_MEM |
| 195 | end |
| 196 | r = enlarge s_n n |
| 197 | if ne r 0 |
| 198 | ret E_MEM |
| 199 | end |
| 200 | r = enlarge s_sI n |
| 201 | if ne r 0 |
| 202 | ret E_MEM |
| 203 | end |
| 204 | r = enlarge s_sC n |
| 205 | if ne r 0 |
| 206 | ret E_MEM |
| 207 | end |
| 208 | r = enlarge s_hI n |
| 209 | if ne r 0 |
| 210 | ret E_MEM |
| 211 | end |
| 212 | r = enlarge s_hC n |
| 213 | if ne r 0 |
| 214 | ret E_MEM |
| 215 | end |
| 216 | r = enlarge s_tfirst n |
| 217 | if ne r 0 |
| 218 | ret E_MEM |
| 219 | end |
| 220 | r = enlarge s_o n |
| 221 | if ne r 0 |
| 222 | ret E_MEM |
| 223 | end |
| 224 | r = enlarge s_z n |
| 225 | if ne r 0 |
| 226 | ret E_MEM |
| 227 | end |
| 228 | r = enlarge s_i n |
| 229 | if ne r 0 |
| 230 | ret E_MEM |
| 231 | end |
| 232 | r = enlarge s_owed n |
| 233 | if ne r 0 |
| 234 | ret E_MEM |
| 235 | end |
| 236 | r = enlarge s_base n |
| 237 | if ne r 0 |
| 238 | ret E_MEM |
| 239 | end |
| 240 | r = enlarge s_K n |
| 241 | if ne r 0 |
| 242 | ret E_MEM |
| 243 | end |
| 244 | ret 0 |
| 245 | end |
| 246 | fn growto n ulong to ulong |
| 247 | if ge len s_K n |
| 248 | ret 0 |
| 249 | end |
| 250 | ret growby n |
| 251 | end |
| 252 | note The same for the arm pools: they must hold n arms. |
| 253 | fn growarms n ulong to ulong |
| 254 | if ge len s_status n |
| 255 | ret 0 |
| 256 | end |
| 257 | r = enlarge s_status n |
| 258 | if ne r 0 |
| 259 | ret E_MEM |
| 260 | end |
| 261 | r = enlarge s_counts n |
| 262 | if ne r 0 |
| 263 | ret E_MEM |
| 264 | end |
| 265 | ret 0 |
| 266 | end |
| 267 | note known c is 1 when class c has been declared, else 0. |
| 268 | fn known c ulong to ulong |
| 269 | if ge c len s_K |
| 270 | ret 0 |
| 271 | end |
| 272 | if eq get s_K c 0 |
| 273 | ret 0 |
| 274 | end |
| 275 | ret 1 |
| 276 | end |
| 277 | note ix c a is the place of arm a of class c in s_status and s_counts. |
| 278 | fn ix c ulong a ulong to ulong |
| 279 | ret add get s_base c a |
| 280 | end |
| 281 | note A new class: arm 0 is the admitted reference and the incumbent. Every refusal leaves the class |
| 282 | note tables as they were: nothing is written before the memory is there. |
| 283 | fn setup c ulong kk ulong to ulong |
| 284 | if or ge c CTOP or lt kk 2 gt kk KMAX |
| 285 | ret E_ARG |
| 286 | end |
| 287 | if lt c len s_K |
| 288 | if ne get s_K c 0 |
| 289 | ret E_DUP |
| 290 | end |
| 291 | end |
| 292 | m = growto add c 1 |
| 293 | if ne m 0 |
| 294 | ret E_MEM |
| 295 | end |
| 296 | base = get g_arms 0 |
| 297 | m = growarms add base kk |
| 298 | if ne m 0 |
| 299 | ret E_MEM |
| 300 | end |
| 301 | put g_arms 0 add base kk |
| 302 | put s_base c base |
| 303 | put s_status base 1 |
| 304 | put s_rest c WARM |
| 305 | put s_restlen c REST0 |
| 306 | put s_z c 1 |
| 307 | put s_K c kk |
| 308 | ret 0 |
| 309 | end |
| 310 | note |
| 311 | note Events. evshadow: CLASS B|M ARM I. evduel: CLASS A|R|Am|D ARM I N W L SI SC. |
| 312 | fn evshadow c ulong letter ulong to void |
| 313 | onum c |
| 314 | ochr 32 |
| 315 | ochr letter |
| 316 | osn get s_ch c |
| 317 | osn get s_i c |
| 318 | ochr 10 |
| 319 | end |
| 320 | fn evduel c ulong out ulong to void |
| 321 | onum c |
| 322 | ochr 32 |
| 323 | if eq out 1 |
| 324 | ochr 65 |
| 325 | end |
| 326 | if eq out 2 |
| 327 | ochr 82 |
| 328 | end |
| 329 | if eq out 3 |
| 330 | ochr 65 |
| 331 | ochr 109 |
| 332 | end |
| 333 | if eq out 4 |
| 334 | ochr 68 |
| 335 | end |
| 336 | osn get s_ch c |
| 337 | osn get s_i c |
| 338 | osn get s_n c |
| 339 | osn get s_w c |
| 340 | osn get s_l c |
| 341 | osn get s_sI c |
| 342 | osn get s_sC c |
| 343 | ochr 10 |
| 344 | end |
| 345 | note |
| 346 | note 3.1 Choose. pick is the round-robin: the first arm after s_next that is not the incumbent |
| 347 | note and not banned, or K when there is none. |
| 348 | fn pick c ulong to ulong |
| 349 | kk = get s_K c |
| 350 | inc = get s_inc c |
| 351 | nx = get s_next c |
| 352 | for s ulong kk |
| 353 | a = rem add add nx s 1 kk |
| 354 | if ne a inc |
| 355 | if ne get s_status ix c a 2 |
| 356 | ret a |
| 357 | end |
| 358 | end |
| 359 | end |
| 360 | ret kk |
| 361 | end |
| 362 | fn choose c ulong to ulong |
| 363 | if eq known c 0 |
| 364 | ret add ERR E_CLASS |
| 365 | end |
| 366 | if ne get s_owed c 0 |
| 367 | ret add ERR E_STATE |
| 368 | end |
| 369 | put s_i c add get s_i c 1 |
| 370 | inc = get s_inc c |
| 371 | arm = inc |
| 372 | if eq get s_mode c 0 |
| 373 | if gt get s_rest c 0 |
| 374 | put s_rest c sub get s_rest c 1 |
| 375 | else |
| 376 | kk = get s_K c |
| 377 | found = pick c |
| 378 | if eq found kk |
| 379 | put s_rest c get s_restlen c |
| 380 | else |
| 381 | put s_ch c found |
| 382 | put s_next c found |
| 383 | if eq get s_status ix c found 0 |
| 384 | put s_mode c 1 |
| 385 | put s_vcount c 0 |
| 386 | else |
| 387 | start c |
| 388 | end |
| 389 | end |
| 390 | end |
| 391 | end |
| 392 | kind = get s_mode c |
| 393 | if eq kind 2 |
| 394 | k = get s_k c |
| 395 | if eq and k 1 0 |
| 396 | z = rem add mul get s_z c LCGA LCGC LCGM |
| 397 | put s_z c z |
| 398 | put s_o c and shr z 16 1 |
| 399 | end |
| 400 | if ne and k 1 get s_o c |
| 401 | arm = get s_ch c |
| 402 | end |
| 403 | end |
| 404 | ai = ix c arm |
| 405 | put s_counts ai add get s_counts ai 1 |
| 406 | put s_owed c kind |
| 407 | ret or arm or shl kind 32 shl get s_ch c 16 |
| 408 | end |
| 409 | note |
| 410 | note 3.2 Verdict. |
| 411 | fn verdict c ulong equal ulong to ulong |
| 412 | if eq known c 0 |
| 413 | ret E_CLASS |
| 414 | end |
| 415 | if ne get s_owed c 1 |
| 416 | ret E_STATE |
| 417 | end |
| 418 | if gt equal 1 |
| 419 | ret E_ARG |
| 420 | end |
| 421 | put s_owed c 0 |
| 422 | ci = ix c get s_ch c |
| 423 | if eq equal 0 |
| 424 | put s_status ci 2 |
| 425 | evshadow c 66 |
| 426 | put s_mode c 0 |
| 427 | put s_rest c get s_restlen c |
| 428 | else |
| 429 | vc = add get s_vcount c 1 |
| 430 | put s_vcount c vc |
| 431 | if eq vc V |
| 432 | put s_status ci 1 |
| 433 | evshadow c 77 |
| 434 | start c |
| 435 | end |
| 436 | end |
| 437 | ret 0 |
| 438 | end |
| 439 | note |
| 440 | note 3.3 Observe. Rule 2, at NMAX pairs; nothing is adopted before it. The first half of the |
| 441 | note duel is (hI, hC), the second (rI, rC). |
| 442 | note mean: 3 adopt by mean when the challenger's sum is below MNUM / MDEN of the incumbent's in |
| 443 | note both halves, else 4 draw. |
| 444 | fn mean c ulong to ulong |
| 445 | hI = get s_hI c |
| 446 | hC = get s_hC c |
| 447 | rI = sub get s_sI c hI |
| 448 | rC = sub get s_sC c hC |
| 449 | if and lt mul MDEN hC mul MNUM hI lt mul MDEN rC mul MNUM rI |
| 450 | ret 3 |
| 451 | end |
| 452 | ret 4 |
| 453 | end |
| 454 | note full: 1 adopt when the challenger's sum is lower in both halves and it leads the count by H; |
| 455 | note otherwise what mean says. |
| 456 | fn full c ulong to ulong |
| 457 | hI = get s_hI c |
| 458 | hC = get s_hC c |
| 459 | rI = sub get s_sI c hI |
| 460 | rC = sub get s_sC c hC |
| 461 | w = get s_w c |
| 462 | l = get s_l c |
| 463 | if and lt hC hI and lt rC rI ge w add l H |
| 464 | ret 1 |
| 465 | end |
| 466 | ret mean c |
| 467 | end |
| 468 | note pair adds one finished pair (tI the incumbent's time, tC the challenger's) and gives how |
| 469 | note the duel ends: 0 not yet, 1 adopt, 2 reject, 3 adopt by mean, 4 draw. Rule 1, the early |
| 470 | note reject: the incumbent leads the count by H after NMIN pairs or more and the sums agree. |
| 471 | fn pair c ulong tI ulong tC ulong to ulong |
| 472 | sI = add get s_sI c tI |
| 473 | sC = add get s_sC c tC |
| 474 | put s_sI c sI |
| 475 | put s_sC c sC |
| 476 | w = get s_w c |
| 477 | l = get s_l c |
| 478 | if lt tC tI |
| 479 | w = add w 1 |
| 480 | end |
| 481 | if gt tC tI |
| 482 | l = add l 1 |
| 483 | end |
| 484 | put s_w c w |
| 485 | put s_l c l |
| 486 | n = add get s_n c 1 |
| 487 | put s_n c n |
| 488 | if eq n div NMAX 2 |
| 489 | put s_hI c sI |
| 490 | put s_hC c sC |
| 491 | end |
| 492 | if and ge n NMIN and ge l add w H ge sC sI |
| 493 | ret 2 |
| 494 | end |
| 495 | if ge n NMAX |
| 496 | ret full c |
| 497 | end |
| 498 | ret 0 |
| 499 | end |
| 500 | note observe: the first launch of a pair is kept; the second completes the pair. In the second |
| 501 | note launch (k odd) the incumbent ran when the order bit is 1, the challenger when it is 0. |
| 502 | fn observe c ulong t ulong to ulong |
| 503 | if eq known c 0 |
| 504 | ret E_CLASS |
| 505 | end |
| 506 | if ne get s_owed c 2 |
| 507 | ret E_STATE |
| 508 | end |
| 509 | if ge t TMAX |
| 510 | ret E_ARG |
| 511 | end |
| 512 | put s_owed c 0 |
| 513 | k = get s_k c |
| 514 | if eq and k 1 0 |
| 515 | put s_tfirst c t |
| 516 | else |
| 517 | out = ulong 0 |
| 518 | if eq get s_o c 1 |
| 519 | out = pair c t get s_tfirst c |
| 520 | else |
| 521 | out = pair c get s_tfirst c t |
| 522 | end |
| 523 | if ne out 0 |
| 524 | evduel c out |
| 525 | if or eq out 1 eq out 3 |
| 526 | put s_inc c get s_ch c |
| 527 | put s_restlen c REST0 |
| 528 | else |
| 529 | rl = mul get s_restlen c 2 |
| 530 | if gt rl RESTMAX |
| 531 | rl = RESTMAX |
| 532 | end |
| 533 | put s_restlen c rl |
| 534 | end |
| 535 | put s_rest c get s_restlen c |
| 536 | put s_mode c 0 |
| 537 | end |
| 538 | end |
| 539 | put s_k c add k 1 |
| 540 | ret 0 |
| 541 | end |
| 542 | note |
| 543 | note One launch, as an engine would run it: choose, then the verdict or the time that is owed. |
| 544 | fn step c ulong mask ulong to void |
| 545 | r = choose c |
| 546 | if ge r ERR |
| 547 | trap 8 |
| 548 | end |
| 549 | what = and shr r 32 3 |
| 550 | e = ulong 0 |
| 551 | if eq what 1 |
| 552 | e = verdict c and shr mask and shr r 16 65535 1 |
| 553 | end |
| 554 | if eq what 2 |
| 555 | e = observe c get g_tm and r 65535 |
| 556 | end |
| 557 | if ne e 0 |
| 558 | trap 8 |
| 559 | end |
| 560 | end |
| 561 | note |
| 562 | note The trace reader (section 4). Bad input: the events so far, one line on standard error, status 2. |
| 563 | fn bad to void |
| 564 | oflush 1 |
| 565 | for j ulong len t_bad |
| 566 | ochr ulong get t_bad j |
| 567 | end |
| 568 | onum get g_line 0 |
| 569 | ochr 10 |
| 570 | oflush 2 |
| 571 | exit 2 |
| 572 | end |
| 573 | note nomem: the host refused memory for the tables. The events so far, one line on standard error, |
| 574 | note status 4. |
| 575 | fn nomem to void |
| 576 | oflush 1 |
| 577 | for j ulong len t_mem |
| 578 | ochr ulong get t_mem j |
| 579 | end |
| 580 | onum get g_line 0 |
| 581 | ochr 10 |
| 582 | oflush 2 |
| 583 | exit 4 |
| 584 | end |
| 585 | note getc gives the next input byte, or 256 at the end of input. |
| 586 | fn getc to ulong |
| 587 | p = get g_ip 0 |
| 588 | if ge p get g_il 0 |
| 589 | r = read 0 g_ib 0 65536 |
| 590 | if lt r 0 |
| 591 | oflush 1 |
| 592 | for j ulong len t_io |
| 593 | ochr ulong get t_io j |
| 594 | end |
| 595 | oflush 2 |
| 596 | exit 3 |
| 597 | end |
| 598 | if eq r 0 |
| 599 | ret 256 |
| 600 | end |
| 601 | put g_il 0 ulong r |
| 602 | p = 0 |
| 603 | end |
| 604 | put g_ip 0 add p 1 |
| 605 | ret ulong get g_ib p |
| 606 | end |
| 607 | note num reads a decimal number of 1 to 15 digits and leaves the byte after it in g_term. |
| 608 | fn num to ulong |
| 609 | v = ulong 0 |
| 610 | nd = ulong 0 |
| 611 | b = getc |
| 612 | while and ge b 48 le b 57 bound 9223372036854775807 |
| 613 | if ge nd 15 |
| 614 | bad |
| 615 | end |
| 616 | v = add mul v 10 sub b 48 |
| 617 | nd = add nd 1 |
| 618 | b = getc |
| 619 | end |
| 620 | if eq nd 0 |
| 621 | bad |
| 622 | end |
| 623 | put g_term 0 b |
| 624 | ret v |
| 625 | end |
| 626 | note sep refuses the line unless the last number ended with byte want (32 space, 10 newline). |
| 627 | fn sep want ulong to void |
| 628 | if ne get g_term 0 want |
| 629 | bad |
| 630 | end |
| 631 | end |
| 632 | note c CLASS K |
| 633 | fn cline to void |
| 634 | if ne getc 32 |
| 635 | bad |
| 636 | end |
| 637 | c = num |
| 638 | sep 32 |
| 639 | kk = num |
| 640 | sep 10 |
| 641 | if or lt kk 2 gt kk KTRACE |
| 642 | bad |
| 643 | end |
| 644 | r = setup c kk |
| 645 | if eq r E_MEM |
| 646 | nomem |
| 647 | end |
| 648 | if ne r 0 |
| 649 | bad |
| 650 | end |
| 651 | m = enlarge g_tm kk |
| 652 | if ne m 0 |
| 653 | nomem |
| 654 | end |
| 655 | end |
| 656 | note t CLASS t0 .. t(K-1) OKMASK |
| 657 | fn tline to void |
| 658 | if ne getc 32 |
| 659 | bad |
| 660 | end |
| 661 | c = num |
| 662 | sep 32 |
| 663 | if eq known c 0 |
| 664 | bad |
| 665 | end |
| 666 | kk = get s_K c |
| 667 | for j ulong kk |
| 668 | put g_tm j num |
| 669 | sep 32 |
| 670 | end |
| 671 | mask = num |
| 672 | sep 10 |
| 673 | if ge mask shl 1 kk |
| 674 | bad |
| 675 | end |
| 676 | step c mask |
| 677 | end |
| 678 | note At end of input: CLASS F INC RESTLEN COUNTS.. STATUS.. for each declared class. |
| 679 | fn finals to void |
| 680 | nc = len s_K |
| 681 | for c ulong nc bound 9223372036854775807 |
| 682 | kk = get s_K c |
| 683 | if ne kk 0 |
| 684 | onum c |
| 685 | ochr 32 |
| 686 | ochr 70 |
| 687 | osn get s_inc c |
| 688 | osn get s_restlen c |
| 689 | for a ulong kk |
| 690 | osn get s_counts ix c a |
| 691 | end |
| 692 | for e ulong kk |
| 693 | osn get s_status ix c e |
| 694 | end |
| 695 | ochr 10 |
| 696 | end |
| 697 | end |
| 698 | end |
| 699 | fn main to int |
| 700 | b = getc |
| 701 | while ne b 256 bound 9223372036854775807 |
| 702 | put g_line 0 add get g_line 0 1 |
| 703 | if eq b 99 |
| 704 | cline |
| 705 | else |
| 706 | if eq b 116 |
| 707 | tline |
| 708 | else |
| 709 | bad |
| 710 | end |
| 711 | end |
| 712 | b = getc |
| 713 | end |
| 714 | finals |
| 715 | oflush 1 |
| 716 | ret 0 |
| 717 | end |