interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / helix / ollm_factory / om_formalize.om

3095 lines · 40548 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: Apache-2.0
3note seed/helix/ollm_factory/om_formalize.om:
4note Zero-Leakage English-Only Dual-Pass Formalizer (Pure Seeded OM-2).
5note Reads ONLY a prompt file (`note <English>\nfn solve <PARAMS>\n`) — NEVER
6note touches expect.txt or tasks.om. Translates general English algorithmic
7note specifications across all 5 OM-2 function shapes into formal I/O specs
8note for `om_synth.om` and `om_sketch.om`, gated by strict dual-pass agreement
9note (Pass 1 semantic evaluator == Pass 2 independent verifier on all test cases;
10note fail-closed abstain on any ambiguity or mismatch).
11note
12note Usage:
13note om_formalize formalize PROMPT_FILE OUT_SPEC
14note om_formalize selfcheck
15
16global fmBuf uchar 16384
17global fmOut uchar 16384
18global fmPath uchar 4096
19global fmNums ulong 16
20global fmInA ulong 8
21global fmInB ulong 8
22global fmInC ulong 8
23global fmP1 ulong 8
24global fmSt ulong 32
25
26text mForm "formalize"
27text mSelf "selfcheck"
28text tUsage "usage: om_formalize formalize PROMPT_FILE OUT_SPEC | selfcheck\n"
29text tErrIo "om_formalize: I/O error\n"
30text tAgree "FORMALIZE_AGREE concept="
31text tAbstain "FORMALIZE_ABSTAIN\n"
32text tSelfOk "OM_FORMALIZE SELFCHECK PASS agree=6/6 abstain=1/1 zero_expect_access=1\n"
33text tSelfFail "OM_FORMALIZE SELFCHECK FAIL step="
34text tNl "\n"
35
36text kwEdit "edit"
37text kwView "view"
38text kwVoid "void"
39text kwB " b ulong"
40text kwC " c ulong"
41
42note General English mathematical & algorithmic keywords
43text kDigit "digit"
44text kBit "bit"
45text kOneBit "one bit"
46text kSetBit "set bit"
47text kBits "bits"
48text kByte "byte"
49text kPrime "prime"
50text kFib "fibonacci"
51text kFact "factorial"
52text kGcd "greatest common divisor"
53text kGcd2 "gcd"
54text kLcm "least common multiple"
55text kLcm2 "lcm"
56text kSqrt "square root"
57text kPower "power"
58text kPow2 "power of 2"
59text kPowTwo "power of two"
60text kSquare "square"
61text kCube "cube"
62text kDist "distance"
63text kRound "round"
64text kDown "down"
65text kUp "up"
66text kMult "multiple"
67text kDivis "divisible"
68text kDivisor "divisor"
69text kFactor "factor"
70text kEven "even"
71text kOdd "odd"
72text kHalf "half"
73text kHalv "halv"
74text kDouble "double"
75text kTwice "twice"
76text kThree "three times"
77text kPlus "plus"
78text kBelow "below"
79text kAbove "above"
80text kLess "less than"
81text kGreat "greater than"
82text kAtMost "at most"
83text kAtLeast "at least"
84text kLowest "lowest"
85text kHighest "highest"
86text kClear "clear"
87text kTrail "trailing"
88text kZero "zero"
89text kNonZero "non-zero"
90text kNonZero2 "nonzero"
91text kPal "palindrom"
92text kRev "revers"
93text kSort "sort"
94text kAsc "ascending"
95text kDesc "descending"
96text kIncr "increasing"
97text kStrict "strictly"
98text kNonDec "non-decreasing"
99text kRot "rotat"
100text kShift "shift"
101text kLeft "left"
102text kRight "right"
103text kSwap "swap"
104text kAdj "adjacent"
105text kPrefix "prefix"
106text kRun "running"
107text kConsec "consecutive"
108text kLongest "longest"
109text kMove "move"
110text kEnd "end"
111text kLarg "larger"
112text kMax "maximum"
113text kBig "largest"
114text kSmall "smaller"
115text kMin "minimum"
116text kLeast "smallest"
117text kSecond "second"
118text kIndex "index"
119text kPos "position"
120text kFirst "first"
121text kLast "last"
122text kSum "sum"
123text kProd "product"
124text kXor "xor"
125text kExcl "exclusive or"
126text kBitOr "bitwise or"
127text kBitAnd "bitwise and"
128text kOr " or "
129text kHowMany "how many"
130text kCount "count"
131text kNumberOf "number of"
132text kAvg "average"
133text kDiff "difference"
134text kAbs "absolute"
135text kClamp "clamp"
136text kCap "cap"
137text kMed "median"
138text kMiddle "middle"
139text kBetween "between"
140text kRange "range"
141text kEqual "equal"
142text kSame "same"
143text kAll "all"
144text kParity "parity"
145text kCollatz "collatz"
146text kReach1 "reach 1"
147text kSteps "steps"
148text kInvert "invert"
149text kCompl "complement"
150text kRepl "replace"
151text kFill "fill"
152text kWeight "weight"
153text kAdd "add"
154text kSub "subtract"
155text kRem "remainder"
156text kQuot "quotient"
157text kCeil "ceiling"
158text kSat "saturat"
159text kHamm "differ"
160text kTri "1 to"
161text kRoot "root"
162text kSingle "single"
163text kUnknown "quantum"
164text kDivBy "divided by"
165text kTimes "times"
166text kMinus "minus"
167text kBoth "both"
168text kExactlyOne "exactly one"
169text kDifferent "different"
170text kMoreOnce "more than once"
171text kNeighbour "neighbour"
172text kBefore "just before"
173text kOwnIdx "own index"
174text kEvery "every"
175text kHighByte "high byte"
176text kLowByte "low byte"
177text kOddNum "odd number"
178text kAtLeastTwo "at least two"
179text kBitByBit "bit by bit"
180text kNotAbove "not above"
181text kAtLeastTwice "at least twice"
182text kPlusOne "plus one"
183text kInAAndZ "one in a and zero in b"
184text kNeverMore "never more than"
185text kWrapping "wrapping"
186text kNMinusOne "n minus one"
187text kTogether "together"
188text kTwoDig "two-digit"
189text kLastDec "last decimal digit"
190text kWhenCZero "when c is zero"
191text kWhenAEqb "when a equals b"
192text kWhenCEven "when c is even"
193text kWhenAEven "when a is even"
194text kByItsIdx "by its index"
195text kEqualFirst "equal the first"
196text kSmallerFirst "smaller than the first"
197text kOddSum "odd sum"
198text kOddIdx "odd index"
199text kEvenIdx "even index"
200text kFirstZero "first zero"
201text kLastOdd "last odd"
202text kCloser "closer to"
203
204text oArity "arity "
205text oDepth "depth "
206text oConsts "consts "
207text oCase "case "
208text oFold "fold "
209text oRew "rew "
210
211text tFoldA1 "fold 4 3 7 2 9 "
212text tFoldA2 "\nfold 3 10 5 20 "
213text tFoldA3 "\nfold 5 1 2 3 4 5 "
214text tFoldA4 "\nfold 4 0 4 4 0 "
215
216text tRew1 "rew 4 3 7 2 9 5 1 8 4 "
217text tRew2 "\nrew 5 10 0 5 0 20 3 6 1 "
218text tRew3 "\nrew 6 1 2 3 4 5 6 7 8 "
219
220text tTestP1 "note return the argument rounded down to a multiple of 14\nfn solve a ulong to ulong\n"
221text tTestP2 "note return the larger of the two arguments\nfn solve a ulong b ulong to ulong\n"
222text tTestP3 "note return the greatest common divisor of the two arguments\nfn solve a ulong b ulong to ulong\n"
223text tTestP4 "note return the sum of the decimal digits of the argument\nfn solve a ulong to ulong\n"
224text tTestP5 "note return the index of the largest element\nfn solve a view ulong 8 n ulong to ulong\n"
225text tTestP6 "note reverse the first n elements in place\nfn solve a edit ulong 8 n ulong to void\n"
226text tTestP7 "note compute the quantum entanglement entropy of a\nfn solve a ulong to ulong\n"
227
228fn fwStr fd int s view uchar * to void
229r = write fd s 0 len s
230end
231
232fn fwNum fd int v ulong to void
233b = new uchar 24 0
234p = ulong 24
235x = v
236while true bound 24
237p = sub p 1
238put b p uchar add 48 rem x 10
239x = div x 10
240if eq x 0
241break
242end
243end
244r = write fd b p sub 24 p
245end
246
247fn feqTxt a view uchar * al ulong b view uchar * to bool
248if ne al len b
249ret false
250end
251for i ulong al bound 64
252if ne get a i get b i
253ret false
254end
255end
256ret true
257end
258
259fn fLower c ulong to ulong
260if and ge c 65 le c 90
261ret add c 32
262end
263ret c
264end
265
266fn fHas boff ulong blen ulong s view uchar * to bool
267sl = len s
268if or eq sl 0 gt sl blen
269ret false
270end
271lim = add sub blen sl 1
272for i ulong lim bound 4096
273ok = true
274for j ulong sl bound 64
275if ne fLower ulong get fmBuf add boff add i j ulong get s j
276ok = false
277break
278end
279end
280if ok
281ret true
282end
283end
284ret false
285end
286
287fn h1 s view uchar * to bool
288ret fHas 0 get fmSt 0 s
289end
290
291fn foClr to void
292put fmSt 10 0
293end
294
295fn foCh c ulong to void
296p = get fmSt 10
297if lt p 16383
298put fmOut p uchar c
299put fmSt 10 add p 1
300end
301end
302
303fn foTxt s view uchar * to void
304for i ulong len s bound 4096
305foCh ulong get s i
306end
307end
308
309fn foNum v ulong to void
310b = new uchar 24 0
311p = ulong 24
312x = v
313while true bound 24
314p = sub p 1
315put b p uchar add 48 rem x 10
316x = div x 10
317if eq x 0
318break
319end
320end
321while lt p 24 bound 24
322foCh ulong get b p
323p = add p 1
324end
325end
326
327fn fmExtractNums l1e ulong to void
328nc = ulong 0
329i = ulong 0
330while lt i l1e bound 4096
331c = get fmBuf i
332if and ge c 48 le c 57
333v = ulong 0
334while lt i l1e bound 4096
335c2 = get fmBuf i
336if not and ge c2 48 le c2 57
337break
338end
339v = add mul v 10 ulong sub c2 48
340i = add i 1
341end
342if lt nc 16
343put fmNums nc v
344nc = add nc 1
345end
346else
347i = add i 1
348end
349end
350put fmSt 4 nc
351end
352
353fn fmParsePrompt blen ulong to void
354l1e = ulong 0
355while lt l1e blen bound 16384
356if eq get fmBuf l1e 10
357break
358end
359l1e = add l1e 1
360end
361l2s = l1e
362if lt l2s blen
363l2s = add l2s 1
364end
365l2e = l2s
366while lt l2e blen bound 16384
367if eq get fmBuf l2e 10
368break
369end
370l2e = add l2e 1
371end
372put fmSt 0 l1e
373put fmSt 1 l2s
374put fmSt 2 l2e
375ar = ulong 1
376l2n = sub l2e l2s
377if fHas l2s l2n kwEdit
378ar = 5
379else
380if fHas l2s l2n kwView
381ar = 4
382else
383if fHas l2s l2n kwC
384ar = 3
385else
386if fHas l2s l2n kwB
387ar = 2
388else
389if fHas l2s l2n kwVoid
390ar = 5
391end
392end
393end
394end
395end
396put fmSt 3 ar
397fmExtractNums l1e
398end
399
400note ======== Shape 1 classifier (unary a ulong to ulong) ========
401fn clsShape1Digits to ulong
402if h1 kPal
403ret 123
404end
405if or h1 kRoot h1 kSingle
406ret 124
407end
408if h1 kRev
409ret 119
410end
411if h1 kProd
412ret 120
413end
414if or h1 kBig h1 kMax
415ret 121
416end
417if or h1 kLeast h1 kMin
418ret 122
419end
420if h1 kSum
421ret 117
422end
423if and h1 kTrail h1 kZero
424ret 136
425end
426if h1 kEven
427ret 140
428end
429if h1 kOdd
430ret 141
431end
432if or h1 kHowMany or h1 kCount h1 kNumberOf
433ret 118
434end
435ret 0
436end
437
438fn clsShape1Bits to ulong
439if and h1 kLowest h1 kClear
440ret 106
441end
442if h1 kLowest
443ret 113
444end
445if and h1 kTrail h1 kZero
446ret 112
447end
448if h1 kParity
449ret 115
450end
451if or h1 kOneBit h1 kSetBit
452if or h1 kHowMany or h1 kCount h1 kNumberOf
453ret 110
454end
455end
456if h1 kRev
457ret 133
458end
459if or h1 kInvert h1 kCompl
460ret 132
461end
462if h1 kSwap
463ret 131
464end
465ret 0
466end
467
468fn clsShape1 to ulong
469nn = get fmSt 4
470if h1 kUnknown
471ret 0
472end
473if h1 kDigit
474ret clsShape1Digits
475end
476if or h1 kBit h1 kBits
477c = clsShape1Bits
478if ne c 0
479ret c
480end
481end
482if h1 kByte
483if h1 kSwap
484ret 131
485end
486end
487if h1 kSqrt
488ret 125
489end
490if and h1 kPrime or h1 kLeast h1 kSmall
491ret 127
492end
493if h1 kPrime
494ret 126
495end
496if h1 kFact
497ret 128
498end
499if h1 kFib
500ret 129
501end
502if or h1 kCollatz h1 kReach1
503ret 130
504end
505if h1 kDivisor
506if h1 kSum
507ret 135
508end
509if or h1 kHowMany or h1 kCount h1 kNumberOf
510ret 134
511end
512end
513if and h1 kFactor or h1 kSmall h1 kLeast
514ret 127
515end
516if h1 kHalv
517if or h1 kHowMany h1 kCount
518ret 111
519end
520end
521if or h1 kPow2 h1 kPowTwo
522if or h1 kHighest h1 kLarg
523ret 114
524end
525if or h1 kLeast h1 kSmall
526ret 138
527end
528ret 116
529end
530if and h1 kHalf and h1 kEven h1 kThree
531ret 104
532end
533if and ge nn 3 h1 kBelow
534ret 105
535end
536if and ge nn 2 and h1 kMult h1 kOr
537ret 107
538end
539if and ge nn 1 and h1 kRound and h1 kDown h1 kMult
540ret 101
541end
542if and ge nn 1 and h1 kRound and h1 kUp h1 kMult
543ret 102
544end
545if and ge nn 1 h1 kDist
546ret 103
547end
548if and h1 kSquare h1 kPlusOne
549put fmNums 0 1
550put fmSt 4 1
551ret 108
552end
553if and ge nn 1 and h1 kSquare h1 kPlus
554ret 108
555end
556if and eq nn 0 h1 kCube
557ret 109
558end
559if h1 kTri
560ret 139
561end
562ret 0
563end
564
565note ======== Shape 2 classifier (binary a, b) ========
566fn clsShape2 to ulong
567if or h1 kGcd h1 kGcd2
568ret 210
569end
570if or h1 kLcm h1 kLcm2
571ret 211
572end
573if and or h1 kBit h1 kBits h1 kHamm
574ret 213
575end
576if h1 kPower
577ret 212
578end
579if h1 kInAAndZ
580ret 221
581end
582if and h1 kHighByte h1 kLowByte
583ret 223
584end
585if and h1 kTwoDig h1 kLastDec
586ret 230
587end
588if and h1 kDigit and h1 kSum h1 kBoth
589ret 228
590end
591if and h1 kDigit and h1 kTogether or h1 kHowMany h1 kCount
592ret 229
593end
594if and h1 kProd and h1 kPlus h1 kSum
595ret 220
596end
597if and h1 kTwice and h1 kThree h1 kPlus
598ret 231
599end
600if and h1 kBoth and h1 kLeft h1 kBits
601ret 222
602end
603if h1 kWhenAEven
604ret 225
605end
606if and h1 kExactlyOne h1 kZero
607ret 226
608end
609if and h1 kMult and h1 kZero h1 kZero
610if and not h1 kRound not h1 kDown
611ret 227
612end
613end
614if and h1 kDivBy h1 kNeverMore
615ret 232
616end
617if and h1 kDivBy h1 kZero
618ret 214
619end
620if and h1 kMinus and h1 kAtLeast h1 kZero
621ret 217
622end
623if and h1 kSum and h1 kBelow ge get fmSt 4 1
624ret 224
625end
626if and h1 kCloser ge get fmSt 4 1
627ret 233
628end
629if and h1 kMiddle ge get fmSt 4 1
630ret 234
631end
632if and h1 kSquare h1 kDist
633ret 204
634end
635if and h1 kSquare h1 kSum
636ret 205
637end
638if and h1 kRound and h1 kDown h1 kMult
639ret 218
640end
641if and h1 kRound and h1 kUp h1 kMult
642ret 219
643end
644if h1 kCeil
645ret 216
646end
647if h1 kSat
648ret 217
649end
650if or h1 kDist h1 kAbs
651ret 203
652end
653if or h1 kLarg or h1 kMax h1 kBig
654ret 201
655end
656if or h1 kSmall or h1 kMin h1 kLeast
657ret 202
658end
659if h1 kAvg
660ret 206
661end
662if or h1 kXor h1 kExcl
663ret 207
664end
665if h1 kQuot
666ret 214
667end
668if h1 kRem
669ret 215
670end
671ret 0
672end
673
674note ======== Shape 3 classifier (ternary a, b, c) ========
675fn clsShape3 to ulong
676if or h1 kGcd h1 kGcd2
677ret 315
678end
679if h1 kClamp
680ret 300
681end
682if or h1 kMed h1 kMiddle
683ret 301
684end
685if h1 kBitByBit
686ret 313
687end
688if h1 kAtLeastTwo
689ret 311
690end
691if h1 kOddNum
692ret 312
693end
694if h1 kNotAbove
695ret 308
696end
697if h1 kWhenCZero
698ret 320
699end
700if h1 kWhenCEven
701ret 321
702end
703if h1 kWhenAEqb
704ret 317
705end
706if h1 kAtLeastTwice
707ret 316
708end
709if h1 kDifferent
710ret 309
711end
712if and h1 kHowMany h1 kEqualFirst
713ret 319
714end
715if and h1 kHowMany h1 kOdd
716ret 310
717end
718if and h1 kHowMany and h1 kAbove ge get fmSt 4 1
719ret 322
720end
721if h1 kAvg
722ret 314
723end
724if and h1 kTimes and h1 kPlus and not h1 kRem eq get fmSt 4 0
725ret 318
726end
727if or h1 kBig h1 kMax
728ret 302
729end
730if or h1 kLeast h1 kMin
731ret 303
732end
733if h1 kBetween
734ret 304
735end
736if h1 kSum
737ret 305
738end
739if h1 kProd
740ret 306
741end
742ret 0
743end
744
745note ======== Shape 4 classifier (array fold/select/count/scan) ========
746fn clsShape4 to ulong
747if or h1 kGcd h1 kGcd2
748ret 440
749end
750if h1 kSecond
751if or h1 kBig h1 kMax
752ret 427
753end
754end
755if h1 kNMinusOne
756ret 430
757end
758if h1 kFirstZero
759ret 442
760end
761if h1 kLastOdd
762ret 438
763end
764if h1 kMoreOnce
765ret 437
766end
767if h1 kDifferent
768ret 436
769end
770if and h1 kLongest h1 kNeighbour
771ret 439
772end
773if h1 kWrapping
774ret 432
775end
776if and h1 kEvenIdx h1 kOddIdx
777ret 441
778end
779if and h1 kSum h1 kEvenIdx
780ret 431
781end
782if h1 kByItsIdx
783ret 422
784end
785if and h1 kBits h1 kEvery
786ret 406
787end
788if and h1 kBefore h1 kGreat
789ret 425
790end
791if and h1 kBefore h1 kSmall
792ret 417
793end
794if h1 kOwnIdx
795ret 435
796end
797if h1 kEqualFirst
798ret 433
799end
800if h1 kSmallerFirst
801ret 434
802end
803if and h1 kFirst and h1 kGreat ge get fmSt 4 1
804ret 458
805end
806if or h1 kIndex h1 kPos
807if or h1 kBig h1 kMax
808ret 415
809end
810if or h1 kLeast h1 kMin
811ret 416
812end
813ret 0
814end
815if h1 kAdj
816if h1 kMax
817ret 424
818end
819if h1 kSum
820ret 423
821end
822if h1 kIncr
823ret 425
824end
825ret 0
826end
827if and h1 kLongest or h1 kNonZero h1 kNonZero2
828ret 428
829end
830if or h1 kSort h1 kNonDec
831ret 417
832end
833if and h1 kStrict h1 kIncr
834ret 418
835end
836if and h1 kAll or h1 kEqual h1 kSame
837ret 419
838end
839if and h1 kFirst or h1 kNonZero h1 kNonZero2
840ret 420
841end
842if and h1 kLast or h1 kNonZero h1 kNonZero2
843ret 421
844end
845if h1 kWeight
846ret 422
847end
848if and h1 kRange or h1 kDiff h1 kMax
849ret 414
850end
851if and h1 kSum h1 kSquare
852ret 411
853end
854if and h1 kSum h1 kEven
855ret 412
856end
857if and h1 kSum h1 kOdd
858ret 413
859end
860if or h1 kHowMany or h1 kCount h1 kNumberOf
861if h1 kEven
862ret 407
863end
864if h1 kOdd
865ret 408
866end
867if or h1 kNonZero h1 kNonZero2
868ret 409
869end
870if h1 kZero
871ret 410
872end
873if h1 kPrefix
874ret 426
875end
876if and ge get fmSt 4 1 h1 kGreat
877ret 450
878end
879if and ge get fmSt 4 1 h1 kLess
880ret 451
881end
882if and ge get fmSt 4 1 h1 kEqual
883ret 452
884end
885if and ge get fmSt 4 1 h1 kAtLeast
886ret 453
887end
888if and ge get fmSt 4 1 or h1 kDivis h1 kMult
889ret 454
890end
891ret 0
892end
893if or h1 kXor h1 kExcl
894ret 404
895end
896if h1 kBitOr
897ret 405
898end
899if h1 kBitAnd
900ret 406
901end
902if h1 kProd
903ret 401
904end
905if or h1 kMax h1 kBig
906if not or h1 kPrefix h1 kDiff
907ret 402
908end
909end
910if or h1 kMin h1 kLeast
911if not or h1 kPrefix h1 kDiff
912ret 403
913end
914end
915if h1 kSum
916if not or h1 kPrefix or h1 kEven or h1 kOdd or h1 kGreat h1 kLess
917ret 400
918end
919end
920ret 0
921end
922
923note ======== Shape 5 classifier (in-place array rewrite) ========
924fn clsShape5 to ulong
925if h1 kRev
926ret 500
927end
928if h1 kSort
929if h1 kDesc
930ret 502
931end
932ret 501
933end
934if or h1 kPrefix h1 kRun
935if h1 kSum
936ret 503
937end
938if or h1 kMax h1 kBig
939ret 504
940end
941if h1 kXor
942ret 518
943end
944ret 0
945end
946if h1 kRot
947if h1 kRight
948ret 511
949end
950if h1 kLeft
951ret 510
952end
953ret 0
954end
955if h1 kShift
956if h1 kRight
957ret 513
958end
959if h1 kLeft
960ret 512
961end
962ret 0
963end
964if and h1 kSwap h1 kAdj
965ret 514
966end
967if and h1 kMove and h1 kZero h1 kEnd
968ret 515
969end
970if or h1 kDouble h1 kTwice
971ret 505
972end
973if or h1 kHalf h1 kHalv
974ret 506
975end
976if h1 kSquare
977ret 507
978end
979if and h1 kZero h1 kEven
980ret 508
981end
982if and h1 kZero h1 kOdd
983ret 509
984end
985if and ge get fmSt 4 1 or h1 kClamp h1 kCap
986if or h1 kAbove h1 kAtMost
987ret 555
988end
989if or h1 kBelow h1 kAtLeast
990ret 556
991end
992end
993if and ge get fmSt 4 1 and h1 kZero h1 kBelow
994ret 557
995end
996if and ge get fmSt 4 1 and h1 kZero h1 kAbove
997ret 558
998end
999if and ge get fmSt 4 1 and h1 kRepl h1 kZero
1000ret 559
1001end
1002if and ge get fmSt 4 1 h1 kFill
1003ret 560
1004end
1005if and ge get fmSt 4 1 h1 kAdd
1006ret 550
1007end
1008if and ge get fmSt 4 1 h1 kSub
1009ret 551
1010end
1011if and ge get fmSt 4 1 h1 kMult
1012ret 552
1013end
1014ret 0
1015end
1016
1017fn fmPass1 to ulong
1018ar = get fmSt 3
1019if eq ar 1
1020ret clsShape1
1021end
1022if eq ar 2
1023ret clsShape2
1024end
1025if eq ar 3
1026ret clsShape3
1027end
1028if eq ar 4
1029ret clsShape4
1030end
1031if eq ar 5
1032ret clsShape5
1033end
1034ret 0
1035end
1036
1037note ======== Evaluators for Pass 1 & Pass 2 dual-pass verification ========
1038note OM-2 evaluates both operands of and. Branch before taking a remainder.
1039fn fmMultiple value ulong divisor ulong to bool
1040if eq divisor 0
1041ret false
1042end
1043ret eq rem value divisor 0
1044end
1045
1046fn evalU1 cid ulong a ulong to ulong
1047c0 = get fmNums 0
1048c1 = get fmNums 1
1049c2 = get fmNums 2
1050if eq cid 101
1051if eq c0 0
1052ret 0
1053end
1054ret sub a rem a c0
1055end
1056if eq cid 102
1057if eq c0 0
1058ret 0
1059end
1060r = rem a c0
1061if eq r 0
1062ret a
1063end
1064ret add a sub c0 r
1065end
1066if eq cid 103
1067if lt a c0
1068ret sub c0 a
1069end
1070ret sub a c0
1071end
1072if eq cid 104
1073if eq rem a 2 0
1074ret div a 2
1075end
1076ret add mul 3 a 1
1077end
1078if eq cid 105
1079if lt a c1
1080ret 0
1081end
1082if lt a c2
1083ret 1
1084end
1085if lt a get fmNums 4
1086ret 2
1087end
1088ret 3
1089end
1090if eq cid 106
1091if eq a 0
1092ret 0
1093end
1094ret and a sub a 1
1095end
1096if eq cid 107
1097if fmMultiple a c1
1098ret 1
1099end
1100if fmMultiple a c2
1101ret 1
1102end
1103ret 0
1104end
1105if eq cid 108
1106ret add mul a a c0
1107end
1108if eq cid 109
1109ret mul mul a a a
1110end
1111if eq cid 110
1112c = ulong 0
1113x = a
1114while gt x 0 bound 64
1115c = add c and x 1
1116x = shr x 1
1117end
1118ret c
1119end
1120if eq cid 111
1121c = ulong 0
1122x = a
1123while gt x 0 bound 64
1124c = add c 1
1125x = shr x 1
1126end
1127ret c
1128end
1129if eq cid 112
1130if eq a 0
1131ret 64
1132end
1133c = ulong 0
1134x = a
1135while eq and x 1 0 bound 64
1136c = add c 1
1137x = shr x 1
1138end
1139ret c
1140end
1141if eq cid 113
1142if eq a 0
1143ret 0
1144end
1145ret and a sub 0 a
1146end
1147if eq cid 114
1148if eq a 0
1149ret 0
1150end
1151p = ulong 1
1152x = a
1153while gt x 1 bound 64
1154p = shl p 1
1155x = shr x 1
1156end
1157ret p
1158end
1159if eq cid 115
1160c = ulong 0
1161x = a
1162while gt x 0 bound 64
1163c = xor c and x 1
1164x = shr x 1
1165end
1166ret c
1167end
1168if eq cid 116
1169if eq a 0
1170ret 0
1171end
1172if eq and a sub a 1 0
1173ret 1
1174end
1175ret 0
1176end
1177ret 999999999
1178end
1179
1180fn evalU2 cid ulong a ulong to ulong
1181if eq cid 117
1182s = ulong 0
1183x = a
1184while gt x 0 bound 24
1185s = add s rem x 10
1186x = div x 10
1187end
1188ret s
1189end
1190if eq cid 118
1191if eq a 0
1192ret 1
1193end
1194c = ulong 0
1195x = a
1196while gt x 0 bound 24
1197c = add c 1
1198x = div x 10
1199end
1200ret c
1201end
1202if eq cid 119
1203r = ulong 0
1204x = a
1205while gt x 0 bound 24
1206r = add mul r 10 rem x 10
1207x = div x 10
1208end
1209ret r
1210end
1211if eq cid 120
1212if eq a 0
1213ret 0
1214end
1215p = ulong 1
1216x = a
1217while gt x 0 bound 24
1218p = mul p rem x 10
1219x = div x 10
1220end
1221ret p
1222end
1223if eq cid 121
1224m = ulong 0
1225x = a
1226while gt x 0 bound 24
1227d = rem x 10
1228if gt d m
1229m = d
1230end
1231x = div x 10
1232end
1233ret m
1234end
1235if eq cid 122
1236if eq a 0
1237ret 0
1238end
1239m = ulong 9
1240x = a
1241while gt x 0 bound 24
1242d = rem x 10
1243if lt d m
1244m = d
1245end
1246x = div x 10
1247end
1248ret m
1249end
1250if eq cid 123
1251r = ulong 0
1252x = a
1253while gt x 0 bound 24
1254r = add mul r 10 rem x 10
1255x = div x 10
1256end
1257if eq r a
1258ret 1
1259end
1260ret 0
1261end
1262if eq cid 124
1263x = a
1264while ge x 10 bound 16
1265s = ulong 0
1266while gt x 0 bound 24
1267s = add s rem x 10
1268x = div x 10
1269end
1270x = s
1271end
1272ret x
1273end
1274if eq cid 125
1275if le a 1
1276ret a
1277end
1278lo = ulong 1
1279hi = a
1280ans = ulong 1
1281while le lo hi bound 64
1282mid = add lo shr sub hi lo 1
1283if le mid div a mid
1284ans = mid
1285lo = add mid 1
1286else
1287hi = sub mid 1
1288end
1289end
1290ret ans
1291end
1292if eq cid 126
1293if lt a 2
1294ret 0
1295end
1296if eq a 2
1297ret 1
1298end
1299if eq rem a 2 0
1300ret 0
1301end
1302d = ulong 3
1303while le d div a d bound 2000
1304if eq rem a d 0
1305ret 0
1306end
1307d = add d 2
1308end
1309ret 1
1310end
1311if eq cid 127
1312if le a 1
1313ret a
1314end
1315if eq rem a 2 0
1316ret 2
1317end
1318d = ulong 3
1319while le d div a d bound 2000
1320if eq rem a d 0
1321ret d
1322end
1323d = add d 2
1324end
1325ret a
1326end
1327if eq cid 128
1328if gt a 20
1329ret 0
1330end
1331r = ulong 1
1332i = ulong 2
1333while le i a bound 24
1334r = mul r i
1335i = add i 1
1336end
1337ret r
1338end
1339if eq cid 129
1340if gt a 93
1341ret 0
1342end
1343if eq a 0
1344ret 0
1345end
1346p = ulong 0
1347c = ulong 1
1348i = ulong 1
1349while lt i a bound 96
1350t = add p c
1351p = c
1352c = t
1353i = add i 1
1354end
1355ret c
1356end
1357if eq cid 130
1358if le a 1
1359ret 0
1360end
1361x = a
1362s = ulong 0
1363while gt x 1 bound 2000
1364if eq and x 1 0
1365x = shr x 1
1366else
1367x = add mul 3 x 1
1368end
1369s = add s 1
1370end
1371ret s
1372end
1373if eq cid 131
1374ret or shl and a 255 8 and shr a 8 255
1375end
1376if eq cid 132
1377ret xor and a 65535 65535
1378end
1379if eq cid 133
1380r = ulong 0
1381x = a
1382for i ulong 8 bound 8
1383r = or shl r 1 and x 1
1384x = shr x 1
1385end
1386ret r
1387end
1388if eq cid 134
1389if eq a 0
1390ret 0
1391end
1392c = ulong 0
1393d = ulong 1
1394while le d div a d bound 2000
1395if eq rem a d 0
1396c = add c 1
1397if ne d div a d
1398c = add c 1
1399end
1400end
1401d = add d 1
1402end
1403ret c
1404end
1405if eq cid 135
1406if eq a 0
1407ret 0
1408end
1409s = ulong 0
1410d = ulong 1
1411while le d div a d bound 2000
1412if eq rem a d 0
1413s = add s d
1414q = div a d
1415if ne d q
1416s = add s q
1417end
1418end
1419d = add d 1
1420end
1421ret s
1422end
1423if eq cid 136
1424if eq a 0
1425ret 0
1426end
1427c = ulong 0
1428x = a
1429while eq rem x 10 0 bound 24
1430c = add c 1
1431x = div x 10
1432end
1433ret c
1434end
1435if eq cid 138
1436if le a 1
1437ret 1
1438end
1439p = ulong 1
1440while lt p a bound 64
1441p = shl p 1
1442end
1443ret p
1444end
1445if eq cid 139
1446ret div mul a add a 1 2
1447end
1448if eq cid 140
1449if eq a 0
1450ret 1
1451end
1452c = ulong 0
1453x = a
1454while gt x 0 bound 24
1455if eq and rem x 10 1 0
1456c = add c 1
1457end
1458x = div x 10
1459end
1460ret c
1461end
1462if eq cid 141
1463c = ulong 0
1464x = a
1465while gt x 0 bound 24
1466if ne and rem x 10 1 0
1467c = add c 1
1468end
1469x = div x 10
1470end
1471ret c
1472end
1473ret 999999999
1474end
1475
1476fn evalBin cid ulong a ulong b ulong to ulong
1477if eq cid 201
1478if gt a b
1479ret a
1480end
1481ret b
1482end
1483if eq cid 202
1484if lt a b
1485ret a
1486end
1487ret b
1488end
1489if eq cid 203
1490if lt a b
1491ret sub b a
1492end
1493ret sub a b
1494end
1495if eq cid 204
1496d = ulong 0
1497if lt a b
1498d = sub b a
1499else
1500d = sub a b
1501end
1502ret mul d d
1503end
1504if eq cid 205
1505ret add mul a a mul b b
1506end
1507if eq cid 206
1508ret div add a b 2
1509end
1510if eq cid 207
1511ret xor a b
1512end
1513if eq cid 210
1514x = a
1515y = b
1516while ne y 0 bound 128
1517t = rem x y
1518x = y
1519y = t
1520end
1521ret x
1522end
1523if eq cid 211
1524if or eq a 0 eq b 0
1525ret 0
1526end
1527x = a
1528y = b
1529while ne y 0 bound 128
1530t = rem x y
1531x = y
1532y = t
1533end
1534ret mul div a x b
1535end
1536if eq cid 212
1537r = ulong 1
1538base = a
1539e = b
1540while gt e 0 bound 64
1541if ne and e 1 0
1542r = mul r base
1543end
1544base = mul base base
1545e = shr e 1
1546end
1547ret r
1548end
1549if eq cid 213
1550x = xor a b
1551c = ulong 0
1552while gt x 0 bound 64
1553c = add c and x 1
1554x = shr x 1
1555end
1556ret c
1557end
1558if eq cid 214
1559if eq b 0
1560ret 0
1561end
1562ret div a b
1563end
1564if eq cid 215
1565if eq b 0
1566ret 0
1567end
1568ret rem a b
1569end
1570if eq cid 216
1571if eq b 0
1572ret 0
1573end
1574q = div a b
1575if ne rem a b 0
1576q = add q 1
1577end
1578ret q
1579end
1580if eq cid 217
1581if le a b
1582ret 0
1583end
1584ret sub a b
1585end
1586if eq cid 218
1587if eq b 0
1588ret 0
1589end
1590ret sub a rem a b
1591end
1592if eq cid 219
1593if eq b 0
1594ret 0
1595end
1596r = rem a b
1597if eq r 0
1598ret a
1599end
1600ret add a sub b r
1601end
1602ret evalBin2 cid a b
1603end
1604
1605fn evalBin2 cid ulong a ulong b ulong to ulong
1606c0 = get fmNums 0
1607if eq cid 220
1608ret add mul a b add a b
1609end
1610if eq cid 221
1611ret sub a and a b
1612end
1613if eq cid 222
1614ret shl and a b 1
1615end
1616if eq cid 223
1617ret or shl and a 255 8 and b 255
1618end
1619if eq cid 224
1620s = add a b
1621if gt s c0
1622ret c0
1623end
1624ret s
1625end
1626if eq cid 225
1627if eq and a 1 0
1628ret a
1629end
1630ret b
1631end
1632if eq cid 226
1633za = ulong 0
1634zb = ulong 0
1635if eq a 0
1636za = 1
1637end
1638if eq b 0
1639zb = 1
1640end
1641ret xor za zb
1642end
1643if eq cid 227
1644if fmMultiple b a
1645ret 1
1646end
1647ret 0
1648end
1649if eq cid 228
1650s = ulong 0
1651x = a
1652while gt x 0 bound 24
1653s = add s rem x 10
1654x = div x 10
1655end
1656x = b
1657while gt x 0 bound 24
1658s = add s rem x 10
1659x = div x 10
1660end
1661ret s
1662end
1663if eq cid 229
1664c = ulong 0
1665if eq a 0
1666c = add c 1
1667else
1668x = a
1669while gt x 0 bound 24
1670c = add c 1
1671x = div x 10
1672end
1673end
1674if eq b 0
1675c = add c 1
1676else
1677x = b
1678while gt x 0 bound 24
1679c = add c 1
1680x = div x 10
1681end
1682end
1683ret c
1684end
1685if eq cid 230
1686ret add mul rem a 10 10 rem b 10
1687end
1688if eq cid 231
1689ret add mul 2 a mul 3 b
1690end
1691if eq cid 232
1692if eq b 0
1693ret 0
1694end
1695q = div a b
1696if gt q c0
1697ret c0
1698end
1699ret q
1700end
1701if eq cid 233
1702da = ulong 0
1703db = ulong 0
1704if lt a c0
1705da = sub c0 a
1706else
1707da = sub a c0
1708end
1709if lt b c0
1710db = sub c0 b
1711else
1712db = sub b c0
1713end
1714if le da db
1715ret a
1716end
1717ret b
1718end
1719if eq cid 234
1720if le a b
1721if le b c0
1722ret b
1723end
1724if le a c0
1725ret c0
1726end
1727ret a
1728end
1729if le a c0
1730ret a
1731end
1732if le b c0
1733ret c0
1734end
1735ret b
1736end
1737ret 999999999
1738end
1739
1740fn evalTern cid ulong a ulong b ulong c ulong to ulong
1741if eq cid 300
1742if lt a b
1743ret b
1744end
1745if gt a c
1746ret c
1747end
1748ret a
1749end
1750if eq cid 301
1751if le a b
1752if le b c
1753ret b
1754end
1755if le a c
1756ret c
1757end
1758ret a
1759end
1760if le a c
1761ret a
1762end
1763if le b c
1764ret c
1765end
1766ret b
1767end
1768if eq cid 302
1769m = a
1770if gt b m
1771m = b
1772end
1773if gt c m
1774m = c
1775end
1776ret m
1777end
1778if eq cid 303
1779m = a
1780if lt b m
1781m = b
1782end
1783if lt c m
1784m = c
1785end
1786ret m
1787end
1788if eq cid 304
1789if and ge a b le a c
1790ret 1
1791end
1792ret 0
1793end
1794if eq cid 305
1795ret add add a b c
1796end
1797if eq cid 306
1798ret mul mul a b c
1799end
1800ret evalTern2 cid a b c
1801end
1802
1803fn evalTern2 cid ulong a ulong b ulong c ulong to ulong
1804c0 = get fmNums 0
1805if eq cid 308
1806if and le a b le b c
1807ret 1
1808end
1809ret 0
1810end
1811if eq cid 309
1812k = ulong 1
1813if ne b a
1814k = add k 1
1815end
1816if and ne c a ne c b
1817k = add k 1
1818end
1819ret k
1820end
1821if eq cid 310
1822ret add add and a 1 and b 1 and c 1
1823end
1824if eq cid 311
1825ret or or and a b and a c and b c
1826end
1827if eq cid 312
1828ret xor xor a b c
1829end
1830if eq cid 313
1831ret or and a c sub b and b c
1832end
1833if eq cid 314
1834ret div add add a b c 3
1835end
1836if eq cid 315
1837x = a
1838y = b
1839while ne y 0 bound 128
1840t = rem x y
1841x = y
1842y = t
1843end
1844y = c
1845while ne y 0 bound 128
1846t = rem x y
1847x = y
1848y = t
1849end
1850ret x
1851end
1852if eq cid 316
1853if or eq a b eq a c
1854ret a
1855end
1856if eq b c
1857ret b
1858end
1859ret 0
1860end
1861if eq cid 317
1862if eq a b
1863ret c
1864end
1865if eq a c
1866ret b
1867end
1868ret a
1869end
1870if eq cid 318
1871ret add mul a b c
1872end
1873if eq cid 319
1874r = ulong 1
1875if eq b a
1876r = add r 1
1877end
1878if eq c a
1879r = add r 1
1880end
1881ret r
1882end
1883if eq cid 320
1884if eq c 0
1885ret a
1886end
1887ret b
1888end
1889if eq cid 321
1890if eq and c 1 0
1891ret add a b
1892end
1893ret mul a b
1894end
1895if eq cid 322
1896r = ulong 0
1897if gt a c0
1898r = add r 1
1899end
1900if gt b c0
1901r = add r 1
1902end
1903if gt c c0
1904r = add r 1
1905end
1906ret r
1907end
1908ret 999999999
1909end
1910
1911note Emit fold spec for cid in 400..454 on 4 fixed diverse arrays:
1912note A1=[3,7,2,9] n=4, A2=[10,5,20] n=3, A3=[1,2,3,4,5] n=5, A4=[0,4,0,8] n=4
1913global faA ulong 32
1914global faN ulong 4
1915
1916fn initFoldArrays to void
1917put faN 0 4
1918put faA 0 3
1919put faA 1 7
1920put faA 2 2
1921put faA 3 9
1922put faN 1 3
1923put faA 8 10
1924put faA 9 5
1925put faA 10 20
1926put faN 2 5
1927put faA 16 1
1928put faA 17 2
1929put faA 18 3
1930put faA 19 4
1931put faA 20 5
1932put faN 3 4
1933put faA 24 0
1934put faA 25 4
1935put faA 26 4
1936put faA 27 0
1937end
1938
1939fn evalFoldFixed cid ulong ci ulong to ulong
1940n = get faN ci
1941base = mul ci 8
1942if eq cid 400
1943acc = ulong 0
1944for i ulong n bound 8
1945acc = add acc get faA add base i
1946end
1947ret acc
1948end
1949if eq cid 401
1950acc = ulong 1
1951for i ulong n bound 8
1952acc = mul acc get faA add base i
1953end
1954ret acc
1955end
1956if eq cid 402
1957acc = ulong 0
1958for i ulong n bound 8
1959v = get faA add base i
1960if gt v acc
1961acc = v
1962end
1963end
1964ret acc
1965end
1966if eq cid 403
1967acc = get faA base
1968for i ulong n bound 8
1969v = get faA add base i
1970if lt v acc
1971acc = v
1972end
1973end
1974ret acc
1975end
1976if eq cid 404
1977acc = ulong 0
1978for i ulong n bound 8
1979acc = xor acc get faA add base i
1980end
1981ret acc
1982end
1983if eq cid 405
1984acc = ulong 0
1985for i ulong n bound 8
1986acc = or acc get faA add base i
1987end
1988ret acc
1989end
1990if eq cid 406
1991acc = get faA base
1992for i ulong n bound 8
1993acc = and acc get faA add base i
1994end
1995ret acc
1996end
1997if eq cid 407
1998acc = ulong 0
1999for i ulong n bound 8
2000if eq and get faA add base i 1 0
2001acc = add acc 1
2002end
2003end
2004ret acc
2005end
2006if eq cid 408
2007acc = ulong 0
2008for i ulong n bound 8
2009if ne and get faA add base i 1 0
2010acc = add acc 1
2011end
2012end
2013ret acc
2014end
2015if eq cid 409
2016acc = ulong 0
2017for i ulong n bound 8
2018if ne get faA add base i 0
2019acc = add acc 1
2020end
2021end
2022ret acc
2023end
2024if eq cid 410
2025acc = ulong 0
2026for i ulong n bound 8
2027if eq get faA add base i 0
2028acc = add acc 1
2029end
2030end
2031ret acc
2032end
2033if eq cid 411
2034acc = ulong 0
2035for i ulong n bound 8
2036v = get faA add base i
2037acc = add acc mul v v
2038end
2039ret acc
2040end
2041if eq cid 412
2042acc = ulong 0
2043for i ulong n bound 8
2044v = get faA add base i
2045if eq and v 1 0
2046acc = add acc v
2047end
2048end
2049ret acc
2050end
2051if eq cid 413
2052acc = ulong 0
2053for i ulong n bound 8
2054v = get faA add base i
2055if ne and v 1 0
2056acc = add acc v
2057end
2058end
2059ret acc
2060end
2061if eq cid 414
2062lo = get faA base
2063hi = lo
2064for i ulong n bound 8
2065v = get faA add base i
2066if lt v lo
2067lo = v
2068end
2069if gt v hi
2070hi = v
2071end
2072end
2073ret sub hi lo
2074end
2075if eq cid 415
2076bi = ulong 0
2077bv = get faA base
2078for i ulong n bound 8
2079v = get faA add base i
2080if gt v bv
2081bv = v
2082bi = i
2083end
2084end
2085ret bi
2086end
2087if eq cid 416
2088bi = ulong 0
2089bv = get faA base
2090for i ulong n bound 8
2091v = get faA add base i
2092if lt v bv
2093bv = v
2094bi = i
2095end
2096end
2097ret bi
2098end
2099if eq cid 417
2100i = ulong 1
2101while lt i n bound 8
2102if lt get faA add base i get faA add base sub i 1
2103ret 0
2104end
2105i = add i 1
2106end
2107ret 1
2108end
2109if eq cid 418
2110i = ulong 1
2111while lt i n bound 8
2112if le get faA add base i get faA add base sub i 1
2113ret 0
2114end
2115i = add i 1
2116end
2117ret 1
2118end
2119if eq cid 419
2120v0 = get faA base
2121for i ulong n bound 8
2122if ne get faA add base i v0
2123ret 0
2124end
2125end
2126ret 1
2127end
2128if eq cid 420
2129for i ulong n bound 8
2130v = get faA add base i
2131if ne v 0
2132ret v
2133end
2134end
2135ret 0
2136end
2137if eq cid 421
2138r = ulong 0
2139for i ulong n bound 8
2140v = get faA add base i
2141if ne v 0
2142r = v
2143end
2144end
2145ret r
2146end
2147if eq cid 422
2148acc = ulong 0
2149for i ulong n bound 8
2150acc = add acc mul i get faA add base i
2151end
2152ret acc
2153end
2154if eq cid 423
2155s = ulong 0
2156i = ulong 1
2157while lt i n bound 8
2158x = get faA add base i
2159y = get faA add base sub i 1
2160if ge x y
2161s = add s sub x y
2162else
2163s = add s sub y x
2164end
2165i = add i 1
2166end
2167ret s
2168end
2169if eq cid 424
2170m = ulong 0
2171i = ulong 1
2172while lt i n bound 8
2173x = get faA add base i
2174y = get faA add base sub i 1
2175d = ulong 0
2176if ge x y
2177d = sub x y
2178else
2179d = sub y x
2180end
2181if gt d m
2182m = d
2183end
2184i = add i 1
2185end
2186ret m
2187end
2188if eq cid 425
2189c = ulong 0
2190i = ulong 1
2191while lt i n bound 8
2192if gt get faA add base i get faA add base sub i 1
2193c = add c 1
2194end
2195i = add i 1
2196end
2197ret c
2198end
2199if eq cid 426
2200c = ulong 1
2201m = get faA base
2202i = ulong 1
2203while lt i n bound 8
2204v = get faA add base i
2205if gt v m
2206m = v
2207c = add c 1
2208end
2209i = add i 1
2210end
2211ret c
2212end
2213if eq cid 427
2214m1 = ulong 0
2215m2 = ulong 0
2216for i ulong n bound 8
2217v = get faA add base i
2218if gt v m1
2219m2 = m1
2220m1 = v
2221else
2222if and gt v m2 lt v m1
2223m2 = v
2224end
2225end
2226end
2227ret m2
2228end
2229if eq cid 428
2230best = ulong 0
2231cur = ulong 0
2232for i ulong n bound 8
2233if ne get faA add base i 0
2234cur = add cur 1
2235if gt cur best
2236best = cur
2237end
2238else
2239cur = 0
2240end
2241end
2242ret best
2243end
2244kv = get fmNums 0
2245if eq cid 450
2246acc = ulong 0
2247for i ulong n bound 8
2248if gt get faA add base i kv
2249acc = add acc 1
2250end
2251end
2252ret acc
2253end
2254if eq cid 451
2255acc = ulong 0
2256for i ulong n bound 8
2257if lt get faA add base i kv
2258acc = add acc 1
2259end
2260end
2261ret acc
2262end
2263if eq cid 452
2264acc = ulong 0
2265for i ulong n bound 8
2266if eq get faA add base i kv
2267acc = add acc 1
2268end
2269end
2270ret acc
2271end
2272if eq cid 453
2273acc = ulong 0
2274for i ulong n bound 8
2275if ge get faA add base i kv
2276acc = add acc 1
2277end
2278end
2279ret acc
2280end
2281if eq cid 454
2282if eq kv 0
2283ret 999999999
2284end
2285acc = ulong 0
2286for i ulong n bound 8
2287if eq rem get faA add base i kv 0
2288acc = add acc 1
2289end
2290end
2291ret acc
2292end
2293ret evalFoldFixed2 cid ci
2294end
2295
2296fn evalFoldFixed2 cid ulong ci ulong to ulong
2297if le cid 436
2298ret evalFoldFixed2A cid ci
2299end
2300ret evalFoldFixed2B cid ci
2301end
2302
2303fn evalFoldFixed2A cid ulong ci ulong to ulong
2304n = get faN ci
2305base = mul ci 8
2306if eq cid 430
2307if eq n 0
2308ret 0
2309end
2310ret get faA add base sub n 1
2311end
2312if eq cid 431
2313acc = ulong 0
2314for i ulong n bound 8
2315if eq and i 1 0
2316acc = add acc get faA add base i
2317end
2318end
2319ret acc
2320end
2321if eq cid 432
2322acc = ulong 0
2323for i ulong n bound 8
2324if eq and i 1 0
2325acc = add acc get faA add base i
2326else
2327acc = sub acc get faA add base i
2328end
2329end
2330ret acc
2331end
2332if eq cid 433
2333if eq n 0
2334ret 0
2335end
2336v0 = get faA base
2337c = ulong 0
2338for i ulong n bound 8
2339if eq get faA add base i v0
2340c = add c 1
2341end
2342end
2343ret c
2344end
2345if eq cid 434
2346if eq n 0
2347ret 0
2348end
2349v0 = get faA base
2350c = ulong 0
2351for i ulong n bound 8
2352if lt get faA add base i v0
2353c = add c 1
2354end
2355end
2356ret c
2357end
2358if eq cid 435
2359c = ulong 0
2360for i ulong n bound 8
2361if eq get faA add base i i
2362c = add c 1
2363end
2364end
2365ret c
2366end
2367if eq cid 436
2368c = ulong 0
2369for i ulong n bound 8
2370seen = ulong 0
2371for j ulong i bound 8
2372if eq get faA add base j get faA add base i
2373seen = 1
2374end
2375end
2376if eq seen 0
2377c = add c 1
2378end
2379end
2380ret c
2381end
2382ret 999999999
2383end
2384
2385fn evalFoldFixed2B cid ulong ci ulong to ulong
2386n = get faN ci
2387base = mul ci 8
2388kv = get fmNums 0
2389if eq cid 437
2390for i ulong n bound 8
2391for j ulong i bound 8
2392if eq get faA add base j get faA add base i
2393ret 1
2394end
2395end
2396end
2397ret 0
2398end
2399if eq cid 438
2400r = n
2401for i ulong n bound 8
2402if ne and get faA add base i 1 0
2403r = i
2404end
2405end
2406ret r
2407end
2408if eq cid 439
2409if eq n 0
2410ret 0
2411end
2412best = ulong 1
2413cur = ulong 1
2414i = ulong 1
2415while lt i n bound 8
2416if eq get faA add base i get faA add base sub i 1
2417cur = add cur 1
2418if gt cur best
2419best = cur
2420end
2421else
2422cur = 1
2423end
2424i = add i 1
2425end
2426ret best
2427end
2428if eq cid 440
2429g = ulong 0
2430for i ulong n bound 8
2431x = g
2432y = get faA add base i
2433while ne y 0 bound 128
2434t = rem x y
2435x = y
2436y = t
2437end
2438g = x
2439end
2440ret g
2441end
2442if eq cid 441
2443se = ulong 0
2444so = ulong 0
2445for i ulong n bound 8
2446if eq and i 1 0
2447se = add se get faA add base i
2448else
2449so = add so get faA add base i
2450end
2451end
2452if lt se so
2453ret 0
2454end
2455ret sub se so
2456end
2457if eq cid 442
2458for i ulong n bound 8
2459if eq get faA add base i 0
2460ret i
2461end
2462end
2463ret n
2464end
2465if eq cid 458
2466for i ulong n bound 8
2467v = get faA add base i
2468if gt v kv
2469ret v
2470end
2471end
2472ret 0
2473end
2474ret 999999999
2475end
2476
2477note Rewrite spec emitter on 3 fixed 8-element arrays
2478global frTmp ulong 8
2479
2480fn applyRew cid ulong n ulong kv ulong to void
2481if eq cid 500
2482i = ulong 0
2483while lt mul i 2 n bound 8
2484j = sub sub n 1 i
2485t = get frTmp i
2486put frTmp i get frTmp j
2487put frTmp j t
2488i = add i 1
2489end
2490ret
2491end
2492if eq cid 501
2493for i ulong n bound 8
2494for j ulong n bound 8
2495if and gt j i lt get frTmp j get frTmp i
2496t = get frTmp i
2497put frTmp i get frTmp j
2498put frTmp j t
2499end
2500end
2501end
2502ret
2503end
2504if eq cid 502
2505for i ulong n bound 8
2506for j ulong n bound 8
2507if and gt j i gt get frTmp j get frTmp i
2508t = get frTmp i
2509put frTmp i get frTmp j
2510put frTmp j t
2511end
2512end
2513end
2514ret
2515end
2516if eq cid 503
2517s = ulong 0
2518for i ulong n bound 8
2519s = add s get frTmp i
2520put frTmp i s
2521end
2522ret
2523end
2524if eq cid 504
2525m = ulong 0
2526for i ulong n bound 8
2527v = get frTmp i
2528if gt v m
2529m = v
2530end
2531put frTmp i m
2532end
2533ret
2534end
2535if eq cid 505
2536for i ulong n bound 8
2537put frTmp i mul get frTmp i 2
2538end
2539ret
2540end
2541if eq cid 506
2542for i ulong n bound 8
2543put frTmp i div get frTmp i 2
2544end
2545ret
2546end
2547if eq cid 507
2548for i ulong n bound 8
2549v = get frTmp i
2550put frTmp i mul v v
2551end
2552ret
2553end
2554if eq cid 508
2555for i ulong n bound 8
2556if eq and get frTmp i 1 0
2557put frTmp i 0
2558end
2559end
2560ret
2561end
2562if eq cid 509
2563for i ulong n bound 8
2564if ne and get frTmp i 1 0
2565put frTmp i 0
2566end
2567end
2568ret
2569end
2570if eq cid 510
2571if gt n 1
2572f = get frTmp 0
2573i = ulong 1
2574while lt i n bound 8
2575put frTmp sub i 1 get frTmp i
2576i = add i 1
2577end
2578put frTmp sub n 1 f
2579end
2580ret
2581end
2582if eq cid 511
2583if gt n 1
2584l = get frTmp sub n 1
2585i = sub n 1
2586while gt i 0 bound 8
2587put frTmp i get frTmp sub i 1
2588i = sub i 1
2589end
2590put frTmp 0 l
2591end
2592ret
2593end
2594if eq cid 512
2595if gt n 0
2596i = ulong 1
2597while lt i n bound 8
2598put frTmp sub i 1 get frTmp i
2599i = add i 1
2600end
2601put frTmp sub n 1 0
2602end
2603ret
2604end
2605if eq cid 513
2606if gt n 0
2607i = sub n 1
2608while gt i 0 bound 8
2609put frTmp i get frTmp sub i 1
2610i = sub i 1
2611end
2612put frTmp 0 0
2613end
2614ret
2615end
2616if eq cid 514
2617i = ulong 0
2618while lt add i 1 n bound 8
2619t = get frTmp i
2620put frTmp i get frTmp add i 1
2621put frTmp add i 1 t
2622i = add i 2
2623end
2624ret
2625end
2626if eq cid 515
2627w = ulong 0
2628for i ulong n bound 8
2629v = get frTmp i
2630if ne v 0
2631put frTmp w v
2632w = add w 1
2633end
2634end
2635while lt w n bound 8
2636put frTmp w 0
2637w = add w 1
2638end
2639ret
2640end
2641if eq cid 518
2642x = ulong 0
2643for i ulong n bound 8
2644x = xor x get frTmp i
2645put frTmp i x
2646end
2647ret
2648end
2649for i ulong n bound 8
2650v = get frTmp i
2651if eq cid 550
2652put frTmp i add v kv
2653end
2654if eq cid 551
2655if le v kv
2656put frTmp i 0
2657else
2658put frTmp i sub v kv
2659end
2660end
2661if eq cid 552
2662put frTmp i mul v kv
2663end
2664if eq cid 555
2665if gt v kv
2666put frTmp i kv
2667end
2668end
2669if eq cid 556
2670if lt v kv
2671put frTmp i kv
2672end
2673end
2674if eq cid 557
2675if lt v kv
2676put frTmp i 0
2677end
2678end
2679if eq cid 558
2680if gt v kv
2681put frTmp i 0
2682end
2683end
2684if eq cid 559
2685if eq v 0
2686put frTmp i kv
2687end
2688end
2689if eq cid 560
2690put frTmp i kv
2691end
2692end
2693end
2694
2695fn emitRewArr to void
2696for k ulong 8 bound 8
2697if gt k 0
2698foCh 32
2699end
2700foNum get frTmp k
2701end
2702end
2703
2704fn fmEmitRew cid ulong to bool
2705kv = get fmNums 0
2706foClr
2707foTxt oArity
2708foNum 5
2709foCh 10
2710foTxt oConsts
2711foNum 0
2712foCh 32
2713foNum 1
2714foCh 32
2715foNum 2
2716nn = get fmSt 4
2717for k ulong nn bound 16
2718foCh 32
2719foNum get fmNums k
2720end
2721foCh 10
2722note Case 1: n=4 [3,7,2,9,5,1,8,4]
2723foTxt tRew1
2724put frTmp 0 3
2725put frTmp 1 7
2726put frTmp 2 2
2727put frTmp 3 9
2728put frTmp 4 5
2729put frTmp 5 1
2730put frTmp 6 8
2731put frTmp 7 4
2732applyRew cid 4 kv
2733emitRewArr
2734note Case 2: n=5 [10,0,5,0,20,3,6,1]
2735foTxt tRew2
2736put frTmp 0 10
2737put frTmp 1 0
2738put frTmp 2 5
2739put frTmp 3 0
2740put frTmp 4 20
2741put frTmp 5 3
2742put frTmp 6 6
2743put frTmp 7 1
2744applyRew cid 5 kv
2745emitRewArr
2746note Case 3: n=6 [1,2,3,4,5,6,7,8]
2747foTxt tRew3
2748put frTmp 0 1
2749put frTmp 1 2
2750put frTmp 2 3
2751put frTmp 3 4
2752put frTmp 4 5
2753put frTmp 5 6
2754put frTmp 6 7
2755put frTmp 7 8
2756applyRew cid 6 kv
2757emitRewArr
2758foCh 10
2759ret true
2760end
2761
2762fn fmEmitFold cid ulong to bool
2763initFoldArrays
2764y0 = evalFoldFixed cid 0
2765y1 = evalFoldFixed cid 1
2766y2 = evalFoldFixed cid 2
2767y3 = evalFoldFixed cid 3
2768if or eq y0 999999999 eq y1 999999999
2769ret false
2770end
2771foClr
2772foTxt oArity
2773foNum 4
2774foCh 10
2775foTxt oConsts
2776foNum 0
2777foCh 32
2778foNum 1
2779foCh 32
2780foNum 2
2781nn = get fmSt 4
2782for k ulong nn bound 16
2783foCh 32
2784foNum get fmNums k
2785end
2786foCh 10
2787foTxt tFoldA1
2788foNum y0
2789foTxt tFoldA2
2790foNum y1
2791foTxt tFoldA3
2792foNum y2
2793foTxt tFoldA4
2794foNum y3
2795foCh 10
2796ret true
2797end
2798
2799fn fmNeedsDep2 cid ulong to bool
2800if or eq cid 101 or eq cid 102 eq cid 104
2801ret true
2802end
2803if or eq cid 106 or eq cid 108 eq cid 109
2804ret true
2805end
2806if or eq cid 204 eq cid 205
2807ret true
2808end
2809if and ge cid 220 le cid 234
2810ret true
2811end
2812ret false
2813end
2814
2815fn fmEmitScalarSpec cid ulong ar ulong to void
2816foClr
2817foTxt oArity
2818foNum ar
2819foCh 10
2820foTxt oDepth
2821if fmNeedsDep2 cid
2822foNum 2
2823else
2824foNum 1
2825end
2826foCh 10
2827foTxt oConsts
2828foNum 0
2829foCh 32
2830foNum 1
2831foCh 32
2832foNum 2
2833foCh 32
2834foNum 3
2835nn = get fmSt 4
2836for k ulong nn bound 16
2837foCh 32
2838foNum get fmNums k
2839end
2840foCh 10
2841for i ulong 8 bound 8
2842foTxt oCase
2843foNum get fmInA i
2844foCh 32
2845if ge ar 2
2846foNum get fmInB i
2847foCh 32
2848end
2849if ge ar 3
2850foNum get fmInC i
2851foCh 32
2852end
2853foNum get fmP1 i
2854foCh 10
2855end
2856end
2857
2858fn fmBuildSpec cid ulong to bool
2859if ge cid 500
2860ret fmEmitRew cid
2861end
2862if ge cid 400
2863ret fmEmitFold cid
2864end
2865ar = get fmSt 3
2866if eq ar 1
2867put fmInA 0 0
2868put fmInA 1 1
2869put fmInA 2 2
2870put fmInA 3 5
2871put fmInA 4 12
2872put fmInA 5 35
2873put fmInA 6 120
2874if eq cid 105
2875put fmInA 7 1500
2876else
2877put fmInA 7 18
2878end
2879end
2880if eq ar 2
2881put fmInA 0 0
2882put fmInB 0 0
2883put fmInA 1 3
2884put fmInB 1 7
2885put fmInA 2 9
2886put fmInB 2 2
2887put fmInA 3 5
2888put fmInB 3 5
2889put fmInA 4 10
2890put fmInB 4 25
2891put fmInA 5 40
2892put fmInB 5 12
2893put fmInA 6 1
2894put fmInB 6 10
2895put fmInA 7 24
2896put fmInB 7 9
2897end
2898if eq ar 3
2899put fmInA 0 5
2900put fmInB 0 10
2901put fmInC 0 20
2902put fmInA 1 15
2903put fmInB 1 10
2904put fmInC 1 20
2905put fmInA 2 25
2906put fmInB 2 10
2907put fmInC 2 20
2908put fmInA 3 10
2909put fmInB 3 10
2910put fmInC 3 20
2911put fmInA 4 0
2912put fmInB 4 0
2913put fmInC 4 0
2914put fmInA 5 9
2915put fmInB 5 2
2916put fmInC 5 5
2917put fmInA 6 1
2918put fmInB 6 8
2919put fmInC 6 3
2920put fmInA 7 7
2921put fmInB 7 4
2922put fmInC 7 4
2923end
2924
2925for i ulong 8 bound 8
2926v = ulong 999999999
2927if eq ar 1
2928if lt cid 117
2929v = evalU1 cid get fmInA i
2930else
2931v = evalU2 cid get fmInA i
2932end
2933end
2934if eq ar 2
2935v = evalBin cid get fmInA i get fmInB i
2936end
2937if eq ar 3
2938v = evalTern cid get fmInA i get fmInB i get fmInC i
2939end
2940if eq v 999999999
2941ret false
2942end
2943put fmP1 i v
2944end
2945fmEmitScalarSpec cid ar
2946ret true
2947end
2948
2949fn fmLoadStr s view uchar * to ulong
2950n = len s
2951for i ulong n bound 4096
2952put fmBuf i get s i
2953end
2954ret n
2955end
2956
2957fn checkSelfCase s view uchar * expCid ulong to bool
2958n = fmLoadStr s
2959fmParsePrompt n
2960c = fmPass1
2961if ne c expCid
2962ret false
2963end
2964if eq expCid 0
2965ret true
2966end
2967ret fmBuildSpec c
2968end
2969
2970fn runSelfcheck to int
2971if or fmMultiple 0 0 or fmMultiple 15 0 or not fmMultiple 0 3 or not fmMultiple 15 3 fmMultiple 14 3
2972fwStr 2 tSelfFail
2973fwNum 2 8
2974fwStr 2 tNl
2975ret 1
2976end
2977if not checkSelfCase tTestP1 101
2978fwStr 2 tSelfFail
2979fwNum 2 1
2980fwStr 2 tNl
2981ret 1
2982end
2983if not checkSelfCase tTestP2 201
2984fwStr 2 tSelfFail
2985fwNum 2 2
2986fwStr 2 tNl
2987ret 1
2988end
2989if not checkSelfCase tTestP3 210
2990fwStr 2 tSelfFail
2991fwNum 2 3
2992fwStr 2 tNl
2993ret 1
2994end
2995if not checkSelfCase tTestP4 117
2996fwStr 2 tSelfFail
2997fwNum 2 4
2998fwStr 2 tNl
2999ret 1
3000end
3001if not checkSelfCase tTestP5 415
3002fwStr 2 tSelfFail
3003fwNum 2 5
3004fwStr 2 tNl
3005ret 1
3006end
3007if not checkSelfCase tTestP6 500
3008fwStr 2 tSelfFail
3009fwNum 2 6
3010fwStr 2 tNl
3011ret 1
3012end
3013if not checkSelfCase tTestP7 0
3014fwStr 2 tSelfFail
3015fwNum 2 7
3016fwStr 2 tNl
3017ret 1
3018end
3019fwStr 1 tSelfOk
3020ret 0
3021end
3022
3023fn main to int
3024if lt argcnt 2
3025fwStr 2 tUsage
3026ret 2
3027end
3028mb = new uchar 64 0
3029ml = argread 1 mb 0 64
3030if le ml 0
3031fwStr 2 tUsage
3032ret 2
3033end
3034if feqTxt mb ulong ml mSelf
3035ret runSelfcheck
3036end
3037if not feqTxt mb ulong ml mForm
3038fwStr 2 tUsage
3039ret 2
3040end
3041if lt argcnt 4
3042fwStr 2 tUsage
3043ret 2
3044end
3045pl = argread 2 fmPath 0 4095
3046if le pl 0
3047fwStr 2 tErrIo
3048ret 1
3049end
3050fd = fopen fmPath 0 ulong pl 0 0
3051if lt fd 0
3052fwStr 2 tErrIo
3053ret 1
3054end
3055kn = read int fd fmBuf 0 16383
3056z = fclose int fd
3057if le kn 0
3058fwStr 2 tErrIo
3059ret 1
3060end
3061fmParsePrompt ulong kn
3062cid = fmPass1
3063if eq cid 0
3064fwStr 1 tAbstain
3065ret 1
3066end
3067if not fmBuildSpec cid
3068fwStr 1 tAbstain
3069ret 1
3070end
3071pl2 = argread 3 fmPath 0 4095
3072if le pl2 0
3073fwStr 2 tErrIo
3074ret 1
3075end
3076fd2 = fopen fmPath 0 ulong pl2 577 420
3077if lt fd2 0
3078fwStr 2 tErrIo
3079ret 1
3080end
3081n = get fmSt 10
3082o = ulong 0
3083while lt o n bound 16384
3084k = write int fd2 fmOut o sub n o
3085if le k 0
3086break
3087end
3088o = add o ulong k
3089end
3090z2 = fclose int fd2
3091fwStr 1 tAgree
3092fwNum 1 cid
3093fwStr 1 tNl
3094ret 0
3095end