@mark1russell7/vex-compiler 0.18.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.d.ts +3 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +163 -32
- package/build/index.js.map +1 -1
- package/build/{memo-08WGBHWI.js → memo-J-Km2qc0.js} +2 -2
- package/build/{memo-08WGBHWI.js.map → memo-J-Km2qc0.js.map} +1 -1
- package/build/module-entry.d.ts +5 -0
- package/build/module-entry.d.ts.map +1 -1
- package/build/module-entry.js +644 -607
- package/build/module-entry.js.map +1 -1
- package/package.json +5 -5
- package/src/lower.ts +139 -19
- package/src/module-tidy.ts +47 -36
- package/src/module.ts +697 -662
package/src/module.ts
CHANGED
|
@@ -514,664 +514,699 @@ type GraphHelper = "GR" | "GI" | "RB";
|
|
|
514
514
|
/** The helpers of a sheet (COMPILE.MODULE.SHEET): the state of a run of a sheet, and the order of the keys of a tree. */
|
|
515
515
|
type SheetHelper = "SH" | "KO" | "DO" | "LO" | "LC" | "LV" | "LCP";
|
|
516
516
|
|
|
517
|
-
/**
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
"function
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
"
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
"}",
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
"
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
"function
|
|
629
|
-
"
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
"
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
"
|
|
654
|
-
"
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
"
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
"
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
]
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
"
|
|
721
|
-
"}",
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
"
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
"
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
"
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
"
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
"
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
"
|
|
782
|
-
|
|
783
|
-
"
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
"
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
"
|
|
866
|
-
"
|
|
867
|
-
"
|
|
868
|
-
|
|
869
|
-
"
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
"
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
"
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
"function
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
"
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
"
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
"
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
"function
|
|
992
|
-
|
|
993
|
-
"
|
|
994
|
-
"
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
"
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
"
|
|
1024
|
-
"
|
|
1025
|
-
|
|
1026
|
-
"
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
"
|
|
1038
|
-
|
|
1039
|
-
"
|
|
1040
|
-
|
|
1041
|
-
"
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
"
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
"
|
|
1099
|
-
"
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
"
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
"
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
517
|
+
/**
|
|
518
|
+
* The texts of a part of the helpers. The object `HELPERS` joins the parts, and its type asks for each helper. Each part is
|
|
519
|
+
* a function, and its closing line ends a segment of the cost map. Thus the mutation lane can cut the helpers into parts.
|
|
520
|
+
*/
|
|
521
|
+
type HelperTexts = Readonly<Partial<Record<Exclude<Helper, "S">, () => string>>>;
|
|
522
|
+
|
|
523
|
+
/** This function gives a part of the texts of the helpers, in the order of `HELPERS`. */
|
|
524
|
+
function helperTexts1() {
|
|
525
|
+
return {
|
|
526
|
+
F: () => "function F() { throw E; }",
|
|
527
|
+
C: () => "function C(m) { throw new VexContractError(m); }",
|
|
528
|
+
// An exception of an op is an error of Vex too, as in the interpreter. Only the checks of the contract throw a
|
|
529
|
+
// VexContractError, and they come before the run, so each catch of the run gives the sentinel.
|
|
530
|
+
T: () => "function T(f) { try { return f(); } catch { return E; } }",
|
|
531
|
+
// A constant of the run with a loop evaluates on its first read, so a loop in a branch that no run takes costs nothing.
|
|
532
|
+
L: () => "function L(f) { let done = false, v; return () => { if (!done) { done = true; try { v = f(); } catch { v = E; } } return v; }; }",
|
|
533
|
+
// A column of the run: the value of an expression at each record, on its first read.
|
|
534
|
+
Q: () => "function Q(f) { const m = []; return (j) => { let v = m[j]; if (v === undefined) { try { v = f(j); } catch { v = E; } m[j] = v; } return v; }; }",
|
|
535
|
+
G: () => "function G(x) { return x === E ? F() : x; }",
|
|
536
|
+
// The pack of a column of the run: an array with the value of the column at each record (`#packed`).
|
|
537
|
+
QA: () => "function QA(n, f) { const a = []; for (let j = 0; j < n; j++) a.push(f(j)); return a; }",
|
|
538
|
+
// A fold over the targets before the origin, as a scan (COMPILE.MODULE.SCAN). The first call evaluates the first
|
|
539
|
+
// state and the value of each target. When these values do not decrease, the targets of each origin are a prefix,
|
|
540
|
+
// and each prefix keeps its state. NaN satisfies no test, so its target is in no prefix.
|
|
541
|
+
PF: () => [
|
|
542
|
+
"function PF(n, init, key, test, step, result) {",
|
|
543
|
+
" let ts, js, a0, bad, mono, pa;",
|
|
544
|
+
" return (q) => {",
|
|
545
|
+
" if (ts === undefined) {",
|
|
546
|
+
" ts = [];",
|
|
547
|
+
" js = [];",
|
|
548
|
+
" bad = false;",
|
|
549
|
+
" try { a0 = init(); } catch { a0 = E; }",
|
|
550
|
+
" for (let j = 0; j < n; j++) {",
|
|
551
|
+
" let t;",
|
|
552
|
+
" try { t = key(j); } catch { bad = true; break; }",
|
|
553
|
+
" if (t !== t) continue;",
|
|
554
|
+
" ts.push(t);",
|
|
555
|
+
" js.push(j);",
|
|
556
|
+
" }",
|
|
557
|
+
" mono = true;",
|
|
558
|
+
" for (let i = 1; i < ts.length; i++) if (!(ts[i - 1] <= ts[i])) mono = false;",
|
|
559
|
+
" pa = [a0];",
|
|
560
|
+
" }",
|
|
561
|
+
" if (a0 === E || bad || q === E) F();",
|
|
562
|
+
" if (!mono) {",
|
|
563
|
+
" let a = a0;",
|
|
564
|
+
" for (let i = 0; i < ts.length; i++) if (test(ts[i], q)) a = step(a, js[i]);",
|
|
565
|
+
" return result(a);",
|
|
566
|
+
" }",
|
|
567
|
+
" let lo = 0, hi = ts.length;",
|
|
568
|
+
" while (lo < hi) {",
|
|
569
|
+
" const h = (lo + hi) >> 1;",
|
|
570
|
+
" if (test(ts[h], q)) lo = h + 1;",
|
|
571
|
+
" else hi = h;",
|
|
572
|
+
" }",
|
|
573
|
+
" for (let i = pa.length - 1; i < lo; i++) pa.push(step(pa[i], js[i]));",
|
|
574
|
+
" return result(pa[lo]);",
|
|
575
|
+
" };",
|
|
576
|
+
"}",
|
|
577
|
+
].join("\n"),
|
|
578
|
+
B: () => 'function B(x) { return typeof x === "boolean" ? x : F(); }',
|
|
579
|
+
U: () => 'function U(x) { return typeof x === "number" ? x : F(); }',
|
|
580
|
+
X: () => "function X(t, n) { return t >= 0 && t < n ? t : F(); }",
|
|
581
|
+
// The index of a computed position, and of a computed key: a key of an array space is the text of its index.
|
|
582
|
+
I: () => "function I(t, n) { return Number.isInteger(t) && t >= 0 && t < n ? t : F(); }",
|
|
583
|
+
K: () => 'function K(k, n) { return typeof k === "string" && /^(0|[1-9]\\d*)$/.test(k) && +k < n ? +k : F(); }',
|
|
584
|
+
// The ops on the order of a list (LIST.SORT, LIST.TAKE, LIST.NTH) work on the items with values. With strict, an
|
|
585
|
+
// error item is the error of the op. A sort is stable, and the values are numbers without NaN or texts of one kind.
|
|
586
|
+
// V gives a list without error items itself, so it is not copied (COMPILE.MODULE.LIST.SHARE). No op changes it. V does
|
|
587
|
+
// not keep the cache of VW: a property on each new list made the list ops slower (ireko, vex-compiler 0.17.1).
|
|
588
|
+
V: () => "function V(l, s) { if (!l.some((x) => x[1] === E)) return l; if (s) F(); return l.filter((x) => x[1] !== E); }",
|
|
589
|
+
SB: () => [
|
|
590
|
+
"function SB(l, s) {",
|
|
591
|
+
" const o = V(l, s);",
|
|
592
|
+
" const t = o.length > 0 ? typeof o[0][1] : \"number\";",
|
|
593
|
+
" for (const [, v] of o) if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F();",
|
|
594
|
+
" return o.slice().sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0));",
|
|
595
|
+
"}",
|
|
596
|
+
].join("\n"),
|
|
597
|
+
// The option by of sortBy (LIST.SORT.BY): the function f gives the sort value of each item with a value, in order.
|
|
598
|
+
SK: () => [
|
|
599
|
+
"function SK(l, s, f) {",
|
|
600
|
+
" const o = V(l, s).map((x) => [x, f(x[1])]);",
|
|
601
|
+
" const t = o.length > 0 ? typeof o[0][1] : \"number\";",
|
|
602
|
+
" for (const [, v] of o) if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F();",
|
|
603
|
+
" return o.sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0)).map((x) => x[0]);",
|
|
604
|
+
"}",
|
|
605
|
+
].join("\n"),
|
|
606
|
+
TK: () => "function TK(l, n, s) { const o = V(l, s); return Number.isSafeInteger(n) && n >= 0 ? o.slice(0, n) : F(); }",
|
|
607
|
+
// The items with values of a list stay on its array, as GT keeps its index. Thus a loop that reads each item of a list
|
|
608
|
+
// of n items with nth costs O(n), not O(n squared). count and first read them too (COMPILE.MODULE.NTH.INDEX).
|
|
609
|
+
// A list without error items is its own array of items with values.
|
|
610
|
+
VW: () => [
|
|
611
|
+
"function VW(l) {",
|
|
612
|
+
" let o = l.vw;",
|
|
613
|
+
" if (o === undefined) {",
|
|
614
|
+
" o = l.some((x) => x[1] === E) ? l.filter((x) => x[1] !== E) : l;",
|
|
615
|
+
" l.vw = o;",
|
|
616
|
+
" }",
|
|
617
|
+
" return o;",
|
|
618
|
+
"}",
|
|
619
|
+
].join("\n"),
|
|
620
|
+
NT: () => [
|
|
621
|
+
"function NT(l, i, s) {",
|
|
622
|
+
" const o = VW(l);",
|
|
623
|
+
" if (s && o.length < l.length) F();",
|
|
624
|
+
" return Number.isSafeInteger(i) && i >= 0 && i < o.length ? o[i][1] : F();",
|
|
625
|
+
"}",
|
|
626
|
+
].join("\n"),
|
|
627
|
+
// count and first of a list that is not an each. With strict, an error item is the error of the op.
|
|
628
|
+
CT: () => "function CT(l, s) { const o = VW(l); return s && o.length < l.length ? F() : o.length; }",
|
|
629
|
+
FS: () => "function FS(l, s) { const o = VW(l); return (s && o.length < l.length) || o.length === 0 ? F() : o[0][1]; }",
|
|
630
|
+
// fill (LIST.FILL, COMPILE.MODULE.FILL): n items with the value v, and the keys 0 to n - 1. A count that is not a whole
|
|
631
|
+
// number from 0 to 100000, or a missing value, is the error of the interpreter. Each item can share the array of a list.
|
|
632
|
+
// setNth (LIST.SETNTH, COMPILE.MODULE.SETNTH): a new array with a new pair at the position i. The item keeps its key.
|
|
633
|
+
// V removes the error items, so the position counts the items with values. The old array and its pairs do not change.
|
|
634
|
+
SN: () => [
|
|
635
|
+
"function SN(l, i, v, s) {",
|
|
636
|
+
" const x = V(l, s);",
|
|
637
|
+
" if (!(Number.isSafeInteger(i) && i >= 0 && i < x.length && v !== undefined)) F();",
|
|
638
|
+
" const o = x.slice();",
|
|
639
|
+
" o[i] = [x[i][0], v];",
|
|
640
|
+
" return o;",
|
|
641
|
+
"}",
|
|
642
|
+
].join("\n"),
|
|
643
|
+
FI: () => [
|
|
644
|
+
"function FI(n, v) {",
|
|
645
|
+
" if (!(Number.isSafeInteger(n) && n >= 0 && n <= 100000 && v !== undefined)) F();",
|
|
646
|
+
" const o = [];",
|
|
647
|
+
" for (let i = 0; i < n; i++) o.push([i, v]);",
|
|
648
|
+
" return o;",
|
|
649
|
+
"}",
|
|
650
|
+
].join("\n"),
|
|
651
|
+
// A range of fill (COMPILE.MODULE.FILL.RANGE). FN gives the count of the items of `fill(n, v)` with the check of FI.
|
|
652
|
+
// TN gives the count of `take` of a range with the check of TK. A sum with 0 changes -0 to 0, as the length does.
|
|
653
|
+
FN: () => "function FN(n, v) { return Number.isSafeInteger(n) && n >= 0 && n <= 100000 && v !== undefined ? n + 0 : F(); }",
|
|
654
|
+
TN: () => "function TN(n, k) { return Number.isSafeInteger(k) && k >= 0 ? Math.min(n, k + 0) : F(); }",
|
|
655
|
+
} satisfies HelperTexts;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/** This function gives a part of the texts of the helpers, in the order of `HELPERS`. */
|
|
659
|
+
function helperTexts2() {
|
|
660
|
+
return {
|
|
661
|
+
// A list of a field of the input (COMPILE.MODULE.LIST.INPUT): the array of a `vex.list` whose items have lists to the
|
|
662
|
+
// depth `h`, and values that pass the test `t`. The shape is the shape of `isVexList`. An error item holds the sentinel.
|
|
663
|
+
// An input that is not such a list, or an error item of `#DEPTH!`, gives undefined, so the check of the run fails.
|
|
664
|
+
LI: () => [
|
|
665
|
+
"function LI(x, h, t) {",
|
|
666
|
+
" if (typeof x !== \"object\" || x === null || x.kind !== \"vex.list\" || !Array.isArray(x.items)) return undefined;",
|
|
667
|
+
" const o = [];",
|
|
668
|
+
" for (let i = 0; i < x.items.length; i++) {",
|
|
669
|
+
" const it = x.items[i];",
|
|
670
|
+
" const r = typeof it === \"object\" && it !== null ? it.result : undefined;",
|
|
671
|
+
" if (typeof r !== \"object\" || r === null || typeof it.key !== \"string\") return undefined;",
|
|
672
|
+
" if (r.ok === true) {",
|
|
673
|
+
" const v = h > 0 ? LI(r.value, h - 1, t) : r.value !== undefined && t(r.value) ? r.value : undefined;",
|
|
674
|
+
" if (v === undefined) return undefined;",
|
|
675
|
+
" o.push([it.key, v]);",
|
|
676
|
+
" } else {",
|
|
677
|
+
" const e = r.error;",
|
|
678
|
+
" if (r.ok !== false || typeof e !== \"object\" || e === null || typeof e.code !== \"string\" || typeof e.kind !== \"string\" || typeof e.message !== \"string\" || !Array.isArray(e.path) || e.kind === \"depth\") return undefined;",
|
|
679
|
+
" o.push([it.key, E]);",
|
|
680
|
+
" }",
|
|
681
|
+
" }",
|
|
682
|
+
" return o;",
|
|
683
|
+
"}",
|
|
684
|
+
].join("\n"),
|
|
685
|
+
// A plain list of a field of the input (COMPILE.MODULE.LIST.PLAIN): an array of the values of the items, with arrays to
|
|
686
|
+
// the depth `h`. The key of an item is its position. The value VEX_ERROR is an error item, also if `t` accepts it.
|
|
687
|
+
// Another value gives undefined, so the check of the run fails.
|
|
688
|
+
LA: () => [
|
|
689
|
+
"function LA(x, h, t) {",
|
|
690
|
+
" if (!Array.isArray(x)) return undefined;",
|
|
691
|
+
" const o = [];",
|
|
692
|
+
" for (let i = 0; i < x.length; i++) {",
|
|
693
|
+
" const y = x[i];",
|
|
694
|
+
" if (y === VEX_ERROR) {",
|
|
695
|
+
" o.push([i, E]);",
|
|
696
|
+
" continue;",
|
|
697
|
+
" }",
|
|
698
|
+
" const v = h > 0 ? LA(y, h - 1, t) : y !== undefined && t(y) ? y : undefined;",
|
|
699
|
+
" if (v === undefined) return undefined;",
|
|
700
|
+
" o.push([i, v]);",
|
|
701
|
+
" }",
|
|
702
|
+
" return o;",
|
|
703
|
+
"}",
|
|
704
|
+
].join("\n"),
|
|
705
|
+
// The ops that build a list (LIST.BUILD) keep the key and the value of each item. A key is a position or a text, so
|
|
706
|
+
// `without` and `get` compare the text of each key. A list is an array that does not change after the op that makes it.
|
|
707
|
+
// The one exception is the array of AO of a loop, which no other part reads (COMPILE.MODULE.APPEND.OWN).
|
|
708
|
+
DR: () => "function DR(l, n, s) { const o = V(l, s); return Number.isSafeInteger(n) && n >= 0 ? o.slice(n) : F(); }",
|
|
709
|
+
AP: () => "function AP(l, o, s) { return V(l, s).concat(V(o, s)); }",
|
|
710
|
+
// append into the list of a name of a loop (COMPILE.MODULE.APPEND.OWN). w is the array of the last append of the name
|
|
711
|
+
// in this run of the loop, and no other part reads it. Such an array has no error items, so VW of it is the array.
|
|
712
|
+
// With fuse, w can start with a first list that is clean and fresh (COMPILE.MODULE.FUSE.ADOPT). An append in the result
|
|
713
|
+
// of the loop can use w too, when no part reads the list after it.
|
|
714
|
+
// Each other list gets the copy of AP. V(o, s) throws before the first push, so an error does not change l. The loop
|
|
715
|
+
// reads n items, so it ends also if a is l.
|
|
716
|
+
AO: () => "function AO(l, o, s, w) { if (l !== w) return AP(l, o, s); const a = V(o, s), n = a.length; for (let i = 0; i < n; i++) l.push(a[i]); return l; }",
|
|
717
|
+
// The target of an append of an owner that pushes the items of its second list (COMPILE.MODULE.APPEND.PUSH). The
|
|
718
|
+
// array w is the target. Each other list gets a copy without its error items, and with s an error item is an error.
|
|
719
|
+
// AI pushes the one item x of fill(1, x) into the target. The code of x evaluates before the call, as in the interpreter.
|
|
720
|
+
AW: () => "function AW(l, s, w) { if (l === w) return l; const a = V(l, s), o = []; for (let i = 0; i < a.length; i++) o.push(a[i]); return o; }",
|
|
721
|
+
AI: () => "function AI(l, x, s, w) { const o = AW(l, s, w); o.push(x); return o; }",
|
|
722
|
+
WO: () => [
|
|
723
|
+
"function WO(l, o, s) {",
|
|
724
|
+
" const a = V(l, s), g = new Map();",
|
|
725
|
+
" for (const [k] of V(o, s)) g.set(`${k}`, 1);",
|
|
726
|
+
" return a.filter((x) => !g.has(`${x[0]}`));",
|
|
727
|
+
"}",
|
|
728
|
+
].join("\n"),
|
|
729
|
+
FL: () => [
|
|
730
|
+
"function FL(l, s) {",
|
|
731
|
+
" const o = [];",
|
|
732
|
+
" for (const [, v] of V(l, s)) for (const x of V(v, s)) o.push(x);",
|
|
733
|
+
" return o;",
|
|
734
|
+
"}",
|
|
735
|
+
].join("\n"),
|
|
736
|
+
// The index of the keys of a list (LIST.GET): the value of the first item of each key. The array keeps its index, so a
|
|
737
|
+
// step that reads each key of a list of n items costs O(n). The key E marks an error item, for strict.
|
|
738
|
+
// The array of a loop gets a push after a read (COMPILE.MODULE.APPEND.LET), so `n` counts the items in the index.
|
|
739
|
+
// A push only adds items at the end, so the index adds only the new items. No other change of an array has a read before it.
|
|
740
|
+
GT: () => [
|
|
741
|
+
"function GT(l, k, s) {",
|
|
742
|
+
" let x = l.ix;",
|
|
743
|
+
" if (x === undefined) {",
|
|
744
|
+
" x = new Map();",
|
|
745
|
+
" x.n = 0;",
|
|
746
|
+
" l.ix = x;",
|
|
747
|
+
" }",
|
|
748
|
+
" if (x.n !== l.length) {",
|
|
749
|
+
" for (let i = x.n; i < l.length; i++) {",
|
|
750
|
+
" const [q, v] = l[i], t = `${q}`;",
|
|
751
|
+
" if (v === E) x.set(E, E);",
|
|
752
|
+
" else if (!x.has(t)) x.set(t, v);",
|
|
753
|
+
" }",
|
|
754
|
+
" x.n = l.length;",
|
|
755
|
+
" }",
|
|
756
|
+
' return (s && x.has(E)) || typeof k !== "string" || !x.has(k) ? F() : x.get(k);',
|
|
757
|
+
"}",
|
|
758
|
+
].join("\n"),
|
|
759
|
+
// The position of the first value among the items with a value (LIST.INDEXOF). `===` compares the values.
|
|
760
|
+
IX: () => [
|
|
761
|
+
"function IX(l, x, s) {",
|
|
762
|
+
" const o = V(l, s);",
|
|
763
|
+
' if (typeof x !== "number" && typeof x !== "string" && typeof x !== "boolean") F();',
|
|
764
|
+
" for (let i = 0; i < o.length; i++) if (o[i][1] === x) return i;",
|
|
765
|
+
" return F();",
|
|
766
|
+
"}",
|
|
767
|
+
].join("\n"),
|
|
768
|
+
// The keys of a run (COMPILE.MODULE.KEYS): a map of each key to its position. A key that is not a text, or a second
|
|
769
|
+
// use of a key, is against the contract, because a space has no such keys.
|
|
770
|
+
KC: () => [
|
|
771
|
+
"function KC(a, n) {",
|
|
772
|
+
' if (!Array.isArray(a) || a.length !== n) C("the keys are not an array with one key for each record");',
|
|
773
|
+
" const m = new Map();",
|
|
774
|
+
" for (let i = 0; i < n; i++) {",
|
|
775
|
+
" const k = a[i];",
|
|
776
|
+
' if (typeof k !== "string" || m.has(k)) C(`the key ${i} is not a text of its own`);',
|
|
777
|
+
" m.set(k, i);",
|
|
778
|
+
" }",
|
|
779
|
+
" return m;",
|
|
780
|
+
"}",
|
|
781
|
+
].join("\n"),
|
|
782
|
+
// The position of a key of a move. A value that is not a key of the space is an error of the move (NAV.KEY).
|
|
783
|
+
KP: () => "function KP(m, k) { const i = m.get(k); return i === undefined ? F() : i; }",
|
|
784
|
+
} satisfies HelperTexts;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
/** This function gives a part of the texts of the helpers, in the order of `HELPERS`. */
|
|
788
|
+
function helperTexts3() {
|
|
789
|
+
return {
|
|
790
|
+
// The parents of a tree space: the position of the parent of each record, or -1 for a root. A walk up from each record
|
|
791
|
+
// marks its keys, so a walk that meets a key of the same walk finds a cycle. Each key is in one walk, so the check is linear.
|
|
792
|
+
PT: () => [
|
|
793
|
+
"function PT(p, n) {",
|
|
794
|
+
' if (!Array.isArray(p) || p.length !== n) C("the parents are not an array with one parent for each record");',
|
|
795
|
+
" const P = new Int32Array(n), s = new Uint8Array(n);",
|
|
796
|
+
" for (let i = 0; i < n; i++) {",
|
|
797
|
+
" const x = p[i];",
|
|
798
|
+
" if (x !== -1 && !(Number.isInteger(x) && x >= 0 && x < n)) C(`the parent of the record ${i} is not -1 or the position of a record`);",
|
|
799
|
+
" P[i] = x;",
|
|
800
|
+
" }",
|
|
801
|
+
" for (let i = 0; i < n; i++) {",
|
|
802
|
+
" let k = i;",
|
|
803
|
+
" while (k >= 0 && s[k] === 0) {",
|
|
804
|
+
" s[k] = 1;",
|
|
805
|
+
" k = P[k];",
|
|
806
|
+
" }",
|
|
807
|
+
" if (k >= 0 && s[k] === 1) C(`the parents of the record ${i} make a cycle`);",
|
|
808
|
+
" for (k = i; k >= 0 && s[k] === 1; k = P[k]) s[k] = 2;",
|
|
809
|
+
" }",
|
|
810
|
+
" return P;",
|
|
811
|
+
"}",
|
|
812
|
+
].join("\n"),
|
|
813
|
+
// The children of each record and the roots, in key order.
|
|
814
|
+
CH: () => [
|
|
815
|
+
"function CH(P, n) {",
|
|
816
|
+
" const h = [], r = [];",
|
|
817
|
+
" for (let i = 0; i < n; i++) h.push([]);",
|
|
818
|
+
" for (let i = 0; i < n; i++) (P[i] < 0 ? r : h[P[i]]).push(i);",
|
|
819
|
+
" return [h, r];",
|
|
820
|
+
"}",
|
|
821
|
+
].join("\n"),
|
|
822
|
+
// The siblings of each record with the record itself, and its position among them. The roots are the siblings of a root.
|
|
823
|
+
SG: () => [
|
|
824
|
+
"function SG(P, h, r, n) {",
|
|
825
|
+
" const g = [], s = new Int32Array(n);",
|
|
826
|
+
" for (let i = 0; i < n; i++) g.push(P[i] < 0 ? r : h[P[i]]);",
|
|
827
|
+
" for (const x of [r, ...h]) for (let q = 0; q < x.length; q++) s[x[q]] = q;",
|
|
828
|
+
" return [g, s];",
|
|
829
|
+
"}",
|
|
830
|
+
].join("\n"),
|
|
831
|
+
// The keys depth first, with each parent before its children, and the position and the size of the subtree of each
|
|
832
|
+
// record. Thus the descendants of a record are the keys after it, as many as its subtree has under it.
|
|
833
|
+
PR: () => [
|
|
834
|
+
"function PR(P, n) {",
|
|
835
|
+
" const f = new Int32Array(n).fill(-1), x = new Int32Array(n), o = new Int32Array(n), at = new Int32Array(n), z = new Int32Array(n);",
|
|
836
|
+
" let r = -1;",
|
|
837
|
+
" for (let i = n - 1; i >= 0; i--) {",
|
|
838
|
+
" const p = P[i];",
|
|
839
|
+
" if (p < 0) {",
|
|
840
|
+
" x[i] = r;",
|
|
841
|
+
" r = i;",
|
|
842
|
+
" } else {",
|
|
843
|
+
" x[i] = f[p];",
|
|
844
|
+
" f[p] = i;",
|
|
845
|
+
" }",
|
|
846
|
+
" }",
|
|
847
|
+
" let m = 0;",
|
|
848
|
+
" for (let k = r; k >= 0; ) {",
|
|
849
|
+
" at[k] = m;",
|
|
850
|
+
" o[m++] = k;",
|
|
851
|
+
" if (f[k] >= 0) k = f[k];",
|
|
852
|
+
" else {",
|
|
853
|
+
" while (k >= 0 && x[k] < 0) k = P[k];",
|
|
854
|
+
" if (k >= 0) k = x[k];",
|
|
855
|
+
" }",
|
|
856
|
+
" }",
|
|
857
|
+
" for (let q = n - 1; q >= 0; q--) {",
|
|
858
|
+
" const k = o[q], p = P[k];",
|
|
859
|
+
" z[k]++;",
|
|
860
|
+
" if (p >= 0) z[p] += z[k];",
|
|
861
|
+
" }",
|
|
862
|
+
" return [o, at, z];",
|
|
863
|
+
"}",
|
|
864
|
+
].join("\n"),
|
|
865
|
+
UP: () => "function UP(P, k) { const p = P[k]; return p < 0 ? F() : p; }",
|
|
866
|
+
SO: () => "function SO(g, s, k, d) { const t = g[k][s[k] + d]; return t === undefined ? F() : t; }",
|
|
867
|
+
AN: () => "function AN(P, k) { const o = []; for (let p = P[k]; p >= 0; p = P[p]) o.push(p); return o; }",
|
|
868
|
+
// The record and the keys under it, in the order of a breadth-first search. A tree has no cycle, so no key comes twice.
|
|
869
|
+
BF: () => "function BF(h, k) { const o = [k]; for (let i = 0; i < o.length; i++) for (const c of h[o[i]]) o.push(c); return o; }",
|
|
870
|
+
// The edges of a graph space: the positions of the ends of the edges from each record, in order. A second edge to the
|
|
871
|
+
// same end is the same edge, as in the interpreter. The mark of an end is the number of its record plus 1.
|
|
872
|
+
GR: () => [
|
|
873
|
+
"function GR(e, n) {",
|
|
874
|
+
' if (!Array.isArray(e) || e.length !== n) C("the edges are not an array with the ends of the edges of each record");',
|
|
875
|
+
" const O = [], s = new Int32Array(n);",
|
|
876
|
+
" for (let i = 0; i < n; i++) {",
|
|
877
|
+
" const x = e[i];",
|
|
878
|
+
" if (!Array.isArray(x)) C(`the edges of the record ${i} are not an array`);",
|
|
879
|
+
" const o = [];",
|
|
880
|
+
" for (const t of x) {",
|
|
881
|
+
" if (!(Number.isInteger(t) && t >= 0 && t < n)) C(`an edge of the record ${i} does not go to the position of a record`);",
|
|
882
|
+
" if (s[t] !== i + 1) {",
|
|
883
|
+
" s[t] = i + 1;",
|
|
884
|
+
" o.push(t);",
|
|
885
|
+
" }",
|
|
886
|
+
" }",
|
|
887
|
+
" O.push(o);",
|
|
888
|
+
" }",
|
|
889
|
+
" return O;",
|
|
890
|
+
"}",
|
|
891
|
+
].join("\n"),
|
|
892
|
+
// The predecessors of each record of a graph, in key order: the records in key order, and the ends of the edges of each record.
|
|
893
|
+
GI: () => "function GI(O, n) { const I = []; for (let i = 0; i < n; i++) I.push([]); for (let i = 0; i < n; i++) for (const t of O[i]) I[t].push(i); return I; }",
|
|
894
|
+
// The record and the keys that a path of edges reaches from it, once, in the order of a breadth-first search.
|
|
895
|
+
RB: () => "function RB(O, k, n) { const s = new Uint8Array(n), o = [k]; s[k] = 1; for (let i = 0; i < o.length; i++) for (const c of O[o[i]]) if (s[c] === 0) { s[c] = 1; o.push(c); } return o; }",
|
|
896
|
+
// The state of a run of a sheet (COMPILE.MODULE.SHEET), as in SheetRun: the result of each finished cell, the search
|
|
897
|
+
// of Tarjan with its stack, and the budget of the stack. `r` reads a cell, `f` fills a cell, and `o` gives a column.
|
|
898
|
+
// A cell without a formula has the error sentinel from the start, as the error of its read.
|
|
899
|
+
// A column of a direct fill (COMPILE.MODULE.SHEET.DIRECT): the error sentinel of a cell changes to VEX_ERROR.
|
|
900
|
+
DO: () => "function DO(d) { for (let k = 0; k < d.length; k++) if (d[k] === E) d[k] = VEX_ERROR; return d; }",
|
|
901
|
+
// A list of a result (COMPILE.MODULE.LIST.RESULT): a `vex.list` with a text key for each item. An error item has the
|
|
902
|
+
// error VEX_ERROR. The depth `h` is the number of the levels of lists in the items. One key gets the list, so it is not frozen.
|
|
903
|
+
LO: () => 'function LO(l, h) { return { kind: "vex.list", items: l.map(([k, v]) => ({ key: `${k}`, result: v === E ? { ok: false, error: VEX_ERROR } : { ok: true, value: h > 0 ? LO(v, h - 1) : v } })) }; }',
|
|
904
|
+
// The list of a run that each key gets (COMPILE.MODULE.FUSE.OUT): LO, but frozen, because the keys share the object. LS
|
|
905
|
+
// keeps no object on the array, because a property on each new list made the list ops slower (ireko, vex-compiler 0.17.1).
|
|
906
|
+
LS: () => 'function LS(l, h) { return Object.freeze({ kind: "vex.list", items: Object.freeze(l.map(([k, v]) => ({ key: `${k}`, result: v === E ? { ok: false, error: VEX_ERROR } : { ok: true, value: h > 0 ? LS(v, h - 1) : v } }))) }); }',
|
|
907
|
+
// A plain list of a result (COMPILE.MODULE.LIST.PLAIN): an array of the values of the items, with no keys. An error
|
|
908
|
+
// item is VEX_ERROR. The depth `h` is the number of the levels of lists in the items.
|
|
909
|
+
LP: () => "function LP(l, h) { return l.map((x) => (x[1] === E ? VEX_ERROR : h > 0 ? LP(x[1], h - 1) : x[1])); }",
|
|
910
|
+
// A list in an argument of an op or in a field of a record (COMPILE.MODULE.LIST.ARG, COMPILE.MODULE.LIST.FIELD): a frozen
|
|
911
|
+
// `vex.list`, as the interpreter gives. The array keeps it, so each later read of the list gives the same object.
|
|
912
|
+
// The array of a loop gets a push after a read (COMPILE.MODULE.APPEND.LET). A push only adds items, so an object with
|
|
913
|
+
// another count of items is old, and LV makes a new object.
|
|
914
|
+
LV: () => [
|
|
915
|
+
"function LV(l, h) {",
|
|
916
|
+
" let o = l.vl;",
|
|
917
|
+
" if (o === undefined || o.items.length !== l.length) {",
|
|
918
|
+
' o = Object.freeze({ kind: "vex.list", items: Object.freeze(l.map(([k, v]) => ({ key: `${k}`, result: v === E ? { ok: false, error: VEX_ERROR } : { ok: true, value: h > 0 ? LV(v, h - 1) : v } }))) });',
|
|
919
|
+
" l.vl = o;",
|
|
920
|
+
" }",
|
|
921
|
+
" return o;",
|
|
922
|
+
"}",
|
|
923
|
+
].join("\n"),
|
|
924
|
+
// The clean form (COMPILE.MODULE.FUSE.CLEAN): the facts prove that no item of a list holds the sentinel. Thus V gives
|
|
925
|
+
// the list itself, VW gives the list, and strict cannot fail. Each helper below is the helper without its last letter,
|
|
926
|
+
// with no scan, no test of the sentinel and no option strict. The other checks stay, so each error stays an error.
|
|
927
|
+
SBC: () => [
|
|
928
|
+
"function SBC(l) {",
|
|
929
|
+
" const t = l.length > 0 ? typeof l[0][1] : \"number\";",
|
|
930
|
+
" for (const [, v] of l) if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F();",
|
|
931
|
+
" return l.slice().sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0));",
|
|
932
|
+
"}",
|
|
933
|
+
].join("\n"),
|
|
934
|
+
} satisfies HelperTexts;
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
/** This function gives a part of the texts of the helpers, in the order of `HELPERS`. */
|
|
938
|
+
function helperTexts4() {
|
|
939
|
+
return {
|
|
940
|
+
SKC: () => [
|
|
941
|
+
"function SKC(l, f) {",
|
|
942
|
+
" const o = l.map((x) => [x, f(x[1])]);",
|
|
943
|
+
" const t = o.length > 0 ? typeof o[0][1] : \"number\";",
|
|
944
|
+
" for (const [, v] of o) if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F();",
|
|
945
|
+
" return o.sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0)).map((x) => x[0]);",
|
|
946
|
+
"}",
|
|
947
|
+
].join("\n"),
|
|
948
|
+
// The sort in place (COMPILE.MODULE.FUSE): a clean list that is fresh is its own sort buffer, so the sort makes no
|
|
949
|
+
// copy. No other part holds the array, so a failure leaves no changed list that a part reads. The sort is stable.
|
|
950
|
+
// A list of 64 items or fewer gets an insertion sort (COMPILE.MODULE.FUSE.SORT), which calls no comparator function.
|
|
951
|
+
// Its test ">" moves an item only past a larger value, so equal values keep their order. A longer list gets the
|
|
952
|
+
// stable sort of the engine. The kinds of the sort values go through the same check before each of the two sorts.
|
|
953
|
+
SBI: () => [
|
|
954
|
+
"function SBI(l) {",
|
|
955
|
+
" const n = l.length, t = n > 0 ? typeof l[0][1] : \"number\";",
|
|
956
|
+
" for (let i = 0; i < n; i++) { const v = l[i][1]; if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F(); }",
|
|
957
|
+
" if (n > 64) l.sort((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0));",
|
|
958
|
+
" else for (let i = 1; i < n; i++) {",
|
|
959
|
+
" const x = l[i], v = x[1];",
|
|
960
|
+
" let j = i - 1;",
|
|
961
|
+
" for (; j >= 0 && l[j][1] > v; j--) l[j + 1] = l[j];",
|
|
962
|
+
" l[j + 1] = x;",
|
|
963
|
+
" }",
|
|
964
|
+
" return l;",
|
|
965
|
+
"}",
|
|
966
|
+
].join("\n"),
|
|
967
|
+
// The option by: the array k holds the sort value of each item, so the sort makes no pair for an item. f runs once
|
|
968
|
+
// for each item, in order, and the array l does not change before the check of the sort values. A long list sorts an
|
|
969
|
+
// array of indices by k, and then reads the items from a copy of l.
|
|
970
|
+
SKI: () => [
|
|
971
|
+
"function SKI(l, f) {",
|
|
972
|
+
" const n = l.length, k = new Array(n);",
|
|
973
|
+
" for (let i = 0; i < n; i++) k[i] = f(l[i][1]);",
|
|
974
|
+
" const t = n > 0 ? typeof k[0] : \"number\";",
|
|
975
|
+
" for (const v of k) if (typeof v !== t || (t !== \"number\" && t !== \"string\") || v !== v) F();",
|
|
976
|
+
" if (n > 64) {",
|
|
977
|
+
" const o = new Array(n), c = l.slice();",
|
|
978
|
+
" for (let i = 0; i < n; i++) o[i] = i;",
|
|
979
|
+
" o.sort((a, b) => (k[a] < k[b] ? -1 : k[a] > k[b] ? 1 : 0));",
|
|
980
|
+
" for (let i = 0; i < n; i++) l[i] = c[o[i]];",
|
|
981
|
+
" } else for (let i = 1; i < n; i++) {",
|
|
982
|
+
" const v = k[i], x = l[i];",
|
|
983
|
+
" let j = i - 1;",
|
|
984
|
+
" for (; j >= 0 && k[j] > v; j--) { k[j + 1] = k[j]; l[j + 1] = l[j]; }",
|
|
985
|
+
" k[j + 1] = v; l[j + 1] = x;",
|
|
986
|
+
" }",
|
|
987
|
+
" return l;",
|
|
988
|
+
"}",
|
|
989
|
+
].join("\n"),
|
|
990
|
+
TKC: () => "function TKC(l, n) { return Number.isSafeInteger(n) && n >= 0 ? l.slice(0, n) : F(); }",
|
|
991
|
+
DRC: () => "function DRC(l, n) { return Number.isSafeInteger(n) && n >= 0 ? l.slice(n) : F(); }",
|
|
992
|
+
// The loop reads n items, so it ends also if o is l.
|
|
993
|
+
AWC: () => "function AWC(l, w) { if (l === w) return l; const o = []; for (let i = 0; i < l.length; i++) o.push(l[i]); return o; }",
|
|
994
|
+
AIC: () => "function AIC(l, x, w) { if (l !== w) { const o = []; for (let i = 0; i < l.length; i++) o.push(l[i]); l = o; } l.push(x); return l; }",
|
|
995
|
+
// The end of a filter of the array of a loop in place (COMPILE.MODULE.APPEND.KEEP): the array keeps its first n items.
|
|
996
|
+
// GT and LV count only the pushes of their array, so the filter removes their caches. VW of the array is the array.
|
|
997
|
+
KS: () => "function KS(l, n) { l.length = n; if (l.ix !== undefined) l.ix = undefined; if (l.vl !== undefined) l.vl = undefined; }",
|
|
998
|
+
AOC: () => "function AOC(l, o, w) { if (l !== w) return l.concat(o); const n = o.length; for (let i = 0; i < n; i++) l.push(o[i]); return l; }",
|
|
999
|
+
WOC: () => [
|
|
1000
|
+
"function WOC(l, o) {",
|
|
1001
|
+
" const g = new Map();",
|
|
1002
|
+
" for (const [k] of o) g.set(`${k}`, 1);",
|
|
1003
|
+
" return l.filter((x) => !g.has(`${x[0]}`));",
|
|
1004
|
+
"}",
|
|
1005
|
+
].join("\n"),
|
|
1006
|
+
FLC: () => "function FLC(l) { const o = []; for (const [, v] of l) for (const x of v) o.push(x); return o; }",
|
|
1007
|
+
IXC: () => [
|
|
1008
|
+
"function IXC(l, x) {",
|
|
1009
|
+
' if (typeof x !== "number" && typeof x !== "string" && typeof x !== "boolean") F();',
|
|
1010
|
+
" for (let i = 0; i < l.length; i++) if (l[i][1] === x) return i;",
|
|
1011
|
+
" return F();",
|
|
1012
|
+
"}",
|
|
1013
|
+
].join("\n"),
|
|
1014
|
+
NTC: () => "function NTC(l, i) { return Number.isSafeInteger(i) && i >= 0 && i < l.length ? l[i][1] : F(); }",
|
|
1015
|
+
SNC: () => [
|
|
1016
|
+
"function SNC(l, i, v) {",
|
|
1017
|
+
" if (!(Number.isSafeInteger(i) && i >= 0 && i < l.length && v !== undefined)) F();",
|
|
1018
|
+
" const o = l.slice();",
|
|
1019
|
+
" o[i] = [l[i][0], v];",
|
|
1020
|
+
" return o;",
|
|
1021
|
+
"}",
|
|
1022
|
+
].join("\n"),
|
|
1023
|
+
FSC: () => "function FSC(l) { return l.length === 0 ? F() : l[0][1]; }",
|
|
1024
|
+
LOC: () => 'function LOC(l, h) { return { kind: "vex.list", items: l.map(([k, v]) => ({ key: `${k}`, result: { ok: true, value: h > 0 ? LOC(v, h - 1) : v } })) }; }',
|
|
1025
|
+
LSC: () => 'function LSC(l, h) { return Object.freeze({ kind: "vex.list", items: Object.freeze(l.map(([k, v]) => ({ key: `${k}`, result: { ok: true, value: h > 0 ? LSC(v, h - 1) : v } }))) }); }',
|
|
1026
|
+
LPC: () => "function LPC(l, h) { return l.map(h > 0 ? (x) => LPC(x[1], h - 1) : (x) => x[1]); }",
|
|
1027
|
+
// LV and LVC keep one object on the array. A clean list gives the same object from both.
|
|
1028
|
+
LVC: () => [
|
|
1029
|
+
"function LVC(l, h) {",
|
|
1030
|
+
" let o = l.vl;",
|
|
1031
|
+
" if (o === undefined || o.items.length !== l.length) {",
|
|
1032
|
+
' o = Object.freeze({ kind: "vex.list", items: Object.freeze(l.map(([k, v]) => ({ key: `${k}`, result: { ok: true, value: h > 0 ? LVC(v, h - 1) : v } }))) });',
|
|
1033
|
+
" l.vl = o;",
|
|
1034
|
+
" }",
|
|
1035
|
+
" return o;",
|
|
1036
|
+
"}",
|
|
1037
|
+
].join("\n"),
|
|
1038
|
+
// A column of lists (COMPILE.MODULE.SHEET.LIST): each cell with a list gives a `vex.list`. No two cells share it, so LC uses LO.
|
|
1039
|
+
LC: () => "function LC(d, h) { for (let k = 0; k < d.length; k++) if (d[k] !== VEX_ERROR) d[k] = LO(d[k], h); return d; }",
|
|
1040
|
+
// A plain column of lists (COMPILE.MODULE.LIST.PLAIN): each cell with a list gives an array.
|
|
1041
|
+
LCP: () => "function LCP(d, h) { for (let k = 0; k < d.length; k++) if (d[k] !== VEX_ERROR) d[k] = LP(d[k], h); return d; }",
|
|
1042
|
+
} satisfies HelperTexts;
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
/** This function gives a part of the texts of the helpers, in the order of `HELPERS`. */
|
|
1046
|
+
function helperTexts5() {
|
|
1047
|
+
return {
|
|
1048
|
+
SH: () => [
|
|
1049
|
+
"function SH(n, cols, own, budget) {",
|
|
1050
|
+
" const pg = (c, k) => {",
|
|
1051
|
+
" const o = own[c];",
|
|
1052
|
+
" const g = o === undefined ? undefined : o[k];",
|
|
1053
|
+
" return g === undefined ? cols[c] : g;",
|
|
1054
|
+
" };",
|
|
1055
|
+
" const D = [];",
|
|
1056
|
+
" for (let c = 0; c < cols.length; c++) {",
|
|
1057
|
+
" const d = new Array(n);",
|
|
1058
|
+
" for (let k = 0; k < n; k++) if (pg(c, k) === undefined) d[k] = E;",
|
|
1059
|
+
" D.push(d);",
|
|
1060
|
+
" }",
|
|
1061
|
+
" const seen = new Map(), stack = [], FILL = {};",
|
|
1062
|
+
" let top = FILL, missed = false, next = 0, used = 0;",
|
|
1063
|
+
" const start = (c, k) => {",
|
|
1064
|
+
" const i = next++;",
|
|
1065
|
+
" const v = { id: c * n + k, c, k, i, low: i, re: false, r: E, p: false, need: undefined };",
|
|
1066
|
+
" seen.set(v.id, v);",
|
|
1067
|
+
" stack.push(v);",
|
|
1068
|
+
" return v;",
|
|
1069
|
+
" };",
|
|
1070
|
+
" const ev = (v, g) => {",
|
|
1071
|
+
" const caller = top;",
|
|
1072
|
+
" v.p = false;",
|
|
1073
|
+
" v.need = undefined;",
|
|
1074
|
+
" top = v;",
|
|
1075
|
+
" used += g[1];",
|
|
1076
|
+
" const x = g[0](v.k);",
|
|
1077
|
+
" used -= g[1];",
|
|
1078
|
+
" top = caller;",
|
|
1079
|
+
" if (!v.p) v.r = x;",
|
|
1080
|
+
" };",
|
|
1081
|
+
" const end = (v, caller) => {",
|
|
1082
|
+
" if (v.low === v.i) {",
|
|
1083
|
+
" const ms = stack.splice(stack.lastIndexOf(v));",
|
|
1084
|
+
" const cycle = ms.length > 1 || v.re;",
|
|
1085
|
+
" for (const m of ms) {",
|
|
1086
|
+
" seen.delete(m.id);",
|
|
1087
|
+
" D[m.c][m.k] = cycle ? E : m.r;",
|
|
1088
|
+
" }",
|
|
1089
|
+
" } else if (caller !== undefined) caller.low = Math.min(caller.low, v.low);",
|
|
1090
|
+
" };",
|
|
1091
|
+
" const r = (c, k) => {",
|
|
1092
|
+
" const d = D[c][k];",
|
|
1093
|
+
" if (d !== undefined) return d;",
|
|
1094
|
+
" const caller = top;",
|
|
1095
|
+
" if (caller === FILL) {",
|
|
1096
|
+
" missed = true;",
|
|
1097
|
+
" return E;",
|
|
1098
|
+
" }",
|
|
1099
|
+
" if (caller.p) return E;",
|
|
1100
|
+
" const s = seen.get(c * n + k);",
|
|
1101
|
+
" if (s !== undefined) {",
|
|
1102
|
+
" caller.low = Math.min(caller.low, s.i);",
|
|
1103
|
+
" s.re = true;",
|
|
1104
|
+
" return E;",
|
|
1105
|
+
" }",
|
|
1106
|
+
" const g = pg(c, k);",
|
|
1107
|
+
" const v = start(c, k);",
|
|
1108
|
+
" if (used + g[1] > budget) {",
|
|
1109
|
+
" caller.p = true;",
|
|
1110
|
+
" caller.need = v;",
|
|
1111
|
+
" return E;",
|
|
1112
|
+
" }",
|
|
1113
|
+
" ev(v, g);",
|
|
1114
|
+
" if (v.p) {",
|
|
1115
|
+
" caller.p = true;",
|
|
1116
|
+
" caller.need = v;",
|
|
1117
|
+
" return E;",
|
|
1118
|
+
" }",
|
|
1119
|
+
" end(v, caller);",
|
|
1120
|
+
" const x = D[c][k];",
|
|
1121
|
+
" return x === undefined ? E : x;",
|
|
1122
|
+
" };",
|
|
1123
|
+
" const drive = (c, k) => {",
|
|
1124
|
+
" const work = [start(c, k)];",
|
|
1125
|
+
" for (let v = work[0]; v !== undefined; v = work[work.length - 1]) {",
|
|
1126
|
+
" ev(v, pg(v.c, v.k));",
|
|
1127
|
+
" if (v.p) {",
|
|
1128
|
+
" for (let q = v.need; q !== undefined; q = q.need) work.push(q);",
|
|
1129
|
+
" continue;",
|
|
1130
|
+
" }",
|
|
1131
|
+
" work.pop();",
|
|
1132
|
+
" end(v, work[work.length - 1]);",
|
|
1133
|
+
" }",
|
|
1134
|
+
" };",
|
|
1135
|
+
" const f = (c, k) => {",
|
|
1136
|
+
" if (D[c][k] !== undefined) return;",
|
|
1137
|
+
" missed = false;",
|
|
1138
|
+
" const x = pg(c, k)[0](k);",
|
|
1139
|
+
" if (missed) drive(c, k);",
|
|
1140
|
+
" else D[c][k] = x;",
|
|
1141
|
+
" };",
|
|
1142
|
+
" const o = (c) => {",
|
|
1143
|
+
" const d = D[c], out = new Array(n);",
|
|
1144
|
+
" for (let k = 0; k < n; k++) out[k] = d[k] === E ? VEX_ERROR : d[k];",
|
|
1145
|
+
" return out;",
|
|
1146
|
+
" };",
|
|
1147
|
+
" return { r, f, o };",
|
|
1148
|
+
"}",
|
|
1149
|
+
].join("\n"),
|
|
1150
|
+
// The keys of a tree in the order of a fill (SHEET.FILL): each parent before its children, or after them with `post`.
|
|
1151
|
+
// `back` reverses the order of the siblings.
|
|
1152
|
+
KO: () => [
|
|
1153
|
+
"function KO(h, r, post, back) {",
|
|
1154
|
+
" const o = [], st = [];",
|
|
1155
|
+
" const push = (x) => {",
|
|
1156
|
+
" if (back) for (let q = 0; q < x.length; q++) st.push(x[q]);",
|
|
1157
|
+
" else for (let q = x.length - 1; q >= 0; q--) st.push(x[q]);",
|
|
1158
|
+
" };",
|
|
1159
|
+
" push(r);",
|
|
1160
|
+
" while (st.length > 0) {",
|
|
1161
|
+
" const k = st.pop();",
|
|
1162
|
+
" o.push(k);",
|
|
1163
|
+
" push(h[k]);",
|
|
1164
|
+
" }",
|
|
1165
|
+
" return post ? o.reverse() : o;",
|
|
1166
|
+
"}",
|
|
1167
|
+
].join("\n"),
|
|
1168
|
+
// The memo of a function of one key, and of more keys. -0 has its own entry, because an op can tell it from 0.
|
|
1169
|
+
M1: () => [
|
|
1170
|
+
"function M1(f) {",
|
|
1171
|
+
" const m = new Map();",
|
|
1172
|
+
" return (k) => {",
|
|
1173
|
+
" const s = Object.is(k, -0) ? Z : k;",
|
|
1174
|
+
" let r = m.get(s);",
|
|
1175
|
+
" if (r === undefined) {",
|
|
1176
|
+
" try { r = f(k); } catch { r = E; }",
|
|
1177
|
+
" m.set(s, r);",
|
|
1178
|
+
" }",
|
|
1179
|
+
" return G(r);",
|
|
1180
|
+
" };",
|
|
1181
|
+
"}",
|
|
1182
|
+
].join("\n"),
|
|
1183
|
+
M: () => [
|
|
1184
|
+
"function M(f) {",
|
|
1185
|
+
" const root = new Map();",
|
|
1186
|
+
" return (...k) => {",
|
|
1187
|
+
" let m = root;",
|
|
1188
|
+
" for (const x of k.slice(0, -1)) {",
|
|
1189
|
+
" const s = Object.is(x, -0) ? Z : x;",
|
|
1190
|
+
" let c = m.get(s);",
|
|
1191
|
+
" if (c === undefined) m.set(s, (c = new Map()));",
|
|
1192
|
+
" m = c;",
|
|
1193
|
+
" }",
|
|
1194
|
+
" const last = k.at(-1);",
|
|
1195
|
+
" const s = Object.is(last, -0) ? Z : last;",
|
|
1196
|
+
" let r = m.get(s);",
|
|
1197
|
+
" if (r === undefined) {",
|
|
1198
|
+
" try { r = f(...k); } catch { r = E; }",
|
|
1199
|
+
" m.set(s, r);",
|
|
1200
|
+
" }",
|
|
1201
|
+
" return G(r);",
|
|
1202
|
+
" };",
|
|
1203
|
+
"}",
|
|
1204
|
+
].join("\n"),
|
|
1205
|
+
} satisfies HelperTexts;
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
/** The helpers of the generated code. A module has only the helpers that it uses. Each entry is a function, so the mutation lane gives the text of a helper to the tests that emit it, not to all tests. */
|
|
1209
|
+
const HELPERS: Readonly<Record<Exclude<Helper, "S">, () => string>> = { ...helperTexts1(), ...helperTexts2(), ...helperTexts3(), ...helperTexts4(), ...helperTexts5() };
|
|
1175
1210
|
|
|
1176
1211
|
/**
|
|
1177
1212
|
* This list has the globals of JavaScript that the code of a module reads. This code is the helpers, the run and the
|
|
@@ -1317,9 +1352,9 @@ const NEEDS: Readonly<Record<Helper, readonly Helper[]>> = {
|
|
|
1317
1352
|
* The forms of the helpers of the list fields, for a module where each list field has the depth 0. The test of the
|
|
1318
1353
|
* value takes the place of the recursion, and the helper has no argument `h` (COMPILE.MODULE.SIZE.FLAT).
|
|
1319
1354
|
*/
|
|
1320
|
-
const FLAT_LIST_HELPERS: Readonly<Record<"LI" | "LA", string>> = {
|
|
1321
|
-
LI: HELPERS.LI.replace("LI(x, h, t)", "LI(x, t)").replace("h > 0 ? LI(r.value, h - 1, t) : ", ""),
|
|
1322
|
-
LA: HELPERS.LA.replace("LA(x, h, t)", "LA(x, t)").replace("h > 0 ? LA(y, h - 1, t) : ", ""),
|
|
1355
|
+
const FLAT_LIST_HELPERS: Readonly<Record<"LI" | "LA", () => string>> = {
|
|
1356
|
+
LI: () => HELPERS.LI().replace("LI(x, h, t)", "LI(x, t)").replace("h > 0 ? LI(r.value, h - 1, t) : ", ""),
|
|
1357
|
+
LA: () => HELPERS.LA().replace("LA(x, h, t)", "LA(x, t)").replace("h > 0 ? LA(y, h - 1, t) : ", ""),
|
|
1323
1358
|
};
|
|
1324
1359
|
|
|
1325
1360
|
/** The clean form of each helper that gives a list of the code to the outside (COMPILE.MODULE.FUSE.CLEAN). */
|
|
@@ -5352,7 +5387,7 @@ function printModule(p: Printer, options: ModuleOptions, run: Run, body: readonl
|
|
|
5352
5387
|
[...p.helpers].toSorted().forEach(add);
|
|
5353
5388
|
const bad = nameReason(name);
|
|
5354
5389
|
if (bad !== undefined) return { ok: false, reason: bad };
|
|
5355
|
-
const textOf = (h: Helper): string => (h === "S" ? helperS(p.features) : flat && (h === "LI" || h === "LA") ? FLAT_LIST_HELPERS[h] : HELPERS[h]);
|
|
5390
|
+
const textOf = (h: Helper): string => (h === "S" ? helperS(p.features) : flat && (h === "LI" || h === "LA") ? FLAT_LIST_HELPERS[h]() : HELPERS[h]());
|
|
5356
5391
|
const params = `(records, vars${tree ? ", parents" : graph ? ", edges" : ""}${p.runKeys ? ", keys" : ""}) {`;
|
|
5357
5392
|
const start = [
|
|
5358
5393
|
' if (!Array.isArray(records)) C("the records are not an array");',
|