interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / om / golden / golden.om

1460 lines · 24559 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note om/golden/golden.om: golden-token recorder and comparator (Core #853, DRA-52). Every judgment is
3note made here; record.sh only sequences and curl only moves bytes. Build (build.sh does this):
4note { printf 'om2\n'; cat om/vendor/json.om om/vendor/sha256.om om/golden/golden.om; } | omc
5note Modes:
6note golden req PROMPTS MODEL_REPO MAX_TOKENS OUTDIR
7note validates the prompt set, writes one greedy request body per prompt to OUTDIR/<i>.json
8note (temperature 0, aien_return_token_ids true) and prints the prompt count.
9note golden rec PROMPTS MODEL_REPO MODEL_REV MAX_TOKENS ENGINE_SHA256 HOST_ID LABEL RESPDIR
10note reads the engine reply RESPDIR/<i>.json for every prompt, takes usage.aien_token_ids, refuses a
11note reply whose usage.aien_sampling.mode is not greedy, and prints one aien-golden-record/1.
12note golden cmp REF CAND
13note prints MATCH or DIVERGE id index=K ref=X cand=Y per prompt (K is the first differing position;
14note a list that ends early shows end).
15note Exit: 0 written or all match, 1 token mismatch, 2 usage or bad argument, 3 not comparable
16note (label, engine_sha256, model_repo, model_rev, sampling, max_tokens, prompts_sha256 or prompt ids
17note differ), 4 file, json or schema refusal, 5 engine reply refused. Schema: om/golden/SCHEMA.md.
18
19const SRCCAP ulong 524288
20const TAPECAP ulong 786432
21const OUTCAP ulong 524288
22const PMAX ulong 1024
23const TKMAX ulong 262144
24const NFLD ulong 12
25const FCAP ulong 128
26const MAXTOK ulong 1000000
27
28global ga uchar 16384
29global gas ulong 16
30global gal ulong 16
31global gna ulong 1
32global gpth uchar 4352
33global gout uchar 524288
34global gon ulong 1
35global gst ulong 4
36global gsd ulong 1
37global gseen uchar 32
38global gpid uchar 131072
39global gpidl ulong 2048
40global gpco ulong 1024
41global gpcs ulong 1024
42global gtk uint 524288
43global gtko ulong 2048
44global gtkn ulong 2048
45global gtkt ulong 2
46global gnp ulong 2
47global ghv uchar 3072
48global ghl ulong 24
49global gstb uchar 128
50global gutc uchar 20
51
52text tpre "golden: refused "
53text tuse "usage: golden req PROMPTS MODEL_REPO MAX_TOKENS OUTDIR | golden rec PROMPTS MODEL_REPO MODEL_REV MAX_TOKENS ENGINE_SHA256 HOST_ID LABEL RESPDIR | golden cmp REF CAND"
54text tmreq "req"
55text tmrec "rec"
56text tmcmp "cmp"
57text tio "io"
58text tnreg "not-a-regular-file"
59text tbig "too-large"
60text tjson "json"
61text tpsch "prompts-schema"
62text trsch "record-schema"
63text tclock "clock"
64text tbrepo "bad-model-repo"
65text tbrev "bad-model-rev"
66text tbmax "bad-max-tokens"
67text tbeng "bad-engine-sha256"
68text tbhost "bad-host-id"
69text tblabel "bad-label"
70text tengerr "engine-error-reply"
71text tnoids "no-usage-aien_token_ids"
72text tsamp "aien_sampling-not-object"
73text tsampled "not-greedy"
74text tbadtok "bad-token-id"
75text tjsuf ".json"
76
77text tq1 "{\"model\":\""
78text tq2 "\",\"messages\":[{\"role\":\"user\",\"content\":"
79text tq3 "}],\"temperature\":0,\"top_p\":1,\"seed\":0,\"max_tokens\":"
80text tq4 ",\"stream\":false,\"chat_template_kwargs\":{\"enable_thinking\":"
81text tq5 "},\"aien_return_token_ids\":true}\n"
82text ttrue "true"
83text tfalse "false"
84
85text tr1 "{\"schema\":\"aien-golden-record/1\",\"label\":\""
86text tr2 "\",\"engine_sha256\":\""
87text tr3 "\",\"model_repo\":\""
88text tr4 "\",\"model_rev\":\""
89text tr5 "\",\"sampling\":{\"temperature\":0,\"top_p\":1,\"seed\":0,\"enable_thinking\":"
90text tr6 "},\"max_tokens\":"
91text tr7 ",\"prompts_sha256\":\""
92text tr8 "\",\"host_id\":\""
93text tr9 "\",\"utc\":\""
94text tr10 "\",\"prompts\":["
95text tp1 "{\"id\":\""
96text tp2 "\",\"ids\":["
97text tp3 "]}"
98text tend "\n]}\n"
99
100text kschema "schema"
101text kthink "enable_thinking"
102text kprom "prompts"
103text kid "id"
104text kcont "content"
105text klabel "label"
106text keng "engine_sha256"
107text krepo "model_repo"
108text krev "model_rev"
109text ksamp "sampling"
110text kmaxt "max_tokens"
111text kpsha "prompts_sha256"
112text khost "host_id"
113text kutc "utc"
114text kids "ids"
115text ktemp "temperature"
116text ktopp "top_p"
117text kseed "seed"
118text kerror "error"
119text kusage "usage"
120text ktids "aien_token_ids"
121text kasamp "aien_sampling"
122text kmode "mode"
123text vpsch "aien-golden-prompts/1"
124text vrsch "aien-golden-record/1"
125text vgreedy "greedy"
126text vreal "REAL"
127text vfake "FAKE"
128text vsynth "SYNTHETIC"
129
130text cinfo1 "golden-cmp ref_host="
131text cinfo2 " ref_utc="
132text cinfo3 " cand_host="
133text cinfo4 " cand_utc="
134text cmatch "MATCH "
135text cdivg "DIVERGE "
136text cidx " index="
137text cref " ref="
138text ccand " cand="
139text cn " n="
140text cend "end"
141text cnot "NOTCOMPARABLE "
142text cpcnt "prompt_count"
143text cpidx "prompt_id index="
144text csum "golden-cmp prompts="
145text csm " match="
146text csd " diverge="
147
148note ======== failure and output ========
149
150fn die code int msg view uchar * to void
151d = new uchar 2 0
152w = write 2 tpre 0 len tpre
153put d 0 uchar add 48 code
154put d 1 32
155w = write 2 d 0 2
156w = write 2 msg 0 len msg
157put d 0 10
158w = write 2 d 0 1
159exit code
160end
161
162fn obyte c uchar to void
163n = get gon 0
164if ge n OUTCAP
165die 4 tbig
166end
167put gout n c
168put gon 0 add n 1
169end
170
171fn ot t view uchar * to void
172for i ulong len t
173obyte get t i
174end
175end
176
177fn osp src view uchar * off ulong n ulong to void
178for i ulong n
179obyte get src add off i
180end
181end
182
183fn ou v ulong to void
184d = new uchar 24 0
185x = v
186k = ulong 0
187put d k uchar add 48 rem x 10
188x = div x 10
189k = add k 1
190while gt x 0 bound 20
191put d k uchar add 48 rem x 10
192x = div x 10
193k = add k 1
194end
195while gt k 0 bound 21
196k = sub k 1
197obyte get d k
198end
199end
200
201fn oflush fd int to void
202n = get gon 0
203o = ulong 0
204while lt o n bound 999999
205r = write fd gout o sub n o
206if le r 0
207die 4 tio
208end
209o = add o ulong r
210end
211put gon 0 0
212end
213
214note ======== arguments and paths ========
215
216fn argeq a ulong t view uchar * to bool
217n = get gal a
218if ne n len t
219ret false
220end
221o = get gas a
222for i ulong n
223if ne get ga add o i get t i
224ret false
225end
226end
227ret true
228end
229
230fn argp a ulong to ulong
231n = get gal a
232if or eq n 0 gt n 4000
233die 2 tuse
234end
235o = get gas a
236for i ulong n
237put gpth i get ga add o i
238end
239ret n
240end
241
242fn oarg a ulong to void
243o = get gas a
244for i ulong get gal a
245obyte get ga add o i
246end
247end
248
249fn namec c uchar sl bool to bool
250if and ge c 48 le c 57
251ret true
252end
253if and ge c 65 le c 90
254ret true
255end
256if and ge c 97 le c 122
257ret true
258end
259if or eq c 46 or eq c 95 eq c 45
260ret true
261end
262ret and sl eq c 47
263end
264
265fn hexc c uchar to bool
266if and ge c 48 le c 57
267ret true
268end
269ret and ge c 97 le c 102
270end
271
272fn aname a ulong mx ulong sl bool to bool
273n = get gal a
274if or eq n 0 gt n mx
275ret false
276end
277o = get gas a
278for i ulong n
279if not namec get ga add o i sl
280ret false
281end
282end
283ret true
284end
285
286fn ahex a ulong n ulong to bool
287if ne get gal a n
288ret false
289end
290o = get gas a
291for i ulong n
292if not hexc get ga add o i
293ret false
294end
295end
296ret true
297end
298
299note argu: a decimal argument 1 .. MAXTOK without a leading zero, or 0 when it is anything else
300fn argu a ulong to ulong
301n = get gal a
302if or eq n 0 gt n 7
303ret 0
304end
305o = get gas a
306if eq get ga o 48
307ret 0
308end
309v = ulong 0
310for i ulong n
311c = get ga add o i
312if or lt c 48 gt c 57
313ret 0
314end
315v = add mul v 10 ulong sub c 48
316end
317if gt v MAXTOK
318ret 0
319end
320ret v
321end
322
323fn pdec n ulong v ulong to ulong
324d = new uchar 24 0
325x = v
326k = ulong 0
327put d k uchar add 48 rem x 10
328x = div x 10
329k = add k 1
330while gt x 0 bound 20
331put d k uchar add 48 rem x 10
332x = div x 10
333k = add k 1
334end
335m = n
336while gt k 0 bound 21
337k = sub k 1
338put gpth m get d k
339m = add m 1
340end
341ret m
342end
343
344note pthidx: gpth = argument a, a slash, decimal i, .json
345fn pthidx a ulong i ulong to ulong
346n = argp a
347put gpth n 47
348n = pdec add n 1 i
349for j ulong len tjsuf
350put gpth add n j get tjsuf j
351end
352ret add n len tjsuf
353end
354
355note ======== files ========
356
357fn le64 o ulong to ulong
358v = ulong 0
359for i ulong 8
360v = or v shl ulong get gstb add o i mul 8 i
361end
362ret v
363end
364
365note rdfile: read the regular file named by gpth[0..n) (O_RDONLY|O_CLOEXEC|O_NOFOLLOW) into src
366fn rdfile n ulong src edit uchar * to ulong
367fd = fopen gpth 0 n 557056 0
368if lt fd 0
369die 4 tio
370end
371r = fstat int fd gstb 0 128
372if ne r 0
373die 4 tio
374end
375md = or ulong get gstb 16 shl ulong get gstb 17 8
376if ne and md 61440 32768
377die 4 tnreg
378end
379sz = le64 48
380if gt sz len src
381die 4 tbig
382end
383o = ulong 0
384while lt o sz bound 999999
385q = pread int fd src o sub sz o long o
386if le q 0
387die 4 tio
388end
389o = add o ulong q
390end
391r = fclose int fd
392if ne r 0
393die 4 tio
394end
395ret sz
396end
397
398note wrfile: create gpth[0..n) (O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC, 0600) from gout
399fn wrfile n ulong to void
400fd = fopen gpth 0 n 557249 384
401if lt fd 0
402die 4 tio
403end
404oflush int fd
405r = fclose int fd
406if ne r 0
407die 4 tio
408end
409end
410
411fn jparse src view uchar * tape edit uint * n ulong to void
412r = js_init tape n 64
413if lt r 0
414die 4 tjson
415end
416r = js_parse src tape n
417if lt r 0
418die 4 tjson
419end
420if ne js_type tape 0 jsobj
421die 4 tjson
422end
423end
424
425note ======== token checks ========
426
427fn keyis src view uchar * off ulong key view uchar * to bool
428ret js_streq src off key
429end
430
431note vname: kind 0 is an id (1..64 of [A-Za-z0-9._-]), kind 1 a model repo (1..96, slash allowed)
432fn vname src view uchar * tape view uint * t ulong kind ulong to bool
433mx = ulong 64
434sl = false
435if eq kind 1
436mx = ulong 96
437sl = true
438end
439if ne js_type tape t jsstr
440ret false
441end
442off = js_off tape t
443sp = js_span tape t
444if or lt sp 3 gt sp add mx 2
445ret false
446end
447for i ulong sub sp 2
448if not namec get src add off add i 1 sl
449ret false
450end
451end
452ret true
453end
454
455fn vhex src view uchar * tape view uint * t ulong n ulong to bool
456if ne js_type tape t jsstr
457ret false
458end
459if ne js_span tape t add n 2
460ret false
461end
462off = js_off tape t
463for i ulong n
464if not hexc get src add off add i 1
465ret false
466end
467end
468ret true
469end
470
471fn utcc i ulong c uchar to bool
472if or eq i 4 eq i 7
473ret eq c 45
474end
475if eq i 10
476ret eq c 84
477end
478if or eq i 13 eq i 16
479ret eq c 58
480end
481if eq i 19
482ret eq c 90
483end
484ret and ge c 48 le c 57
485end
486
487fn vutc src view uchar * tape view uint * t ulong to bool
488if ne js_type tape t jsstr
489ret false
490end
491if ne js_span tape t 22
492ret false
493end
494off = add js_off tape t 1
495for i ulong 20
496if not utcc i get src add off i
497ret false
498end
499end
500ret true
501end
502
503fn vuint src view uchar * tape view uint * t ulong to bool
504if ne js_type tape t jsnum
505ret false
506end
507ret js_isu src js_off tape t
508end
509
510fn tokv src view uchar * tape view uint * e ulong code int to ulong
511if not vuint src tape e
512die code tbadtok
513end
514v = js_tou src js_off tape e
515if gt v 4294967295
516die code tbadtok
517end
518ret v
519end
520
521fn seenclr to void
522for i ulong 32
523put gseen i 0
524end
525end
526
527fn seen i ulong msg view uchar * to void
528if ne get gseen i 0
529die 4 msg
530end
531put gseen i 1
532end
533
534fn seenall a ulong n ulong msg view uchar * to void
535for i ulong n
536if eq get gseen add a i 0
537die 4 msg
538end
539end
540end
541
542note ======== prompt ids (side 0 = prompt set or reference, side 1 = candidate) ========
543
544fn cpcopy src view uchar * off ulong n ulong s ulong to void
545b = mul s 64
546for j ulong n
547put gpid add b j get src add off j
548end
549put gpidl s n
550end
551
552fn cpid src view uchar * tape view uint * t ulong i ulong to void
553s = add mul get gsd 0 PMAX i
554cpcopy src add js_off tape t 1 sub js_span tape t 2 s
555end
556
557fn pideq x ulong y ulong n ulong to bool
558p = mul x 64
559q = mul y 64
560for j ulong n
561if ne get gpid add p j get gpid add q j
562ret false
563end
564end
565ret true
566end
567
568fn ideq sa ulong a ulong sb ulong b ulong to bool
569x = add mul sa PMAX a
570y = add mul sb PMAX b
571if ne get gpidl x get gpidl y
572ret false
573end
574ret pideq x y get gpidl x
575end
576
577fn dupid sd ulong i ulong to bool
578for j ulong i
579if ideq sd j sd i
580ret true
581end
582end
583ret false
584end
585
586fn oid sd ulong i ulong to void
587s = add mul sd PMAX i
588for j ulong get gpidl s
589obyte get gpid add mul s 64 j
590end
591end
592
593note ======== prompt set (aien-golden-prompts/1) ========
594
595fn pr_key src view uchar * tape view uint * k ulong to ulong
596if keyis src js_off tape k kschema
597ret 0
598end
599if keyis src js_off tape k kthink
600ret 1
601end
602if keyis src js_off tape k kprom
603ret 2
604end
605die 4 tpsch
606ret 9
607end
608
609fn pr_think tape view uint * v ulong to void
610ty = js_type tape v
611if eq ty jstrue
612put gst 1 1
613ret
614end
615if eq ty jsfalse
616put gst 1 0
617ret
618end
619die 4 tpsch
620end
621
622fn pr_mem src view uchar * tape view uint * k ulong i ulong to ulong
623v = add k 1
624if keyis src js_off tape k kid
625if not vname src tape v 0
626die 4 tpsch
627end
628cpid src tape v i
629if dupid 0 i
630die 4 tpsch
631end
632ret 0
633end
634if keyis src js_off tape k kcont
635if ne js_type tape v jsstr
636die 4 tpsch
637end
638if eq js_slen tape v 0
639die 4 tpsch
640end
641put gpco i js_off tape v
642put gpcs i js_span tape v
643ret 1
644end
645die 4 tpsch
646ret 9
647end
648
649fn pr_one src view uchar * tape view uint * o ulong i ulong to void
650if ne js_type tape o jsobj
651die 4 tpsch
652end
653if ne js_nkids tape o 2
654die 4 tpsch
655end
656k = js_first tape o
657a = pr_mem src tape k i
658b = pr_mem src tape js_mnext tape k i
659if eq a b
660die 4 tpsch
661end
662end
663
664fn pr_list src view uchar * tape view uint * a ulong to void
665if ne js_type tape a jsarr
666die 4 tpsch
667end
668np = js_nkids tape a
669if or eq np 0 gt np PMAX
670die 4 tpsch
671end
672put gnp 0 np
673e = js_first tape a
674for i ulong np
675pr_one src tape e i
676e = js_enext tape e
677end
678end
679
680note pr_load: read argument 2, hash its exact bytes into hd, validate it, keep ids and contents
681fn pr_load src edit uchar * tape edit uint * to void
682put gsd 0 0
683n = rdfile argp 2 src
684hreset
685for i ulong n
686b = hpush get src i
687end
688ok = hfinal
689if not ok
690die 4 tio
691end
692jparse src tape n
693seenclr
694k = js_first tape 0
695while ne k jsnone bound 999999
696id = pr_key src tape k
697seen id tpsch
698v = add k 1
699if eq id 0
700if not js_streq src js_off tape v vpsch
701die 4 tpsch
702end
703end
704if eq id 1
705pr_think tape v
706end
707if eq id 2
708pr_list src tape v
709end
710k = js_mnext tape k
711end
712seenall 0 3 tpsch
713end
714
715note ======== req ========
716
717fn othink to void
718if eq get gst 1 1
719ot ttrue
720else
721ot tfalse
722end
723end
724
725fn req_one src view uchar * i ulong mt ulong to void
726ot tq1
727oarg 3
728ot tq2
729osp src get gpco i get gpcs i
730ot tq3
731ou mt
732ot tq4
733othink
734ot tq5
735end
736
737fn m_req src edit uchar * tape edit uint * to int
738if ne get gna 0 6
739die 2 tuse
740end
741if not aname 3 96 true
742die 2 tbrepo
743end
744mt = argu 4
745if eq mt 0
746die 2 tbmax
747end
748pr_load src tape
749np = get gnp 0
750for i ulong np
751req_one src i mt
752wrfile pthidx 5 i
753end
754ou np
755obyte 10
756oflush 1
757ret 0
758end
759
760note ======== rec ========
761
762fn put2 p ulong v ulong to void
763put gutc p uchar add 48 div v 10
764put gutc add p 1 uchar add 48 rem v 10
765end
766
767fn civyoe doe ulong to ulong
768a = sub doe div doe 1460
769a = add a div doe 36524
770a = sub a div doe 146096
771ret div a 365
772end
773
774fn civput y ulong m ulong d ulong to void
775put gutc 0 uchar add 48 div y 1000
776put gutc 1 uchar add 48 rem div y 100 10
777put gutc 2 uchar add 48 rem div y 10 10
778put gutc 3 uchar add 48 rem y 10
779put gutc 4 45
780put2 5 m
781put gutc 7 45
782put2 8 d
783end
784
785note civil: proleptic Gregorian date of a day count since 1970-01-01 (days-to-civil, era of 146097 days)
786fn civil days ulong to void
787z = add days 719468
788era = div z 146097
789doe = sub z mul era 146097
790yoe = civyoe doe
791doy = sub doe sub add mul 365 yoe div yoe 4 div yoe 100
792mp = div add mul 5 doy 2 153
793m = add mp 3
794if ge mp 10
795m = sub mp 9
796end
797y = add yoe mul era 400
798if le m 2
799y = add y 1
800end
801civput y m add sub doy div add mul 153 mp 2 5 1
802end
803
804fn utcfmt to void
805t = realns
806if lt t 0
807die 4 tclock
808end
809s = div ulong t 1000000000
810civil div s 86400
811sod = rem s 86400
812put gutc 10 84
813put2 11 div sod 3600
814put gutc 13 58
815put2 14 div rem sod 3600 60
816put gutc 16 58
817put2 17 rem sod 60
818put gutc 19 90
819end
820
821fn rs_greedy src view uchar * tape view uint * s ulong to void
822if ne js_type tape s jsobj
823die 5 tsamp
824end
825k = js_first tape s
826while ne k jsnone bound 999999
827if keyis src js_off tape k kmode
828if js_streq src js_off tape add k 1 vgreedy
829ret
830end
831end
832k = js_mnext tape k
833end
834die 5 tsampled
835end
836
837fn rs_usage src view uchar * tape view uint * u ulong to ulong
838a = jsnone
839k = js_first tape u
840while ne k jsnone bound 999999
841v = add k 1
842if keyis src js_off tape k ktids
843if or ne a jsnone ne js_type tape v jsarr
844die 5 tnoids
845end
846a = v
847end
848if keyis src js_off tape k kasamp
849rs_greedy src tape v
850end
851k = js_mnext tape k
852end
853if eq a jsnone
854die 5 tnoids
855end
856ret a
857end
858
859fn rs_one src view uchar * tape view uint * i ulong to void
860u = jsnone
861k = js_first tape 0
862while ne k jsnone bound 999999
863if keyis src js_off tape k kerror
864die 5 tengerr
865end
866if keyis src js_off tape k kusage
867if ne u jsnone
868die 5 tnoids
869end
870u = add k 1
871end
872k = js_mnext tape k
873end
874if eq u jsnone
875die 5 tnoids
876end
877if ne js_type tape u jsobj
878die 5 tnoids
879end
880a = rs_usage src tape u
881ot tp1
882oid 0 i
883ot tp2
884e = js_first tape a
885c = ulong 0
886while ne e jsnone bound 999999
887if gt c 0
888obyte 44
889end
890ou tokv src tape e 5
891c = add c 1
892e = js_enext tape e
893end
894ot tp3
895end
896
897fn rec_head mt ulong to void
898ot tr1
899oarg 8
900ot tr2
901oarg 6
902ot tr3
903oarg 3
904ot tr4
905oarg 4
906ot tr5
907othink
908ot tr6
909ou mt
910ot tr7
911for i ulong 64
912obyte get hd i
913end
914ot tr8
915oarg 7
916ot tr9
917for j ulong 20
918obyte get gutc j
919end
920ot tr10
921end
922
923fn m_rec src edit uchar * tape edit uint * to int
924if ne get gna 0 10
925die 2 tuse
926end
927if not aname 3 96 true
928die 2 tbrepo
929end
930if not ahex 4 40
931die 2 tbrev
932end
933mt = argu 5
934if eq mt 0
935die 2 tbmax
936end
937if not ahex 6 64
938die 2 tbeng
939end
940if not aname 7 64 false
941die 2 tbhost
942end
943if not or argeq 8 vreal argeq 8 vfake
944die 2 tblabel
945end
946pr_load src tape
947utcfmt
948rec_head mt
949np = get gnp 0
950for i ulong np
951if gt i 0
952obyte 44
953end
954obyte 10
955n = rdfile pthidx 9 i src
956jparse src tape n
957rs_one src tape i
958end
959ot tend
960oflush 1
961ret 0
962end
963
964note ======== cmp: record loading (aien-golden-record/1) ========
965note header fields kept per side: 0 label, 1 engine_sha256, 2 model_repo, 3 model_rev, 4 temperature,
966note 5 top_p, 6 seed, 7 enable_thinking, 8 max_tokens, 9 prompts_sha256 (all compared), 10 host_id, 11 utc
967note (printed, never compared). Values are kept as their exact JSON lexeme.
968
969fn rc_need ok bool to void
970if not ok
971die 4 trsch
972end
973end
974
975fn fcopy src view uchar * off ulong n ulong s ulong to void
976b = mul s FCAP
977for j ulong n
978put ghv add b j get src add off j
979end
980put ghl s n
981end
982
983fn fstore src view uchar * tape view uint * v ulong f ulong to void
984if gt js_span tape v FCAP
985die 4 trsch
986end
987fcopy src js_off tape v js_span tape v add mul get gsd 0 NFLD f
988end
989
990fn rc_key src view uchar * tape view uint * k ulong to ulong
991if keyis src js_off tape k kschema
992ret 0
993end
994if keyis src js_off tape k klabel
995ret 1
996end
997if keyis src js_off tape k keng
998ret 2
999end
1000if keyis src js_off tape k krepo
1001ret 3
1002end
1003if keyis src js_off tape k krev
1004ret 4
1005end
1006if keyis src js_off tape k ksamp
1007ret 5
1008end
1009if keyis src js_off tape k kmaxt
1010ret 6
1011end
1012if keyis src js_off tape k kpsha
1013ret 7
1014end
1015if keyis src js_off tape k khost
1016ret 8
1017end
1018if keyis src js_off tape k kutc
1019ret 9
1020end
1021if keyis src js_off tape k kprom
1022ret 10
1023end
1024die 4 trsch
1025ret 99
1026end
1027
1028fn rc_skey src view uchar * tape view uint * k ulong to ulong
1029if keyis src js_off tape k ktemp
1030ret 4
1031end
1032if keyis src js_off tape k ktopp
1033ret 5
1034end
1035if keyis src js_off tape k kseed
1036ret 6
1037end
1038if keyis src js_off tape k kthink
1039ret 7
1040end
1041die 4 trsch
1042ret 99
1043end
1044
1045note rc_sval: greedy records only: temperature is the integer 0
1046fn rc_sval src view uchar * tape view uint * v ulong f ulong to void
1047ty = js_type tape v
1048if eq f 4
1049rc_need vuint src tape v
1050rc_need eq js_tou src js_off tape v 0
1051ret
1052end
1053if eq f 5
1054rc_need eq ty jsnum
1055ret
1056end
1057if eq f 6
1058rc_need vuint src tape v
1059ret
1060end
1061rc_need or eq ty jstrue eq ty jsfalse
1062end
1063
1064fn rc_samp src view uchar * tape view uint * o ulong to void
1065rc_need eq js_type tape o jsobj
1066rc_need eq js_nkids tape o 4
1067k = js_first tape o
1068while ne k jsnone bound 8
1069f = rc_skey src tape k
1070seen add 16 f trsch
1071v = add k 1
1072rc_sval src tape v f
1073fstore src tape v f
1074k = js_mnext tape k
1075end
1076seenall 20 4 trsch
1077end
1078
1079fn rc_label src view uchar * tape view uint * v ulong to void
1080off = js_off tape v
1081if js_streq src off vreal
1082ret
1083end
1084if js_streq src off vfake
1085ret
1086end
1087if js_streq src off vsynth
1088ret
1089end
1090die 4 trsch
1091end
1092
1093fn rc_fill src view uchar * tape view uint * a ulong d ulong to void
1094e = js_first tape a
1095j = ulong 0
1096while ne e jsnone bound 999999
1097put gtk add d j uint tokv src tape e 4
1098j = add j 1
1099e = js_enext tape e
1100end
1101end
1102
1103fn rc_ids src view uchar * tape view uint * a ulong i ulong to void
1104rc_need eq js_type tape a jsarr
1105sd = get gsd 0
1106s = add mul sd PMAX i
1107t = get gtkt sd
1108n = js_nkids tape a
1109if gt add t n TKMAX
1110die 4 tbig
1111end
1112put gtko s t
1113put gtkn s n
1114put gtkt sd add t n
1115rc_fill src tape a add mul sd TKMAX t
1116end
1117
1118fn rc_pmem src view uchar * tape view uint * k ulong i ulong to ulong
1119v = add k 1
1120if keyis src js_off tape k kid
1121rc_need vname src tape v 0
1122cpid src tape v i
1123if dupid get gsd 0 i
1124die 4 trsch
1125end
1126ret 0
1127end
1128if keyis src js_off tape k kids
1129rc_ids src tape v i
1130ret 1
1131end
1132die 4 trsch
1133ret 9
1134end
1135
1136fn rc_prompt src view uchar * tape view uint * o ulong i ulong to void
1137rc_need eq js_type tape o jsobj
1138rc_need eq js_nkids tape o 2
1139k = js_first tape o
1140a = rc_pmem src tape k i
1141b = rc_pmem src tape js_mnext tape k i
1142if eq a b
1143die 4 trsch
1144end
1145end
1146
1147fn rc_prompts src view uchar * tape view uint * a ulong to void
1148rc_need eq js_type tape a jsarr
1149np = js_nkids tape a
1150if or eq np 0 gt np PMAX
1151die 4 trsch
1152end
1153put gnp get gsd 0 np
1154e = js_first tape a
1155for i ulong np
1156rc_prompt src tape e i
1157e = js_enext tape e
1158end
1159end
1160
1161fn rc_val src view uchar * tape view uint * v ulong id ulong to void
1162if eq id 0
1163rc_need js_streq src js_off tape v vrsch
1164ret
1165end
1166if eq id 1
1167rc_label src tape v
1168fstore src tape v 0
1169ret
1170end
1171if eq id 2
1172rc_need vhex src tape v 64
1173fstore src tape v 1
1174ret
1175end
1176if eq id 3
1177rc_need vname src tape v 1
1178fstore src tape v 2
1179ret
1180end
1181if eq id 4
1182rc_need vhex src tape v 40
1183fstore src tape v 3
1184ret
1185end
1186if eq id 5
1187rc_samp src tape v
1188ret
1189end
1190rc_val2 src tape v id
1191end
1192
1193fn rc_val2 src view uchar * tape view uint * v ulong id ulong to void
1194if eq id 6
1195rc_need vuint src tape v
1196mt = js_tou src js_off tape v
1197rc_need and ge mt 1 le mt MAXTOK
1198fstore src tape v 8
1199ret
1200end
1201if eq id 7
1202rc_need vhex src tape v 64
1203fstore src tape v 9
1204ret
1205end
1206if eq id 8
1207rc_need vname src tape v 0
1208fstore src tape v 10
1209ret
1210end
1211if eq id 9
1212rc_need vutc src tape v
1213fstore src tape v 11
1214ret
1215end
1216rc_prompts src tape v
1217end
1218
1219fn rc_load src edit uchar * tape edit uint * sd ulong to void
1220put gsd 0 sd
1221n = rdfile argp add 2 sd src
1222jparse src tape n
1223seenclr
1224put gtkt sd 0
1225k = js_first tape 0
1226while ne k jsnone bound 999999
1227id = rc_key src tape k
1228seen id trsch
1229rc_val src tape add k 1 id
1230k = js_mnext tape k
1231end
1232seenall 0 11 trsch
1233end
1234
1235note ======== cmp: comparison ========
1236
1237fn feq f ulong to bool
1238a = f
1239b = add NFLD f
1240n = get ghl a
1241if ne n get ghl b
1242ret false
1243end
1244for j ulong n
1245if ne get ghv add mul a FCAP j get ghv add mul b FCAP j
1246ret false
1247end
1248end
1249ret true
1250end
1251
1252fn ofld sd ulong f ulong to void
1253s = add mul sd NFLD f
1254for j ulong get ghl s
1255obyte get ghv add mul s FCAP j
1256end
1257end
1258
1259fn ofname f ulong to void
1260if eq f 0
1261ot klabel
1262end
1263if eq f 1
1264ot keng
1265end
1266if eq f 2
1267ot krepo
1268end
1269if eq f 3
1270ot krev
1271end
1272if eq f 4
1273ot ktemp
1274end
1275if eq f 5
1276ot ktopp
1277end
1278if eq f 6
1279ot kseed
1280end
1281if eq f 7
1282ot kthink
1283end
1284if eq f 8
1285ot kmaxt
1286end
1287if eq f 9
1288ot kpsha
1289end
1290end
1291
1292fn c_info to void
1293ot cinfo1
1294ofld 0 10
1295ot cinfo2
1296ofld 0 11
1297ot cinfo3
1298ofld 1 10
1299ot cinfo4
1300ofld 1 11
1301obyte 10
1302end
1303
1304fn c_ids to ulong
1305bad = ulong 0
1306for i ulong get gnp 0
1307if not ideq 0 i 1 i
1308ot cnot
1309ot cpidx
1310ou i
1311ot cref
1312oid 0 i
1313ot ccand
1314oid 1 i
1315obyte 10
1316bad = add bad 1
1317end
1318end
1319ret bad
1320end
1321
1322fn c_head to ulong
1323bad = ulong 0
1324for f ulong 10
1325if not feq f
1326ot cnot
1327ofname f
1328ot cref
1329ofld 0 f
1330ot ccand
1331ofld 1 f
1332obyte 10
1333bad = add bad 1
1334end
1335end
1336if ne get gnp 0 get gnp 1
1337ot cnot
1338ot cpcnt
1339ot cref
1340ou get gnp 0
1341ot ccand
1342ou get gnp 1
1343obyte 10
1344ret add bad 1
1345end
1346ret add bad c_ids
1347end
1348
1349fn cdiv ao ulong an ulong bo ulong bn ulong to ulong
1350m = an
1351if lt bn m
1352m = bn
1353end
1354k = ulong 0
1355while lt k m bound 999999
1356if ne get gtk add ao k get gtk add bo k
1357ret k
1358end
1359k = add k 1
1360end
1361ret m
1362end
1363
1364fn otokat o ulong n ulong k ulong to void
1365if lt k n
1366ou ulong get gtk add o k
1367else
1368ot cend
1369end
1370end
1371
1372fn c_one i ulong to bool
1373ao = get gtko i
1374an = get gtkn i
1375bo = add TKMAX get gtko add PMAX i
1376bn = get gtkn add PMAX i
1377k = cdiv ao an bo bn
1378if and eq k an eq k bn
1379ot cmatch
1380oid 0 i
1381ot cn
1382ou an
1383obyte 10
1384ret true
1385end
1386ot cdivg
1387oid 0 i
1388ot cidx
1389ou k
1390ot cref
1391otokat ao an k
1392ot ccand
1393otokat bo bn k
1394obyte 10
1395ret false
1396end
1397
1398fn m_cmp src edit uchar * tape edit uint * to int
1399if ne get gna 0 4
1400die 2 tuse
1401end
1402rc_load src tape 0
1403rc_load src tape 1
1404c_info
1405bad = c_head
1406if gt bad 0
1407oflush 1
1408ret 3
1409end
1410mm = ulong 0
1411np = get gnp 0
1412for i ulong np
1413if not c_one i
1414mm = add mm 1
1415end
1416end
1417ot csum
1418ou np
1419ot csm
1420ou sub np mm
1421ot csd
1422ou mm
1423obyte 10
1424oflush 1
1425if gt mm 0
1426ret 1
1427end
1428ret 0
1429end
1430
1431fn main to int
1432c = argcnt
1433if or lt c 2 gt c 10
1434die 2 tuse
1435end
1436o = ulong 0
1437for i ulong ulong c
1438r = argread i ga o sub len ga o
1439if lt r 0
1440die 2 tuse
1441end
1442put gas i o
1443put gal i ulong r
1444o = add o ulong r
1445end
1446put gna 0 ulong c
1447src = new uchar SRCCAP 0
1448tape = new uint TAPECAP 0
1449if argeq 1 tmreq
1450ret m_req src tape
1451end
1452if argeq 1 tmrec
1453ret m_rec src tape
1454end
1455if argeq 1 tmcmp
1456ret m_cmp src tape
1457end
1458die 2 tuse
1459ret 2
1460end