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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
|
/** @file if.c
*
* Routines for the dealing with if statements.
*/
/* #[ License : */
/*
* Copyright (C) 1984-2026 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 : if.c
*/
#include "form3.h"
/*
#] Includes :
#[ If statement :
#[ Syntax :
The `if' is a conglomerate of statements: if,else,endif
The if consists in principle of:
if ( number );
statements
else;
statements
endif;
The first set is taken when number != 0.
The else is not mandatory.
TRUE = 1 and FALSE = 0
The number can be built up via a logical expression:
expr1 condition expr2
each expression can be a subexpression again. It has to be
enclosed in parentheses in that case.
Conditions are:
>, >=, <, <=, ==, !=, ||, &&
When Expressions are chained evaluation is from left to right,
independent of whether this indicates nonsense.
if ( a || b || c || d ); is a perfectly normal statement.
if ( a >= b || c == d ); would be messed up. This should be:
if ( ( a >= b ) || ( c == d ) );
The building blocks of the Expressions are:
Match(option,pattern) The number of times pattern fits in term_
Count(....) The count value of term_
Coeff[icient] The coefficient of term_
FindLoop(options) Are there loops (as in ReplaceLoop).
Implementation for internal notation:
TYPEIF,length,gotolevel(if fail),EXPRTYPE,length,......
EXPRTYPE can be:
SHORTNUMBER ->,4,sign,size
LONGNUMBER ->,|ncoef+2|,ncoef,numer,denom
MATCH ->,patternsiz+3,keyword,pattern
MULTIPLEOF ->,3,thenumber
COUNT ->,countsiz+2,countinfo
TYPEFINDLOOP ->,7 (findloop info)
COEFFICIENT ->,2
IFDOLLAR ->,3,dollarnumber
SUBEXPR ->,size,dummy,size1,EXPRTYPE,length,...
,2,condition1,size2,...
This is like functions.
Note that there must be a restriction to the number of nestings
of parentheses in an if statement. It has been set to 10.
The syntax of match corresponds to the syntax of the left side
of an id statement. The only difference is the keyword
MATCH vs TYPEIDNEW.
#] Syntax :
#[ GetIfDollarNum :
*/
WORD GetIfDollarNum(WORD *ifp, WORD *ifstop)
{
DOLLARS d;
WORD num, *w;
if ( ifp[2] < 0 ) { return(-ifp[2]-1); }
d = Dollars+ifp[2];
if ( ifp+3 < ifstop && ifp[3] == IFDOLLAREXTRA ) {
if ( d->nfactors == 0 ) {
MLOCK(ErrorMessageLock);
MesPrint("Attempt to use a factor of an unfactored $-variable");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
num = GetIfDollarNum(ifp+3,ifstop);
if ( num > d->nfactors ) {
MLOCK(ErrorMessageLock);
MesPrint("Dollar factor number %s out of range",num);
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
if ( num == 0 ) {
return(d->nfactors);
}
w = d->factors[num-1].where;
if ( w == 0 ) return(d->factors[num].value);
getnumber:;
if ( *w == 0 ) return(0);
if ( *w == 4 && w[3] == 3 && w[2] == 1 && w[1] < MAXPOSITIVE && w[4] == 0 ) {
return(w[1]);
}
if ( ( w[w[0]] != 0 ) || ( ABS(w[w[0]-1]) != w[0]-1 ) ) {
MLOCK(ErrorMessageLock);
MesPrint("Dollar factor number expected but found expression");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
else {
MLOCK(ErrorMessageLock);
MesPrint("Dollar factor number out of range");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
return(0);
}
/*
Now we have just a dollar and should evaluate that into a short number
*/
if ( d->type == DOLZERO ) {
return(0);
}
else if ( d->type == DOLNUMBER || d->type == DOLTERMS ) {
w = d->where; goto getnumber;
}
else {
MLOCK(ErrorMessageLock);
MesPrint("Dollar factor number is wrong type");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
return(0);
}
}
/*
#] GetIfDollarNum :
#[ FindVar :
*/
int FindVar(WORD *v, WORD *term)
{
WORD *t, *tstop, *m, *mstop, *f, *fstop, *a, *astop;
GETSTOP(term,tstop);
t = term+1;
while ( t < tstop ) {
if ( *v == *t && *v < FUNCTION ) { /* VECTOR, INDEX, SYMBOL, DOTPRODUCT */
switch ( *v ) {
case SYMBOL:
m = t+2; mstop = t+t[1];
while ( m < mstop ) {
if ( *m == v[1] ) return(1);
m += 2;
}
break;
case INDEX:
case VECTOR:
InVe:
m = t+2; mstop = t+t[1];
while ( m < mstop ) {
if ( *m == v[1] ) return(1);
m++;
}
break;
case DOTPRODUCT:
m = t+2; mstop = t+t[1];
while ( m < mstop ) {
if ( *m == v[1] && m[1] == v[2] ) return(1);
if ( *m == v[2] && m[1] == v[1] ) return(1);
m += 3;
}
break;
}
}
else if ( *v == VECTOR && *t == INDEX ) goto InVe;
else if ( *v == INDEX && *t == VECTOR ) goto InVe;
else if ( ( *v == VECTOR || *v == INDEX ) && *t == DOTPRODUCT ) {
m = t+2; mstop = t+t[1];
while ( m < mstop ) {
if ( v[1] == m[0] || v[1] == m[1] ) return(1);
m += 3;
}
}
else if ( *t >= FUNCTION ) {
if ( *v == FUNCTION && v[1] == *t ) return(1);
if ( functions[*t-FUNCTION].spec > 0 ) {
if ( *v == VECTOR || *v == INDEX ) { /* we need to check arguments */
int i;
for ( i = FUNHEAD; i < t[1]; i++ ) {
if ( v[1] == t[i] ) return(1);
}
}
}
else {
fstop = t + t[1]; f = t + FUNHEAD;
while ( f < fstop ) { /* Do the arguments one by one */
if ( *f <= 0 ) {
switch ( *f ) {
case -SYMBOL:
if ( *v == SYMBOL && v[1] == f[1] ) return(1);
f += 2;
break;
case -VECTOR:
case -MINVECTOR:
case -INDEX:
if ( ( *v == VECTOR || *v == INDEX )
&& ( v[1] == f[1] ) ) return(1);
f += 2;
break;
case -SNUMBER:
f += 2;
break;
default:
if ( *v == FUNCTION && v[1] == -*f && *f <= -FUNCTION ) return(1);
if ( *f <= -FUNCTION ) f++;
else f += 2;
break;
}
}
else {
a = f + ARGHEAD; astop = f + *f;
while ( a < astop ) {
if ( FindVar(v,a) == 1 ) return(1);
a += *a;
}
f = astop;
}
}
}
}
t += t[1];
}
return(0);
}
/*
#] FindVar :
#[ DoIfStatement : WORD DoIfStatement(PHEAD ifcode,term)
The execution time part of the if-statement.
The arguments are a pointer to the TYPEIF and a pointer to the term.
The answer is either 1 (success) or 0 (fail).
The calling routine can figure out where to go in case of failure
by picking up gotolevel.
Note that the whole setup asks for recursions.
*/
int DoIfStatement(PHEAD WORD *ifcode, WORD *term)
{
GETBIDENTITY
WORD *ifstop, *ifp;
UWORD *coef1 = 0, *coef2, *coef3, *cc;
WORD ncoef1, ncoef2, ncoef3, i = 0, first, *r, acoef, ismul1, ismul2, j;
UWORD *Spac1, *Spac2;
ifstop = ifcode + ifcode[1];
ifp = ifcode + 3;
if ( ifp >= ifstop ) return(1);
if ( ( ifp + ifp[1] ) >= ifstop ) {
switch ( *ifp ) {
case LONGNUMBER:
if ( ifp[2] ) return(1);
else return(0);
case MATCH:
case TYPEIF:
if ( HowMany(BHEAD ifp,term) ) return(1);
else return(0);
case TYPEFINDLOOP:
if ( Lus(term,ifp[3],ifp[4],ifp[5],ifp[6],ifp[2]) ) return(1);
else return(0);
case TYPECOUNT:
if ( CountDo(term,ifp) ) return(1);
else return(0);
case COEFFI:
case MULTIPLEOF:
return(1);
case IFDOLLAR:
{
DOLLARS d = Dollars + ifp[2];
#ifdef WITHPTHREADS
int nummodopt, dtype = -1;
if ( AS.MultiThreaded ) {
for ( nummodopt = 0; nummodopt < NumModOptdollars; nummodopt++ ) {
if ( ifp[2] == ModOptdollars[nummodopt].number ) break;
}
if ( nummodopt < NumModOptdollars ) {
dtype = ModOptdollars[nummodopt].type;
if ( dtype == MODLOCAL ) {
d = ModOptdollars[nummodopt].dstruct+AT.identity;
}
}
}
dtype = d->type;
#else
int dtype = d->type; /* We use dtype to make the operation atomic */
#endif
if ( dtype == DOLZERO ) return(0);
if ( dtype == DOLUNDEFINED ) {
if ( AC.UnsureDollarMode == 0 ) {
MesPrint("$%s is undefined",AC.dollarnames->namebuffer+d->name);
Terminate(-1);
}
}
}
return(1);
case IFEXPRESSION:
r = ifp+2; j = ifp[1] - 2;
while ( --j >= 0 ) {
if ( *r == AR.CurExpr ) return(1);
r++;
}
return(0);
case IFISFACTORIZED:
r = ifp+2; j = ifp[1] - 2;
if ( j == 0 ) {
if ( ( Expressions[AR.CurExpr].vflags & ISFACTORIZED ) != 0 )
return(1);
else
return(0);
}
while ( --j >= 0 ) {
if ( ( Expressions[*r].vflags & ISFACTORIZED ) == 0 ) return(0);
r++;
}
return(1);
case IFOCCURS:
{
WORD *OccStop = ifp + ifp[1];
ifp += 2;
while ( ifp < OccStop ) {
if ( FindVar(ifp,term) == 1 ) return(1);
if ( *ifp == DOTPRODUCT ) ifp += 3;
else ifp += 2;
}
}
return(0);
case IFUSERFLAG:
if ( ( Expressions[AR.CurExpr].uflags & (1 << ifp[2]) ) != 0 )
return(1);
return(0);
default:
/*
Now we have a subexpression. Test first for one with a single item.
*/
if ( ifp[3] == ( ifp[1] + 3 ) ) return(DoIfStatement(BHEAD ifp,term));
ifstop = ifp + ifp[1];
ifp += 3;
break;
}
}
/*
Here is the composite condition.
*/
coef3 = NumberMalloc("DoIfStatement");
Spac1 = NumberMalloc("DoIfStatement");
Spac2 = (UWORD *)(TermMalloc("DoIfStatement"));
ncoef1 = 0; first = 1; ismul1 = 0;
do {
if ( !first ) {
ifp += 2;
if ( ifp[-2] == ORCOND && ncoef1 ) {
coef1 = Spac1;
ncoef1 = 1; coef1[0] = coef1[1] = 1;
goto SkipCond;
}
if ( ifp[-2] == ANDCOND && !ncoef1 ) goto SkipCond;
}
coef2 = Spac2;
ncoef2 = 1;
ismul2 = 0;
switch ( *ifp ) {
case LONGNUMBER:
ncoef2 = ifp[2];
j = 2*(ABS(ncoef2));
cc = (UWORD *)(ifp + 3);
for ( i = 0; i < j; i++ ) coef2[i] = cc[i];
break;
#ifdef WITHFLOAT
case IFFLOATNUMBER:
/*
The sloppy solution is: Convert to rational.
This way we can write it over coef2,ncoef2
*/
ncoef2 = FloatFunToRat(BHEAD coef2,ifp);
break;
#endif
case MATCH:
case TYPEIF:
coef2[0] = HowMany(BHEAD ifp,term);
coef2[1] = 1;
if ( coef2[0] == 0 ) ncoef2 = 0;
break;
case TYPECOUNT:
acoef = CountDo(term,ifp);
coef2[0] = ABS(acoef);
coef2[1] = 1;
if ( acoef == 0 ) ncoef2 = 0;
else if ( acoef < 0 ) ncoef2 = -1;
break;
case TYPEFINDLOOP:
acoef = Lus(term,ifp[3],ifp[4],ifp[5],ifp[6],ifp[2]);
coef2[0] = ABS(acoef);
coef2[1] = 1;
if ( acoef == 0 ) ncoef2 = 0;
else if ( acoef < 0 ) ncoef2 = -1;
break;
case COEFFI:
r = term + *term;
ncoef2 = r[-1];
i = ABS(ncoef2);
cc = (UWORD *)(r - i);
if ( ncoef2 < 0 ) ncoef2 = (ncoef2+1)>>1;
else ncoef2 = (ncoef2-1)>>1;
i--; for ( j = 0; j < i; j++ ) coef2[j] = cc[j];
break;
case SUBEXPR:
ncoef2 = coef2[0] = DoIfStatement(BHEAD ifp,term);
coef2[1] = 1;
break;
case MULTIPLEOF:
ncoef2 = 1;
coef2[0] = ifp[2];
coef2[1] = 1;
ismul2 = 1;
break;
case IFDOLLAREXTRA:
break;
case IFDOLLAR:
{
/*
We need to abstract a long rational in coef2
with length ncoef2. What if that cannot be done?
*/
DOLLARS d = Dollars + ifp[2];
#ifdef WITHPTHREADS
int nummodopt, dtype = -1;
if ( AS.MultiThreaded ) {
for ( nummodopt = 0; nummodopt < NumModOptdollars; nummodopt++ ) {
if ( ifp[2] == ModOptdollars[nummodopt].number ) break;
}
if ( nummodopt < NumModOptdollars ) {
dtype = ModOptdollars[nummodopt].type;
if ( dtype == MODLOCAL ) {
d = ModOptdollars[nummodopt].dstruct+AT.identity;
}
else {
LOCK(d->pthreadslockread);
}
}
}
#endif
/*
We have to pick up the IFDOLLAREXTRA pieces for [1], [$y] etc.
*/
if ( ifp+3 < ifstop && ifp[3] == IFDOLLAREXTRA ) {
if ( d->nfactors == 0 ) {
MLOCK(ErrorMessageLock);
MesPrint("Attempt to use a factor of an unfactored $-variable");
MUNLOCK(ErrorMessageLock);
Terminate(-1);
} {
WORD num = GetIfDollarNum(ifp+3,ifstop);
WORD *w;
while ( ifp+3 < ifstop && ifp[3] == IFDOLLAREXTRA ) ifp += 3;
if ( num > d->nfactors ) {
MLOCK(ErrorMessageLock);
MesPrint("Dollar factor number %s out of range",num);
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
if ( num == 0 ) {
ncoef2 = 1; coef2[0] = d->nfactors; coef2[1] = 1;
break;
}
w = d->factors[num-1].where;
if ( w == 0 ) {
if ( d->factors[num-1].value < 0 ) {
ncoef2 = -1; coef2[0] = -d->factors[num-1].value; coef2[1] = 1;
}
else {
ncoef2 = 1; coef2[0] = d->factors[num-1].value; coef2[1] = 1;
}
break;
}
if ( w[*w] == 0 ) {
r = w + *w - 1;
i = ABS(*r);
if ( i == ( *w-1 ) ) {
ncoef2 = (i-1)/2;
if ( *r < 0 ) ncoef2 = -ncoef2;
i--; cc = coef2; r = w + 1;
while ( --i >= 0 ) *cc++ = (UWORD)(*r++);
break;
}
}
goto generic;
}
}
else {
switch ( d->type ) {
case DOLUNDEFINED:
if ( AC.UnsureDollarMode == 0 ) {
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
MLOCK(ErrorMessageLock);
MesPrint("$%s is undefined",AC.dollarnames->namebuffer+d->name);
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
ncoef2 = 0; coef2[0] = 0; coef2[1] = 1;
break;
case DOLZERO:
ncoef2 = coef2[0] = 0; coef2[1] = 1;
break;
case DOLSUBTERM:
if ( d->where[0] != INDEX || d->where[1] != 3
|| d->where[2] < 0 || d->where[2] >= AM.OffsetIndex ) {
if ( AC.UnsureDollarMode == 0 ) {
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
MLOCK(ErrorMessageLock);
MesPrint("$%s is of wrong type",AC.dollarnames->namebuffer+d->name);
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
ncoef2 = 0; coef2[0] = 0; coef2[1] = 1;
break;
}
d->index = d->where[2];
/* fall through */
case DOLINDEX:
if ( d->index == 0 ) {
ncoef2 = coef2[0] = 0; coef2[1] = 1;
}
else if ( d->index > 0 && d->index < AM.OffsetIndex ) {
ncoef2 = 1; coef2[0] = d->index; coef2[1] = 1;
}
else if ( AC.UnsureDollarMode == 0 ) {
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
MLOCK(ErrorMessageLock);
MesPrint("$%s is of wrong type",AC.dollarnames->namebuffer+d->name);
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
ncoef2 = coef2[0] = 0; coef2[1] = 1;
break;
case DOLWILDARGS:
if ( d->where[0] <= -FUNCTION ||
( d->where[0] < 0 && d->where[2] != 0 )
|| ( d->where[0] > 0 && d->where[d->where[0]] != 0 )
) {
if ( AC.UnsureDollarMode == 0 ) {
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
MLOCK(ErrorMessageLock);
MesPrint("$%s is of wrong type",AC.dollarnames->namebuffer+d->name);
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
ncoef2 = coef2[0] = 0; coef2[1] = 1;
break;
}
/* fall through */
case DOLARGUMENT:
if ( d->where[0] == -SNUMBER ) {
if ( d->where[1] == 0 ) {
ncoef2 = coef2[0] = 0;
}
else if ( d->where[1] < 0 ) {
ncoef2 = -1;
coef2[0] = -d->where[1];
}
else {
ncoef2 = 1;
coef2[0] = d->where[1];
}
coef2[1] = 1;
}
else if ( d->where[0] == -INDEX
&& d->where[1] >= 0 && d->where[1] < AM.OffsetIndex ) {
if ( d->where[1] == 0 ) {
ncoef2 = coef2[0] = 0; coef2[1] = 1;
}
else {
ncoef2 = 1; coef2[0] = d->where[1];
coef2[1] = 1;
}
}
else if ( d->where[0] > 0
&& d->where[ARGHEAD] == (d->where[0]-ARGHEAD)
&& ABS(d->where[d->where[0]-1]) ==
(d->where[0] - ARGHEAD-1) ) {
i = d->where[d->where[0]-1];
ncoef2 = (ABS(i)-1)/2;
if ( i < 0 ) { ncoef2 = -ncoef2; i = -i; }
i--; cc = coef2; r = d->where + ARGHEAD+1;
while ( --i >= 0 ) *cc++ = (UWORD)(*r++);
}
else {
if ( AC.UnsureDollarMode == 0 ) {
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
MLOCK(ErrorMessageLock);
MesPrint("$%s is of wrong type",AC.dollarnames->namebuffer+d->name);
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
ncoef2 = 0; coef2[0] = 0; coef2[1] = 1;
}
break;
case DOLNUMBER:
case DOLTERMS:
if ( d->where[d->where[0]] == 0 ) {
r = d->where + d->where[0]-1;
i = ABS(*r);
if ( i == ( d->where[0]-1 ) ) {
ncoef2 = (i-1)/2;
if ( *r < 0 ) ncoef2 = -ncoef2;
i--; cc = coef2; r = d->where + 1;
while ( --i >= 0 ) *cc++ = (UWORD)(*r++);
break;
}
}
generic:;
if ( AC.UnsureDollarMode == 0 ) {
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
MLOCK(ErrorMessageLock);
MesPrint("$%s is of wrong type",AC.dollarnames->namebuffer+d->name);
MUNLOCK(ErrorMessageLock);
Terminate(-1);
}
ncoef2 = 0; coef2[0] = 0; coef2[1] = 1;
break;
}
}
#ifdef WITHPTHREADS
if ( dtype > 0 && dtype != MODLOCAL ) { UNLOCK(d->pthreadslockread); }
#endif
}
break;
case IFEXPRESSION:
r = ifp+2; j = ifp[1] - 2; ncoef2 = 0;
while ( --j >= 0 ) {
if ( *r == AR.CurExpr ) { ncoef2 = 1; break; }
r++;
}
coef2[0] = ncoef2;
coef2[1] = 1;
break;
case IFISFACTORIZED:
r = ifp+2; j = ifp[1] - 2;
if ( j == 0 ) {
ncoef2 = 0;
if ( ( Expressions[AR.CurExpr].vflags & ISFACTORIZED ) != 0 ) {
ncoef2 = 1;
}
}
else {
ncoef2 = 1;
while ( --j >= 0 ) {
if ( ( Expressions[*r].vflags & ISFACTORIZED ) == 0 ) {
ncoef2 = 0;
break;
}
r++;
}
}
coef2[0] = ncoef2;
coef2[1] = 1;
break;
case IFOCCURS:
{
WORD *OccStop = ifp + ifp[1], *ifpp = ifp+2;
ncoef2 = 0;
while ( ifpp < OccStop ) {
if ( FindVar(ifpp,term) == 1 ) {
ncoef2 = 1; break;
}
if ( *ifpp == DOTPRODUCT ) ifp += 3;
else ifpp += 2;
}
coef2[0] = ncoef2;
coef2[1] = 1;
}
break;
case IFUSERFLAG:
{
ncoef2 = 0;
if ( ( Expressions[AR.CurExpr].uflags & (1 << ifp[2]) ) != 0 )
ncoef2 = 1;
coef2[0] = ncoef2;
coef2[1] = 1;
}
break;
default:
break;
}
if ( !first ) {
if ( ifp[-2] != ORCOND && ifp[-2] != ANDCOND ) {
if ( ( ifp[-2] == EQUAL || ifp[-2] == NOTEQUAL ) &&
( ismul2 || ismul1 ) ) {
if ( ismul1 && ismul2 ) {
if ( coef1[0] == coef2[0] ) i = 1;
else i = 0;
}
else {
if ( ismul1 ) {
if ( ncoef2 )
Divvy(BHEAD coef2,&ncoef2,coef1,ncoef1);
cc = coef2; ncoef3 = ncoef2;
}
else {
if ( ncoef1 )
Divvy(BHEAD coef1,&ncoef1,coef2,ncoef2);
cc = coef1; ncoef3 = ncoef1;
}
if ( ncoef3 < 0 ) ncoef3 = -ncoef3;
if ( ncoef3 == 0 ) {
if ( ifp[-2] == EQUAL ) i = 1;
else i = 0;
}
else if ( cc[ncoef3] != 1 ) {
if ( ifp[-2] == EQUAL ) i = 0;
else i = 1;
}
else {
for ( j = 1; j < ncoef3; j++ ) {
if ( cc[ncoef3+j] != 0 ) break;
}
if ( j < ncoef3 ) {
if ( ifp[-2] == EQUAL ) i = 0;
else i = 1;
}
else if ( ifp[-2] == EQUAL ) i = 1;
else i = 0;
}
}
goto donemul;
}
else if ( AddRat(BHEAD coef1,ncoef1,coef2,-ncoef2,coef3,&ncoef3) ) {
NumberFree(coef3,"DoIfStatement"); NumberFree(Spac1,"DoIfStatement"); TermFree(Spac2,"DoIfStatement");
MesCall("DoIfStatement"); return(-1);
}
switch ( ifp[-2] ) {
case GREATER:
if ( ncoef3 > 0 ) i = 1;
else i = 0;
break;
case GREATEREQUAL:
if ( ncoef3 >= 0 ) i = 1;
else i = 0;
break;
case LESS:
if ( ncoef3 < 0 ) i = 1;
else i = 0;
break;
case LESSEQUAL:
if ( ncoef3 <= 0 ) i = 1;
else i = 0;
break;
case EQUAL:
if ( ncoef3 == 0 ) i = 1;
else i = 0;
break;
case NOTEQUAL:
if ( ncoef3 != 0 ) i = 1;
else i = 0;
break;
}
donemul: if ( i ) { ncoef2 = 1; coef2 = Spac2; coef2[0] = coef2[1] = 1; }
else ncoef2 = 0;
ismul1 = ismul2 = 0;
}
}
else {
first = 0;
}
coef1 = Spac1;
i = 2*ABS(ncoef2);
for ( j = 0; j < i; j++ ) coef1[j] = coef2[j];
ncoef1 = ncoef2;
SkipCond:
ifp += ifp[1];
} while ( ifp < ifstop );
NumberFree(coef3,"DoIfStatement"); NumberFree(Spac1,"DoIfStatement"); TermFree(Spac2,"DoIfStatement");
if ( ncoef1 ) return(1);
else return(0);
}
/*
#] DoIfStatement :
#[ HowMany : WORD HowMany(ifcode,term)
Returns the number of times that the pattern in ifcode
can be taken out from term. There is a subkey in ifcode[2];
The notation is identical to the lhs of an id statement.
Most of the code comes from TestMatch.
*/
WORD HowMany(PHEAD WORD *ifcode, WORD *term)
{
GETBIDENTITY
WORD *m, *t, *r, *w, power, RetVal, i, topje, *newterm;
WORD *OldWork, *ww, *mm;
int *RepSto, RepVal;
int numdollars = 0;
m = ifcode + IDHEAD;
AN.FullProto = m;
AN.WildValue = w = m + SUBEXPSIZE;
m += m[1];
AN.WildStop = m;
OldWork = AT.WorkPointer;
if ( ( ifcode[4] & 1 ) != 0 ) { /* We have at least one dollar in the pattern */
AR.Eside = LHSIDEX;
ww = AT.WorkPointer; i = m[0]; mm = m;
NCOPY(ww,mm,i);
*OldWork += 3;
*ww++ = 1; *ww++ = 1; *ww++ = 3;
AT.WorkPointer = ww;
RepSto = AN.RepPoint;
RepVal = *RepSto;
NewSort(BHEAD0);
if ( Generator(BHEAD OldWork,AR.Cnumlhs) ) {
LowerSortLevel();
*RepSto = RepVal;
AN.RepPoint = RepSto;
AT.WorkPointer = OldWork;
return(-1);
}
AT.WorkPointer = ww;
if ( EndSort(BHEAD ww,0) < 0 ) {}
*RepSto = RepVal;
AN.RepPoint = RepSto;
if ( *ww == 0 || *(ww+*ww) != 0 ) {
if ( AP.lhdollarerror == 0 ) {
MLOCK(ErrorMessageLock);
MesPrint("&LHS must be one term");
MUNLOCK(ErrorMessageLock);
AP.lhdollarerror = 1;
}
AT.WorkPointer = OldWork;
return(-1);
}
m = ww; AT.WorkPointer = ww = m + *m;
if ( m[*m-1] < 0 ) { m[*m-1] = -m[*m-1]; }
*m -= m[*m-1];
AR.Eside = RHSIDE;
}
else {
ww = term + *term;
if ( AT.WorkPointer < ww ) AT.WorkPointer = ww;
}
ClearWild(BHEAD0);
while ( w < AN.WildStop ) {
if ( *w == LOADDOLLAR ) numdollars++;
w += w[1];
}
AN.RepFunNum = 0;
AN.RepFunList = AT.WorkPointer;
AT.WorkPointer = (WORD *)(((UBYTE *)(AT.WorkPointer)) + AM.MaxTer);
topje = cbuf[AT.ebufnum].numrhs;
if ( AT.WorkPointer >= AT.WorkTop ) {
MLOCK(ErrorMessageLock);
MesWork();
MUNLOCK(ErrorMessageLock);
return(-1);
}
AN.DisOrderFlag = ifcode[2] & SUBDISORDER;
switch ( ifcode[2] & (~SUBDISORDER) ) {
case SUBONLY :
/* Must be an exact match */
AN.UseFindOnly = 1; AN.ForFindOnly = 0;
/*
Copy the term first to scratchterm. This is needed
because of the Substitute.
*/
i = *term;
t = term; newterm = r = AT.WorkPointer;
NCOPY(r,t,i); AT.WorkPointer = r;
RetVal = 0;
if ( FindRest(BHEAD newterm,m) && ( AN.UsedOtherFind ||
FindOnly(BHEAD newterm,m) ) ) {
Substitute(BHEAD newterm,m,1);
if ( numdollars ) {
WildDollars(BHEAD (WORD *)0);
numdollars = 0;
}
ClearWild(BHEAD0);
RetVal = 1;
}
else RetVal = 0;
break;
case SUBMANY :
/*
Copy the term first to scratchterm. This is needed
because of the Substitute.
*/
i = *term;
t = term; newterm = r = AT.WorkPointer;
NCOPY(r,t,i); AT.WorkPointer = r;
RetVal = 0;
AN.UseFindOnly = 0;
if ( ( power = FindRest(BHEAD newterm,m) ) > 0 ) {
if ( ( power = FindOnce(BHEAD newterm,m) ) > 0 ) {
AN.UseFindOnly = 0;
do {
Substitute(BHEAD newterm,m,1);
if ( numdollars ) {
WildDollars(BHEAD (WORD *)0);
numdollars = 0;
}
ClearWild(BHEAD0);
RetVal++;
} while ( FindRest(BHEAD newterm,m) && (
AN.UsedOtherFind || FindOnce(BHEAD newterm,m) ) );
}
else if ( power < 0 ) {
do {
Substitute(BHEAD newterm,m,1);
if ( numdollars ) {
WildDollars(BHEAD (WORD *)0);
numdollars = 0;
}
ClearWild(BHEAD0);
RetVal++;
} while ( FindRest(BHEAD newterm,m) );
}
}
else if ( power < 0 ) {
if ( FindOnce(BHEAD newterm,m) ) {
do {
Substitute(BHEAD newterm,m,1);
if ( numdollars ) {
WildDollars(BHEAD (WORD *)0);
numdollars = 0;
}
ClearWild(BHEAD0);
} while ( FindOnce(BHEAD newterm,m) );
RetVal = 1;
}
}
break;
case SUBONCE :
/*
Copy the term first to scratchterm. This is needed
because of the Substitute.
*/
i = *term;
t = term; newterm = r = AT.WorkPointer;
NCOPY(r,t,i); AT.WorkPointer = r;
RetVal = 0;
AN.UseFindOnly = 0;
if ( FindRest(BHEAD newterm,m) && ( AN.UsedOtherFind || FindOnce(BHEAD newterm,m) ) ) {
Substitute(BHEAD newterm,m,1);
if ( numdollars ) {
WildDollars(BHEAD (WORD *)0);
numdollars = 0;
}
ClearWild(BHEAD0);
RetVal = 1;
}
else RetVal = 0;
break;
case SUBMULTI :
RetVal = FindMulti(BHEAD term,m);
break;
case SUBVECTOR :
RetVal = 0;
for ( i = 0; i < *term; i++ ) ww[i] = term[i];
while ( ( power = FindAll(BHEAD ww,m,AR.Cnumlhs,ifcode) ) != 0 ) { RetVal += power; }
break;
case SUBSELECT :
ifcode += IDHEAD; ifcode += ifcode[1]; ifcode += *ifcode;
AN.UseFindOnly = 1; AN.ForFindOnly = ifcode;
if ( FindRest(BHEAD term,m) && ( AN.UsedOtherFind ||
FindOnly(BHEAD term,m) ) ) RetVal = 1;
else RetVal = 0;
break;
default :
RetVal = 0;
break;
}
AT.WorkPointer = AN.RepFunList;
cbuf[AT.ebufnum].numrhs = topje;
return(RetVal);
}
/*
#] HowMany :
#[ DoubleIfBuffers :
*/
void DoubleIfBuffers(void)
{
int newmax, i;
WORD *newsumcheck;
LONG *newheap, *newifcount;
if ( AC.MaxIf == 0 ) newmax = 10;
else newmax = 2*AC.MaxIf;
newheap = (LONG *)Malloc1(sizeof(LONG)*(newmax+1),"IfHeap");
newsumcheck = (WORD *)Malloc1(sizeof(WORD)*(newmax+1),"IfSumCheck");
newifcount = (LONG *)Malloc1(sizeof(LONG)*(newmax+1),"IfCount");
if ( AC.MaxIf ) {
for ( i = 0; i < AC.MaxIf; i++ ) {
newheap[i] = AC.IfHeap[i];
newsumcheck[i] = AC.IfSumCheck[i];
newifcount[i] = AC.IfCount[i];
}
AC.IfStack = (AC.IfStack-AC.IfHeap) + newheap;
M_free(AC.IfHeap,"AC.IfHeap");
M_free(AC.IfCount,"AC.IfCount");
M_free(AC.IfSumCheck,"AC.IfSumCheck");
}
else {
AC.IfStack = newheap;
}
AC.IfHeap = newheap;
AC.IfSumCheck = newsumcheck;
AC.IfCount = newifcount;
AC.MaxIf = newmax;
}
/*
#] DoubleIfBuffers :
#] If statement :
#[ Switch statement :
#[ DoSwitch :
*/
int DoSwitch(PHEAD WORD *term, WORD *lhs)
{
/*
For the moment we ignore the compiler buffer problems.
*/
WORD numdollar = lhs[2];
WORD ncase = DolToNumber(BHEAD numdollar);
SWITCHTABLE *swtab = FindCase(lhs[3],ncase);
return(Generator(BHEAD term,swtab->value));
}
/*
#] DoSwitch :
#[ DoEndSwitch :
*/
int DoEndSwitch(PHEAD WORD *term, WORD *lhs)
{
SWITCH *sw = AC.SwitchArray+lhs[2];
return(Generator(BHEAD term,sw->endswitch.value+1));
}
/*
#] DoEndSwitch :
#[ FindCase :
*/
SWITCHTABLE *FindCase(WORD nswitch, WORD ncase)
{
/*
First find the switch table and determine how we have to search.
*/
SWITCH *sw = AC.SwitchArray+nswitch;
WORD hi, lo, med;
if ( sw->typetable == DENSETABLE ) {
med = ncase - sw->caseoffset;
if ( med >= sw->numcases || med < 0 ) return(&sw->defaultcase);
}
else {
/*
We need a binary search in the table.
*/
if ( ncase > sw->maxcase || ncase < sw->mincase ) return(&sw->defaultcase);
hi = sw->numcases-1; lo = 0;
for(;;) {
med = (hi+lo)/2;
if ( ncase == sw->table[med].ncase ) break;
else if ( ncase > sw->table[med].ncase ) {
lo = med+1;
if ( lo > hi ) return(&sw->defaultcase);
}
else {
hi = med-1;
if ( hi < lo ) return(&sw->defaultcase);
}
}
}
return(&sw->table[med]);
}
/*
#] FindCase :
#[ DoubleSwitchBuffers :
*/
int DoubleSwitchBuffers(void)
{
int newmax, i;
SWITCH *newarray;
WORD *newheap;
if ( AC.MaxSwitch == 0 ) newmax = 10;
else newmax = 2*AC.MaxSwitch;
newarray = (SWITCH *)Malloc1(sizeof(SWITCH)*(newmax+1),"SwitchArray");
newheap = (WORD *)Malloc1(sizeof(WORD)*(newmax+1),"SwitchHeap");
if ( AC.MaxSwitch ) {
for ( i = 0; i < AC.MaxSwitch; i++ ) {
newarray[i] = AC.SwitchArray[i];
newheap[i] = AC.SwitchHeap[i];
}
M_free(AC.SwitchHeap,"AC.SwitchHeap");
M_free(AC.SwitchArray,"AC.SwitchArray");
}
for ( i = AC.MaxSwitch; i <= newmax; i++ ) {
newarray[i].table = 0;
newarray[i].tablesize = 0;
newarray[i].defaultcase.ncase = 0;
newarray[i].defaultcase.value = 0;
newarray[i].defaultcase.compbuffer = 0;
newarray[i].endswitch.ncase = 0;
newarray[i].endswitch.value = 0;
newarray[i].endswitch.compbuffer = 0;
newarray[i].typetable = 0;
newarray[i].mincase = 0;
newarray[i].maxcase = 0;
newarray[i].numcases = 0;
newarray[i].caseoffset = 0;
newarray[i].iflevel = 0;
newarray[i].whilelevel = 0;
newarray[i].nestingsum = 0;
newheap[i] = 0;
}
AC.SwitchArray = newarray;
AC.SwitchHeap = newheap;
AC.MaxSwitch = newmax;
return(0);
}
/*
#] DoubleSwitchBuffers :
#[ SwitchSplitMerge :
Sorts an array of WORDs. No adding of equal objects.
*/
void SwitchSplitMergeRec(SWITCHTABLE *array,WORD num,SWITCHTABLE *auxarray)
{
WORD n1,n2,i,j,k;
SWITCHTABLE *t1,*t2, t;
if ( num < 2 ) return;
if ( num == 2 ) {
if ( array[0].ncase > array[1].ncase ) {
t = array[0]; array[0] = array[1]; array[1] = t;
}
return;
}
n1 = num/2;
n2 = num - n1;
SwitchSplitMergeRec(array,n1,auxarray);
SwitchSplitMergeRec(array+n1,n2,auxarray);
if ( array[n1-1].ncase <= array[n1].ncase ) return;
t1 = array; t2 = auxarray; i = n1; NCOPY(t2,t1,i);
i = 0; j = n1; k = 0;
while ( i < n1 && j < num ) {
if ( auxarray[i].ncase <= array[j].ncase ) { array[k++] = auxarray[i++]; }
else { array[k++] = array[j++]; }
}
while ( i < n1 ) array[k++] = auxarray[i++];
/*
Remember: remnants of j are still in place!
*/
}
void SwitchSplitMerge(SWITCHTABLE *array,WORD num)
{
SWITCHTABLE *auxarray = (SWITCHTABLE *)Malloc1(sizeof(SWITCHTABLE)*num/2,"SwitchSplitMerge");
SwitchSplitMergeRec(array,num,auxarray);
M_free(auxarray,"SwitchSplitMerge");
}
/*
#] SwitchSplitMerge :
#] Switch statement :
*/
|