-
Notifications
You must be signed in to change notification settings - Fork 118
/
compiler.c
2228 lines (2170 loc) · 70 KB
/
compiler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
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
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
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
992
993
994
995
996
997
998
999
1000
/** @file compiler.c
*
* The heart of the compiler.
* It contains the tables of statements.
* It finds the statements in the tables and calls the proper routines.
* For algebraic expressions it runs the compilation by first calling
* the tokenizer, splitting things into subexpressions and generating
* the code. There is a system for recognizing already existing
* subexpressions. This economizes on the length of the output.
*
* Note: the compiler of FORM doesn't attempt to normalize the input.
* Hence x+1 and 1+x are different objects during compilation.
* Similarly (a+b-b) will not be simplified to (a).
*/
/* #[ License : */
/*
* Copyright (C) 1984-2017 J.A.M. Vermaseren
* When using this file you are requested to refer to the publication
* J.A.M.Vermaseren "New features of FORM" math-ph/0010025
* This is considered a matter of courtesy as the development was paid
* for by FOM the Dutch physics granting agency and we would like to
* be able to track its scientific use to convince FOM of its value
* for the community.
*
* This file is part of FORM.
*
* FORM is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* FORM is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with FORM. If not, see <http://www.gnu.org/licenses/>.
*/
/* #] License : */
/*
#[ includes :
*/
#include "form3.h"
/*
com1commands are the commands of which only part of the word has to
be present. The order is rather important here.
com2commands are the commands that must have their whole word match.
here we can do a binary search.
{[(
*/
static KEYWORD com1commands[] = {
{"also", (TFUN)CoIdOld, STATEMENT, PARTEST}
,{"abrackets", (TFUN)CoAntiBracket, TOOUTPUT, PARTEST}
,{"antisymmetrize", (TFUN)CoAntiSymmetrize, STATEMENT, PARTEST}
,{"antibrackets", (TFUN)CoAntiBracket, TOOUTPUT, PARTEST}
,{"brackets", (TFUN)CoBracket, TOOUTPUT, PARTEST}
,{"cfunctions", (TFUN)CoCFunction, DECLARATION, PARTEST|WITHAUTO}
,{"commuting", (TFUN)CoCFunction, DECLARATION, PARTEST|WITHAUTO}
,{"compress", (TFUN)CoCompress, DECLARATION, PARTEST}
,{"ctensors", (TFUN)CoCTensor, DECLARATION, PARTEST|WITHAUTO}
,{"cyclesymmetrize",(TFUN)CoCycleSymmetrize, STATEMENT, PARTEST}
,{"dimension", (TFUN)CoDimension, DECLARATION, PARTEST}
,{"discard", (TFUN)CoDiscard, STATEMENT, PARTEST}
,{"functions", (TFUN)CoNFunction, DECLARATION, PARTEST|WITHAUTO}
,{"format", (TFUN)CoFormat, TOOUTPUT, PARTEST}
,{"fixindex", (TFUN)CoFixIndex, DECLARATION, PARTEST}
,{"global", (TFUN)CoGlobal, DEFINITION, PARTEST}
,{"gfactorized", (TFUN)CoGlobalFactorized, DEFINITION, PARTEST}
,{"globalfactorized",(TFUN)CoGlobalFactorized,DEFINITION, PARTEST}
,{"goto", (TFUN)CoGoTo, STATEMENT, PARTEST}
,{"indexes", (TFUN)CoIndex, DECLARATION, PARTEST|WITHAUTO}
,{"indices", (TFUN)CoIndex, DECLARATION, PARTEST|WITHAUTO}
,{"identify", (TFUN)CoId, STATEMENT, PARTEST}
,{"idnew", (TFUN)CoIdNew, STATEMENT, PARTEST}
,{"idold", (TFUN)CoIdOld, STATEMENT, PARTEST}
,{"local", (TFUN)CoLocal, DEFINITION, PARTEST}
,{"lfactorized", (TFUN)CoLocalFactorized, DEFINITION, PARTEST}
,{"localfactorized",(TFUN)CoLocalFactorized, DEFINITION, PARTEST}
,{"load", (TFUN)CoLoad, DECLARATION, PARTEST}
,{"label", (TFUN)CoLabel, STATEMENT, PARTEST}
,{"modulus", (TFUN)CoModulus, DECLARATION, PARTEST}
,{"multiply", (TFUN)CoMultiply, STATEMENT, PARTEST}
,{"nfunctions", (TFUN)CoNFunction, DECLARATION, PARTEST|WITHAUTO}
,{"nprint", (TFUN)CoNPrint, TOOUTPUT, PARTEST}
,{"ntensors", (TFUN)CoNTensor, DECLARATION, PARTEST|WITHAUTO}
,{"nwrite", (TFUN)CoNWrite, DECLARATION, PARTEST}
,{"print", (TFUN)CoPrint, MIXED, 0}
,{"redefine", (TFUN)CoRedefine, STATEMENT, 0}
,{"rcyclesymmetrize",(TFUN)CoRCycleSymmetrize,STATEMENT, PARTEST}
,{"symbols", (TFUN)CoSymbol, DECLARATION, PARTEST|WITHAUTO}
,{"save", (TFUN)CoSave, DECLARATION, PARTEST}
,{"symmetrize", (TFUN)CoSymmetrize, STATEMENT, PARTEST}
,{"tensors", (TFUN)CoCTensor, DECLARATION, PARTEST|WITHAUTO}
,{"unittrace", (TFUN)CoUnitTrace, DECLARATION, PARTEST}
,{"vectors", (TFUN)CoVector, DECLARATION, PARTEST|WITHAUTO}
,{"write", (TFUN)CoWrite, DECLARATION, PARTEST}
};
static KEYWORD com2commands[] = {
{"antiputinside", (TFUN)CoAntiPutInside, STATEMENT, PARTEST}
,{"apply", (TFUN)CoApply, STATEMENT, PARTEST}
,{"aputinside", (TFUN)CoAntiPutInside, STATEMENT, PARTEST}
,{"argexplode", (TFUN)CoArgExplode, STATEMENT, PARTEST}
,{"argimplode", (TFUN)CoArgImplode, STATEMENT, PARTEST}
,{"argtoextrasymbol",(TFUN)CoArgToExtraSymbol,STATEMENT, PARTEST}
,{"argument", (TFUN)CoArgument, STATEMENT, PARTEST}
,{"assign", (TFUN)CoAssign, STATEMENT, PARTEST}
,{"auto", (TFUN)CoAuto, DECLARATION, PARTEST}
,{"autodeclare", (TFUN)CoAuto, DECLARATION, PARTEST}
,{"chainin", (TFUN)CoChainin, STATEMENT, PARTEST}
,{"chainout", (TFUN)CoChainout, STATEMENT, PARTEST}
,{"chisholm", (TFUN)CoChisholm, STATEMENT, PARTEST}
,{"cleartable", (TFUN)CoClearTable, DECLARATION, PARTEST}
,{"collect", (TFUN)CoCollect, SPECIFICATION,PARTEST}
,{"commuteinset", (TFUN)CoCommuteInSet, DECLARATION, PARTEST}
,{"contract", (TFUN)CoContract, STATEMENT, PARTEST}
,{"copyspectator" ,(TFUN)CoCopySpectator, DEFINITION, PARTEST}
,{"createspectator",(TFUN)CoCreateSpectator, DECLARATION, PARTEST}
,{"ctable", (TFUN)CoCTable, DECLARATION, PARTEST}
,{"deallocatetable",(TFUN)CoDeallocateTable, DECLARATION, PARTEST}
,{"delete", (TFUN)CoDelete, SPECIFICATION,PARTEST}
,{"denominators", (TFUN)CoDenominators, STATEMENT, PARTEST}
,{"disorder", (TFUN)CoDisorder, STATEMENT, PARTEST}
,{"do", (TFUN)CoDo, STATEMENT, PARTEST}
,{"drop", (TFUN)CoDrop, SPECIFICATION,PARTEST}
,{"dropcoefficient",(TFUN)CoDropCoefficient, STATEMENT, PARTEST}
,{"dropsymbols", (TFUN)CoDropSymbols, STATEMENT, PARTEST}
,{"else", (TFUN)CoElse, STATEMENT, PARTEST}
,{"elseif", (TFUN)CoElseIf, STATEMENT, PARTEST}
,{"emptyspectator", (TFUN)CoEmptySpectator, SPECIFICATION,PARTEST}
,{"endargument", (TFUN)CoEndArgument, STATEMENT, PARTEST}
,{"enddo", (TFUN)CoEndDo, STATEMENT, PARTEST}
,{"endif", (TFUN)CoEndIf, STATEMENT, PARTEST}
,{"endinexpression",(TFUN)CoEndInExpression, STATEMENT, PARTEST}
,{"endinside", (TFUN)CoEndInside, STATEMENT, PARTEST}
,{"endrepeat", (TFUN)CoEndRepeat, STATEMENT, PARTEST}
,{"endterm", (TFUN)CoEndTerm, STATEMENT, PARTEST}
,{"endwhile", (TFUN)CoEndWhile, STATEMENT, PARTEST}
,{"exit", (TFUN)CoExit, STATEMENT, PARTEST}
,{"extrasymbols", (TFUN)CoExtraSymbols, DECLARATION, PARTEST}
,{"factarg", (TFUN)CoFactArg, STATEMENT, PARTEST}
,{"factdollar", (TFUN)CoFactDollar, STATEMENT, PARTEST}
,{"factorize", (TFUN)CoFactorize, TOOUTPUT, PARTEST}
,{"fill", (TFUN)CoFill, DECLARATION, PARTEST}
,{"fillexpression", (TFUN)CoFillExpression, DECLARATION, PARTEST}
,{"frompolynomial", (TFUN)CoFromPolynomial, STATEMENT, PARTEST}
,{"funpowers", (TFUN)CoFunPowers, DECLARATION, PARTEST}
,{"hide", (TFUN)CoHide, SPECIFICATION,PARTEST}
,{"if", (TFUN)CoIf, STATEMENT, PARTEST}
,{"ifmatch", (TFUN)CoIfMatch, STATEMENT, PARTEST}
,{"ifnomatch", (TFUN)CoIfNoMatch, STATEMENT, PARTEST}
,{"ifnotmatch", (TFUN)CoIfNoMatch, STATEMENT, PARTEST}
,{"inexpression", (TFUN)CoInExpression, STATEMENT, PARTEST}
,{"inparallel", (TFUN)CoInParallel, SPECIFICATION,PARTEST}
,{"inside", (TFUN)CoInside, STATEMENT, PARTEST}
,{"insidefirst", (TFUN)CoInsideFirst, DECLARATION, PARTEST}
,{"intohide", (TFUN)CoIntoHide, SPECIFICATION,PARTEST}
,{"keep", (TFUN)CoKeep, SPECIFICATION,PARTEST}
,{"makeinteger", (TFUN)CoMakeInteger, STATEMENT, PARTEST}
,{"many", (TFUN)CoMany, STATEMENT, PARTEST}
,{"merge", (TFUN)CoMerge, STATEMENT, PARTEST}
,{"metric", (TFUN)CoMetric, DECLARATION, PARTEST}
,{"moduleoption", (TFUN)CoModuleOption, ATENDOFMODULE,PARTEST}
,{"multi", (TFUN)CoMulti, STATEMENT, PARTEST}
,{"multibracket", (TFUN)CoMultiBracket, STATEMENT, PARTEST}
,{"ndrop", (TFUN)CoNoDrop, SPECIFICATION,PARTEST}
,{"nfactorize", (TFUN)CoNFactorize, TOOUTPUT, PARTEST}
,{"nhide", (TFUN)CoNoHide, SPECIFICATION,PARTEST}
,{"normalize", (TFUN)CoNormalize, STATEMENT, PARTEST}
,{"notinparallel", (TFUN)CoNotInParallel, SPECIFICATION,PARTEST}
,{"nskip", (TFUN)CoNoSkip, SPECIFICATION,PARTEST}
,{"ntable", (TFUN)CoNTable, DECLARATION, PARTEST}
,{"nunfactorize", (TFUN)CoNUnFactorize, TOOUTPUT, PARTEST}
,{"nunhide", (TFUN)CoNoUnHide, SPECIFICATION,PARTEST}
,{"off", (TFUN)CoOff, DECLARATION, PARTEST}
,{"on", (TFUN)CoOn, DECLARATION, PARTEST}
,{"once", (TFUN)CoOnce, STATEMENT, PARTEST}
,{"only", (TFUN)CoOnly, STATEMENT, PARTEST}
,{"polyfun", (TFUN)CoPolyFun, DECLARATION, PARTEST}
,{"polyratfun", (TFUN)CoPolyRatFun, DECLARATION, PARTEST}
,{"pophide", (TFUN)CoPopHide, SPECIFICATION,PARTEST}
,{"print[]", (TFUN)CoPrintB, TOOUTPUT, PARTEST}
,{"printtable", (TFUN)CoPrintTable, MIXED, PARTEST}
,{"processbucketsize",(TFUN)CoProcessBucket, DECLARATION, PARTEST}
,{"propercount", (TFUN)CoProperCount, DECLARATION, PARTEST}
,{"pushhide", (TFUN)CoPushHide, SPECIFICATION,PARTEST}
,{"putinside", (TFUN)CoPutInside, STATEMENT, PARTEST}
,{"ratio", (TFUN)CoRatio, STATEMENT, PARTEST}
,{"removespectator",(TFUN)CoRemoveSpectator, SPECIFICATION,PARTEST}
,{"renumber", (TFUN)CoRenumber, STATEMENT, PARTEST}
,{"repeat", (TFUN)CoRepeat, STATEMENT, PARTEST}
,{"replaceloop", (TFUN)CoReplaceLoop, STATEMENT, PARTEST}
,{"select", (TFUN)CoSelect, STATEMENT, PARTEST}
,{"set", (TFUN)CoSet, DECLARATION, PARTEST}
,{"setexitflag", (TFUN)CoSetExitFlag, STATEMENT, PARTEST}
,{"shuffle", (TFUN)CoMerge, STATEMENT, PARTEST}
,{"skip", (TFUN)CoSkip, SPECIFICATION,PARTEST}
,{"sort", (TFUN)CoSort, STATEMENT, PARTEST}
,{"splitarg", (TFUN)CoSplitArg, STATEMENT, PARTEST}
,{"splitfirstarg", (TFUN)CoSplitFirstArg, STATEMENT, PARTEST}
,{"splitlastarg", (TFUN)CoSplitLastArg, STATEMENT, PARTEST}
,{"stuffle", (TFUN)CoStuffle, STATEMENT, PARTEST}
,{"sum", (TFUN)CoSum, STATEMENT, PARTEST}
,{"table", (TFUN)CoTable, DECLARATION, PARTEST}
,{"tablebase", (TFUN)CoTableBase, DECLARATION, PARTEST}
,{"tb", (TFUN)CoTableBase, DECLARATION, PARTEST}
,{"term", (TFUN)CoTerm, STATEMENT, PARTEST}
,{"testuse", (TFUN)CoTestUse, STATEMENT, PARTEST}
,{"threadbucketsize",(TFUN)CoThreadBucket, DECLARATION, PARTEST}
,{"topolynomial", (TFUN)CoToPolynomial, STATEMENT, PARTEST}
,{"tospectator", (TFUN)CoToSpectator, STATEMENT, PARTEST}
,{"totensor", (TFUN)CoToTensor, STATEMENT, PARTEST}
,{"tovector", (TFUN)CoToVector, STATEMENT, PARTEST}
,{"trace4", (TFUN)CoTrace4, STATEMENT, PARTEST}
,{"tracen", (TFUN)CoTraceN, STATEMENT, PARTEST}
,{"transform", (TFUN)CoTransform, STATEMENT, PARTEST}
,{"tryreplace", (TFUN)CoTryReplace, STATEMENT, PARTEST}
,{"unfactorize", (TFUN)CoUnFactorize, TOOUTPUT, PARTEST}
,{"unhide", (TFUN)CoUnHide, SPECIFICATION,PARTEST}
,{"while", (TFUN)CoWhile, STATEMENT, PARTEST}
};
int alfatable1[27];
#define OPTION0 1
#define OPTION1 2
#define OPTION2 3
typedef struct SuBbUf {
WORD subexpnum;
WORD buffernum;
} SUBBUF;
SUBBUF *subexpbuffers = 0;
SUBBUF *topsubexpbuffers = 0;
LONG insubexpbuffers = 0;
#define REDUCESUBEXPBUFFERS { if ( (topsubexpbuffers-subexpbuffers) > 256 ) {\
M_free(subexpbuffers,"subexpbuffers");\
subexpbuffers = (SUBBUF *)Malloc1(256*sizeof(SUBBUF),"subexpbuffers");\
topsubexpbuffers = subexpbuffers+256; } insubexpbuffers = 0; }
#if defined(ILP32)
#define PUTNUMBER128(t,n) { if ( n >= 16384 ) { \
*t++ = n/(128*128); *t++ = (n/128)%128; *t++ = n%128; } \
else if ( n >= 128 ) { *t++ = n/128; *t++ = n%128; } \
else *t++ = n; }
#define PUTNUMBER100(t,n) { if ( n >= 10000 ) { \
*t++ = n/10000; *t++ = (n/100)%100; *t++ = n%100; } \
else if ( n >= 100 ) { *t++ = n/100; *t++ = n%100; } \
else *t++ = n; }
#elif ( defined(LLP64) || defined(LP64) )
#define PUTNUMBER128(t,n) { if ( n >= 2097152 ) { \
*t++ = ((n/128)/128)/128; *t++ = ((n/128)/128)%128; *t++ = (n/128)%128; *t++ = n%128; } \
else if ( n >= 16384 ) { \
*t++ = n/(128*128); *t++ = (n/128)%128; *t++ = n%128; } \
else if ( n >= 128 ) { *t++ = n/128; *t++ = n%128; } \
else *t++ = n; }
#define PUTNUMBER100(t,n) { if ( n >= 1000000 ) { \
*t++ = ((n/100)/100)/100; *t++ = ((n/100)/100)%100; *t++ = (n/100)%100; *t++ = n%100; } \
else if ( n >= 10000 ) { \
*t++ = n/10000; *t++ = (n/100)%100; *t++ = n%100; } \
else if ( n >= 100 ) { *t++ = n/100; *t++ = n%100; } \
else *t++ = n; }
#endif
/*
)]}
#] includes :
#[ Compiler :
#[ inictable :
Routine sets the table for 1-st characters that allow a faster
start in the search in table 1 which should be sequential.
Search in table 2 can be binary.
*/
VOID inictable()
{
KEYWORD *k = com1commands;
int i, j, ksize;
ksize = sizeof(com1commands)/sizeof(KEYWORD);
j = 0;
alfatable1[0] = 0;
for ( i = 0; i < 26; i++ ) {
while ( j < ksize && k[j].name[0] == 'a'+i ) j++;
alfatable1[i+1] = j;
}
}
/*
#] inictable :
#[ findcommand :
Checks whether a command is in the command table.
If so a pointer to the table element is returned.
If not we return 0.
Note that when a command is not in the table, we have
to test whether it is an id command without id. It should
then have the structure pattern = rhs. This should be done
in the calling routine.
*/
KEYWORD *findcommand(UBYTE *in)
{
int hi, med, lo, i;
UBYTE *s, c;
s = in;
while ( FG.cTable[*s] <= 1 ) s++;
if ( s > in && *s == '[' && s[1] == ']' ) s += 2;
if ( *s ) { c = *s; *s = 0; }
else c = 0;
/*
First do a binary search in the second table
*/
lo = 0;
hi = sizeof(com2commands)/sizeof(KEYWORD)-1;
do {
med = ( hi + lo ) / 2;
i = StrICmp(in,(UBYTE *)com2commands[med].name);
if ( i == 0 ) { if ( c ) *s = c; return(com2commands+med); }
if ( i < 0 ) hi = med-1;
else lo = med+1;
} while ( hi >= lo );
/*
Now do a 'hashed' search in the first table. It is sequential.
*/
i = tolower(*in) - 'a';
med = alfatable1[i];
hi = alfatable1[i+1];
while ( med < hi ) {
if ( StrICont(in,(UBYTE *)com1commands[med].name) == 0 )
{ if ( c ) *s = c; return(com1commands+med); }
med++;
}
if ( c ) *s = c;
/*
Unrecognized. Too bad!
*/
return(0);
}
/*
#] findcommand :
#[ ParenthesesTest :
*/
int ParenthesesTest(UBYTE *sin)
{
WORD L1 = 0, L2 = 0, L3 = 0;
UBYTE *s = sin;
while ( *s ) {
if ( *s == '[' ) L1++;
else if ( *s == ']' ) {
L1--;
if ( L1 < 0 ) { MesPrint("&Unmatched []"); return(1); }
}
s++;
}
if ( L1 > 0 ) { MesPrint("&Unmatched []"); return(1); }
s = sin;
while ( *s ) {
if ( *s == '[' ) SKIPBRA1(s)
else if ( *s == '(' ) { L2++; s++; }
else if ( *s == ')' ) {
L2--; s++;
if ( L2 < 0 ) { MesPrint("&Unmatched ()"); return(1); }
}
else s++;
}
if ( L2 > 0 ) { MesPrint("&Unmatched ()"); return(1); }
s = sin;
while ( *s ) {
if ( *s == '[' ) SKIPBRA1(s)
else if ( *s == '[' ) SKIPBRA4(s)
else if ( *s == '{' ) { L3++; s++; }
else if ( *s == '}' ) {
L3--; s++;
if ( L3 < 0 ) { MesPrint("&Unmatched {}"); return(1); }
}
else s++;
}
if ( L3 > 0 ) { MesPrint("&Unmatched {}"); return(1); }
return(0);
}
/*
#] ParenthesesTest :
#[ SkipAName :
Skips a name and gives a pointer to the object after the name.
If there is not a proper name, it returns a zero pointer.
In principle the brackets match already, so the `if ( *s == 0 )'
code is not really needed, but you never know how the program
is extended later.
*/
UBYTE *SkipAName(UBYTE *s)
{
UBYTE *t = s;
if ( *s == '[' ) {
SKIPBRA1(s)
if ( *s == 0 ) {
MesPrint("&Illegal name: '%s'",t);
return(0);
}
s++;
}
else if ( FG.cTable[*s] == 0 || *s == '_' || *s == '$' ) {
if ( *s == '$' ) s++;
while ( FG.cTable[*s] <= 1 ) s++;
if ( *s == '_' ) s++;
}
else {
MesPrint("&Illegal name: '%s'",t);
return(0);
}
return(s);
}
/*
#] SkipAName :
#[ IsRHS :
*/
UBYTE *IsRHS(UBYTE *s, UBYTE c)
{
while ( *s && *s != c ) {
if ( *s == '[' ) {
SKIPBRA1(s);
if ( *s != ']' ) {
MesPrint("&Unmatched []");
return(0);
}
}
else if ( *s == '{' ) {
SKIPBRA2(s);
if ( *s != '}' ) {
MesPrint("&Unmatched {}");
return(0);
}
}
else if ( *s == '(' ) {
SKIPBRA3(s);
if ( *s != ')' ) {
MesPrint("&Unmatched ()");
return(0);
}
}
else if ( *s == ')' ) {
MesPrint("&Unmatched ()");
return(0);
}
else if ( *s == '}' ) {
MesPrint("&Unmatched {}");
return(0);
}
else if ( *s == ']' ) {
MesPrint("&Unmatched []");
return(0);
}
s++;
}
return(s);
}
/*
#] IsRHS :
#[ IsIdStatement :
*/
int IsIdStatement(UBYTE *s)
{
DUMMYUSE(s);
return(0);
}
/*
#] IsIdStatement :
#[ CompileAlgebra :
Returns either the number of the main level RHS (>= 0)
or an error code (< 0)
*/
int CompileAlgebra(UBYTE *s, int leftright, WORD *prototype)
{
GETIDENTITY
int error;
WORD *oldproto = AC.ProtoType;
AC.ProtoType = prototype;
if ( AC.TokensWriteFlag ) {
MesPrint("To tokenize: %s",s);
error = tokenize(s,leftright);
MesPrint(" The contents of the token buffer are:");
WriteTokens(AC.tokens);
}
else error = tokenize(s,leftright);
if ( error == 0 ) {
AR.Eside = leftright;
AC.CompileLevel = 0;
if ( leftright == LHSIDE ) { AC.DumNum = AR.CurDum = 0; }
error = CompileSubExpressions(AC.tokens);
REDUCESUBEXPBUFFERS
}
else {
AC.ProtoType = oldproto;
return(-1);
}
AC.ProtoType = oldproto;
if ( error < 0 ) return(-1);
else if ( leftright == LHSIDE ) return(cbuf[AC.cbufnum].numlhs);
else return(cbuf[AC.cbufnum].numrhs);
}
/*
#] CompileAlgebra :
#[ CompileStatement :
*/
int CompileStatement(UBYTE *in)
{
KEYWORD *k;
UBYTE *s;
int error1 = 0, error2;
/* A.iStatement = */ s = in;
if ( *s == 0 ) return(0);
if ( *s == '$' ) {
k = findcommand((UBYTE *)"assign");
}
else {
if ( ( k = findcommand(s) ) == 0 && IsIdStatement(s) == 0 ) {
MesPrint("&Unrecognized statement");
return(1);
}
if ( k == 0 ) { /* Id statement without id. Note: id must be in table */
k = com1commands + alfatable1['i'-'a'];
while ( k->name[1] != 'd' || k->name[2] ) k++;
}
else {
while ( FG.cTable[*s] <= 1 ) s++;
if ( s > in && *s == '[' && s[1] == ']' ) s += 2;
/*
The next statement is rather mysterious
It is undone in DoPrint and CoMultiply, but it also causes effects
in other (wrong) statements like dimension -4; or Trace4 -1;
The code in pre.c (LoadStatement) has been changed 8-sep-2009
to force a comma after the keyword. This means that the
'mysterious' line is automatically inactive. Hence it is taken out.
if ( *s == '+' || *s == '-' ) s++;
*/
if ( *s == ',' ) s++;
}
}
/*
First the test on the order of the statements.
This is relatively new (2.2c) and may cause some problems with old
programs. Hence the first error message should explain!
*/
if ( AP.PreAssignFlag == 0 && AM.OldOrderFlag == 0 ) {
if ( AP.PreInsideLevel ) {
if ( k->type != STATEMENT && k->type != MIXED ) {
MesPrint("&Only executable and print statements are allowed in an %#inside/%#endinside construction");
return(-1);
}
}
else {
if ( ( AC.compiletype == DECLARATION || AC.compiletype == SPECIFICATION )
&& ( k->type == STATEMENT || k->type == DEFINITION || k->type == TOOUTPUT ) ) {
if ( AC.tablecheck == 0 ) {
AC.tablecheck = 1;
if ( TestTables() ) error1 = 1;
}
}
if ( k->type == MIXED ) {
if ( AC.compiletype <= DEFINITION ) {
AC.compiletype = STATEMENT;
}
}
else if ( k->type > AC.compiletype ) {
if ( StrCmp((UBYTE *)(k->name),(UBYTE *)"format") != 0 )
AC.compiletype = k->type;
}
else if ( k->type < AC.compiletype ) {
switch ( k->type ) {
case DECLARATION:
MesPrint("&Declaration out of order");
MesPrint("& %s",in);
break;
case DEFINITION:
MesPrint("&Definition out of order");
MesPrint("& %s",in);
break;
case SPECIFICATION:
MesPrint("&Specification out of order");
MesPrint("& %s",in);
break;
case STATEMENT:
MesPrint("&Statement out of order");
break;
case TOOUTPUT:
MesPrint("&Output control statement out of order");
MesPrint("& %s",in);
break;
}
AC.compiletype = k->type;
if ( AC.firstctypemessage == 0 ) {
MesPrint("&Proper order inside a module is:");
MesPrint("Declarations, specifications, definitions, statements, output control statements");
AC.firstctypemessage = 1;
}
error1 = 1;
}
}
}
/*
Now we execute the tests that are prescribed by the flags.
*/
if ( AC.AutoDeclareFlag && ( ( k->flags & WITHAUTO ) == 0 ) ) {
MesPrint("&Illegal type of auto-declaration");
return(1);
}
if ( ( ( k->flags & PARTEST ) != 0 ) && ParenthesesTest(s) ) return(1);
error2 = (*k->func)(s);
if ( error2 == 0 ) return(error1);
return(error2);
}
/*
#] CompileStatement :
#[ TestTables :
*/
int TestTables()
{
FUNCTIONS f = functions;
TABLES t;
WORD j;
int error = 0, i;
LONG x;
i = NumFunctions + FUNCTION - MAXBUILTINFUNCTION - 1;
f = f + MAXBUILTINFUNCTION - FUNCTION + 1;
if ( AC.MustTestTable > 0 ) {
while ( i > 0 ) {
if ( ( t = f->tabl ) != 0 && t->strict > 0 && !t->sparse ) {
for ( x = 0, j = 0; x < t->totind; x++ ) {
if ( t->tablepointers[TABLEEXTENSION*x] < 0 ) j++;
}
if ( j > 0 ) {
if ( j > 1 ) {
MesPrint("&In table %s there are %d unfilled elements",
AC.varnames->namebuffer+f->name,j);
}
else {
MesPrint("&In table %s there is one unfilled element",
AC.varnames->namebuffer+f->name);
}
error = 1;
}
}
i--; f++;
}
AC.MustTestTable--;
}
return(error);
}
/*
#] TestTables :
#[ CompileSubExpressions :
Now we attack the subexpressions from inside out.
We try to see whether we had any of them already.
We have to worry about adding the wildcard sum parameter
to the prototype.
*/
int CompileSubExpressions(SBYTE *tokens)
{
GETIDENTITY
SBYTE *fill = tokens, *s = tokens, *t;
WORD number[MAXNUMSIZE], *oldwork, *w1, *w2;
int level, num, i, sumlevel = 0, sumtype = SYMTOSYM;
int retval, error = 0;
/*
Eliminate all subexpressions. They are marked by LPARENTHESIS,RPARENTHESIS
*/
AC.CompileLevel++;
while ( *s != TENDOFIT ) {
if ( *s == TFUNOPEN ) {
if ( fill < s ) *fill = TENDOFIT;
t = fill - 1;
while ( t >= tokens && t[0] >= 0 ) t--;
if ( t >= tokens && *t == TFUNCTION ) {
t++; i = 0; while ( *t >= 0 ) i = 128*i + *t++;
if ( i == AM.sumnum || i == AM.sumpnum ) {
t = s + 1;
if ( *t == TSYMBOL || *t == TINDEX ) {
t++; i = 0; while ( *t >= 0 ) i = 128*i + *t++;
if ( s[1] == TINDEX ) {
i += AM.OffsetIndex;
sumtype = INDTOIND;
}
else sumtype = SYMTOSYM;
sumlevel = i;
}
}
}
*fill++ = *s++;
}
else if ( *s == TFUNCLOSE ) { sumlevel = 0; *fill++ = *s++; }
else if ( *s == LPARENTHESIS ) {
/*
We must make an exception here.
If the subexpression is just an integer, whatever its length,
we should try to keep it.
This is important when we have a function with an integer
argument. In particular this is relevant for the MZV program.
*/
t = s; level = 0;
while ( level >= 0 ) {
s++;
if ( *s == LPARENTHESIS ) level++;
else if ( *s == RPARENTHESIS ) level--;
else if ( *s == TENDOFIT ) {
MesPrint("&Unbalanced subexpression parentheses");
return(-1);
}
}
t++; *s = TENDOFIT;
if ( sumlevel > 0 ) { /* Inside sum. Add wildcard to prototype */
oldwork = w1 = AT.WorkPointer;
w2 = AC.ProtoType;
i = w2[1];
while ( --i >= 0 ) *w1++ = *w2++;
oldwork[1] += 4;
*w1++ = sumtype; *w1++ = 4; *w1++ = sumlevel; *w1++ = sumlevel;
w2 = AC.ProtoType; AT.WorkPointer = w1;
AC.ProtoType = oldwork;
num = CompileSubExpressions(t);
AC.ProtoType = w2; AT.WorkPointer = oldwork;
}
else num = CompileSubExpressions(t);
if ( num < 0 ) return(-1);
/*
Note that the subexpression code should always fit.
We had two parentheses and at least two bytes contents.
There cannot be more than 2^21 subexpressions or we get outside
this minimum. Ignoring this might lead to really rare and
hard to find errors, years from now.
*/
if ( insubexpbuffers >= MAXSUBEXPRESSIONS ) {
MesPrint("&More than %d subexpressions inside one expression",(WORD)MAXSUBEXPRESSIONS);
Terminate(-1);
}
if ( subexpbuffers+insubexpbuffers >= topsubexpbuffers ) {
DoubleBuffer((void **)((VOID *)(&subexpbuffers))
,(void **)((VOID *)(&topsubexpbuffers)),sizeof(SUBBUF),"subexpbuffers");
}
subexpbuffers[insubexpbuffers].subexpnum = num;
subexpbuffers[insubexpbuffers].buffernum = AC.cbufnum;
num = insubexpbuffers++;
*fill++ = TSUBEXP;
i = 0;
do { number[i++] = num & 0x7F; num >>= 7; } while ( num );
while ( --i >= 0 ) *fill++ = (SBYTE)(number[i]);
s++;
}
else if ( *s == TEMPTY ) s++;
else *fill++ = *s++;
}
*fill = TENDOFIT;
/*
At this stage there are no more subexpressions.
Hence we can do the basic compilation.
*/
if ( AC.CompileLevel == 1 && AC.ToBeInFactors ) {
error = CodeFactors(tokens);
}
AC.CompileLevel--;
retval = CodeGenerator(tokens);
if ( error < 0 ) return(error);
return(retval);
}
/*
#] CompileSubExpressions :
#[ CodeGenerator :
This routine does the real code generation.
It returns the number of the rhs subexpression.
At this point we do not have to worry about subexpressions,
sets, setelements, simple vs complicated function arguments
simple vs complicated powers etc.
The variable 'first' indicates whether we are starting a new term
The major complication are the set elements of type set[n].
We have marked them as TSETNUM,n,Ttype,setnum
They go into
SETSET,size,subterm,relocation list
in which the subterm should be ready to become a regular
subterm in which the sets have been replaced by their element
The relocation list consists of pairs of numbers:
1: offset in the subterm, 2: the symbol n.
Note that such a subterm can be a whole function with its arguments.
We use the variable inset to indicate that we have something going.
The relocation list is collected in the top of the WorkSpace.
*/
static UWORD *CGscrat7 = 0;
int CodeGenerator(SBYTE *tokens)
{
GETIDENTITY
SBYTE *s = tokens, c;
int i, sign = 1, first = 1, deno = 1, error = 0, minus, n, needarg, numexp, cc;
int base, sumlevel = 0, sumtype = SYMTOSYM, firstsumarg, inset = 0;
int funflag = 0, settype, x1, x2, mulflag = 0;
WORD *t, *v, *r, *term, nnumerator, ndenominator, *oldwork, x3, y, nin;
WORD *w1, *w2, *tsize = 0, *relo = 0;
UWORD *numerator, *denominator, *innum;
CBUF *C;
POSITION position;
WORD TMproto[SUBEXPSIZE];
/*
#ifdef WITHPTHREADS
RENUMBER renumber;
#endif
*/
RENUMBER renumber;
if ( AC.TokensWriteFlag ) WriteTokens(tokens);
if ( CGscrat7 == 0 )
CGscrat7 = (UWORD *)Malloc1((AM.MaxTal+2)*sizeof(WORD),"CodeGenerator");
AddRHS(AC.cbufnum,0);
C = cbuf + AC.cbufnum;
numexp = C->numrhs;
C->NumTerms[numexp] = 0;
C->numdum[numexp] = 0;
oldwork = AT.WorkPointer;
numerator = (UWORD *)(AT.WorkPointer);
denominator = numerator + 2*AM.MaxTal;
innum = denominator + 2*AM.MaxTal;
term = (WORD *)(innum + 2*AM.MaxTal);
AT.WorkPointer = term + AM.MaxTer/sizeof(WORD);
if ( AT.WorkPointer > AT.WorkTop ) goto OverWork;
cc = 0;
t = term+1;
numerator[0] = denominator[0] = 1;
nnumerator = ndenominator = 1;
while ( *s != TENDOFIT ) {
if ( *s == TPLUS || *s == TMINUS ) {
if ( first || mulflag ) { if ( *s == TMINUS ) sign = -sign; }
else {
*term = t-term;
C->NumTerms[numexp]++;
if ( cc && sign ) C->CanCommu[numexp]++;
CompleteTerm(term,numerator,denominator,nnumerator,ndenominator,sign);
first = 1; cc = 0; t = term + 1; deno = 1;
numerator[0] = denominator[0] = 1;
nnumerator = ndenominator = 1;
if ( *s == TMINUS ) sign = -1;
else sign = 1;
}
s++;
}
else {
mulflag = first = 0; c = *s++;
switch ( c ) {
case TSYMBOL:
x1 = 0; while ( *s >= 0 ) { x1 = x1*128 + *s++; }
if ( *s == TWILDCARD ) { s++; x1 += 2*MAXPOWER; }
*t++ = SYMBOL; *t++ = 4; *t++ = x1;
if ( inset ) *relo = 2;
TryPower: if ( *s == TPOWER ) {
s++;
if ( *s == TMINUS ) { s++; deno = -deno; }
c = *s++;
base = ( c == TNUMBER ) ? 100: 128;
x2 = 0; while ( *s >= 0 ) { x2 = base*x2 + *s++; }
if ( c == TSYMBOL ) {
if ( *s == TWILDCARD ) s++;
x2 += 2*MAXPOWER;
}
*t++ = deno*x2;
}
else *t++ = deno;
fin: deno = 1;
if ( inset ) {
while ( relo < AT.WorkTop ) *t++ = *relo++;
inset = 0; tsize[1] = t - tsize;
}
break;
case TINDEX:
x1 = 0; while ( *s >= 0 ) { x1 = x1*128 + *s++; }
*t++ = INDEX; *t++ = 3;
if ( *s == TWILDCARD ) { s++; x1 += WILDOFFSET; }
if ( inset ) { *t++ = x1; *relo = 2; }
else *t++ = x1 + AM.OffsetIndex;
if ( t[-1] > AM.IndDum ) {
x1 = t[-1] - AM.IndDum;
if ( x1 > C->numdum[numexp] ) C->numdum[numexp] = x1;
}
goto fin;
case TGENINDEX:
*t++ = INDEX; *t++ = 3; *t++ = AC.DumNum+WILDOFFSET;
deno = 1;
break;
case TVECTOR:
x1 = 0; while ( *s >= 0 ) { x1 = x1*128 + *s++; }
dovector: if ( inset == 0 ) x1 += AM.OffsetVector;
if ( *s == TWILDCARD ) { s++; x1 += WILDOFFSET; }
if ( inset ) *relo = 2;
if ( *s == TDOT ) { /* DotProduct ? */
s++;
if ( *s == TSETNUM || *s == TSETDOL ) {
settype = ( *s == TSETDOL );
s++; x2 = 0; while ( *s >= 0 ) { x2 = x2*128 + *s++; }
if ( settype ) x2 = -x2;
if ( inset == 0 ) {
tsize = t; *t++ = SETSET; *t++ = 0;
relo = AT.WorkTop;
}
inset += 2;
*--relo = x2; *--relo = 3;
}
if ( *s != TVECTOR && *s != TDUBIOUS ) {
MesPrint("&Illegally formed dotproduct");
error = 1;
}
s++; x2 = 0; while ( *s >= 0 ) { x2 = x2*128 + *s++; }
if ( inset < 2 ) x2 += AM.OffsetVector;
if ( *s == TWILDCARD ) { s++; x2 += WILDOFFSET; }
*t++ = DOTPRODUCT; *t++ = 5; *t++ = x1; *t++ = x2;
goto TryPower;
}
else if ( *s == TFUNOPEN ) {
s++;
if ( *s == TSETNUM || *s == TSETDOL ) {
settype = ( *s == TSETDOL );
s++; x2 = 0; while ( *s >= 0 ) { x2 = x2*128 + *s++; }
if ( settype ) x2 = -x2;
if ( inset == 0 ) {
tsize = t; *t++ = SETSET; *t++ = 0;
relo = AT.WorkTop;
}
inset += 2;
*--relo = x2; *--relo = 3;
}
if ( *s == TINDEX || *s == TDUBIOUS ) {
s++;
x2 = 0; while ( *s >= 0 ) { x2 = x2*128 + *s++; }
if ( inset < 2 ) x2 += AM.OffsetIndex;
if ( *s == TWILDCARD ) { s++; x2 += WILDOFFSET; }
*t++ = VECTOR; *t++ = 4; *t++ = x1; *t++ = x2;
if ( t[-1] > AM.IndDum ) {
x2 = t[-1] - AM.IndDum;
if ( x2 > C->numdum[numexp] ) C->numdum[numexp] = x2;
}
}
else if ( *s == TGENINDEX ) {
*t++ = VECTOR; *t++ = 4; *t++ = x1;
*t++ = AC.DumNum + WILDOFFSET;
}
else if ( *s == TNUMBER || *s == TNUMBER1 ) {
base = ( *s == TNUMBER ) ? 100: 128;
s++;
x2 = 0; while ( *s >= 0 ) { x2 = x2*base + *s++; }
if ( x2 >= AM.OffsetIndex && inset < 2 ) {
MesPrint("&Fixed index in vector greater than %d",
AM.OffsetIndex);
return(-1);
}
*t++ = VECTOR; *t++ = 4; *t++ = x1; *t++ = x2;
}
else if ( *s == TVECTOR || ( *s == TMINUS && s[1] == TVECTOR ) ) {
if ( *s == TMINUS ) { s++; sign = -sign; }
s++;
x2 = 0; while ( *s >= 0 ) { x2 = x2*128 + *s++; }
if ( inset < 2 ) x2 += AM.OffsetVector;
if ( *s == TWILDCARD ) { s++; x2 += WILDOFFSET; }
*t++ = DOTPRODUCT; *t++ = 5; *t++ = x1; *t++ = x2; *t++ = deno;
}
else {
MesPrint("&Illegal argument for vector");
return(-1);
}
if ( *s != TFUNCLOSE ) {
MesPrint("&Illegal argument for vector");
return(-1);