interplane.Get Omega
Read in your language

sovereign-intelligence

Front door to Drake Stapleton's sovereign stack.

Brought by drakestapleton

branch main Branches Tags History

sovereign-intelligence / tools / ledger.om

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