interplane.Get Omega
Read in your language

aien-train

AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.

Brought by drakestapleton

branch main Branches Tags History

aien-train / tools / omegallm / wscore / wscore.om

1834 lines · 25150 bytes Raw Blame History
1om2
2note wscore: score `wprobe gen` output against a split (pure Omega port of DRA-187 EV/score.py).
3note usage: wscore SPLIT.rec SPLIT.meta GEN.out
4note SPLIT.rec: records PROMPT 0x1f ANSWER 0x00, or a G prompt alone (answer derived, see prec).
5note SPLIT.meta: lines `idx role length kind`, role
6note target|control, kind path|word, idx equal to the line number from 0. GEN.out: `G idx<TAB>text`
7note lines, text escaped as \t \n \r \" \\ \xHH. A path row passes when its text, cut at the first
8note `\nDone.` and stripped of Python whitespace, equals the answer as JSON (both parse) or as a
9note string. A word row passes when the stripped first line equals the answer.
10note Prints `target P/T L<n> p/t ...`, `control P/T ...`, then `P idx 0|1` per row.
11note Exit: 0 ok, 2 usage, 3 open/read, 4 capacity, 5 malformed rec/meta/G key, 6 G rows do not
12note cover the split exactly, 7 bad escape in GEN text, 8 JSON nesting over 9990 (inside the scorer's
13note check_answer, CPython 3.12.3 raises RecursionError on some inputs from 9991 levels), 9 write failure.
14
15global ab uchar 4096
16global rb uchar 16777216
17global mb uchar 4194304
18global gb uchar 16777216
19global fz ulong 3
20global rn ulong 1
21global ra ulong 262144
22global rl ulong 262144
23global rr ulong 262144
24global rw ulong 262144
25global rg ulong 262144
26global rk ulong 262144
27global gs ulong 262144
28global gl ulong 262144
29global gq ulong 262144
30global pv ulong 262144
31global mp ulong 1
32global ca ulong 1048576
33global cb ulong 1048576
34global sr ulong 2
35global pw ulong 1
36global pp ulong 1
37global px ulong 1
38global pd ulong 1
39global pf ulong 1
40global nt ulong 2097152
41global n1 ulong 2097152
42global n2 ulong 2097152
43global nx ulong 2097152
44global nq ulong 2097152
45global nv long 2097152
46global nf ulong 2097152
47global bg ulong 1536
48global bgl ulong 4
49global fr ulong 4
50global nn ulong 1
51global pl ulong 2097152
52global pn ulong 1
53global js long 4
54global tp ulong 8192
55global tt ulong 8192
56global ob uchar 65536
57global obn ulong 1
58text ttarget "target"
59text tcontr "control"
60text tpath "path"
61text tword "word"
62text tnull "null"
63text ttrue "true"
64text tfalse "false"
65text tnan "NaN"
66text tinf "Infinity"
67text tdone "\nDone."
68text tgpre "Task: Write Omega that prints "
69text tgpost " followed by a newline.\nAnswer:\n"
70text tmsg "text msg \""
71
72fn main to int
73if ne argcnt 4
74exit 2
75end
76ldfile 1 0
77ldfile 2 1
78ldfile 3 2
79prec
80pmeta
81pgen
82tally
83pline 0
84pline 1
85prows
86oflush
87ret 0
88end
89
90note ---- output ----
91fn oflush to void
92n = get obn 0
93o = ulong 0
94while lt o n bound 65537
95k = write 1 ob o sub n o
96if le k 0
97exit 9
98end
99o = add o ulong k
100end
101put obn 0 0
102end
103
104fn oput c uchar to void
105n = get obn 0
106if ge n 65536
107oflush
108n = ulong 0
109end
110put ob n c
111put obn 0 add n 1
112end
113
114fn pdec v ulong to void
115d = new uchar 24 0
116k = ulong 0
117x = v
118c = true
119while c bound 25
120put d k uchar add 48 rem x 10
121k = add k 1
122x = div x 10
123if eq x 0
124c = false
125end
126end
127while gt k 0 bound 25
128k = sub k 1
129oput get d k
130end
131end
132
133fn pstr t view uchar * to void
134for i ulong len t
135oput get t i
136end
137end
138
139fn pline r ulong to void
140if eq r 0
141pstr ttarget
142else
143pstr tcontr
144end
145b = mul r 4096
146p = ulong 0
147t = ulong 0
148for l ulong 4096
149p = add p get tp add b l
150t = add t get tt add b l
151end
152oput 32
153pdec p
154oput 47
155pdec t
156oput 32
157oput 32
158pbylen b
159oput 10
160end
161
162fn pbylen b ulong to void
163c = true
164for l ulong 4096
165x = add b l
166if gt get tt x 0
167if not c
168oput 32
169end
170c = false
171oput 76
172pdec l
173oput 32
174pdec get tp x
175oput 47
176pdec get tt x
177end
178end
179end
180
181fn prows to void
182nr = get rn 0
183i = ulong 0
184while lt i nr bound 999999
185oput 80
186oput 32
187pdec i
188oput 32
189pdec get pv i
190oput 10
191i = add i 1
192end
193end
194
195note ---- input files ----
196fn capof w ulong to ulong
197if eq w 0
198ret ulong 16777216
199end
200if eq w 1
201ret ulong 4194304
202end
203ret ulong 16777216
204end
205
206fn rdbuf w ulong fd int off ulong to long
207if eq w 0
208ret read fd rb off sub 16777216 off
209end
210if eq w 1
211ret read fd mb off sub 4194304 off
212end
213ret read fd gb off sub 16777216 off
214end
215
216fn ldfile ai ulong w ulong to void
217k = argread ai ab 0 4096
218if lt k 0
219exit 3
220end
221if ge k 4096
222exit 3
223end
224fd = int fopen ab 0 ulong k 0 0
225if lt fd 0
226exit 3
227end
228cap = capof w
229off = ulong 0
230go = true
231while go bound 999999
232if ge off cap
233exit 4
234end
235r = rdbuf w fd off
236if lt r 0
237exit 3
238end
239if eq r 0
240go = false
241else
242off = add off ulong r
243end
244end
245x = fclose fd
246put fz w off
247end
248
249note SPLIT.rec: record i ends at its 0x00; its answer is the bytes after the first 0x1f in it.
250note A record without 0x1f is a G prompt (make_splits.py gsplit, wsplit): the answer is
251note `text msg "WORD\n"` with WORD cut from `Task: Write Omega that prints WORD followed by a
252note newline.\nAnswer:\n`; ra/rl then hold WORD and rw is 1. Any other record is malformed.
253fn prec to void
254n = get fz 0
255p = ulong 0
256i = ulong 0
257while lt p n bound 999999
258e = recsep p n 0
259if ge e n
260exit 5
261end
262if ge i 262144
263exit 4
264end
265q = recsep p e 31
266if lt q e
267a = add q 1
268put ra i a
269put rl i sub e a
270put rw i 0
271else
272gword i p e
273end
274i = add i 1
275p = add e 1
276end
277put rn 0 i
278end
279
280fn rbeq p ulong t view uchar * to bool
281for k ulong len t
282if ne get rb add p k get t k
283ret false
284end
285end
286ret true
287end
288
289fn gword i ulong p ulong e ulong to void
290a = add p len tgpre
291b = sub e len tgpost
292if or lt e add a len tgpost ge a b
293exit 5
294end
295if or not rbeq p tgpre not rbeq b tgpost
296exit 5
297end
298put ra i a
299put rl i sub b a
300put rw i 1
301end
302
303fn recsep p ulong n ulong b uchar to ulong
304q = p
305while lt q n bound 999999
306if ge sub q p 999990
307exit 4
308end
309if eq get rb q b
310ret q
311end
312q = add q 1
313end
314ret n
315end
316
317note SPLIT.meta
318fn pmeta to void
319n = get fz 1
320nr = get rn 0
321put mp 0 0
322i = ulong 0
323while lt get mp 0 n bound 999999
324if ge i nr
325exit 5
326end
327if ne mnum i
328exit 5
329end
330msp
331put rr i mrole
332msp
333x = mnum
334if ge x 4096
335exit 5
336end
337put rg i x
338msp
339put rk i mkind
340meol
341i = add i 1
342end
343if ne i nr
344exit 5
345end
346end
347
348fn mnum to ulong
349p = get mp 0
350n = get fz 1
351v = ulong 0
352s = p
353while lt p n bound 999999
354if ge sub p s 999990
355exit 5
356end
357c = ulong get mb p
358if or lt c 48 gt c 57
359break
360end
361if lt v 100000000000
362v = add mul v 10 sub c 48
363end
364p = add p 1
365end
366if eq p s
367exit 5
368end
369put mp 0 p
370ret v
371end
372
373fn msp to void
374p = get mp 0
375if ge p get fz 1
376exit 5
377end
378if ne get mb p 32
379exit 5
380end
381put mp 0 add p 1
382end
383
384fn meol to void
385p = get mp 0
386if lt p get fz 1
387if ne get mb p 10
388exit 5
389end
390put mp 0 add p 1
391end
392end
393
394fn mtok to ulong
395p = get mp 0
396n = get fz 1
397while lt p n bound 999999
398if ge sub p get mp 0 999990
399exit 5
400end
401c = get mb p
402if or eq c 32 eq c 10
403break
404end
405p = add p 1
406end
407ret p
408end
409
410fn mteq s ulong e ulong t view uchar * to bool
411if ne sub e s len t
412ret false
413end
414for i ulong len t
415if ne get mb add s i get t i
416ret false
417end
418end
419ret true
420end
421
422fn mrole to ulong
423s = get mp 0
424e = mtok
425put mp 0 e
426if mteq s e ttarget
427ret ulong 0
428end
429if mteq s e tcontr
430ret ulong 1
431end
432exit 5
433end
434
435fn mkind to ulong
436s = get mp 0
437e = mtok
438put mp 0 e
439if mteq s e tpath
440ret ulong 0
441end
442if mteq s e tword
443ret ulong 1
444end
445exit 5
446end
447
448note GEN.out: lines end at \n, \r\n or \r (Python universal newlines); `G ` lines carry rows
449fn pgen to void
450n = get fz 2
451p = ulong 0
452c = ulong 0
453ln = ulong 0
454while lt p n bound 999999
455ln = add ln 1
456if ge ln 999990
457exit 4
458end
459e = lnend p n
460if isg p e
461c = add c gline p e
462end
463p = lnnext e n
464end
465if ne c get rn 0
466exit 6
467end
468end
469
470fn lnend p ulong n ulong to ulong
471q = p
472while lt q n bound 999999
473if ge sub q p 999990
474exit 4
475end
476c = get gb q
477if or eq c 10 eq c 13
478ret q
479end
480q = add q 1
481end
482ret n
483end
484
485fn lnnext e ulong n ulong to ulong
486if ge e n
487ret n
488end
489if eq get gb e 13
490q = add e 1
491if lt q n
492if eq get gb q 10
493ret add e 2
494end
495end
496end
497ret add e 1
498end
499
500fn isg p ulong e ulong to bool
501if ge add p 1 e
502ret false
503end
504ret and eq get gb p 71 eq get gb add p 1 32
505end
506
507note a G line: decimal key, TAB, text; returns 1 when the key is new (later lines replace earlier)
508fn gline p ulong e ulong to ulong
509q = add p 2
510v = ulong 0
511s = q
512while lt q e bound 999999
513c = ulong get gb q
514if eq c 9
515break
516end
517if or lt c 48 gt c 57
518exit 5
519end
520if lt v 100000000000
521v = add mul v 10 sub c 48
522end
523q = add q 1
524end
525if eq q s
526exit 5
527end
528if ge v get rn 0
529exit 6
530end
531t = q
532if lt q e
533t = add q 1
534end
535gchk t e
536put gs v t
537put gl v sub e t
538if eq get gq v 1
539ret ulong 0
540end
541put gq v 1
542ret ulong 1
543end
544
545note ---- decoding to code points: w 0 is GEN text into ca, w 1 is the answer into cb ----
546fn bat w ulong p ulong to ulong
547if eq w 0
548ret ulong get gb p
549end
550ret ulong get rb p
551end
552
553fn cput w ulong i ulong v ulong to void
554if ge i 999990
555exit 4
556end
557if eq w 0
558put ca i v
559else
560put cb i v
561end
562end
563
564fn u8n b ulong to ulong
565if and ge b 194 le b 223
566ret ulong 1
567end
568if and ge b 224 le b 239
569ret ulong 2
570end
571if and ge b 240 le b 244
572ret ulong 3
573end
574ret ulong 0
575end
576
577fn u8ok b ulong i ulong c ulong to bool
578lo = ulong 128
579hi = ulong 191
580if eq i 0
581if eq b 224
582lo = ulong 160
583end
584if eq b 240
585lo = ulong 144
586end
587if eq b 237
588hi = ulong 159
589end
590if eq b 244
591hi = ulong 143
592end
593end
594ret and ge c lo le c hi
595end
596
597note one UTF-8 character at p, maximal ill-formed subparts become U+FFFD (Python errors=replace)
598fn dutf w ulong p ulong e ulong o ulong to ulong
599b = bat w p
600if lt b 128
601cput w o b
602ret add p 1
603end
604k = u8n b
605if eq k 0
606cput w o 65533
607ret add p 1
608end
609v = and b shr 127 add k 1
610q = add p 1
611for i ulong k
612if ge q e
613cput w o 65533
614ret q
615end
616c = bat w q
617if not u8ok b i c
618cput w o 65533
619ret q
620end
621v = or shl v 6 and c 63
622q = add q 1
623end
624cput w o v
625ret q
626end
627
628fn hexv c ulong to ulong
629if and ge c 48 le c 57
630ret sub c 48
631end
632if and ge c 97 le c 102
633ret sub c 87
634end
635if and ge c 65 le c 70
636ret sub c 55
637end
638ret ulong 16
639end
640
641fn gsimp c ulong to ulong
642if eq c 116
643ret ulong 9
644end
645if eq c 110
646ret ulong 10
647end
648if eq c 114
649ret ulong 13
650end
651if or eq c 34 eq c 92
652ret c
653end
654exit 7
655end
656
657note value of the escape at p (a backslash); exits 7 when it is not one score.py's unescape takes
658fn escv p ulong e ulong to ulong
659if ge add p 1 e
660exit 7
661end
662c = ulong get gb add p 1
663if ne c 120
664ret gsimp c
665end
666if ge add p 3 e
667exit 7
668end
669h = hexv ulong get gb add p 2
670l = hexv ulong get gb add p 3
671if or gt h 15 gt l 15
672exit 7
673end
674ret add shl h 4 l
675end
676
677fn esclen p ulong to ulong
678if eq get gb add p 1 120
679ret ulong 4
680end
681ret ulong 2
682end
683
684fn desc p ulong e ulong o ulong to ulong
685cput 0 o escv p e
686ret add p esclen p
687end
688
689note score.py unescapes every G line as it reads it, so a bad escape fails even on a replaced line
690fn gchk p ulong e ulong to void
691q = p
692while lt q e bound 999999
693if eq get gb q 92
694v = escv q e
695q = add q esclen q
696else
697q = add q 1
698end
699end
700end
701
702fn dgen i ulong to ulong
703p = get gs i
704e = add p get gl i
705o = ulong 0
706while lt p e bound 999999
707if eq get gb p 92
708p = desc p e o
709else
710p = dutf 0 p e o
711end
712o = add o 1
713end
714ret o
715end
716
717fn dans i ulong to ulong
718p = get ra i
719e = add p get rl i
720o = ulong 0
721w = eq get rw i 1
722if w
723if ne get rk i 1
724exit 5
725end
726for k ulong len tmsg
727cput 1 o ulong get tmsg k
728o = add o 1
729end
730end
731while lt p e bound 999999
732p = dutf 1 p e o
733o = add o 1
734end
735if w
736cput 1 o 92
737cput 1 add o 1 110
738cput 1 add o 2 34
739o = add o 3
740end
741ret o
742end
743
744note ---- text cuts on ca ----
745note Python str.isspace
746fn isws c ulong to bool
747if and ge c 9 le c 13
748ret true
749end
750if and ge c 28 le c 32
751ret true
752end
753if or eq c 133 eq c 160
754ret true
755end
756if or eq c 5760 and ge c 8192 le c 8202
757ret true
758end
759if or or eq c 8232 eq c 8233 or eq c 8239 eq c 8287
760ret true
761end
762ret eq c 12288
763end
764
765fn strip e ulong to void
766s = ulong 0
767t = e
768while lt s t bound 999999
769if not isws get ca s
770break
771end
772s = add s 1
773end
774while gt t s bound 999999
775if not isws get ca sub t 1
776break
777end
778t = sub t 1
779end
780put sr 0 s
781put sr 1 t
782end
783
784fn fnl n ulong to ulong
785i = ulong 0
786while lt i n bound 999999
787if eq get ca i 10
788ret i
789end
790i = add i 1
791end
792ret n
793end
794
795fn isdone i ulong to bool
796for k ulong 6
797if ne get ca add i k ulong get tdone k
798ret false
799end
800end
801ret true
802end
803
804fn fdone n ulong to ulong
805i = ulong 0
806while le add i 6 n bound 999999
807if isdone i
808ret i
809end
810i = add i 1
811end
812ret n
813end
814
815fn cmpab s ulong e ulong m ulong to bool
816if ne sub e s m
817ret false
818end
819k = ulong 0
820while lt k m bound 999999
821if ne get ca add s k get cb k
822ret false
823end
824k = add k 1
825end
826ret true
827end
828
829note ---- JSON (Python json.loads) ----
830note node types: 0 null, 1 false, 2 true, 3 number, 4 string, 5 array, 6 object, 7 NaN,
831note 8 Infinity, 9 -Infinity. Strings: n1 pool start, n2 count of code points. Numbers and bools:
832note value = (pool digits n1, count n2, no leading or trailing zero) * 10^nv, nq 1 when negative,
833note n2 0 for zero. Arrays and objects: n1 first child, n2 child count, nx next sibling; an object's
834note children alternate key, value.
835fn pc i ulong to ulong
836if eq get pw 0 0
837ret get ca i
838end
839ret get cb i
840end
841
842fn pat k ulong to ulong
843p = add get pp 0 k
844if lt p get px 0
845ret pc p
846end
847ret ulong 4294967295
848end
849
850fn pcur to ulong
851ret pat 0
852end
853
854fn padv k ulong to void
855put pp 0 add get pp 0 k
856end
857
858fn jfail to ulong
859put pf 0 1
860ret ulong 0
861end
862
863fn bad to bool
864ret ne get pf 0 0
865end
866
867fn isdig c ulong to bool
868ret and ge c 48 le c 57
869end
870
871fn nnew t ulong to ulong
872i = get nn 0
873if ge i 2097152
874exit 4
875end
876put nt i t
877put n1 i 0
878put n2 i 0
879put nx i 0
880put nq i 0
881put nv i 0
882put nf i 0
883put nn 0 add i 1
884ret i
885end
886
887fn ppush v ulong to void
888i = get pn 0
889if ge i 2097152
890exit 4
891end
892put pl i v
893put pn 0 add i 1
894end
895
896fn jlink nd ulong last ulong v ulong to void
897if eq get n2 nd 0
898put n1 nd v
899else
900put nx last v
901end
902put n2 nd add get n2 nd 1
903end
904
905fn jdeep to void
906d = add get pd 0 1
907if gt d 9990
908exit 8
909end
910put pd 0 d
911end
912
913fn jup to void
914put pd 0 sub get pd 0 1
915end
916
917fn jws to void
918c = true
919while c bound 999999
920x = pcur
921if or or eq x 32 eq x 9 or eq x 10 eq x 13
922padv 1
923else
924c = false
925end
926end
927end
928
929fn jlit t view uchar * to bool
930for i ulong len t
931if ne pat i ulong get t i
932ret false
933end
934end
935padv len t
936ret true
937end
938
939fn jkw t view uchar * ty ulong to ulong
940if not jlit t
941ret jfail
942end
943nd = nnew ty
944if eq ty 2
945put n1 nd get pn 0
946put n2 nd 1
947ppush 1
948end
949ret nd
950end
951
952fn jtop w ulong s ulong e ulong to ulong
953put pw 0 w
954put pp 0 s
955put px 0 e
956put pf 0 0
957put pd 0 0
958jws
959v = jval
960if bad
961ret ulong 0
962end
963jws
964if ne get pp 0 e
965ret jfail
966end
967ret v
968end
969
970fn jval to ulong
971c = pcur
972if eq c 34
973ret jstr
974end
975if eq c 123
976ret jobj
977end
978if eq c 91
979ret jarr
980end
981if eq c 110
982ret jkw tnull 0
983end
984if eq c 116
985ret jkw ttrue 2
986end
987if eq c 102
988ret jkw tfalse 1
989end
990if eq c 78
991ret jkw tnan 7
992end
993if eq c 73
994ret jkw tinf 8
995end
996if eq c 45
997if eq pat 1 73
998padv 1
999ret jkw tinf 9
1000end
1001end
1002ret jnum
1003end
1004
1005fn jdigs to void
1006c = true
1007while c bound 999999
1008if isdig pcur
1009padv 1
1010else
1011c = false
1012end
1013end
1014end
1015
1016note exponent after the number body; js 0 is 1 when present; saturates near 1e15
1017fn jexp to long
1018put js 0 0
1019c = pcur
1020if and ne c 101 ne c 69
1021ret long 0
1022end
1023k = ulong 1
1024s = long 1
1025c2 = pat 1
1026if or eq c2 43 eq c2 45
1027if eq c2 45
1028s = long -1
1029end
1030k = ulong 2
1031end
1032if not isdig pat k
1033ret long 0
1034end
1035padv k
1036put js 0 1
1037v = long 0
1038while isdig pcur bound 999999
1039if lt v 1000000000000000
1040v = add mul v 10 long sub pcur 48
1041end
1042padv 1
1043end
1044ret mul s v
1045end
1046
1047fn jnum to ulong
1048put js 1 0
1049if eq pcur 45
1050put js 1 1
1051padv 1
1052end
1053if not isdig pcur
1054ret jfail
1055end
1056i0 = get pp 0
1057if eq pcur 48
1058padv 1
1059else
1060jdigs
1061end
1062i1 = get pp 0
1063f0 = i1
1064f1 = i1
1065if eq pcur 46
1066if isdig pat 1
1067padv 1
1068f0 = get pp 0
1069jdigs
1070f1 = get pp 0
1071end
1072end
1073put js 2 jexp
1074put js 3 get js 0
1075if ne f0 f1
1076put js 3 1
1077end
1078if eq get js 3 0
1079if gt sub i1 i0 4300
1080ret jfail
1081end
1082end
1083ret jmkn i0 i1 f0 f1
1084end
1085
1086fn jpush a ulong b ulong st ulong to void
1087k = a
1088while lt k b bound 999999
1089d = sub pc k 48
1090if or ne d 0 gt get pn 0 st
1091ppush d
1092end
1093k = add k 1
1094end
1095end
1096
1097fn jmkn i0 ulong i1 ulong f0 ulong f1 ulong to ulong
1098nd = nnew 3
1099st = get pn 0
1100put n1 nd st
1101jpush i0 i1 st
1102jpush f0 f1 st
1103t = ulong 0
1104e = get pn 0
1105while gt e st bound 999999
1106if ne get pl sub e 1 0
1107break
1108end
1109e = sub e 1
1110t = add t 1
1111end
1112put pn 0 e
1113put n2 nd sub e st
1114put nf nd ulong get js 3
1115if gt e st
1116put nq nd ulong get js 1
1117x = sub long t long sub f1 f0
1118put nv nd add get js 2 x
1119end
1120ret nd
1121end
1122
1123fn jsimp c ulong to ulong
1124if or or eq c 34 eq c 92 eq c 47
1125ret c
1126end
1127if eq c 98
1128ret ulong 8
1129end
1130if eq c 102
1131ret ulong 12
1132end
1133if eq c 110
1134ret ulong 10
1135end
1136if eq c 114
1137ret ulong 13
1138end
1139if eq c 116
1140ret ulong 9
1141end
1142ret ulong 4294967295
1143end
1144
1145fn hex4 k ulong to ulong
1146v = ulong 0
1147for i ulong 4
1148h = hexv pat add k i
1149if gt h 15
1150ret ulong 65536
1151end
1152v = add shl v 4 h
1153end
1154ret v
1155end
1156
1157note one escape at pp (a backslash); surrogate pairs combine, lone surrogates stay
1158fn jesc to bool
1159c = pat 1
1160v = jsimp c
1161if ne v 4294967295
1162ppush v
1163padv 2
1164ret true
1165end
1166if ne c 117
1167ret false
1168end
1169u = hex4 2
1170if gt u 65535
1171ret false
1172end
1173padv 6
1174if and ge u 55296 le u 56319
1175if and eq pcur 92 eq pat 1 117
1176u2 = hex4 2
1177if gt u2 65535
1178ret false
1179end
1180if and ge u2 56320 le u2 57343
1181u = add add 65536 shl sub u 55296 10 sub u2 56320
1182padv 6
1183end
1184end
1185end
1186ppush u
1187ret true
1188end
1189
1190fn jstr to ulong
1191padv 1
1192nd = nnew 4
1193st = get pn 0
1194put n1 nd st
1195go = true
1196while go bound 999999
1197c = pcur
1198if eq c 34
1199padv 1
1200go = false
1201else
1202if lt c 32
1203ret jfail
1204end
1205if eq c 4294967295
1206ret jfail
1207end
1208if eq c 92
1209if not jesc
1210ret jfail
1211end
1212else
1213ppush c
1214padv 1
1215end
1216end
1217end
1218put n2 nd sub get pn 0 st
1219ret nd
1220end
1221
1222fn jarr to ulong
1223padv 1
1224nd = nnew 5
1225jdeep
1226jws
1227if eq pcur 93
1228padv 1
1229jup
1230ret nd
1231end
1232last = nd
1233go = true
1234while go bound 999999
1235v = jval
1236if bad
1237ret ulong 0
1238end
1239jlink nd last v
1240last = v
1241jws
1242c = pcur
1243if eq c 93
1244padv 1
1245go = false
1246else
1247if ne c 44
1248ret jfail
1249end
1250padv 1
1251jws
1252end
1253end
1254jup
1255ret nd
1256end
1257
1258fn jobj to ulong
1259padv 1
1260nd = nnew 6
1261jdeep
1262jws
1263if eq pcur 125
1264padv 1
1265jup
1266ret nd
1267end
1268last = nd
1269go = true
1270while go bound 999999
1271if ne pcur 34
1272ret jfail
1273end
1274k = jstr
1275if bad
1276ret ulong 0
1277end
1278jlink nd last k
1279jws
1280if ne pcur 58
1281ret jfail
1282end
1283padv 1
1284jws
1285v = jval
1286if bad
1287ret ulong 0
1288end
1289jlink nd k v
1290last = v
1291jws
1292c = pcur
1293if eq c 125
1294padv 1
1295go = false
1296else
1297if ne c 44
1298ret jfail
1299end
1300padv 1
1301jws
1302end
1303end
1304jup
1305ret nd
1306end
1307
1308note ---- Python == on the parsed values: bool equals 0/1, ints compare exactly, a float equals
1309note what its literal rounds to in binary64 (ties to even), -0 equals 0, NaN equals nothing at the
1310note top level but equals NaN inside a list or dict (json shares one NaN object, and Python
1311note containers compare identical items as equal), dicts compare as key sets with the last
1312note duplicate key winning ----
1313fn pooleq x ulong y ulong k ulong to bool
1314i = ulong 0
1315while lt i k bound 999999
1316if ne get pl add x i get pl add y i
1317ret false
1318end
1319i = add i 1
1320end
1321ret true
1322end
1323
1324fn isnum t ulong to bool
1325if and ge t 1 le t 3
1326ret true
1327end
1328ret or eq t 8 eq t 9
1329end
1330
1331fn isint a ulong to bool
1332t = get nt a
1333if or eq t 1 eq t 2
1334ret true
1335end
1336ret and eq t 3 eq get nf a 0
1337end
1338
1339fn nsign a ulong to ulong
1340t = get nt a
1341if eq t 9
1342ret ulong 1
1343end
1344if eq t 3
1345ret get nq a
1346end
1347ret ulong 0
1348end
1349
1350note two exact decimals
1351fn deq a ulong b ulong to bool
1352k = get n2 a
1353if ne k get n2 b
1354ret false
1355end
1356if eq k 0
1357ret true
1358end
1359if ne get nq a get nq b
1360ret false
1361end
1362if ne get nv a get nv b
1363ret false
1364end
1365ret pooleq get n1 a get n1 b k
1366end
1367
1368note bignums: slot d (0..3) is limbs bg[d*384 ..], 32 bits per limb, low limb first
1369fn bclr d ulong to void
1370put bgl d 0
1371end
1372
1373fn nrm d ulong m ulong to void
1374b = mul d 384
1375n = m
1376while gt n 0 bound 999999
1377if ne get bg add b sub n 1 0
1378break
1379end
1380n = sub n 1
1381end
1382put bgl d n
1383end
1384
1385fn bmuladd d ulong m ulong a ulong to void
1386b = mul d 384
1387n = get bgl d
1388c = a
1389i = ulong 0
1390while lt i n bound 999999
1391t = add mul get bg add b i m c
1392put bg add b i and t 4294967295
1393c = shr t 32
1394i = add i 1
1395end
1396if ne c 0
1397if ge n 384
1398exit 4
1399end
1400put bg add b n c
1401put bgl d add n 1
1402end
1403end
1404
1405fn bsv d ulong j ulong w ulong r ulong to ulong
1406b = mul d 384
1407n = get bgl d
1408hi = ulong 0
1409lo = ulong 0
1410if ge j w
1411x = sub j w
1412if lt x n
1413hi = get bg add b x
1414end
1415end
1416if gt j w
1417x = sub sub j w 1
1418if lt x n
1419lo = get bg add b x
1420end
1421end
1422v = shl hi r
1423if gt r 0
1424v = or v shr lo sub 32 r
1425end
1426ret and v 4294967295
1427end
1428
1429fn bshl d ulong k ulong to void
1430n = get bgl d
1431if eq n 0
1432ret
1433end
1434b = mul d 384
1435w = shr k 5
1436r = and k 31
1437m = add n add w 1
1438if gt m 384
1439exit 4
1440end
1441j = m
1442while gt j 0 bound 999999
1443j = sub j 1
1444put bg add b j bsv d j w r
1445end
1446nrm d m
1447end
1448
1449fn bshr1 d ulong to void
1450b = mul d 384
1451n = get bgl d
1452i = ulong 0
1453while lt i n bound 999999
1454v = shr get bg add b i 1
1455if lt add i 1 n
1456v = or v and shl get bg add b add i 1 31 4294967295
1457end
1458put bg add b i v
1459i = add i 1
1460end
1461nrm d n
1462end
1463
1464note 0 when slot x is less than slot y, 1 equal, 2 greater
1465fn bcmp x ulong y ulong to ulong
1466na = get bgl x
1467nb = get bgl y
1468if ne na nb
1469if gt na nb
1470ret ulong 2
1471end
1472ret ulong 0
1473end
1474i = na
1475while gt i 0 bound 999999
1476i = sub i 1
1477u = get bg add mul x 384 i
1478v = get bg add mul y 384 i
1479if ne u v
1480if gt u v
1481ret ulong 2
1482end
1483ret ulong 0
1484end
1485end
1486ret ulong 1
1487end
1488
1489note slot x minus slot y, x not less than y
1490fn bsub x ulong y ulong to void
1491na = get bgl x
1492nb = get bgl y
1493bo = ulong 0
1494i = ulong 0
1495while lt i na bound 999999
1496u = get bg add mul x 384 i
1497v = bo
1498if lt i nb
1499v = add v get bg add mul y 384 i
1500end
1501if ge u v
1502put bg add mul x 384 i sub u v
1503bo = ulong 0
1504else
1505put bg add mul x 384 i sub add u 4294967296 v
1506bo = ulong 1
1507end
1508i = add i 1
1509end
1510nrm x na
1511end
1512
1513fn bcopy d ulong s ulong to void
1514n = get bgl s
1515for i ulong n
1516put bg add mul d 384 i get bg add mul s 384 i
1517end
1518put bgl d n
1519end
1520
1521fn bbits d ulong to ulong
1522n = get bgl d
1523if eq n 0
1524ret ulong 0
1525end
1526t = get bg add mul d 384 sub n 1
1527k = mul sub n 1 32
1528while gt t 0 bound 64
1529t = shr t 1
1530k = add k 1
1531end
1532ret k
1533end
1534
1535note fr 2 = floor(num * 2^s / den) (num slot 0, den slot 1), fr 3 = 1 when a remainder is left
1536fn fqs s long to void
1537bcopy 2 0
1538bcopy 3 1
1539if ge s 0
1540bshl 2 ulong s
1541else
1542bshl 3 ulong sub long 0 s
1543end
1544bshl 3 55
1545q = ulong 0
1546for i ulong 56
1547q = shl q 1
1548if ne bcmp 2 3 0
1549bsub 2 3
1550q = or q 1
1551end
1552bshr1 3
1553end
1554put fr 2 q
1555put fr 3 0
1556if gt get bgl 2 0
1557put fr 3 1
1558end
1559end
1560
1561note num/den into binary64 bits (fr 0) and an exact flag (fr 1); st 1 when digits were dropped
1562fn fround st ulong to void
1563d = sub long bbits 0 long bbits 1
1564s = sub long 54 d
1565fqs s
1566if ge get fr 2 18014398509481984
1567s = sub s 1
1568fqs s
1569end
1570e2 = sub long 53 s
1571if lt e2 -1022
1572s = long 1075
1573fqs s
1574e2 = long -1022
1575end
1576q = get fr 2
1577m = shr q 1
1578r = and q 1
1579sk = or get fr 3 st
1580if ne r 0
1581if or ne sk 0 ne and m 1 0
1582m = add m 1
1583end
1584end
1585ex = ulong 0
1586if and eq r 0 eq sk 0
1587ex = ulong 1
1588end
1589bits = add shl ulong add e2 1022 52 m
1590if ge bits 9218868437227405312
1591bits = ulong 9218868437227405312
1592ex = ulong 0
1593end
1594put fr 0 bits
1595put fr 1 ex
1596end
1597
1598note magnitude of number node a in binary64: fr 0 bits, fr 1 = 1 when exact
1599fn f64of a ulong to void
1600put fr 0 0
1601put fr 1 1
1602t = get nt a
1603if or eq t 8 eq t 9
1604put fr 0 9218868437227405312
1605put fr 1 0
1606ret
1607end
1608n = get n2 a
1609if eq n 0
1610ret
1611end
1612e = get nv a
1613p = add long n e
1614if gt p 310
1615put fr 0 9218868437227405312
1616put fr 1 0
1617ret
1618end
1619if lt p -330
1620put fr 0 0
1621put fr 1 0
1622ret
1623end
1624k = n
1625st = ulong 0
1626if gt n 800
1627k = ulong 800
1628st = ulong 1
1629e = add e long sub n 800
1630end
1631bclr 0
1632b = get n1 a
1633for i ulong k
1634bmuladd 0 10 get pl add b i
1635end
1636bclr 1
1637bmuladd 1 1 1
1638if ge e 0
1639for i ulong ulong e
1640bmuladd 0 10 0
1641end
1642else
1643for i ulong ulong sub long 0 e
1644bmuladd 1 10 0
1645end
1646end
1647fround st
1648end
1649
1650fn numeq a ulong b ulong to bool
1651if and isint a isint b
1652ret deq a b
1653end
1654f64of a
1655x = get fr 0
1656xe = get fr 1
1657f64of b
1658y = get fr 0
1659ye = get fr 1
1660if and eq x 0 eq y 0
1661ret true
1662end
1663if ne x y
1664ret false
1665end
1666if ne nsign a nsign b
1667ret false
1668end
1669if and isint a eq xe 0
1670ret false
1671end
1672if and isint b eq ye 0
1673ret false
1674end
1675ret true
1676end
1677
1678fn streq a ulong b ulong to bool
1679k = get n2 a
1680if ne k get n2 b
1681ret false
1682end
1683ret pooleq get n1 a get n1 b k
1684end
1685
1686fn arreq a ulong b ulong to bool
1687k = get n2 a
1688if ne k get n2 b
1689ret false
1690end
1691x = get n1 a
1692y = get n1 b
1693i = ulong 0
1694while lt i k bound 999999
1695if not jeq x y 0
1696ret false
1697end
1698x = get nx x
1699y = get nx y
1700i = add i 1
1701end
1702ret true
1703end
1704
1705note key x has no equal key among the r keys after it
1706fn klast x ulong r ulong to bool
1707y = x
1708i = ulong 0
1709while lt i r bound 999999
1710y = get nx get nx y
1711if streq x y
1712ret false
1713end
1714i = add i 1
1715end
1716ret true
1717end
1718
1719fn okeys a ulong to ulong
1720m = shr get n2 a 1
1721x = get n1 a
1722c = ulong 0
1723i = ulong 0
1724while lt i m bound 999999
1725if klast x sub sub m i 1
1726c = add c 1
1727end
1728x = get nx get nx x
1729i = add i 1
1730end
1731ret c
1732end
1733
1734note value of the last key in object b equal to key x, else 4294967295
1735fn ofind b ulong x ulong to ulong
1736m = shr get n2 b 1
1737y = get n1 b
1738v = ulong 4294967295
1739i = ulong 0
1740while lt i m bound 999999
1741if streq x y
1742v = get nx y
1743end
1744y = get nx get nx y
1745i = add i 1
1746end
1747ret v
1748end
1749
1750fn objeq a ulong b ulong to bool
1751if ne okeys a okeys b
1752ret false
1753end
1754m = shr get n2 a 1
1755x = get n1 a
1756i = ulong 0
1757while lt i m bound 999999
1758if klast x sub sub m i 1
1759v = ofind b x
1760if eq v 4294967295
1761ret false
1762end
1763if not jeq get nx x v 0
1764ret false
1765end
1766end
1767x = get nx get nx x
1768i = add i 1
1769end
1770ret true
1771end
1772
1773note top 1 for the two whole documents, 0 for items inside a list or dict
1774fn jeq a ulong b ulong top ulong to bool
1775ta = get nt a
1776tb = get nt b
1777if or eq ta 7 eq tb 7
1778ret and and eq ta 7 eq tb 7 eq top 0
1779end
1780if and isnum ta isnum tb
1781ret numeq a b
1782end
1783if ne ta tb
1784ret false
1785end
1786if eq ta 4
1787ret streq a b
1788end
1789if eq ta 5
1790ret arreq a b
1791end
1792if eq ta 6
1793ret objeq a b
1794end
1795ret true
1796end
1797
1798note ---- rows ----
1799fn srow i ulong to bool
1800n = dgen i
1801m = dans i
1802if eq get rk i 1
1803strip fnl n
1804ret cmpab get sr 0 get sr 1 m
1805end
1806strip fdone n
1807s = get sr 0
1808t = get sr 1
1809put nn 0 0
1810put pn 0 0
1811a = jtop 0 s t
1812if not bad
1813b = jtop 1 0 m
1814if not bad
1815ret jeq a b 1
1816end
1817end
1818ret cmpab s t m
1819end
1820
1821fn tally to void
1822nr = get rn 0
1823i = ulong 0
1824while lt i nr bound 999999
1825ok = srow i
1826x = add mul get rr i 4096 get rg i
1827put tt x add get tt x 1
1828if ok
1829put tp x add get tp x 1
1830put pv i 1
1831end
1832i = add i 1
1833end
1834end