interplane.Get Omega
Read in your language

omega-harness

Agent skills for operating, encoding and communicating in Omega (.om) and the AIEN repositories

Brought by drakestapleton

branch main Branches Tags History

omega-harness / tools / guides.om

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