forked from vermaseren/form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre.c
7007 lines (6671 loc) · 177 KB
/
pre.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 pre.c
*
* This is the preprocessor and all its routines.
*/
/* #[ License : */
/*
* Copyright (C) 1984-2022 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"
static UBYTE pushbackchar = 0;
static int oldmode = 0;
static int stopdelay = 0;
static STREAM *oldstream = 0;
static UBYTE underscore[2] = {'_',0};
static PREVAR *ThePreVar = 0;
static KEYWORD precommands[] = {
{"add" , DoPreAdd , 0, 0}
,{"addseparator" , DoPreAddSeparator,0,0}
,{"append" , DoPreAppend , 0, 0}
,{"appendpath" , DoPreAppendPath, 0, 0}
,{"assign" , DoPreAssign , 0, 0}
,{"break" , DoPreBreak , 0, 0}
,{"breakdo" , DoBreakDo , 0, 0}
,{"call" , DoCall , 0, 0}
,{"case" , DoPreCase , 0, 0}
,{"clearoptimize", DoClearOptimize, 0, 0}
,{"close" , DoPreClose , 0, 0}
,{"closedictionary", DoPreCloseDictionary,0,0}
,{"commentchar" , DoCommentChar , 0, 0}
,{"create" , DoPreCreate , 0, 0}
,{"debug" , DoDebug , 0, 0}
,{"default" , DoPreDefault , 0, 0}
,{"define" , DoDefine , 0, 0}
,{"do" , DoDo , 0, 0}
,{"else" , DoElse , 0, 0}
,{"elseif" , DoElseif , 0, 0}
,{"enddo" , DoEnddo , 0, 0}
,{"endif" , DoEndif , 0, 0}
,{"endinside" , DoEndInside , 0, 0}
,{"endprocedure" , DoEndprocedure , 0, 0}
,{"endswitch" , DoPreEndSwitch , 0, 0}
,{"exchange" , DoPreExchange , 0, 0}
,{"external" , DoExternal , 0, 0}
,{"factdollar" , DoFactDollar , 0, 0}
,{"fromexternal" , DoFromExternal , 0, 0}
,{"if" , DoIf , 0, 0}
,{"ifdef" , DoIfydef , 0, 0}
,{"ifndef" , DoIfndef , 0, 0}
,{"include" , DoInclude , 0, 0}
,{"inside" , DoInside , 0, 0}
,{"message" , DoMessage , 0, 0}
,{"opendictionary", DoPreOpenDictionary,0,0}
,{"optimize" , DoOptimize , 0, 0}
,{"pipe" , DoPipe , 0, 0}
,{"preout" , DoPreOut , 0, 0}
,{"prependpath" , DoPrePrependPath,0, 0}
,{"printtimes" , DoPrePrintTimes, 0, 0}
,{"procedure" , DoProcedure , 0, 0}
,{"procedureextension" , DoPrcExtension , 0, 0}
,{"prompt" , DoPrompt , 0, 0}
,{"redefine" , DoRedefine , 0, 0}
,{"remove" , DoPreRemove , 0, 0}
,{"reset" , DoPreReset , 0, 0}
,{"reverseinclude" , DoReverseInclude , 0, 0}
,{"rmexternal" , DoRmExternal , 0, 0}
,{"rmseparator" , DoPreRmSeparator,0, 0}
,{"setexternal" , DoSetExternal , 0, 0}
,{"setexternalattr" , DoSetExternalAttr , 0, 0}
,{"setrandom" , DoSetRandom , 0, 0}
,{"show" , DoPreShow , 0, 0}
,{"skipextrasymbols" , DoSkipExtraSymbols , 0, 0}
,{"switch" , DoPreSwitch , 0, 0}
,{"system" , DoSystem , 0, 0}
,{"terminate" , DoTerminate , 0, 0}
,{"timeoutafter" , DoTimeOutAfter , 0, 0}
,{"toexternal" , DoToExternal , 0, 0}
,{"undefine" , DoUndefine , 0, 0}
,{"usedictionary", DoPreUseDictionary,0,0}
,{"write" , DoPreWrite , 0, 0}
};
/*
#] Includes :
# [ PreProcessor :
#[ GetInput :
Gets one input character. If we reach the end of a stream
we pop to the previous stream and try again.
If there are no more streams we let this be known.
*/
UBYTE GetInput()
{
UBYTE c;
while ( AC.CurrentStream ) {
c = GetFromStream(AC.CurrentStream);
if ( c != ENDOFSTREAM ) {
#ifdef WITHMPI
if ( PF.me == MASTER
&& AC.NoShowInput <= 0
&& AC.CurrentStream->type != PREVARSTREAM )
#else
if ( AC.NoShowInput <= 0 && AC.CurrentStream->type != PREVARSTREAM )
#endif
CharOut(c);
return(c);
}
AC.CurrentStream = CloseStream(AC.CurrentStream);
if ( stopdelay && AC.CurrentStream == oldstream ) {
stopdelay = 0; AP.AllowDelay = 1;
}
}
return(ENDOFINPUT);
}
/*
#] GetInput :
#[ ClearPushback :
*/
VOID ClearPushback()
{
pushbackchar = 0;
}
/*
#] ClearPushback :
#[ GetChar :
Reads one character. If it encounters a quote it immediately
takes the whole preprocessor variable and opens a stream
for it and starts reading the stream.
Note that we have to take special precautions for escaped quotes.
That is why we remember the previous character. We allow the
(dubious?) construction of ending a stream with a backslash and
then using it to escape an object in the parent stream.
*/
UBYTE GetChar(int level)
{
UBYTE namebuf[MAXPRENAMESIZE+2], c, *s, *t;
static UBYTE lastchar, charinbuf = 0;
int i, j, raiselow, olddelay;
STREAM *stream;
if ( level > 0 ) {
lastchar = '`';
goto higherlevel;
}
if ( pushbackchar ) { c = pushbackchar; pushbackchar = 0; return(c); }
if ( charinbuf ) { c = charinbuf; charinbuf = 0; return(c); }
c = GetInput();
for(;;) {
if ( c == '\\' ) {
charinbuf = GetInput();
if ( charinbuf != LINEFEED ) {
pushbackchar = charinbuf;
charinbuf = 0;
break;
}
charinbuf = 0; /* Escaped linefeed -> skip leading blanks */
while ( ( c = GetInput() ) == ' ' || c == '\t' ) {}
}
else if ( c == '\'' || c == '`' ) {
if ( AP.DelayPrevar == 1 && c == '\'' ) {
AP.DelayPrevar = 0;
break;
}
lastchar = c;
higherlevel:
c = GetInput();
if ( c == '!' && lastchar == '`' ) {
if ( stopdelay == 0 ) oldstream = AC.CurrentStream;
AP.AllowDelay = 0;
stopdelay = 1;
c = GetInput();
}
if ( c == '~' && lastchar == '`' ) {
if ( AP.AllowDelay ) {
pushbackchar = c;
c = lastchar;
AP.DelayPrevar = 1;
break;
}
}
else {
pushbackchar = c;
}
olddelay = AP.DelayPrevar;
AP.DelayPrevar = 0;
i = 0; lastchar = 0;
for (;;) {
if ( pushbackchar ) { c = pushbackchar; pushbackchar = 0; }
else { c = GetInput(); }
if ( c == ENDOFINPUT || ( ( c == '\'' || c == LINEFEED )
&& lastchar != '\\' ) ) {
break;
}
if ( c == '{' ) { /* Try the preprocessor calculator */
if ( PreCalc() == 0 ) Terminate(-1);
c = GetInput(); /* This is either a { or a number */
if ( c == '{' ) {
MesPrint("@Illegal set inside preprocessor variable name");
Terminate(-1);
}
}
if ( c == '`' && lastchar != '\\' ) {
c = GetChar(1);
if ( c == ENDOFINPUT || ( ( c == '\'' || c == LINEFEED )
&& lastchar != '\\' ) ) {
break;
}
}
if ( lastchar == '\\' ) { i--; lastchar = 0; }
else lastchar = c;
namebuf[i++] = c;
if ( i > MAXPRENAMESIZE ) {
namebuf[i] = 0;
Error1("Preprocessor variable name too long: ",namebuf);
}
}
namebuf[i++] = 0;
if ( c != '\'' ) {
Error1("Unmatched quotes for preprocessor variable",namebuf);
}
AP.DelayPrevar = olddelay;
if ( namebuf[0] == '$' ) {
raiselow = PRENOACTION;
if ( AP.PreproFlag && *AP.preStart) {
s = EndOfToken(AP.preStart);
c = *s; *s = 0;
if ( ( StrICmp(AP.preStart,(UBYTE *)"ifdef") == 0
|| StrICmp(AP.preStart,(UBYTE *)"ifndef") == 0 )
&& GetDollar(namebuf+1) < 0 ) {
*s = c; c = ' ';
break;
}
*s = c;
}
else {
s = EndOfToken(namebuf+1);
if ( *s == '[' ) { while ( *s ) s++; }
}
if ( *s == '-' && s[1] == '-' && s[2] == 0 )
raiselow = PRELOWERAFTER;
else if ( *s == '+' && s[1] == '+' && s[2] == 0 )
raiselow = PRERAISEAFTER;
c = *s; *s = 0;
if ( OpenStream(namebuf+1,DOLLARSTREAM,0,raiselow) == 0 ) {
*s = c;
MesPrint("@Undefined variable %s used as preprocessor variable",
namebuf);
Terminate(-1);
}
*s = c;
}
else {
raiselow = PRENOACTION;
if ( AP.PreproFlag && *AP.preStart) {
s = EndOfToken(AP.preStart);
c = *s; *s = 0;
if ( ( StrICmp(AP.preStart,(UBYTE *)"ifdef") == 0
|| StrICmp(AP.preStart,(UBYTE *)"ifndef") == 0 )
&& GetPreVar(namebuf,WITHOUTERROR) == 0 ) {
*s = c; c = ' ';
break;
}
*s = c;
}
s = EndOfToken(namebuf);
if ( *s == '_' ) s++;
if ( *s == '-' && s[1] == '-' && s[2] == 0 )
raiselow = PRELOWERAFTER;
else if ( *s == '+' && s[1] == '+' && s[2] == 0 )
raiselow = PRERAISEAFTER;
else if ( *s == '(' && namebuf[i-2] == ')' ) {
/*
Now count the arguments and separate them by zeroes
Check on the ?var construction and if present, reset
some comma's.
Make the assignments of the variables
Run the macro.
Undefine the variables
*/
int nargs = 1;
PREVAR *p;
*s++ = 0; namebuf[i-2] = 0;
if ( StrICmp(namebuf,(UBYTE *)"random_") == 0 ) {
UBYTE *ranvalue;
ranvalue = PreRandom(s);
PutPreVar(namebuf,ranvalue,(UBYTE *)"?a",1);
M_free(ranvalue,"PreRandom");
goto dostream;
}
else if ( StrICmp(namebuf,(UBYTE *)"tolower_") == 0 ) {
UBYTE *ss = s;
while ( *ss ) { *ss = (UBYTE)(tolower(*ss)); ss++; }
PutPreVar(namebuf,s,(UBYTE *)"?a",1);
goto dostream;
}
else if ( StrICmp(namebuf,(UBYTE *)"toupper_") == 0 ) {
UBYTE *ss = s;
while ( *ss ) { *ss = (UBYTE)(toupper(*ss)); ss++; }
PutPreVar(namebuf,s,(UBYTE *)"?a",1);
goto dostream;
}
while ( *s ) {
if ( *s == '\\' ) s++;
if ( *s == ',' ) { *s = 0; nargs++; }
s++;
}
GetPreVar(namebuf,WITHERROR);
p = ThePreVar;
if ( p == 0 ) {
MesPrint("@Illegal use of arguments in preprocessor variable %s",namebuf);
Terminate(-1);
}
if ( p->nargs <= 0 || ( p->wildarg == 0 && nargs != p->nargs )
|| ( p->wildarg > 0 && nargs < p->nargs-1 ) ) {
MesPrint("@Arguments of macro %s do not match",namebuf);
Terminate(-1);
}
if ( p->wildarg > 0 ) {
/*
Change some zeroes into commas
*/
s = namebuf;
for ( j = 0; j < p->wildarg; j++ ) {
while ( *s ) s++;
s++;
}
for ( j = 0; j < nargs-p->nargs; j++ ) {
while ( *s ) s++;
*s++ = ',';
}
}
/*
Now we can make the assignments
*/
s = namebuf;
while ( *s ) s++;
s++;
t = p->argnames;
for ( j = 0; j < p->nargs; j++ ) {
if ( ( nargs == p->nargs-1 ) && ( *t == '?' ) ) {
PutPreVar(t,0,0,0);
}
else {
PutPreVar(t,s,0,0);
while ( *s ) s++;
s++;
}
while ( *t ) t++;
t++;
}
}
dostream:;
if ( ( stream = OpenStream(namebuf,PREVARSTREAM,0,raiselow) ) == 0 ) {
/*
Eat comma before or after. This is `no value'
*/
}
else if ( stream->inbuffer == 0 ) {
c = GetInput();
if ( level > 0 && c == '\'' ) return(c);
goto endofloop;
}
}
c = GetInput();
}
else if ( c == '{' ) { /* Try the preprocessor calculator */
if ( PreCalc() == 0 ) Terminate(-1);
c = GetInput(); /* This is either a { or a number */
break;
}
else break;
endofloop:;
}
return(c);
}
/*
#] GetChar :
#[ CharOut :
*/
VOID CharOut(UBYTE c)
{
if ( c == LINEFEED ) {
AM.OutBuffer[AP.InOutBuf++] = c;
WriteString(INPUTOUT,AM.OutBuffer,AP.InOutBuf);
AP.InOutBuf = 0;
}
else {
if ( AP.InOutBuf >= AM.OutBufSize || c == LINEFEED ) {
WriteString(INPUTOUT,AM.OutBuffer,AP.InOutBuf);
AP.InOutBuf = 0;
}
AM.OutBuffer[AP.InOutBuf++] = c;
}
}
/*
#] CharOut :
#[ UnsetAllowDelay :
*/
VOID UnsetAllowDelay()
{
if ( ThePreVar != 0 ) {
if ( ThePreVar->nargs > 0 ) AP.AllowDelay = 0;
}
}
/*
#] UnsetAllowDelay :
#[ GetPreVar :
We use the model of a heap. If the same name has been used more
than once the last definition is used. This gives the impression
of local variables.
There are two types: The regular ones and the expression variables.
The last ones are like UNCHANGED_exprname and ZERO_exprname or
UNCHANGED_ and ZERO_.
*/
static UBYTE *yes = (UBYTE *)"1";
static UBYTE *no = (UBYTE *)"0";
static UBYTE numintopolynomial[12];
#include "vector.h"
static Vector(UBYTE, exprstr); /* Used for numactiveexprs_ and activeexprnames_. */
UBYTE *GetPreVar(UBYTE *name, int flag)
{
GETIDENTITY
int i, mode;
WORD number;
UBYTE *t, c = 0, *tt = 0;
t = name; while ( *t ) t++;
if ( t[-1] == '-' && t[-2] == '-' && t-2 > name && t[-3] != '_' ) {
t -= 2; c = *t; *t = 0; tt = t;
}
else if ( t[-1] == '+' && t[-2] == '+' && t-2 > name && t[-3] != '_' ) {
t -= 2; c = *t; *t = 0; tt = t;
}
else if ( StrICmp(name,(UBYTE *)"time_") == 0 ) {
UBYTE millibuf[24];
LONG millitime, timepart;
int timepart1, timepart2;
static char timestring[40];
/* millitime = TimeCPU(1); */
millitime = GetRunningTime();
timepart = millitime%1000;
millitime /= 1000;
timepart /= 10;
timepart1 = timepart / 10;
timepart2 = timepart % 10;
NumToStr(millibuf,millitime);
sprintf(timestring,"%s.%1d%1d",millibuf,timepart1,timepart2);
return((UBYTE *)timestring);
}
else if ( ( StrICmp(name,(UBYTE *)"timer_") == 0 )
|| ( StrICmp(name,(UBYTE *)"stopwatch_") == 0 ) ) {
static char timestring[40];
sprintf(timestring,"%ld",(GetRunningTime() - AP.StopWatchZero));
return((UBYTE *)timestring);
}
else if ( StrICmp(name, (UBYTE *)"numactiveexprs_") == 0 ) {
/* the number of active expressions */
int n = 0;
for ( i = 0; i < NumExpressions; i++ ) {
EXPRESSIONS e = Expressions + i;
switch ( e->status ) {
case LOCALEXPRESSION:
case GLOBALEXPRESSION:
case UNHIDELEXPRESSION:
case UNHIDEGEXPRESSION:
case INTOHIDELEXPRESSION:
case INTOHIDEGEXPRESSION:
n++;
break;
}
}
VectorReserve(exprstr, 41); /* up to 128-bit */
LongCopy(n, (char *)VectorPtr(exprstr));
return VectorPtr(exprstr);
}
else if ( StrICmp(name, (UBYTE *)"activeexprnames_") == 0 ) {
/* the list of active expressions separated by commas */
int j = 0;
VectorReserve(exprstr, 16); /* at least 1 character for '\0' */
for ( i = 0; i < NumExpressions; i++ ) {
UBYTE *p, *s;
int len, k;
EXPRESSIONS e = Expressions + i;
switch ( e->status ) {
case LOCALEXPRESSION:
case GLOBALEXPRESSION:
case UNHIDELEXPRESSION:
case UNHIDEGEXPRESSION:
case INTOHIDELEXPRESSION:
case INTOHIDEGEXPRESSION:
s = AC.exprnames->namebuffer + e->name;
len = StrLen(s);
VectorSize(exprstr) = j; /* j bytes must be copied in extending the buffer. */
VectorReserve(exprstr, j + len * 2 + 1);
p = VectorPtr(exprstr);
if ( j > 0 ) p[j++] = ',';
for ( k = 0; k < len; k++ ) {
if ( s[k] == ',' || s[k] == '|' ) p[j++] = '\\';
p[j++] = s[k];
}
break;
}
}
VectorPtr(exprstr)[j] = '\0';
return VectorPtr(exprstr);
}
else if ( StrICmp(name, (UBYTE *)"path_") == 0 ) {
/* the current FORM path (for debugging both in .c and .frm) */
if ( AM.Path ) {
return(AM.Path);
}
else {
return((UBYTE *)"");
}
}
t = name;
while ( *t && *t != '_' ) t++;
for ( i = NumPre-1; i >= 0; i-- ) {
if ( *t == '_' && ( StrICmp(name,PreVar[i].name) == 0 ) ) {
if ( c ) *tt = c;
ThePreVar = PreVar+i;
return(PreVar[i].value);
}
else if ( StrCmp(name,PreVar[i].name) == 0 ) {
if ( c ) *tt = c;
ThePreVar = PreVar+i;
return(PreVar[i].value);
}
}
if ( *t == '_' ) {
if ( StrICmp(name,(UBYTE *)"EXTRASYMBOLS_") == 0 ) goto extrashort;
*t = 0;
if ( StrICmp(name,(UBYTE *)"UNCHANGED") == 0 ) mode = 1;
else if ( StrICmp(name,(UBYTE *)"ZERO") == 0 ) mode = 0;
else if ( StrICmp(name,(UBYTE *)"SHOWINPUT") == 0 ) {
*t++ = '_';
if ( c ) *tt = c;
if ( AC.NoShowInput > 0 ) return(no);
else return(yes);
}
else if ( StrICmp(name,(UBYTE *)"EXTRASYMBOLS") == 0 ) {
*t++ = '_';
extrashort:;
number = cbuf[AM.sbufnum].numrhs;
t = numintopolynomial;
NumCopy(number,t);
return(numintopolynomial);
}
else mode = -1;
*t++ = '_';
if ( mode >= 0 ) {
ThePreVar = 0;
if ( *t ) {
if ( GetName(AC.exprnames,t,&number,NOAUTO) == CEXPRESSION ) {
if ( c ) *tt = c;
if ( ( Expressions[number].vflags & ( 1 << mode ) ) != 0 )
return(yes);
else return(no);
}
}
else {
/*
Here we have to test all active results.
These are in `negative' so the flags have to be zero.
*/
if ( c ) *tt = c;
if ( ( AR.expflags & ( 1 << mode ) ) == 0 ) return(yes);
else return(no);
}
}
}
if ( ( t = (UBYTE *)(getenv((char *)(name))) ) != 0 ) {
if ( c ) *tt = c;
ThePreVar = 0;
return(t);
}
if ( c ) *tt = c;
if ( flag == WITHERROR ) {
Error1("Undefined preprocessor variable",name);
}
return(0);
}
/*
#] GetPreVar :
#[ PutPreVar :
*/
/**
* Inserts/Updates a preprocessor variable in the name administration.
*
* @param name Character string with the variable name.
* @param value Character string with a possible value.
* Special case: if this argument is zero, then we have no
* value. Note: This is different from having an empty
* argument! This should only occur when the name starts
* with a ?
* @param args Character string with possible arguments.
* @param mode =0: always create a new name entry, =1: try to do a
* redefinition if possible.
* @return Index of used entry in name list.
*/
int PutPreVar(UBYTE *name, UBYTE *value, UBYTE *args, int mode)
{
int i, ii, num = 2, nnum = 2, numargs = 0;
UBYTE *s, *t, *u = 0;
PREVAR *p;
if ( value == 0 && name[0] != '?' ) {
MesPrint("@Illegal empty value for preprocessor variable %s",name);
Terminate(-1);
}
if ( args ) {
s = args; num++;
while ( *s ) {
if ( *s != ' ' && *s != '\t' ) num++;
s++;
}
}
if ( mode == 1 ) {
i = NumPre;
while ( --i >= 0 ) {
if ( StrCmp(name,PreVar[i].name) == 0 ) {
u = PreVar[i].name;
break;
}
}
}
else i = -1;
if ( i < 0 ) { p = (PREVAR *)FromList(&AP.PreVarList); ii = p - PreVar; }
else { p = &(PreVar[i]); ii = i; }
if ( value ) {
s = value; while ( *s ) { s++; num++; }
}
else num = 1;
if ( i >= 0 ) {
if ( p->value ) {
s = p->value;
while ( *s ) { s++; nnum++; }
}
else nnum = 1;
if ( nnum >= num ) {
/*
We can keep this in place
*/
if ( value && p->value ) {
s = value;
t = p->value;
while ( *s ) *t++ = *s++;
*t = 0;
}
else p->value = 0;
return(i);
}
}
s = name; while ( *s ) { s++; num++; }
t = (UBYTE *)Malloc1(num,"PreVariable");
p->name = t;
s = name; while ( *s ) *t++ = *s++; *t++ = 0;
if ( value ) {
p->value = t;
s = value; while ( *s ) *t++ = *s++; *t = 0;
if ( AM.atstartup && t[-1] == '\n' ) t[-1] = 0;
}
else p->value = 0;
p->wildarg = 0;
if ( args ) {
int first = 1;
t++; p->argnames = t;
s = args;
while ( *s ) {
if ( *s == ' ' || *s == '\t' ) { s++; continue; }
if ( *s == ',' ) {
s++; *t++ = 0; numargs++;
while ( *s == ' ' || *s == '\t' ) s++;
if ( *s == '?' ) {
if ( p->wildarg > 0 ) {
Error0("More than one ?var in #define");
}
p->wildarg = numargs;
}
}
else if ( *s == '?' && first ) {
p->wildarg = 1; *t++ = *s++;
}
else { *t++ = *s++; }
first = 0;
}
*t = 0;
numargs++;
p->nargs = numargs;
}
else {
p->nargs = 0;
p->argnames = 0;
}
if ( u ) M_free(u,"replace PreVar value");
return(ii);
}
/*
#] PutPreVar :
#[ PopPreVars :
*/
VOID PopPreVars(int tonumber)
{
PREVAR *p = &(PreVar[NumPre]);
while ( NumPre > tonumber ) {
NumPre--; p--;
M_free(p->name,"popping PreVar");
p->name = p->value = 0;
}
}
/*
#] PopPreVars :
#[ IniModule :
*/
VOID IniModule(int type)
{
GETIDENTITY
WORD **w, i;
CBUF *C = cbuf+AC.cbufnum;
/*[05nov2003 mt]:*/
#ifdef WITHMPI
/* To prevent
* (1) FlushOut() and PutOut() on the slaves to send a mess to the master
* compiling a module,
* (2) EndSort() called from poly_factorize_expression() on the master
* waits for the slaves.
*/
PF.parallel=0;
/*BTW, this was the bug preventing usage of more than 1 expression!*/
#endif
AR.BracketOn = 0;
AR.StoreData.dirtyflag = 0;
AC.bracketindexflag = 0;
AT.bracketindexflag = 0;
/*[06nov2003 mt]:*/
#ifdef WITHMPI
/* This flag may be set in the procedure tokenize(). */
AC.RhsExprInModuleFlag = 0;
/*[20oct2009 mt]:*/
PF.mkSlaveInfile=0;
PF.slavebuf.PObuffer=NULL;
for(i=0; i<NumExpressions; i++)
Expressions[i].vflags &= ~ISINRHS;
/*:[20oct2009 mt]*/
#endif
/*:[06nov2003 mt]*/
/*[19nov2003 mt]:*/
/*The module counter:*/
(AC.CModule)++;
/*:[19nov2003 mt]*/
if ( !type ) {
if ( C->rhs ) {
w = C->rhs; i = C->maxrhs;
do { *w++ = 0; } while ( --i > 0 );
}
if ( C->lhs ) {
w = C->lhs; i = C->maxlhs;
do { *w++ = 0; } while ( --i > 0 );
}
}
C->numlhs = C->numrhs = 0;
ClearTree(AC.cbufnum);
while ( AC.NumLabels > 0 ) {
AC.NumLabels--;
if ( AC.LabelNames[AC.NumLabels] ) M_free(AC.LabelNames[AC.NumLabels],"LabelName");
}
C->Pointer = C->Buffer;
AC.Commercial[0] = 0;
AC.IfStack = AC.IfHeap;
AC.arglevel = 0;
AC.termlevel = 0;
AC.IfLevel = 0;
AC.WhileLevel = 0;
AC.RepLevel = 0;
AC.insidelevel = 0;
AC.dolooplevel = 0;
AC.MustTestTable = 0;
AO.PrintType = 0; /* Otherwise statistics can get spoiled */
AC.ComDefer = 0;
AC.CollectFun = 0;
AM.S0->PolyWise = 0;
AC.SymChangeFlag = 0;
AP.lhdollarerror = 0;
AR.PolyFun = AC.lPolyFun;
AR.PolyFunInv = AC.lPolyFunInv;
AR.PolyFunType = AC.lPolyFunType;
AR.PolyFunExp = AC.lPolyFunExp;
AR.PolyFunVar = AC.lPolyFunVar;
AR.PolyFunPow = AC.lPolyFunPow;
AC.mparallelflag = AC.parallelflag | AM.hparallelflag;
AC.inparallelflag = 0;
AC.mProcessBucketSize = AC.ProcessBucketSize;
NumPotModdollars = 0;
AC.topolynomialflag = 0;
#ifdef WITHPTHREADS
if ( AM.totalnumberofthreads > 1 ) AS.MultiThreaded = 1;
else AS.MultiThreaded = 0;
for ( i = 1; i < AM.totalnumberofthreads; i++ ) {
AB[i]->T.S0->PolyWise = 0;
}
#endif
OpenTemp();
}
/*
#] IniModule :
#[ IniSpecialModule :
*/
VOID IniSpecialModule(int type)
{
DUMMYUSE(type);
}
/*
#] IniSpecialModule :
#[ PreProcessor :
*/
VOID PreProcessor()
{
int moduletype = FIRSTMODULE;
int specialtype = 0;
int error1 = 0, error2 = 0, retcode, numstatement, retval;
UBYTE c, *t, *s;
AP.StopWatchZero = GetRunningTime();
AC.compiletype = 0;
AP.PreContinuation = 0;
AP.PreAssignLevel = 0;
AP.gNumPre = NumPre;
AC.iPointer = AC.iBuffer;
AC.iPointer[0] = 0;
if ( AC.CheckpointFlag == -1 ) DoRecovery(&moduletype);
AC.CheckpointStamp = Timer(0);
for(;;) {
/* if ( A.StatisticsFlag ) CharOut(LINEFEED); */
IniModule(moduletype);
/*Re-define preprocessor variable CMODULE_ as a current module number, starting from 1*/
/*The module counter is AC.CModule, it is incremented in IniModule*/
{
UBYTE buf[24];/*64/Log_2[10] = 19.3, this is enough for any integer*/
NumToStr(buf,AC.CModule);
PutPreVar((UBYTE *)"CMODULE_",buf,0,1);
}
if ( specialtype ) IniSpecialModule(specialtype);
numstatement = 0;
for(;;) { /* Read a single line/statement */
c = GetChar(0);
if ( c == AP.ComChar ) { /* This line is commentary */
LoadInstruction(5);
if ( AC.CurrentStream->FoldName ) {
t = AP.preStart;
if ( *t && t[1] && t[2] == '#' && t[3] == ']' ) {
t += 4;
while ( *t == ' ' || *t == '\t' ) t++;
s = AC.CurrentStream->FoldName;
while ( *s == *t ) { s++; t++; }
if ( *s == 0 && ( *t == ' ' || *t == '\t'
|| *t == ':' ) ) {
while ( *t == ' ' || *t == '\t' ) t++;
if ( *t == ':' ) {
AC.CurrentStream = CloseStream(AC.CurrentStream);
}
}
}
}
*AP.preStart = 0;
continue;
}
while ( c == ' ' || c == '\t' ) c = GetChar(0);
if ( c == LINEFEED ) continue;
if ( c == ENDOFINPUT ) {
/* CharOut(LINEFEED); */
Warning(".end instruction generated");
moduletype = ENDMODULE; specialtype = 0;
goto endmodule; /* Fake one */
}
if ( c == '#' ) {
if ( PreProInstruction() ) { error1++; error2++; AP.preError++; }
*AP.preStart = 0;
}
else if ( c == '.' ) {
if ( ( AP.PreIfStack[AP.PreIfLevel] != EXECUTINGIF ) ||
( AP.PreSwitchModes[AP.PreSwitchLevel] != EXECUTINGPRESWITCH ) ) {
LoadInstruction(1);
continue;
}
if ( ModuleInstruction(&moduletype,&specialtype) ) { error2++; AP.preError++; }
if ( specialtype ) SetSpecialMode(moduletype,specialtype);
if ( AP.PreInsideLevel != 0 ) {
MesPrint("@end of module instructions may not be used inside");
MesPrint("@the scope of a %#inside %#endinside construction.");
Terminate(-1);
}
if ( AC.RepLevel > 0 ) {
MesPrint("&EndRepeat statement(s) missing");
error2++; AP.preError++;
}
if ( AC.tablecheck == 0 ) {
AC.tablecheck = 1;
if ( TestTables() ) { error2++; AP.preError++; }
}
if ( AP.PreContinuation ) {
error1++; error2++;
MesPrint("&Unfinished statement. Missing ;?");
}
if ( moduletype == GLOBALMODULE ) MakeGlobal();
else {
endmodule: if ( error2 == 0 && AM.qError == 0 ) {
retcode = ExecModule(moduletype);
#ifdef WITHMPI
if(PF.slavebuf.PObuffer!=NULL){
M_free(PF.slavebuf.PObuffer,"PF inbuf");
PF.slavebuf.PObuffer=NULL;
}
#endif
UpdatePositions();
if ( retcode < 0 ) error1++;
if ( retcode ) { error2++; AP.preError++; }
}
else {
EXPRESSIONS e;
WORD j;
for ( j = 0, e = Expressions; j < NumExpressions; j++, e++ ) {
if ( e->replace == NEWLYDEFINEDEXPRESSION ) e->replace = REGULAREXPRESSION;
}
}
switch ( moduletype ) {
case STOREMODULE:
if ( ExecStore() ) error1++;
break;
case CLEARMODULE:
FullCleanUp();
error1 = error2 = AP.preError = 0;
AM.atstartup = 1;
PutPreVar((UBYTE *)"DATE_",(UBYTE *)MakeDate(),0,1);
AM.atstartup = 0;
if ( AM.resetTimeOnClear ) {
#ifdef WITHPTHREADS
ClearAllThreads();
#endif
AM.SumTime += TimeCPU(1);
TimeCPU(0);