sovereign-intelligence
Front door to Drake Stapleton's sovereign stack.
Brought by drakestapleton
sovereign-intelligence / tools / ledger.om
| 1 | om2 |
| 2 | note tools/ledger.om: the stack ledger, in pure seeded Omega. |
| 3 | note |
| 4 | note It counts what every recovered project is written in, so the migration to Omega is |
| 5 | note measured, not guessed. Build with the seed-born compiler: |
| 6 | note omc < tools/ledger.om > ledger && chmod +x ledger |
| 7 | note { printf 'target x86linux\n'; cat tools/ledger.om; } | omc > ledger (native x86-64) |
| 8 | note Run: |
| 9 | note ledger < LIST > LEDGER.tsv |
| 10 | note LIST stdin, at most 16 MiB: one file per line, "project TAB path". The path is opened |
| 11 | note as given, relative to the current directory; everything after the first TAB is the |
| 12 | note path. All lines of one project are contiguous. tools/ledger-refresh.sh writes it. |
| 13 | note For each path it opens the file read-only and counts its bytes and its newline bytes |
| 14 | note (the count wc -l prints). The category comes from the extension, the bytes after the last |
| 15 | note dot of the final path component (none without a dot): |
| 16 | note om -> om; rs -> rust; c h -> c; sh -> sh; py -> py; |
| 17 | note cpp cc hpp cu go ts tsx js mjs swift zig s S -> other; |
| 18 | note anything else is not code: it counts in files and bytes only. |
| 19 | note stdout: the header line |
| 20 | note project files bytes om rust c sh py other foreign om_permille (TAB-separated) |
| 21 | note then one row per project in input order, then a TOTAL row. Line counts per category; |
| 22 | note foreign = rust + c + sh + py + other; om_permille = floor(om * 1000 / (om + foreign)), |
| 23 | note 0 when both are 0. All numbers in decimal. |
| 24 | note Refusals, each with one stderr line "ledger: ..." and exit 1, and nothing on stdout (the |
| 25 | note output is buffered and written only at the end): a blank line or a line without |
| 26 | note "project TAB path"; a path over 4095 bytes; a file that cannot be opened or read; a project |
| 27 | note whose lines are not contiguous; more than 1024 projects; input over 16 MiB. |
| 28 | note The output depends only on the input list and the files' bytes: no clock, no environment. |
| 29 | |
| 30 | note standard input |
| 31 | global inp uchar 16777216 |
| 32 | global inn ulong 1 |
| 33 | note one read of a file |
| 34 | global fb uchar 1048576 |
| 35 | note the path being opened |
| 36 | global pb uchar 4096 |
| 37 | note the output, written once at the end |
| 38 | global ob uchar 4194304 |
| 39 | global obn ulong 1 |
| 40 | note projects seen so far: name offset and length in inp |
| 41 | global seen ulong 2048 |
| 42 | global nseen ulong 1 |
| 43 | note counters of the current project and the totals: |
| 44 | note 0 files, 1 bytes, 2 om, 3 rust, 4 c, 5 sh, 6 py, 7 other (lines) |
| 45 | global cur ulong 8 |
| 46 | global tot ulong 8 |
| 47 | |
| 48 | text tpre "ledger: " |
| 49 | text tnl "\n" |
| 50 | text ttab "\t" |
| 51 | text tusage "usage: ledger < LIST (LIST: one \"project TAB path\" line per file)\n" |
| 52 | text tbig "input over the 16 MiB buffer" |
| 53 | text tread "cannot read standard input" |
| 54 | text tline "line needs project TAB path: " |
| 55 | text tlong "path over 4095 bytes: " |
| 56 | text topen "cannot open " |
| 57 | text tfread "cannot read " |
| 58 | text tsplit "project lines are not contiguous: " |
| 59 | text tmany "more than 1024 projects at: " |
| 60 | text toverf "output is over the 4 MiB buffer" |
| 61 | text twrite "cannot write standard output" |
| 62 | text thead "project\tfiles\tbytes\tom\trust\tc\tsh\tpy\tother\tforeign\tom_permille\n" |
| 63 | text ttotal "TOTAL" |
| 64 | text xom "om" |
| 65 | text xrs "rs" |
| 66 | text xc "c" |
| 67 | text xh "h" |
| 68 | text xsh "sh" |
| 69 | text xpy "py" |
| 70 | text xcpp "cpp" |
| 71 | text xcc "cc" |
| 72 | text xhpp "hpp" |
| 73 | text xcu "cu" |
| 74 | text xgo "go" |
| 75 | text xts "ts" |
| 76 | text xtsx "tsx" |
| 77 | text xjs "js" |
| 78 | text xmjs "mjs" |
| 79 | text xswift "swift" |
| 80 | text xzig "zig" |
| 81 | text xas "s" |
| 82 | text xbs "S" |
| 83 | |
| 84 | note die: report m and up to 4096 bytes of inp from o on stderr, then exit 1 |
| 85 | fn die m view uchar * o ulong n ulong to void |
| 86 | k = n |
| 87 | if gt k 4096 |
| 88 | k = 4096 |
| 89 | end |
| 90 | w = write 2 tpre 0 len tpre |
| 91 | w = write 2 m 0 len m |
| 92 | if gt k 0 |
| 93 | w = write 2 inp o k |
| 94 | end |
| 95 | w = write 2 tnl 0 len tnl |
| 96 | exit 1 |
| 97 | end |
| 98 | |
| 99 | note em: append a text to ob |
| 100 | fn em t view uchar * to void |
| 101 | p = get obn 0 |
| 102 | n = len t |
| 103 | if gt n sub 4194304 p |
| 104 | die toverf 0 0 |
| 105 | end |
| 106 | for i ulong n |
| 107 | put ob add p i get t i |
| 108 | end |
| 109 | put obn 0 add p n |
| 110 | end |
| 111 | |
| 112 | note emi: append n bytes of inp from o to ob |
| 113 | fn emi o ulong n ulong to void |
| 114 | p = get obn 0 |
| 115 | if gt n sub 4194304 p |
| 116 | die toverf 0 0 |
| 117 | end |
| 118 | for i ulong n bound 4194304 |
| 119 | put ob add p i get inp add o i |
| 120 | end |
| 121 | put obn 0 add p n |
| 122 | end |
| 123 | |
| 124 | note emb: append one byte to ob |
| 125 | fn emb c uchar to void |
| 126 | p = get obn 0 |
| 127 | if ge p 4194304 |
| 128 | die toverf 0 0 |
| 129 | end |
| 130 | put ob p c |
| 131 | put obn 0 add p 1 |
| 132 | end |
| 133 | |
| 134 | note emu: append v in decimal to ob |
| 135 | fn emu v ulong to void |
| 136 | d = new uchar 20 0 |
| 137 | x = v |
| 138 | put d 0 uchar add rem x 10 48 |
| 139 | k = ulong 1 |
| 140 | x = div x 10 |
| 141 | while gt x 0 bound 20 |
| 142 | put d k uchar add rem x 10 48 |
| 143 | k = add k 1 |
| 144 | x = div x 10 |
| 145 | end |
| 146 | while gt k 0 bound 20 |
| 147 | k = sub k 1 |
| 148 | emb get d k |
| 149 | end |
| 150 | end |
| 151 | |
| 152 | note wout: write ob to standard output |
| 153 | fn wout to void |
| 154 | n = get obn 0 |
| 155 | p = ulong 0 |
| 156 | while lt p n bound 4194305 |
| 157 | k = write 1 ob p sub n p |
| 158 | if le k 0 |
| 159 | die twrite 0 0 |
| 160 | end |
| 161 | p = add p ulong k |
| 162 | end |
| 163 | end |
| 164 | |
| 165 | note loadin: read all of standard input into inp |
| 166 | fn loadin to void |
| 167 | t = new uchar 1 0 |
| 168 | p = ulong 0 |
| 169 | going = true |
| 170 | note every pass but the last reads at least one byte of a 16 MiB buffer |
| 171 | while going bound 16777218 |
| 172 | k = read 0 inp p sub 16777216 p |
| 173 | if lt k 0 |
| 174 | die tread 0 0 |
| 175 | end |
| 176 | if eq k 0 |
| 177 | going = false |
| 178 | else |
| 179 | p = add p ulong k |
| 180 | if eq p 16777216 |
| 181 | going = false |
| 182 | end |
| 183 | end |
| 184 | end |
| 185 | if eq p 16777216 |
| 186 | k = read 0 t 0 1 |
| 187 | if ne k 0 |
| 188 | die tbig 0 0 |
| 189 | end |
| 190 | end |
| 191 | put inn 0 p |
| 192 | end |
| 193 | |
| 194 | note eqx: do the n bytes of inp at o equal the text t |
| 195 | fn eqx o ulong n ulong t view uchar * to bool |
| 196 | if ne n len t |
| 197 | ret false |
| 198 | end |
| 199 | for i ulong n |
| 200 | if ne get inp add o i get t i |
| 201 | ret false |
| 202 | end |
| 203 | end |
| 204 | ret true |
| 205 | end |
| 206 | |
| 207 | note eqs: are two slices of inp equal |
| 208 | fn eqs a ulong an ulong b ulong bn ulong to bool |
| 209 | if ne an bn |
| 210 | ret false |
| 211 | end |
| 212 | for i ulong an bound 16777216 |
| 213 | if ne get inp add a i get inp add b i |
| 214 | ret false |
| 215 | end |
| 216 | end |
| 217 | ret true |
| 218 | end |
| 219 | |
| 220 | note eol: the end of the line that starts at p (its newline, or e) |
| 221 | fn eol p ulong e ulong to ulong |
| 222 | q = p |
| 223 | while lt q e bound 16777217 |
| 224 | if eq get inp q 10 |
| 225 | ret q |
| 226 | end |
| 227 | q = add q 1 |
| 228 | end |
| 229 | ret e |
| 230 | end |
| 231 | |
| 232 | note isoth: is the extension at inp[o, o+n) one of the other code languages |
| 233 | fn isoth o ulong n ulong to bool |
| 234 | if eqx o n xcpp |
| 235 | ret true |
| 236 | end |
| 237 | if eqx o n xcc |
| 238 | ret true |
| 239 | end |
| 240 | if eqx o n xhpp |
| 241 | ret true |
| 242 | end |
| 243 | if eqx o n xcu |
| 244 | ret true |
| 245 | end |
| 246 | if eqx o n xgo |
| 247 | ret true |
| 248 | end |
| 249 | if eqx o n xts |
| 250 | ret true |
| 251 | end |
| 252 | if eqx o n xtsx |
| 253 | ret true |
| 254 | end |
| 255 | if eqx o n xjs |
| 256 | ret true |
| 257 | end |
| 258 | if eqx o n xmjs |
| 259 | ret true |
| 260 | end |
| 261 | if eqx o n xswift |
| 262 | ret true |
| 263 | end |
| 264 | if eqx o n xzig |
| 265 | ret true |
| 266 | end |
| 267 | if eqx o n xas |
| 268 | ret true |
| 269 | end |
| 270 | ret eqx o n xbs |
| 271 | end |
| 272 | |
| 273 | note catof: the category of the extension at inp[o, o+n): 0 not code, 1 om, 2 rust, 3 c, |
| 274 | note 4 sh, 5 py, 6 other |
| 275 | fn catof o ulong n ulong to ulong |
| 276 | if eqx o n xom |
| 277 | ret 1 |
| 278 | end |
| 279 | if eqx o n xrs |
| 280 | ret 2 |
| 281 | end |
| 282 | if eqx o n xc |
| 283 | ret 3 |
| 284 | end |
| 285 | if eqx o n xh |
| 286 | ret 3 |
| 287 | end |
| 288 | if eqx o n xsh |
| 289 | ret 4 |
| 290 | end |
| 291 | if eqx o n xpy |
| 292 | ret 5 |
| 293 | end |
| 294 | if isoth o n |
| 295 | ret 6 |
| 296 | end |
| 297 | ret 0 |
| 298 | end |
| 299 | |
| 300 | note extcat: the category of the path at inp[o, o+n), from the bytes after the last dot of |
| 301 | note its final component; 0 when that component has no dot |
| 302 | fn extcat o ulong n ulong to ulong |
| 303 | d = n |
| 304 | i = n |
| 305 | while gt i 0 bound 4096 |
| 306 | i = sub i 1 |
| 307 | c = get inp add o i |
| 308 | if eq c 47 |
| 309 | break |
| 310 | end |
| 311 | if eq c 46 |
| 312 | d = i |
| 313 | break |
| 314 | end |
| 315 | end |
| 316 | if eq d n |
| 317 | ret 0 |
| 318 | end |
| 319 | s = add d 1 |
| 320 | ret catof add o s sub n s |
| 321 | end |
| 322 | |
| 323 | note nlines: the newline bytes among the first m bytes of fb (count checks m against fb, trap 3) |
| 324 | fn nlines m ulong to ulong |
| 325 | ret count fb 0 m 10 |
| 326 | end |
| 327 | |
| 328 | note countf: open the path at inp[o, o+n) and add its file, bytes and lines to cur |
| 329 | fn countf o ulong n ulong to void |
| 330 | if gt n 4095 |
| 331 | die tlong o n |
| 332 | end |
| 333 | for i ulong n bound 4095 |
| 334 | put pb i get inp add o i |
| 335 | end |
| 336 | fd = fopen pb 0 n 0 0 |
| 337 | if lt fd 0 |
| 338 | die topen o n |
| 339 | end |
| 340 | by = ulong 0 |
| 341 | nl = ulong 0 |
| 342 | going = true |
| 343 | note every pass but the last reads at least one byte; no file here reaches 2^40 bytes |
| 344 | while going bound 1099511627776 |
| 345 | k = read int fd fb 0 1048576 |
| 346 | if lt k 0 |
| 347 | die tfread o n |
| 348 | end |
| 349 | if eq k 0 |
| 350 | going = false |
| 351 | else |
| 352 | m = ulong k |
| 353 | by = add by m |
| 354 | nl = add nl nlines m |
| 355 | end |
| 356 | end |
| 357 | c = fclose int fd |
| 358 | cat = extcat o n |
| 359 | put cur 0 add get cur 0 1 |
| 360 | put cur 1 add get cur 1 by |
| 361 | if gt cat 0 |
| 362 | j = add cat 1 |
| 363 | put cur j add get cur j nl |
| 364 | end |
| 365 | end |
| 366 | |
| 367 | note cell: counter k of the current project (w 0) or of the totals (w 1) |
| 368 | fn cell w ulong k ulong to ulong |
| 369 | if eq w 0 |
| 370 | ret get cur k |
| 371 | end |
| 372 | ret get tot k |
| 373 | end |
| 374 | |
| 375 | note emnums: the eight counters, foreign and om_permille of w (see cell), then the newline |
| 376 | fn emnums w ulong to void |
| 377 | for k ulong 8 |
| 378 | em ttab |
| 379 | emu cell w k |
| 380 | end |
| 381 | fo = ulong 0 |
| 382 | for j ulong 5 |
| 383 | fo = add fo cell w add j 3 |
| 384 | end |
| 385 | em ttab |
| 386 | emu fo |
| 387 | om = cell w 2 |
| 388 | pm = ulong 0 |
| 389 | if gt add om fo 0 |
| 390 | pm = div mul om 1000 add om fo |
| 391 | end |
| 392 | em ttab |
| 393 | emu pm |
| 394 | em tnl |
| 395 | end |
| 396 | |
| 397 | note flush: emit the current project's row, add it to the totals and zero it |
| 398 | fn flush to void |
| 399 | k = get nseen 0 |
| 400 | if eq k 0 |
| 401 | ret |
| 402 | end |
| 403 | r = sub k 1 |
| 404 | emi get seen mul r 2 get seen add mul r 2 1 |
| 405 | emnums 0 |
| 406 | for i ulong 8 |
| 407 | put tot i add get tot i get cur i |
| 408 | put cur i 0 |
| 409 | end |
| 410 | end |
| 411 | |
| 412 | note startp: begin the project named inp[po, po+pn); refuse a name seen before |
| 413 | fn startp po ulong pn ulong to void |
| 414 | k = get nseen 0 |
| 415 | for i ulong k bound 1024 |
| 416 | if eqs get seen mul i 2 get seen add mul i 2 1 po pn |
| 417 | die tsplit po pn |
| 418 | end |
| 419 | end |
| 420 | if ge k 1024 |
| 421 | die tmany po pn |
| 422 | end |
| 423 | put seen mul k 2 po |
| 424 | put seen add mul k 2 1 pn |
| 425 | put nseen 0 add k 1 |
| 426 | end |
| 427 | |
| 428 | note iscur: is inp[po, po+pn) the name of the current project |
| 429 | fn iscur po ulong pn ulong to bool |
| 430 | k = get nseen 0 |
| 431 | if eq k 0 |
| 432 | ret false |
| 433 | end |
| 434 | r = sub k 1 |
| 435 | a = get seen mul r 2 |
| 436 | an = get seen add mul r 2 1 |
| 437 | ret eqs a an po pn |
| 438 | end |
| 439 | |
| 440 | note doline: one LIST line [p, q): "project TAB path" |
| 441 | fn doline p ulong q ulong to void |
| 442 | t = p |
| 443 | while lt t q bound 16777217 |
| 444 | if eq get inp t 9 |
| 445 | break |
| 446 | end |
| 447 | t = add t 1 |
| 448 | end |
| 449 | if or eq t q eq t p |
| 450 | die tline p sub q p |
| 451 | end |
| 452 | o = add t 1 |
| 453 | if eq o q |
| 454 | die tline p sub q p |
| 455 | end |
| 456 | pn = sub t p |
| 457 | if not iscur p pn |
| 458 | flush |
| 459 | startp p pn |
| 460 | end |
| 461 | countf o sub q o |
| 462 | end |
| 463 | |
| 464 | fn main to int |
| 465 | if ne argcnt 1 |
| 466 | w = write 2 tusage 0 len tusage |
| 467 | ret 2 |
| 468 | end |
| 469 | loadin |
| 470 | em thead |
| 471 | e = get inn 0 |
| 472 | p = ulong 0 |
| 473 | note every pass consumes at least one byte of the input |
| 474 | while lt p e bound 16777217 |
| 475 | q = eol p e |
| 476 | doline p q |
| 477 | p = add q 1 |
| 478 | end |
| 479 | flush |
| 480 | em ttotal |
| 481 | emnums 1 |
| 482 | wout |
| 483 | ret 0 |
| 484 | end |