55 static KEYWORD com1commands[] = {
56 {
"also", (TFUN)CoIdOld, STATEMENT, PARTEST}
57 ,{
"abrackets", (TFUN)CoAntiBracket, TOOUTPUT, PARTEST}
58 ,{
"antisymmetrize", (TFUN)CoAntiSymmetrize, STATEMENT, PARTEST}
59 ,{
"antibrackets", (TFUN)CoAntiBracket, TOOUTPUT, PARTEST}
60 ,{
"brackets", (TFUN)CoBracket, TOOUTPUT, PARTEST}
61 ,{
"cfunctions", (TFUN)CoCFunction, DECLARATION, PARTEST|WITHAUTO}
62 ,{
"commuting", (TFUN)CoCFunction, DECLARATION, PARTEST|WITHAUTO}
63 ,{
"compress", (TFUN)CoCompress, DECLARATION, PARTEST}
64 ,{
"ctensors", (TFUN)CoCTensor, DECLARATION, PARTEST|WITHAUTO}
65 ,{
"cyclesymmetrize",(TFUN)CoCycleSymmetrize, STATEMENT, PARTEST}
66 ,{
"dimension", (TFUN)CoDimension, DECLARATION, PARTEST}
67 ,{
"discard", (TFUN)CoDiscard, STATEMENT, PARTEST}
68 ,{
"functions", (TFUN)CoNFunction, DECLARATION, PARTEST|WITHAUTO}
69 ,{
"format", (TFUN)CoFormat, TOOUTPUT, PARTEST}
70 ,{
"fixindex", (TFUN)CoFixIndex, DECLARATION, PARTEST}
71 ,{
"global", (TFUN)CoGlobal, DEFINITION, PARTEST}
72 ,{
"gfactorized", (TFUN)CoGlobalFactorized, DEFINITION, PARTEST}
73 ,{
"globalfactorized",(TFUN)CoGlobalFactorized,DEFINITION, PARTEST}
74 ,{
"goto", (TFUN)CoGoTo, STATEMENT, PARTEST}
75 ,{
"indexes", (TFUN)CoIndex, DECLARATION, PARTEST|WITHAUTO}
76 ,{
"indices", (TFUN)CoIndex, DECLARATION, PARTEST|WITHAUTO}
77 ,{
"identify", (TFUN)CoId, STATEMENT, PARTEST}
78 ,{
"idnew", (TFUN)CoIdNew, STATEMENT, PARTEST}
79 ,{
"idold", (TFUN)CoIdOld, STATEMENT, PARTEST}
80 ,{
"local", (TFUN)CoLocal, DEFINITION, PARTEST}
81 ,{
"lfactorized", (TFUN)CoLocalFactorized, DEFINITION, PARTEST}
82 ,{
"localfactorized",(TFUN)CoLocalFactorized, DEFINITION, PARTEST}
83 ,{
"load", (TFUN)CoLoad, DECLARATION, PARTEST}
84 ,{
"label", (TFUN)CoLabel, STATEMENT, PARTEST}
85 ,{
"modulus", (TFUN)CoModulus, DECLARATION, PARTEST}
86 ,{
"multiply", (TFUN)CoMultiply, STATEMENT, PARTEST}
87 ,{
"nfunctions", (TFUN)CoNFunction, DECLARATION, PARTEST|WITHAUTO}
88 ,{
"nprint", (TFUN)CoNPrint, TOOUTPUT, PARTEST}
89 ,{
"ntensors", (TFUN)CoNTensor, DECLARATION, PARTEST|WITHAUTO}
90 ,{
"nwrite", (TFUN)CoNWrite, DECLARATION, PARTEST}
91 ,{
"print", (TFUN)CoPrint, MIXED, 0}
92 ,{
"redefine", (TFUN)CoRedefine, STATEMENT, 0}
93 ,{
"rcyclesymmetrize",(TFUN)CoRCycleSymmetrize,STATEMENT, PARTEST}
94 ,{
"symbols", (TFUN)CoSymbol, DECLARATION, PARTEST|WITHAUTO}
95 ,{
"save", (TFUN)CoSave, DECLARATION, PARTEST}
96 ,{
"symmetrize", (TFUN)CoSymmetrize, STATEMENT, PARTEST}
97 ,{
"tensors", (TFUN)CoCTensor, DECLARATION, PARTEST|WITHAUTO}
98 ,{
"unittrace", (TFUN)CoUnitTrace, DECLARATION, PARTEST}
99 ,{
"vectors", (TFUN)CoVector, DECLARATION, PARTEST|WITHAUTO}
100 ,{
"write", (TFUN)CoWrite, DECLARATION, PARTEST}
103 static KEYWORD com2commands[] = {
104 {
"antiputinside", (TFUN)CoAntiPutInside, STATEMENT, PARTEST}
105 ,{
"apply", (TFUN)CoApply, STATEMENT, PARTEST}
106 ,{
"aputinside", (TFUN)CoAntiPutInside, STATEMENT, PARTEST}
107 ,{
"argexplode", (TFUN)CoArgExplode, STATEMENT, PARTEST}
108 ,{
"argimplode", (TFUN)CoArgImplode, STATEMENT, PARTEST}
109 ,{
"argument", (TFUN)CoArgument, STATEMENT, PARTEST}
110 ,{
"assign", (TFUN)CoAssign, STATEMENT, PARTEST}
111 ,{
"auto", (TFUN)CoAuto, DECLARATION, PARTEST}
112 ,{
"autodeclare", (TFUN)CoAuto, DECLARATION, PARTEST}
113 ,{
"chainin", (TFUN)CoChainin, STATEMENT, PARTEST}
114 ,{
"chainout", (TFUN)CoChainout, STATEMENT, PARTEST}
115 ,{
"chisholm", (TFUN)CoChisholm, STATEMENT, PARTEST}
116 ,{
"cleartable", (TFUN)CoClearTable, DECLARATION, PARTEST}
117 ,{
"collect", (TFUN)CoCollect, SPECIFICATION,PARTEST}
118 ,{
"contract", (TFUN)CoContract, STATEMENT, PARTEST}
119 ,{
"ctable", (TFUN)CoCTable, DECLARATION, PARTEST}
120 ,{
"deallocatetable",(TFUN)CoDeallocateTable, DECLARATION, PARTEST}
121 ,{
"delete", (TFUN)CoDelete, SPECIFICATION,PARTEST}
122 ,{
"denominators", (TFUN)CoDenominators, STATEMENT, PARTEST}
123 ,{
"disorder", (TFUN)CoDisorder, STATEMENT, PARTEST}
124 ,{
"do", (TFUN)CoDo, STATEMENT, PARTEST}
125 ,{
"drop", (TFUN)CoDrop, SPECIFICATION,PARTEST}
126 ,{
"dropcoefficient",(TFUN)CoDropCoefficient, STATEMENT, PARTEST}
127 ,{
"dropsymbols", (TFUN)CoDropSymbols, STATEMENT, PARTEST}
128 ,{
"else", (TFUN)CoElse, STATEMENT, PARTEST}
129 ,{
"elseif", (TFUN)CoElseIf, STATEMENT, PARTEST}
130 ,{
"endargument", (TFUN)CoEndArgument, STATEMENT, PARTEST}
131 ,{
"enddo", (TFUN)CoEndDo, STATEMENT, PARTEST}
132 ,{
"endif", (TFUN)CoEndIf, STATEMENT, PARTEST}
133 ,{
"endinexpression",(TFUN)CoEndInExpression, STATEMENT, PARTEST}
134 ,{
"endinside", (TFUN)CoEndInside, STATEMENT, PARTEST}
135 ,{
"endrepeat", (TFUN)CoEndRepeat, STATEMENT, PARTEST}
136 ,{
"endterm", (TFUN)CoEndTerm, STATEMENT, PARTEST}
137 ,{
"endwhile", (TFUN)CoEndWhile, STATEMENT, PARTEST}
138 ,{
"exit", (TFUN)CoExit, STATEMENT, PARTEST}
139 ,{
"extrasymbols", (TFUN)CoExtraSymbols, DECLARATION, PARTEST}
140 ,{
"factarg", (TFUN)CoFactArg, STATEMENT, PARTEST}
141 ,{
"factdollar", (TFUN)CoFactDollar, STATEMENT, PARTEST}
142 ,{
"factorize", (TFUN)CoFactorize, TOOUTPUT, PARTEST}
143 ,{
"fill", (TFUN)CoFill, DECLARATION, PARTEST}
144 ,{
"fillexpression", (TFUN)CoFillExpression, DECLARATION, PARTEST}
145 ,{
"frompolynomial", (TFUN)CoFromPolynomial, STATEMENT, PARTEST}
146 ,{
"funpowers", (TFUN)CoFunPowers, DECLARATION, PARTEST}
147 ,{
"hide", (TFUN)CoHide, SPECIFICATION,PARTEST}
148 ,{
"if", (TFUN)CoIf, STATEMENT, PARTEST}
149 ,{
"ifmatch", (TFUN)CoIfMatch, STATEMENT, PARTEST}
150 ,{
"ifnomatch", (TFUN)CoIfNoMatch, STATEMENT, PARTEST}
151 ,{
"ifnotmatch", (TFUN)CoIfNoMatch, STATEMENT, PARTEST}
152 ,{
"inexpression", (TFUN)CoInExpression, STATEMENT, PARTEST}
153 ,{
"inparallel", (TFUN)CoInParallel, SPECIFICATION,PARTEST}
154 ,{
"inside", (TFUN)CoInside, STATEMENT, PARTEST}
155 ,{
"insidefirst", (TFUN)CoInsideFirst, DECLARATION, PARTEST}
156 ,{
"intohide", (TFUN)CoIntoHide, SPECIFICATION,PARTEST}
157 ,{
"keep", (TFUN)CoKeep, SPECIFICATION,PARTEST}
158 ,{
"makeinteger", (TFUN)CoMakeInteger, STATEMENT, PARTEST}
159 ,{
"many", (TFUN)CoMany, STATEMENT, PARTEST}
160 ,{
"merge", (TFUN)CoMerge, STATEMENT, PARTEST}
161 ,{
"metric", (TFUN)CoMetric, DECLARATION, PARTEST}
162 ,{
"moduleoption", (TFUN)CoModuleOption, ATENDOFMODULE,PARTEST}
163 ,{
"multi", (TFUN)CoMulti, STATEMENT, PARTEST}
164 ,{
"multibracket", (TFUN)CoMultiBracket, STATEMENT, PARTEST}
165 ,{
"ndrop", (TFUN)CoNoDrop, SPECIFICATION,PARTEST}
166 ,{
"nfactorize", (TFUN)CoNFactorize, TOOUTPUT, PARTEST}
167 ,{
"nhide", (TFUN)CoNoHide, SPECIFICATION,PARTEST}
168 ,{
"normalize", (TFUN)CoNormalize, STATEMENT, PARTEST}
169 ,{
"notinparallel", (TFUN)CoNotInParallel, SPECIFICATION,PARTEST}
170 ,{
"nskip", (TFUN)CoNoSkip, SPECIFICATION,PARTEST}
171 ,{
"ntable", (TFUN)CoNTable, DECLARATION, PARTEST}
172 ,{
"nunfactorize", (TFUN)CoNUnFactorize, TOOUTPUT, PARTEST}
173 ,{
"nunhide", (TFUN)CoNoUnHide, SPECIFICATION,PARTEST}
174 ,{
"off", (TFUN)CoOff, DECLARATION, PARTEST}
175 ,{
"on", (TFUN)CoOn, DECLARATION, PARTEST}
176 ,{
"once", (TFUN)CoOnce, STATEMENT, PARTEST}
177 ,{
"only", (TFUN)CoOnly, STATEMENT, PARTEST}
178 ,{
"polyfun", (TFUN)CoPolyFun, DECLARATION, PARTEST}
179 ,{
"polyratfun", (TFUN)CoPolyRatFun, DECLARATION, PARTEST}
180 ,{
"pophide", (TFUN)CoPopHide, SPECIFICATION,PARTEST}
181 ,{
"print[]", (TFUN)CoPrintB, TOOUTPUT, PARTEST}
182 ,{
"printtable", (TFUN)CoPrintTable, MIXED, PARTEST}
183 ,{
"processbucketsize",(TFUN)CoProcessBucket, DECLARATION, PARTEST}
184 ,{
"propercount", (TFUN)CoProperCount, DECLARATION, PARTEST}
185 ,{
"pushhide", (TFUN)CoPushHide, SPECIFICATION,PARTEST}
186 ,{
"putinside", (TFUN)CoPutInside, STATEMENT, PARTEST}
187 ,{
"ratio", (TFUN)CoRatio, STATEMENT, PARTEST}
188 ,{
"renumber", (TFUN)CoRenumber, STATEMENT, PARTEST}
189 ,{
"repeat", (TFUN)CoRepeat, STATEMENT, PARTEST}
190 ,{
"replaceloop", (TFUN)CoReplaceLoop, STATEMENT, PARTEST}
191 ,{
"select", (TFUN)CoSelect, STATEMENT, PARTEST}
192 ,{
"set", (TFUN)CoSet, DECLARATION, PARTEST}
193 ,{
"setexitflag", (TFUN)CoSetExitFlag, STATEMENT, PARTEST}
194 ,{
"shuffle", (TFUN)CoMerge, STATEMENT, PARTEST}
195 ,{
"skip", (TFUN)CoSkip, SPECIFICATION,PARTEST}
196 ,{
"sort", (TFUN)CoSort, STATEMENT, PARTEST}
197 ,{
"splitarg", (TFUN)CoSplitArg, STATEMENT, PARTEST}
198 ,{
"splitfirstarg", (TFUN)CoSplitFirstArg, STATEMENT, PARTEST}
199 ,{
"splitlastarg", (TFUN)CoSplitLastArg, STATEMENT, PARTEST}
200 ,{
"stuffle", (TFUN)CoStuffle, STATEMENT, PARTEST}
201 ,{
"sum", (TFUN)CoSum, STATEMENT, PARTEST}
202 ,{
"table", (TFUN)CoTable, DECLARATION, PARTEST}
203 ,{
"tablebase", (TFUN)CoTableBase, DECLARATION, PARTEST}
204 ,{
"tb", (TFUN)CoTableBase, DECLARATION, PARTEST}
205 ,{
"term", (TFUN)CoTerm, STATEMENT, PARTEST}
206 ,{
"testuse", (TFUN)CoTestUse, STATEMENT, PARTEST}
207 ,{
"threadbucketsize",(TFUN)CoThreadBucket, DECLARATION, PARTEST}
208 ,{
"topolynomial", (TFUN)CoToPolynomial, STATEMENT, PARTEST}
209 ,{
"totensor", (TFUN)CoToTensor, STATEMENT, PARTEST}
210 ,{
"tovector", (TFUN)CoToVector, STATEMENT, PARTEST}
211 ,{
"trace4", (TFUN)CoTrace4, STATEMENT, PARTEST}
212 ,{
"tracen", (TFUN)CoTraceN, STATEMENT, PARTEST}
213 ,{
"transform", (TFUN)CoTransform, STATEMENT, PARTEST}
214 ,{
"tryreplace", (TFUN)CoTryReplace, STATEMENT, PARTEST}
215 ,{
"unfactorize", (TFUN)CoUnFactorize, TOOUTPUT, PARTEST}
216 ,{
"unhide", (TFUN)CoUnHide, SPECIFICATION,PARTEST}
217 ,{
"while", (TFUN)CoWhile, STATEMENT, PARTEST}
231 SUBBUF *subexpbuffers = 0;
232 SUBBUF *topsubexpbuffers = 0;
233 LONG insubexpbuffers = 0;
235 #define REDUCESUBEXPBUFFERS { if ( (topsubexpbuffers-subexpbuffers) > 256 ) {\ 236 M_free(subexpbuffers,"subexpbuffers");\ 237 subexpbuffers = (SUBBUF *)Malloc1(256*sizeof(SUBBUF),"subexpbuffers");\ 238 topsubexpbuffers = subexpbuffers+256; } insubexpbuffers = 0; } 242 #define PUTNUMBER128(t,n) { if ( n >= 16384 ) { \ 243 *t++ = n/(128*128); *t++ = (n/128)%128; *t++ = n%128; } \ 244 else if ( n >= 128 ) { *t++ = n/128; *t++ = n%128; } \ 246 #define PUTNUMBER100(t,n) { if ( n >= 10000 ) { \ 247 *t++ = n/10000; *t++ = (n/100)%100; *t++ = n%100; } \ 248 else if ( n >= 100 ) { *t++ = n/100; *t++ = n%100; } \ 251 #elif ( defined(LLP64) || defined(LP64) ) 253 #define PUTNUMBER128(t,n) { if ( n >= 2097152 ) { \ 254 *t++ = ((n/128)/128)/128; *t++ = ((n/128)/128)%128; *t++ = (n/128)%128; *t++ = n%128; } \ 255 else if ( n >= 16384 ) { \ 256 *t++ = n/(128*128); *t++ = (n/128)%128; *t++ = n%128; } \ 257 else if ( n >= 128 ) { *t++ = n/128; *t++ = n%128; } \ 259 #define PUTNUMBER100(t,n) { if ( n >= 1000000 ) { \ 260 *t++ = ((n/100)/100)/100; *t++ = ((n/100)/100)%100; *t++ = (n/100)%100; *t++ = n%100; } \ 261 else if ( n >= 10000 ) { \ 262 *t++ = n/10000; *t++ = (n/100)%100; *t++ = n%100; } \ 263 else if ( n >= 100 ) { *t++ = n/100; *t++ = n%100; } \ 283 ksize =
sizeof(com1commands)/
sizeof(
KEYWORD);
286 for ( i = 0; i < 26; i++ ) {
287 while ( j < ksize && k[j].name[0] ==
'a'+i ) j++;
305 KEYWORD *findcommand(UBYTE *in)
310 while ( FG.cTable[*s] <= 1 ) s++;
311 if ( s > in && *s ==
'[' && s[1] ==
']' ) s += 2;
312 if ( *s ) { c = *s; *s = 0; }
318 hi =
sizeof(com2commands)/
sizeof(
KEYWORD)-1;
320 med = ( hi + lo ) / 2;
321 i = StrICmp(in,(UBYTE *)com2commands[med].name);
322 if ( i == 0 ) {
if ( c ) *s = c;
return(com2commands+med); }
323 if ( i < 0 ) hi = med-1;
325 }
while ( hi >= lo );
329 i = tolower(*in) -
'a';
331 hi = alfatable1[i+1];
333 if ( StrICont(in,(UBYTE *)com1commands[med].name) == 0 )
334 {
if ( c ) *s = c;
return(com1commands+med); }
349 int ParenthesesTest(UBYTE *sin)
351 WORD L1 = 0, L2 = 0, L3 = 0;
354 if ( *s ==
'[' ) L1++;
355 else if ( *s ==
']' ) {
357 if ( L1 < 0 ) { MesPrint(
"&Unmatched []");
return(1); }
361 if ( L1 > 0 ) { MesPrint(
"&Unmatched []");
return(1); }
364 if ( *s ==
'[' ) SKIPBRA1(s)
365 else if ( *s ==
'(' ) { L2++; s++; }
366 else if ( *s ==
')' ) {
368 if ( L2 < 0 ) { MesPrint(
"&Unmatched ()");
return(1); }
372 if ( L2 > 0 ) { MesPrint(
"&Unmatched ()");
return(1); }
375 if ( *s ==
'[' ) SKIPBRA1(s)
376 else if ( *s ==
'[' ) SKIPBRA4(s)
377 else if ( *s ==
'{' ) { L3++; s++; }
378 else if ( *s ==
'}' ) {
380 if ( L3 < 0 ) { MesPrint(
"&Unmatched {}");
return(1); }
384 if ( L3 > 0 ) { MesPrint(
"&Unmatched {}");
return(1); }
399 UBYTE *SkipAName(UBYTE *s)
405 MesPrint(
"&Illegal name: '%s'",t);
410 else if ( FG.cTable[*s] == 0 || *s ==
'_' || *s ==
'$' ) {
411 if ( *s ==
'$' ) s++;
412 while ( FG.cTable[*s] <= 1 ) s++;
413 if ( *s ==
'_' ) s++;
416 MesPrint(
"&Illegal name: '%s'",t);
427 UBYTE *IsRHS(UBYTE *s, UBYTE c)
429 while ( *s && *s != c ) {
433 MesPrint(
"&Unmatched []");
437 else if ( *s ==
'{' ) {
440 MesPrint(
"&Unmatched {}");
444 else if ( *s ==
'(' ) {
447 MesPrint(
"&Unmatched ()");
451 else if ( *s ==
')' ) {
452 MesPrint(
"&Unmatched ()");
455 else if ( *s ==
'}' ) {
456 MesPrint(
"&Unmatched {}");
459 else if ( *s ==
']' ) {
460 MesPrint(
"&Unmatched []");
473 int IsIdStatement(UBYTE *s)
487 int CompileAlgebra(UBYTE *s,
int leftright, WORD *prototype)
491 WORD *oldproto = AC.ProtoType;
492 AC.ProtoType = prototype;
493 if ( AC.TokensWriteFlag ) {
494 MesPrint(
"To tokenize: %s",s);
495 error = tokenize(s,leftright);
496 MesPrint(
" The contents of the token buffer are:");
497 WriteTokens(AC.tokens);
499 else error = tokenize(s,leftright);
501 AR.Eside = leftright;
503 if ( leftright == LHSIDE ) { AC.DumNum = AR.CurDum = 0; }
504 error = CompileSubExpressions(AC.tokens);
508 AC.ProtoType = oldproto;
511 AC.ProtoType = oldproto;
512 if ( error < 0 )
return(-1);
513 else if ( leftright == LHSIDE )
return(cbuf[AC.cbufnum].numlhs);
514 else return(cbuf[AC.cbufnum].numrhs);
523 int CompileStatement(UBYTE *in)
527 int error1 = 0, error2;
529 if ( *s == 0 )
return(0);
531 k = findcommand((UBYTE *)
"assign");
534 if ( ( k = findcommand(s) ) == 0 && IsIdStatement(s) == 0 ) {
535 MesPrint(
"&Unrecognized statement");
539 k = com1commands + alfatable1[
'i'-
'a'];
540 while ( k->name[1] !=
'd' || k->name[2] ) k++;
543 while ( FG.cTable[*s] <= 1 ) s++;
544 if ( s > in && *s ==
'[' && s[1] ==
']' ) s += 2;
555 if ( *s ==
',' ) s++;
563 if ( AP.PreAssignFlag == 0 && AM.OldOrderFlag == 0 ) {
564 if ( AP.PreInsideLevel ) {
565 if ( k->type != STATEMENT && k->type != MIXED ) {
566 MesPrint(
"&Only executable and print statements are allowed in an %#inside/%#endinside construction");
571 if ( ( AC.compiletype == DECLARATION || AC.compiletype == SPECIFICATION )
572 && ( k->type == STATEMENT || k->type == DEFINITION || k->type == TOOUTPUT ) ) {
573 if ( AC.tablecheck == 0 ) {
575 if ( TestTables() ) error1 = 1;
578 if ( k->type == MIXED ) {
579 if ( AC.compiletype <= DEFINITION ) {
580 AC.compiletype = STATEMENT;
583 else if ( k->type > AC.compiletype ) {
584 if ( StrCmp((UBYTE *)(k->name),(UBYTE *)
"format") != 0 )
585 AC.compiletype = k->type;
587 else if ( k->type < AC.compiletype ) {
590 MesPrint(
"&Declaration out of order");
594 MesPrint(
"&Definition out of order");
598 MesPrint(
"&Specification out of order");
602 MesPrint(
"&Statement out of order");
605 MesPrint(
"&Output control statement out of order");
609 AC.compiletype = k->type;
610 if ( AC.firstctypemessage == 0 ) {
611 MesPrint(
"&Proper order inside a module is:");
612 MesPrint(
"Declarations, specifications, definitions, statements, output control statements");
613 AC.firstctypemessage = 1;
622 if ( AC.AutoDeclareFlag && ( ( k->flags & WITHAUTO ) == 0 ) ) {
623 MesPrint(
"&Illegal type of auto-declaration");
626 if ( ( ( k->flags & PARTEST ) != 0 ) && ParenthesesTest(s) )
return(1);
627 error2 = (*k->func)(s);
628 if ( error2 == 0 )
return(error1);
644 i = NumFunctions + FUNCTION - MAXBUILTINFUNCTION - 1;
645 f = f + MAXBUILTINFUNCTION - FUNCTION + 1;
646 if ( AC.MustTestTable > 0 ) {
649 for ( x = 0, j = 0; x < t->
totind; x++ ) {
654 MesPrint(
"&In table %s there are %d unfilled elements",
655 AC.varnames->namebuffer+f->
name,j);
658 MesPrint(
"&In table %s there is one unfilled element",
659 AC.varnames->namebuffer+f->
name);
681 int CompileSubExpressions(SBYTE *tokens)
684 SBYTE *fill = tokens, *s = tokens, *t;
685 WORD number[MAXNUMSIZE], *oldwork, *w1, *w2;
686 int level, num, i, sumlevel = 0, sumtype = SYMTOSYM;
687 int retval, error = 0;
692 while ( *s != TENDOFIT ) {
693 if ( *s == TFUNOPEN ) {
694 if ( fill < s ) *fill = TENDOFIT;
696 while ( t >= tokens && t[0] >= 0 ) t--;
697 if ( t >= tokens && *t == TFUNCTION ) {
698 t++; i = 0;
while ( *t >= 0 ) i = 128*i + *t++;
699 if ( i == AM.sumnum || i == AM.sumpnum ) {
701 if ( *t == TSYMBOL || *t == TINDEX ) {
702 t++; i = 0;
while ( *t >= 0 ) i = 128*i + *t++;
703 if ( s[1] == TINDEX ) {
707 else sumtype = SYMTOSYM;
714 else if ( *s == TFUNCLOSE ) { sumlevel = 0; *fill++ = *s++; }
715 else if ( *s == LPARENTHESIS ) {
724 while ( level >= 0 ) {
726 if ( *s == LPARENTHESIS ) level++;
727 else if ( *s == RPARENTHESIS ) level--;
728 else if ( *s == TENDOFIT ) {
729 MesPrint(
"&Unbalanced subexpression parentheses");
734 if ( sumlevel > 0 ) {
735 oldwork = w1 = AT.WorkPointer;
738 while ( --i >= 0 ) *w1++ = *w2++;
740 *w1++ = sumtype; *w1++ = 4; *w1++ = sumlevel; *w1++ = sumlevel;
741 w2 = AC.ProtoType; AT.WorkPointer = w1;
742 AC.ProtoType = oldwork;
743 num = CompileSubExpressions(t);
744 AC.ProtoType = w2; AT.WorkPointer = oldwork;
746 else num = CompileSubExpressions(t);
747 if ( num < 0 )
return(-1);
755 if ( insubexpbuffers >= 0x3FFFFFL ) {
756 MesPrint(
"&More than 2^22 subexpressions inside one expression");
759 if ( subexpbuffers+insubexpbuffers >= topsubexpbuffers ) {
760 DoubleBuffer((
void **)((VOID *)(&subexpbuffers))
761 ,(
void **)((VOID *)(&topsubexpbuffers)),
sizeof(
SUBBUF),
"subexpbuffers");
763 subexpbuffers[insubexpbuffers].subexpnum = num;
764 subexpbuffers[insubexpbuffers].buffernum = AC.cbufnum;
765 num = insubexpbuffers++;
768 do { number[i++] = num & 0x7F; num >>= 7; }
while ( num );
769 while ( --i >= 0 ) *fill++ = (SBYTE)(number[i]);
772 else if ( *s == TEMPTY ) s++;
780 if ( AC.CompileLevel == 1 && AC.ToBeInFactors ) {
781 error = CodeFactors(tokens);
784 retval = CodeGenerator(tokens);
785 if ( error < 0 )
return(error);
814 static UWORD *CGscrat7 = 0;
816 int CodeGenerator(SBYTE *tokens)
819 SBYTE *s = tokens, c;
820 int i, sign = 1, first = 1, deno = 1, error = 0, minus, n, needarg, numexp, cc;
821 int base, sumlevel = 0, sumtype = SYMTOSYM, firstsumarg, inset = 0;
822 int funflag = 0, settype, x1, x2, mulflag = 0;
823 WORD *t, *v, *r, *term, nnumerator, ndenominator, *oldwork, x3, y, nin;
824 WORD *w1, *w2, *tsize = 0, *relo = 0;
825 UWORD *numerator, *denominator, *innum;
828 WORD TMproto[SUBEXPSIZE];
835 if ( AC.TokensWriteFlag ) WriteTokens(tokens);
837 CGscrat7 = (UWORD *)Malloc1((AM.MaxTal+2)*
sizeof(WORD),
"CodeGenerator");
838 AddRHS(AC.cbufnum,0);
839 C = cbuf + AC.cbufnum;
843 oldwork = AT.WorkPointer;
844 numerator = (UWORD *)(AT.WorkPointer);
845 denominator = numerator + 2*AM.MaxTal;
846 innum = denominator + 2*AM.MaxTal;
847 term = (WORD *)(innum + 2*AM.MaxTal);
848 AT.WorkPointer = term + AM.MaxTer/
sizeof(WORD);
849 if ( AT.WorkPointer > AT.WorkTop )
goto OverWork;
852 numerator[0] = denominator[0] = 1;
853 nnumerator = ndenominator = 1;
854 while ( *s != TENDOFIT ) {
855 if ( *s == TPLUS || *s == TMINUS ) {
856 if ( first || mulflag ) {
if ( *s == TMINUS ) sign = -sign; }
860 if ( cc && sign ) C->
CanCommu[numexp]++;
861 CompleteTerm(term,numerator,denominator,nnumerator,ndenominator,sign);
862 first = 1; cc = 0; t = term + 1; deno = 1;
863 numerator[0] = denominator[0] = 1;
864 nnumerator = ndenominator = 1;
865 if ( *s == TMINUS ) sign = -1;
871 mulflag = first = 0; c = *s++;
874 x1 = 0;
while ( *s >= 0 ) { x1 = x1*128 + *s++; }
875 if ( *s == TWILDCARD ) { s++; x1 += 2*MAXPOWER; }
876 *t++ = SYMBOL; *t++ = 4; *t++ = x1;
877 if ( inset ) *relo = 2;
878 TryPower:
if ( *s == TPOWER ) {
880 if ( *s == TMINUS ) { s++; deno = -deno; }
882 base = ( c == TNUMBER ) ? 100: 128;
883 x2 = 0;
while ( *s >= 0 ) { x2 = base*x2 + *s++; }
884 if ( c == TSYMBOL ) {
885 if ( *s == TWILDCARD ) s++;
893 while ( relo < AT.WorkTop ) *t++ = *relo++;
894 inset = 0; tsize[1] = t - tsize;
898 x1 = 0;
while ( *s >= 0 ) { x1 = x1*128 + *s++; }
899 *t++ = INDEX; *t++ = 3;
900 if ( *s == TWILDCARD ) { s++; x1 += WILDOFFSET; }
901 if ( inset ) { *t++ = x1; *relo = 2; }
902 else *t++ = x1 + AM.OffsetIndex;
903 if ( t[-1] > AM.IndDum ) {
904 x1 = t[-1] - AM.IndDum;
909 *t++ = INDEX; *t++ = 3; *t++ = AC.DumNum+WILDOFFSET;
913 x1 = 0;
while ( *s >= 0 ) { x1 = x1*128 + *s++; }
914 dovector:
if ( inset == 0 ) x1 += AM.OffsetVector;
915 if ( *s == TWILDCARD ) { s++; x1 += WILDOFFSET; }
916 if ( inset ) *relo = 2;
919 if ( *s == TSETNUM || *s == TSETDOL ) {
920 settype = ( *s == TSETDOL );
921 s++; x2 = 0;
while ( *s >= 0 ) { x2 = x2*128 + *s++; }
922 if ( settype ) x2 = -x2;
924 tsize = t; *t++ = SETSET; *t++ = 0;
928 *--relo = x2; *--relo = 3;
930 if ( *s != TVECTOR && *s != TDUBIOUS ) {
931 MesPrint(
"&Illegally formed dotproduct");
934 s++; x2 = 0;
while ( *s >= 0 ) { x2 = x2*128 + *s++; }
935 if ( inset < 2 ) x2 += AM.OffsetVector;
936 if ( *s == TWILDCARD ) { s++; x2 += WILDOFFSET; }
937 *t++ = DOTPRODUCT; *t++ = 5; *t++ = x1; *t++ = x2;
940 else if ( *s == TFUNOPEN ) {
942 if ( *s == TSETNUM || *s == TSETDOL ) {
943 settype = ( *s == TSETDOL );
944 s++; x2 = 0;
while ( *s >= 0 ) { x2 = x2*128 + *s++; }
945 if ( settype ) x2 = -x2;
947 tsize = t; *t++ = SETSET; *t++ = 0;
951 *--relo = x2; *--relo = 3;
953 if ( *s == TINDEX || *s == TDUBIOUS ) {
955 x2 = 0;
while ( *s >= 0 ) { x2 = x2*128 + *s++; }
956 if ( inset < 2 ) x2 += AM.OffsetIndex;
957 if ( *s == TWILDCARD ) { s++; x2 += WILDOFFSET; }
958 *t++ = VECTOR; *t++ = 4; *t++ = x1; *t++ = x2;
959 if ( t[-1] > AM.IndDum ) {
960 x2 = t[-1] - AM.IndDum;
964 else if ( *s == TGENINDEX ) {
965 *t++ = VECTOR; *t++ = 4; *t++ = x1;
966 *t++ = AC.DumNum + WILDOFFSET;
968 else if ( *s == TNUMBER || *s == TNUMBER1 ) {
969 base = ( *s == TNUMBER ) ? 100: 128;
971 x2 = 0;
while ( *s >= 0 ) { x2 = x2*base + *s++; }
972 if ( x2 >= AM.OffsetIndex && inset < 2 ) {
973 MesPrint(
"&Fixed index in vector greater than %d",
977 *t++ = VECTOR; *t++ = 4; *t++ = x1; *t++ = x2;
979 else if ( *s == TVECTOR || ( *s == TMINUS && s[1] == TVECTOR ) ) {
980 if ( *s == TMINUS ) { s++; sign = -sign; }
982 x2 = 0;
while ( *s >= 0 ) { x2 = x2*128 + *s++; }
983 if ( inset < 2 ) x2 += AM.OffsetVector;
984 if ( *s == TWILDCARD ) { s++; x2 += WILDOFFSET; }
985 *t++ = DOTPRODUCT; *t++ = 5; *t++ = x1; *t++ = x2; *t++ = deno;
988 MesPrint(
"&Illegal argument for vector");
991 if ( *s != TFUNCLOSE ) {
992 MesPrint(
"&Illegal argument for vector");
999 *t++ = VECTOR; *t++ = 4; *t++ = x1;
1000 *t++ = AC.DumNum + WILDOFFSET;
1003 *t++ = INDEX; *t++ = 3; *t++ = x1;
1008 if ( *s != TFUNOPEN ) {
1009 MesPrint(
"&d_ needs two arguments");
1012 v = t; *t++ = DELTA; *t++ = 4;
1013 needarg = 2; x3 = x1 = -1;
1016 x1 = 0;
while ( *s >= 0 ) { x1 = x1*128 + *s++; }
1017 if ( x1 == AM.sumnum || x1 == AM.sumpnum ) sumlevel = x1;
1019 if ( x1 == FIRSTBRACKET ) {
1020 if ( s[0] == TFUNOPEN && s[1] == TEXPRESSION ) {
1022 *t++ = x1; *t++ = FUNHEAD+2; *t++ = 0;
1024 x2 = 0;
while ( *s >= 0 ) { x2 = x2*128 + *s++; }
1025 *t++ = -EXPRESSION; *t++ = x2;
1031 if ( Expressions[x2].status == STOREDEXPRESSION ) {
1032 TMproto[0] = EXPRESSION;
1033 TMproto[1] = SUBEXPSIZE;
1036 {
int ie;
for ( ie = 4; ie < SUBEXPSIZE; ie++ ) TMproto[ie] = 0; }
1037 AT.TMaddr = TMproto;
1054 if ( ( renumber = GetTable(x2,&position,0) ) == 0 ) {
1056 MesPrint(
"&Problems getting information about stored expression %s(1)" 1059 if ( renumber->
symb.
lo != AN.dummyrenumlist )
1060 M_free(renumber->
symb.
lo,
"VarSpace");
1061 M_free(renumber,
"Renumber");
1062 AR.StoreData.dirtyflag = 1;
1064 if ( *s != TFUNCLOSE ) {
1065 if ( x1 == FIRSTBRACKET )
1066 MesPrint(
"&Problems with argument of FirstBracket_");
1067 else if ( x1 == FIRSTTERM )
1068 MesPrint(
"&Problems with argument of FirstTerm_");
1069 else if ( x1 == CONTENTTERM )
1070 MesPrint(
"&Problems with argument of FirstTerm_");
1071 else if ( x1 == TERMSINEXPR )
1072 MesPrint(
"&Problems with argument of TermsIn_");
1073 else if ( x1 == NUMFACTORS )
1074 MesPrint(
"&Problems with argument of NumFactors_");
1076 MesPrint(
"&Problems with argument of FactorIn_");
1078 while ( *s != TENDOFIT && *s != TFUNCLOSE ) s++;
1080 if ( *s == TFUNCLOSE ) s++;
1084 else if ( x1 == TERMSINEXPR || x1 == FACTORIN
1085 || x1 == NUMFACTORS || x1 == FIRSTTERM || x1 == CONTENTTERM ) {
1086 if ( s[0] == TFUNOPEN && s[1] == TEXPRESSION )
goto doexpr;
1087 if ( s[0] == TFUNOPEN && s[1] == TDOLLAR ) {
1089 *t++ = x1; *t++ = FUNHEAD+2; *t++ = 0;
1091 x2 = 0;
while ( *s >= 0 ) { x2 = x2*128 + *s++; }
1092 *t++ = -DOLLAREXPRESSION; *t++ = x2;
1093 if ( *s != TFUNCLOSE ) {
1094 if ( x1 == TERMSINEXPR )
1095 MesPrint(
"&Problems with argument of TermsIn_");
1096 else if ( x1 == NUMFACTORS )
1097 MesPrint(
"&Problems with argument of NumFactors_");
1099 MesPrint(
"&Problems with argument of FactorIn_");
1101 while ( *s != TENDOFIT && *s != TFUNCLOSE ) s++;
1103 if ( *s == TFUNCLOSE ) s++;
1108 if ( inset && ( t-tsize == 2 ) ) x1 -= FUNCTION;
1109 if ( *s == TWILDCARD ) { x1 += WILDOFFSET; s++; }
1110 if ( functions[x3-FUNCTION].commute ) cc = 1;
1111 if ( *s != TFUNOPEN ) {
1112 *t++ = x1; *t++ = FUNHEAD; *t++ = 0;
1113 FILLFUN3(t) sumlevel = 0;
goto fin;
1115 v = t; *t++ = x1; *t++ = FUNHEAD; *t++ = DIRTYFLAG; FILLFUN3(t)
1117 if ( !inset && functions[x3-FUNCTION].spec >= TENSORFUNCTION ) {
1120 if ( needarg == 0 ) {
1123 if ( x3 >= FUNCTION+WILDOFFSET ) x3 -= WILDOFFSET;
1124 MesPrint(
"&Too many arguments in function %s",
1125 VARNAME(functions,(x3-FUNCTION)) );
1128 MesPrint(
"&d_ needs exactly two arguments");
1132 else if ( needarg > 0 ) needarg--;
1135 if ( c == TMINUS && *s == TVECTOR ) { sign = -sign; c = *s++; }
1136 base = ( c == TNUMBER ) ? 100: 128;
1137 x2 = 0;
while ( *s >= 0 ) { x2 = base*x2 + *s++; }
1138 if ( *s == TWILDCARD && c != TNUMBER ) { x2 += WILDOFFSET; s++; }
1139 if ( c == TSETNUM || c == TSETDOL ) {
1140 if ( c == TSETDOL ) x2 = -x2;
1142 w1 = t; t += 2; w2 = t;
1143 while ( w1 > v ) *--w2 = *--w1;
1144 tsize = v; relo = AT.WorkTop;
1145 *v++ = SETSET; *v++ = 0;
1147 inset = 2; *--relo = x2; *--relo = t - v;
1149 x2 = 0;
while ( *s >= 0 ) x2 = 128*x2 + *s++;
1153 if ( t[-1]+AM.OffsetIndex > AM.IndDum ) {
1154 x2 = t[-1]+AM.OffsetIndex - AM.IndDum;
1161 if ( x2 >= 0 && x2 < AM.OffsetIndex ) {
1165 MesPrint(
"&Illegal type of set inside tensor");
1171 else {
switch ( c ) {
1173 if ( inset < 2 ) *t++ = x2 + AM.OffsetIndex;
1175 if ( x2+AM.OffsetIndex > AM.IndDum ) {
1176 x2 = x2+AM.OffsetIndex - AM.IndDum;
1181 *t++ = AC.DumNum + WILDOFFSET;
1184 if ( inset < 2 ) *t++ = x2 + AM.OffsetVector;
1188 *t++ = FUNNYWILD; *t++ = x2;
1192 *t++ = FUNNYDOLLAR; *t++ = x2;
1195 if ( inset < 2 ) *t++ = x2 + AM.OffsetVector;
1199 if ( x3 != GAMMA ) {
1200 MesPrint(
"&5_,6_,7_ can only be used inside g_");
1207 if ( x2 >= AM.OffsetIndex && inset < 2 ) {
1208 MesPrint(
"&Value of constant index in tensor too large");
1214 MesPrint(
"&Illegal object in tensor");
1218 if ( inset >= 2 ) inset = 1;
1219 }
while ( *s == TCOMMA );
1222 dofunction: firstsumarg = 1;
1226 if ( c == TMINUS && ( *s == TVECTOR || *s == TNUMBER
1227 || *s == TNUMBER1 || *s == TSUBEXP ) ) {
1228 minus = 1; c = *s++;
1231 base = ( c == TNUMBER ) ? 100: 128;
1232 x2 = 0;
while ( *s >= 0 ) { x2 = base*x2 + *s++; }
1236 if ( firstsumarg ) {
1238 if ( sumlevel > 0 ) {
1239 if ( c == TSYMBOL ) {
1240 sumlevel = x2; sumtype = SYMTOSYM;
1242 else if ( c == TINDEX ) {
1243 sumlevel = x2+AM.OffsetIndex; sumtype = INDTOIND;
1244 if ( sumlevel > AM.IndDum ) {
1245 x2 = sumlevel - AM.IndDum;
1251 if ( *s == TWILDCARD ) {
1252 if ( c == TSYMBOL ) x2 += 2*MAXPOWER;
1253 else if ( c != TNUMBER ) x2 += WILDOFFSET;
1258 *t++ = -SYMBOL; *t++ = x2;
break;
1260 *t++ = -DOLLAREXPRESSION; *t++ = x2;
break;
1262 *t++ = -EXPRESSION; *t++ = x2;
1268 if ( Expressions[x2].status == STOREDEXPRESSION ) {
1269 TMproto[0] = EXPRESSION;
1270 TMproto[1] = SUBEXPSIZE;
1273 {
int ie;
for ( ie = 4; ie < SUBEXPSIZE; ie++ ) TMproto[ie] = 0; }
1274 AT.TMaddr = TMproto;
1291 if ( ( renumber = GetTable(x2,&position,0) ) == 0 ) {
1293 MesPrint(
"&Problems getting information about stored expression %s(2)" 1296 if ( renumber->
symb.
lo != AN.dummyrenumlist )
1297 M_free(renumber->
symb.
lo,
"VarSpace");
1298 M_free(renumber,
"Renumber");
1299 AR.StoreData.dirtyflag = 1;
1303 *t++ = -INDEX; *t++ = x2 + AM.OffsetIndex;
1304 if ( t[-1] > AM.IndDum ) {
1305 x2 = t[-1] - AM.IndDum;
1310 *t++ = -INDEX; *t++ = AC.DumNum + WILDOFFSET;
1313 if ( minus ) *t++ = -MINVECTOR;
1314 else *t++ = -VECTOR;
1315 *t++ = x2 + AM.OffsetVector;
1318 MesPrint(
"&5_,6_,7_ can only be used inside g_");
1324 *t++ = -SYMBOL; *t++ = x2;
break;
1326 *t++ = -x2-FUNCTION;
1329 *t++ = -ARGWILD; *t++ = x2;
break;
1334 w1 = t; t += 2; w2 = t;
1335 while ( w1 > v ) *--w2 = *--w1;
1336 tsize = v; relo = AT.WorkTop;
1337 *v++ = SETSET; *v++ = 0;
1340 *--relo = x2; *--relo = t-v+1;
1342 x2 = 0;
while ( *s >= 0 ) x2 = 128*x2 + *s++;
1345 (*relo)--; *t++ = -x2-1;
break;
1347 *t++ = -SYMBOL; *t++ = x2;
break;
1349 *t++ = -INDEX; *t++ = x2;
1350 if ( x2+AM.OffsetIndex > AM.IndDum ) {
1351 x2 = x2+AM.OffsetIndex - AM.IndDum;
1356 *t++ = -VECTOR; *t++ = x2;
break;
1358 *t++ = -SNUMBER; *t++ = x2;
break;
1360 MesPrint(
"&Internal error 435");
1362 *t++ = -SYMBOL; *t++ = x2;
break;
1366 w2 = AC.ProtoType; i = w2[1];
1372 while ( --i >= 0 ) *t++ = *w2++;
1373 w1[ARGHEAD+3] = subexpbuffers[x2].subexpnum;
1374 w1[ARGHEAD+5] = subexpbuffers[x2].buffernum;
1375 if ( sumlevel > 0 ) {
1379 *t++ = sumtype; *t++ = 4;
1380 *t++ = sumlevel; *t++ = sumlevel;
1383 if ( minus ) *t++ = -3;
1388 if ( minus ) x2 = -x2;
1393 MesPrint(
"&Illegal object in function");
1397 }
while ( *s == TCOMMA );
1399 if ( *s != TFUNCLOSE ) {
1400 MesPrint(
"&Illegal argument field for function. Expected )");
1414 x1 = 0;
while ( *s >= 0 ) x1 = 128*x1 + *s++;
1415 if ( *s == TWILDCARD ) s++;
1416 if ( *s == TDOT )
goto dovector;
1417 if ( *s == TFUNOPEN ) {
1420 v = t; *t++ = x1; *t++ = FUNHEAD; *t++ = DIRTYFLAG;
1422 needarg = -1;
goto dofunction;
1424 *t++ = SYMBOL; *t++ = 4; *t++ = 0;
1425 if ( inset ) *relo = 2;
1428 x1 = 0;
while ( *s >= 0 ) { x1 = x1*128 + *s++; }
1429 if ( *s == TPOWER ) {
1431 base = ( c == TNUMBER ) ? 100: 128;
1432 x2 = 0;
while ( *s >= 0 ) { x2 = base*x2 + *s++; }
1433 if ( *s == TWILDCARD ) { x2 += 2*MAXPOWER; s++; }
1434 else if ( c == TSYMBOL ) x2 += 2*MAXPOWER;
1437 r = AC.ProtoType; n = r[1] - 5; r += 5;
1438 *t++ = SUBEXPRESSION; *t++ = r[-4];
1439 *t++ = subexpbuffers[x1].subexpnum;
1441 *t++ = subexpbuffers[x1].buffernum;
1443 if ( cbuf[subexpbuffers[x1].buffernum].CanCommu[subexpbuffers[x1].subexpnum] ) cc = 1;
1455 x1 = 0;
while ( *s >= 0 ) { x1 = x1*128 + *s++; }
1457 *t++ = EXPRESSION; *t++ = SUBEXPSIZE; *t++ = x1; *t++ = deno;
1458 *t++ = 0; FILLSUB(t)
1464 if ( *s == TFUNOPEN ) {
1467 base = ( c == TNUMBER ) ? 100: 128;
1468 x2 = 0;
while ( *s >= 0 ) { x2 = base*x2 + *s++; }
1471 *t++ = SYMBOL; *t++ = 4; *t++ = x2; *t++ = 1;
1474 *t++ = INDEX; *t++ = 3; *t++ = x2+AM.OffsetIndex;
1475 if ( t[-1] > AM.IndDum ) {
1476 x2 = t[-1] - AM.IndDum;
1481 *t++ = INDEX; *t++ = 3; *t++ = x2+AM.OffsetVector;
1484 *t++ = x2+FUNCTION; *t++ = 2;
break;
1487 if ( x2 >= AM.OffsetIndex || x2 < 0 ) {
1488 MesPrint(
"&Index as argument of expression has illegal value");
1491 *t++ = INDEX; *t++ = 3; *t++ = x2;
break;
1496 w1 = t; t += 2; w2 = t;
1497 while ( w1 > v ) *--w2 = *--w1;
1498 tsize = v; relo = AT.WorkTop;
1499 *v++ = SETSET; *v++ = 0;
1502 *--relo = x2; *--relo = t-v+2;
1504 x2 = 0;
while ( *s >= 0 ) x2 = 128*x2 + *s++;
1507 *relo -= 2; *t++ = -x2-1;
break;
1509 *t++ = SYMBOL; *t++ = 4; *t++ = x2; *t++ = 1;
break;
1511 *t++ = INDEX; *t++ = 3; *t++ = x2;
1512 if ( x2+AM.OffsetIndex > AM.IndDum ) {
1513 x2 = x2+AM.OffsetIndex - AM.IndDum;
1518 *t++ = VECTOR; *t++ = 3; *t++ = x2;
break;
1520 *t++ = SNUMBER; *t++ = 4; *t++ = x2; *t++ = 1;
break;
1522 MesPrint(
"&Internal error 435");
1524 *t++ = SYMBOL; *t++ = 4; *t++ = x2; *t++ = 1;
break;
1528 MesPrint(
"&Argument of expression can only be symbol, index, vector or function");
1532 }
while ( *s == TCOMMA );
1533 if ( *s != TFUNCLOSE ) {
1534 MesPrint(
"&Illegal object in argument field for expression");
1536 while ( *s != TFUNCLOSE ) s++;
1540 r = AC.ProtoType; n = r[1];
1541 if ( n > SUBEXPSIZE ) {
1542 *t++ = WILDCARDS; *t++ = n+2;
1550 if ( Expressions[x1].status == STOREDEXPRESSION ) {
1569 if ( ( renumber = GetTable(x1,&position,0) ) == 0 ) {
1571 MesPrint(
"&Problems getting information about stored expression %s(3)" 1574 if ( renumber->
symb.
lo != AN.dummyrenumlist )
1575 M_free(renumber->
symb.
lo,
"VarSpace");
1576 M_free(renumber,
"Renumber");
1577 AR.StoreData.dirtyflag = 1;
1579 if ( *s == LBRACE ) {
1587 if ( *s != TSUBEXP ) {
1588 MesPrint(
"&Internal error 23");
1591 s++; x2 = 0;
while ( *s >= 0 ) { x2 = 128*x2 + *s++; }
1592 r = cbuf[subexpbuffers[x2].buffernum].rhs[subexpbuffers[x2].subexpnum];
1593 *t++ = FROMBRAC; *t++ = *r+2;
1597 MesPrint(
"&Object between [] in expression should be a single term");
1600 if ( *s != RBRACE ) {
1601 MesPrint(
"&Internal error 23b");
1606 if ( *s == TPOWER ) {
1608 base = ( c == TNUMBER ) ? 100: 128;
1609 x2 = 0;
while ( *s >= 0 ) { x2 = base*x2 + *s++; }
1610 if ( *s == TWILDCARD || c == TSYMBOL ) { x2 += 2*MAXPOWER; s++; }
1619 if ( *s == TPOWER ) {
1620 s++;
if ( *s == TMINUS ) { s++; deno = -deno; }
1621 c = *s++; base = ( c == TNUMBER ) ? 100: 128;
1622 x2 = 0;
while ( *s >= 0 ) { x2 = x2*base + *s++; }
1625 MesPrint(
"&Encountered 0^0 during compilation");
1629 MesPrint(
"&Division by zero during compilation (0 to the power negative number)");
1632 else if ( deno < 0 ) {
1634 MesPrint(
"&Division by zero during compilation");
1639 if ( *s >= 0 ) { y = 100*y + *s++; }
1640 innum[0] = y; nin = 1;
1643 if ( *s >= 0 ) { y = 100*y + *s++; x2 = 10000; }
1644 Product(innum,&nin,(WORD)x2);
1645 if ( y ) AddLong(innum,nin,(UWORD *)(&y),(WORD)1,innum,&nin);
1648 if ( *s == TPOWER ) {
1649 s++;
if ( *s == TMINUS ) { s++; deno = -deno; }
1650 c = *s++; base = ( c == TNUMBER ) ? 100: 128;
1651 x2 = 0;
while ( *s >= 0 ) { x2 = x2*base + *s++; }
1653 innum[0] = 1; nin = 1;
1655 else if ( RaisPow(BHEAD innum,&nin,x2) ) {
1656 error = -1; innum[0] = 1; nin = 1;
1660 Simplify(BHEAD innum,&nin,denominator,&ndenominator);
1661 for ( i = 0; i < nnumerator; i++ ) CGscrat7[i] = numerator[i];
1662 MulLong(innum,nin,CGscrat7,nnumerator,numerator,&nnumerator);
1664 else if ( deno < 0 ) {
1665 Simplify(BHEAD innum,&nin,numerator,&nnumerator);
1666 for ( i = 0; i < ndenominator; i++ ) CGscrat7[i] = denominator[i];
1667 MulLong(innum,nin,CGscrat7,ndenominator,denominator,&ndenominator);
1672 if ( *s == 0 ) { s++; sign = 0;
break; }
1674 if ( *s >= 0 ) { y = 128*y + *s++; }
1676 innum[0] = y; nin = 1;
1679 if ( *s >= 0 ) { y = 128*y + *s++; x2 = 16384; }
1680 Product(innum,&nin,(WORD)x2);
1681 if ( y ) AddLong(innum,nin,(UWORD *)&y,(WORD)1,innum,&nin);
1685 *relo = 2; *t++ = SNUMBER; *t++ = 4; *t++ = y;
1690 x1 = 0;
while ( *s >= 0 ) { x1 = x1*128 + *s++; }
1691 if ( AR.Eside != LHSIDE ) {
1692 *t++ = SUBEXPRESSION; *t++ = SUBEXPSIZE; *t++ = x1;
1695 *t++ = DOLLAREXPRESSION; *t++ = SUBEXPSIZE; *t++ = x1;
1698 *t++ = AM.dbufnum; FILLSUB(t)
1702 if ( *s == LBRACE ) {
1705 while ( bracelevel > 0 ) {
1706 if ( *s == RBRACE ) {
1709 else if ( *s == TNUMBER ) {
1711 x2 = 0;
while ( *s >= 0 ) { x2 = 100*x2 + *s++; }
1712 *t++ = DOLLAREXPR2; *t++ = 3; *t++ = -x2-1;
1714 while ( bracelevel > 0 ) {
1715 if ( *s != RBRACE ) {
1718 MesPrint(
"&Improper use of [] in $-variable.");
1726 else if ( *s == TDOLLAR ) {
1728 x1 = 0;
while ( *s >= 0 ) { x1 = x1*128 + *s++; }
1729 *t++ = DOLLAREXPR2; *t++ = 3; *t++ = x1;
1730 if ( *s == RBRACE )
goto CloseBraces;
1731 else if ( *s == LBRACE ) {
1735 else goto ErrorBraces;
1741 if ( *s == TPOWER ) {
1743 if ( *s == TMINUS ) { s++; deno = -deno; }
1745 base = ( c == TNUMBER ) ? 100: 128;
1746 x2 = 0;
while ( *s >= 0 ) { x2 = base*x2 + *s++; }
1747 if ( c == TSYMBOL ) {
1748 if ( *s == TWILDCARD ) s++;
1751 *powplace = deno*x2;
1753 else *powplace = deno;
1764 inset = 1; tsize = t; relo = AT.WorkTop;
1765 *t++ = SETSET; *t++ = 0;
1766 x1 = 0;
while ( *s >= 0 ) x1 = x1*128 + *s++;
1767 *--relo = x1; *--relo = 0;
1770 inset = 1; tsize = t; relo = AT.WorkTop;
1771 *t++ = SETSET; *t++ = 0;
1772 x1 = 0;
while ( *s >= 0 ) x1 = x1*128 + *s++;
1773 *--relo = -x1; *--relo = 0;
1776 MesPrint(
"&Illegal use of function arguments");
1783 MesPrint(
"&Illegal use of function arguments");
1789 MesPrint(
"&Illegal use special gamma symbols 5_, 6_, 7_");
1795 MesPrint(
"&Internal error in code generator. Unknown object: %d",c);
1803 MesPrint(
"&Irregular end of statement.");
1806 if ( !first && error == 0 ) {
1809 if ( cc && sign ) C->
CanCommu[numexp]++;
1810 error = CompleteTerm(term,numerator,denominator,nnumerator,ndenominator,sign);
1812 AT.WorkPointer = oldwork;
1813 if ( error )
return(-1);
1815 if ( AC.CompileLevel > 0 && AR.Eside != LHSIDE ) {
1817 error = InsTree(AC.cbufnum,C->numrhs);
1818 if ( error < (C->numrhs) ) {
1825 MLOCK(ErrorMessageLock);
1827 MUNLOCK(ErrorMessageLock);
1839 int CompleteTerm(WORD *term, UWORD *numer, UWORD *denom, WORD nnum, WORD nden,
int sign)
1843 if ( sign == 0 )
return(0);
1844 if ( nnum >= nden ) nsize = nnum;
1847 for ( i = 0; i < nnum; i++ ) *t++ = numer[i];
1848 for ( ; i < nsize; i++ ) *t++ = 0;
1849 for ( i = 0; i < nden; i++ ) *t++ = denom[i];
1850 for ( ; i < nsize; i++ ) *t++ = 0;
1851 *t++ = (2*nsize+1)*sign;
1853 AddNtoC(AC.cbufnum,*term,term);
1876 int CodeFactors(SBYTE *tokens)
1880 int nfactor = 1, nparenthesis, i, last = 0, error = 0;
1881 SBYTE *t, *startobject, *tt, *s1, *out, *outtokens;
1882 WORD nexp, subexp = 0, power, pow, x2, powfactor, first;
1887 while ( *t != TENDOFIT ) {
1888 if ( *t >= 0 ) {
while ( *t >= 0 ) t++;
continue; }
1889 if ( *t == LPARENTHESIS || *t == LBRACE || *t == TSETOPEN || *t == TFUNOPEN ) {
1890 nparenthesis = 0; t++;
1891 while ( nparenthesis >= 0 ) {
1892 if ( *t == LPARENTHESIS || *t == LBRACE || *t == TSETOPEN || *t == TFUNOPEN ) nparenthesis++;
1893 else if ( *t == RPARENTHESIS || *t == RBRACE || *t == TSETCLOSE || *t == TFUNCLOSE ) nparenthesis--;
1898 else if ( ( *t == TPLUS || *t == TMINUS ) && ( t > tokens )
1899 && ( t[-1] != TPLUS && t[-1] != TMINUS ) ) {
1900 if ( t[-1] >= 0 || t[-1] == RPARENTHESIS || t[-1] == RBRACE
1901 || t[-1] == TSETCLOSE || t[-1] == TFUNCLOSE ) {
1902 subexp = CodeGenerator(tokens);
1903 if ( subexp < 0 ) error = -1;
1904 if ( insubexpbuffers >= 0x3FFFFFL ) {
1905 MesPrint(
"&More than 2^22 subexpressions inside one expression");
1908 if ( subexpbuffers+insubexpbuffers >= topsubexpbuffers ) {
1909 DoubleBuffer((
void **)((VOID *)(&subexpbuffers))
1910 ,(
void **)((VOID *)(&topsubexpbuffers)),
sizeof(
SUBBUF),
"subexpbuffers");
1912 subexpbuffers[insubexpbuffers].subexpnum = subexp;
1913 subexpbuffers[insubexpbuffers].buffernum = AC.cbufnum;
1914 subexp = insubexpbuffers++;
1916 *t++ = TSYMBOL; *t++ = FACTORSYMBOL;
1917 *t++ = TMULTIPLY; *t++ = TSUBEXP;
1918 PUTNUMBER128(t,subexp)
1921 e->vflags |= ISFACTORIZED;
1925 else if ( ( *t == TMULTIPLY || *t == TDIVIDE ) && t > tokens ) {
1928 else if ( *t == TEXPRESSION ) {
1930 nexp = 0;
while ( *t >= 0 ) { nexp = nexp*128 + *t++; }
1931 if ( *t == LBRACE )
continue;
1932 if ( ( AS.Oldvflags[nexp] & ISFACTORIZED ) != 0 ) {
1933 nfactor += AS.OldNumFactors[nexp];
1938 else if ( *t == TDOLLAR ) {
1940 nexp = 0;
while ( *t >= 0 ) { nexp = nexp*128 + *t++; }
1941 if ( *t == LBRACE )
continue;
1942 if ( Dollars[nexp].nfactors > 0 ) {
1943 nfactor += Dollars[nexp].nfactors;
1954 outtokens = (SBYTE *)Malloc1(((t-tokens)+(nfactor+2)*25)*
sizeof(SBYTE),
"CodeFactors");
1956 t = tokens; first = 1; powfactor = 1;
1957 while ( *t == TPLUS || *t == TMINUS ) {
if ( *t == TMINUS ) first = -first; t++; }
1959 *out++ = TMINUS; *out++ = TSYMBOL; *out++ = FACTORSYMBOL;
1960 *out++ = TPOWER; *out++ = TNUMBER; PUTNUMBER100(out,powfactor)
1963 startobject = t; power = 1;
1964 while ( *t != TENDOFIT ) {
1965 if ( *t >= 0 ) {
while ( *t >= 0 ) t++;
continue; }
1966 if ( *t == LPARENTHESIS || *t == LBRACE || *t == TSETOPEN || *t == TFUNOPEN ) {
1967 nparenthesis = 0; t++;
1968 while ( nparenthesis >= 0 ) {
1969 if ( *t == LPARENTHESIS || *t == LBRACE || *t == TSETOPEN || *t == TFUNOPEN ) nparenthesis++;
1970 else if ( *t == RPARENTHESIS || *t == RBRACE || *t == TSETCLOSE || *t == TFUNCLOSE ) nparenthesis--;
1975 else if ( ( *t == TMULTIPLY || *t == TDIVIDE ) && ( t > tokens ) ) {
1976 if ( t[-1] >= 0 || t[-1] == RPARENTHESIS || t[-1] == RBRACE
1977 || t[-1] == TSETCLOSE || t[-1] == TFUNCLOSE ) {
1979 if ( startobject ) {
1981 if ( power < 0 ) { *out++ = TNUMBER; *out++ = 1; *out++ = TDIVIDE; }
1983 while ( s1 < t ) *out++ = *s1++;
1984 *out++ = TMULTIPLY; *out++ = TSYMBOL; *out++ = FACTORSYMBOL;
1985 *out++ = TPOWER; *out++ = TNUMBER; PUTNUMBER100(out,powfactor)
1988 if ( last ) { startobject = 0;
break; }
1990 if ( *t == TDIVIDE ) power = -1;
1991 if ( *t == TMULTIPLY ) power = 1;
1994 else if ( *t == TPOWER ) {
1997 while ( ( *tt == TMINUS ) || ( *tt == TPLUS ) ) {
1998 if ( *tt == TMINUS ) pow = -pow;
2001 if ( *tt == TSYMBOL ) {
2002 tt++;
while ( *tt >= 0 ) tt++;
2005 tt++; x2 = 0;
while ( *tt >= 0 ) { x2 = 100*x2 + *tt++; }
2010 power = power*pow*x2;
2011 if ( power < 0 ) { pow = -power; power = -1; }
2012 else if ( power == 0 ) { t = tt; startobject = tt;
continue; }
2013 else { pow = power; power = 1; }
2016 subexp = GenerateFactors(pow,1);
2017 if ( subexp < 0 ) { error = -1; subexp = 0; }
2018 *out++ = TSUBEXP; PUTNUMBER128(out,subexp);
2020 *out++ = TSYMBOL; *out++ = FACTORSYMBOL;
2021 *out++ = TPOWER; *out++ = TNUMBER; PUTNUMBER100(out,powfactor)
2023 if ( power > 0 ) *out++ = TMULTIPLY;
2024 else *out++ = TDIVIDE;
2025 s1 = startobject;
while ( s1 < t ) *out++ = *s1++;
2026 startobject = 0; t = tt;
continue;
2028 else if ( *t == TEXPRESSION ) {
2031 nexp = 0;
while ( *t >= 0 ) { nexp = nexp*128 + *t++; }
2032 if ( *t == LBRACE )
continue;
2033 if ( *t == LPARENTHESIS ) {
2034 nparenthesis = 0; t++;
2035 while ( nparenthesis >= 0 ) {
2036 if ( *t == LPARENTHESIS ) nparenthesis++;
2037 else if ( *t == RPARENTHESIS ) nparenthesis--;
2041 if ( ( AS.Oldvflags[nexp] & ISFACTORIZED ) == 0 )
continue;
2042 if ( *t == TPOWER ) {
2045 while ( ( *tt == TMINUS ) || ( *tt == TPLUS ) ) {
2046 if ( *tt == TMINUS ) pow = -pow;
2049 if ( *tt != TNUMBER ) {
2050 MesPrint(
"Internal problems(1) in CodeFactors");
2053 tt++; x2 = 0;
while ( *tt >= 0 ) { x2 = 100*x2 + *tt++; }
2059 power = power*pow*x2;
2060 if ( power < 0 ) { pow = -power; power = -1; }
2061 else if ( power == 0 ) { t = tt; startobject = tt;
continue; }
2062 else { pow = power; power = 1; }
2065 subexp = GenerateFactors(pow,AS.OldNumFactors[nexp]);
2066 if ( subexp < 0 ) { error = -1; subexp = 0; }
2067 *out++ = TSUBEXP; PUTNUMBER128(out,subexp)
2072 *out++ = TSYMBOL; *out++ = FACTORSYMBOL;
2074 *out++ = TPOWER; *out++ = TNUMBER; PUTNUMBER100(out,i)
2078 powfactor += AS.OldNumFactors[nexp]*pow;
2080 while ( s1 < t ) *out++ = *s1++;
2081 startobject = 0; t = tt;
continue;
2084 tt = t; pow = 1; x2 = 1;
goto dopower;
2087 else if ( *t == TDOLLAR ) {
2090 nexp = 0;
while ( *t >= 0 ) { nexp = nexp*128 + *t++; }
2091 if ( *t == LBRACE )
continue;
2092 if ( Dollars[nexp].nfactors == 0 )
continue;
2093 if ( *t == TPOWER ) {
2096 while ( ( *tt == TMINUS ) || ( *tt == TPLUS ) ) {
2097 if ( *tt == TMINUS ) pow = -pow;
2100 if ( *tt != TNUMBER ) {
2101 MesPrint(
"Internal problems(2) in CodeFactors");
2104 tt++; x2 = 0;
while ( *tt >= 0 ) { x2 = 100*x2 + *tt++; }
2110 power = power*pow*x2;
2111 if ( power < 0 ) { pow = -power; power = -1; }
2112 else if ( power == 0 ) { t = tt; startobject = tt;
continue; }
2113 else { pow = power; power = 1; }
2115 subexp = GenerateFactors(pow,1);
2116 if ( subexp < 0 ) { error = -1; subexp = 0; }
2118 for ( i = 1; i <= Dollars[nexp].nfactors; i++ ) {
2119 s1 = startobject; *out++ = TPLUS;
2120 while ( s1 < t ) *out++ = *s1++;
2121 *out++ = LBRACE; *out++ = TNUMBER; PUTNUMBER128(out,i)
2124 *out++ = TSYMBOL; *out++ = FACTORSYMBOL;
2125 *out++ = TPOWER; *out++ = TNUMBER; PUTNUMBER100(out,powfactor)
2128 *out++ = TSUBEXP; PUTNUMBER128(out,subexp)
2131 startobject = 0; t = tt;
continue;
2134 tt = t; pow = 1; x2 = 1;
goto dopowerd;
2139 if ( last == 0 ) { last = 1;
goto dolast; }
2141 e->numfactors = powfactor-1;
2142 e->vflags |= ISFACTORIZED;
2143 subexp = CodeGenerator(outtokens);
2144 if ( subexp < 0 ) error = -1;
2145 if ( insubexpbuffers >= 0x3FFFFFL ) {
2146 MesPrint(
"&More than 2^22 subexpressions inside one expression");
2149 if ( subexpbuffers+insubexpbuffers >= topsubexpbuffers ) {
2150 DoubleBuffer((
void **)((VOID *)(&subexpbuffers))
2151 ,(
void **)((VOID *)(&topsubexpbuffers)),
sizeof(
SUBBUF),
"subexpbuffers");
2153 subexpbuffers[insubexpbuffers].subexpnum = subexp;
2154 subexpbuffers[insubexpbuffers].buffernum = AC.cbufnum;
2155 subexp = insubexpbuffers++;
2156 M_free(outtokens,
"CodeFactors");
2158 *s1++ = TSUBEXP; PUTNUMBER128(s1,subexp); *s1++ = TENDOFIT;
2159 if ( error < 0 )
return(-1);
2160 else return(subexp);
2173 WORD GenerateFactors(WORD n,WORD inc)
2178 SBYTE *tokenbuffer = (SBYTE *)Malloc1(8*n*
sizeof(SBYTE),
"GenerateFactors");
2180 *s++ = TNUMBER; *s++ = 1;
2181 for ( i = inc; i < n*inc; i += inc ) {
2182 *s++ = TPLUS; *s++ = TSYMBOL; *s++ = FACTORSYMBOL;
2184 *s++ = TPOWER; *s++ = TNUMBER;
2189 subexp = CodeGenerator(tokenbuffer);
2190 if ( subexp < 0 ) error = -1;
2191 M_free(tokenbuffer,
"GenerateFactors");
2192 if ( insubexpbuffers >= 0x3FFFFFL ) {
2193 MesPrint(
"&More than 2^22 subexpressions inside one expression");
2196 if ( subexpbuffers+insubexpbuffers >= topsubexpbuffers ) {
2197 DoubleBuffer((
void **)((VOID *)(&subexpbuffers))
2198 ,(
void **)((VOID *)(&topsubexpbuffers)),
sizeof(
SUBBUF),
"subexpbuffers");
2200 subexpbuffers[insubexpbuffers].subexpnum = subexp;
2201 subexpbuffers[insubexpbuffers].buffernum = AC.cbufnum;
2202 subexp = insubexpbuffers++;
2203 if ( error < 0 )
return(error);