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 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
|
/** @file token.c
*
* The tokenizer. This is a part of the compiler that does an intermediate
* type of translation. It does look up the names etc and can do a number
* of optimizations. The resulting output is a stream of bytes which can
* be processed by the code generator (in the file compiler.c)
*/
/* #[ 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 :
*/
#include "form3.h"
/*
#] Includes :
#[ Compiler :
#[ tokenize :
Takes the input in 'in' and translates it into tokens.
The tokens are put in the token buffer which starts at 'AC.tokens'
and runs till 'AC.toptokens'
We may assume that the various types of brackets match properly.
object = -1: after , or (
object = 0: name/variable/number etc is allowed
object = 1: variable.
object = 2: number
object = 3: ) after subexpression
*/
#define CHECKPOLY {if(polyflag)MesPrint("&Illegal use of polynomial function"); polyflag = 0; }
int tokenize(UBYTE *in, WORD leftright)
{
int error = 0, object, funlevel = 0, bracelevel = 0, explevel = 0, numexp;
int polyflag = 0;
WORD number, type;
UBYTE *s = in, c;
SBYTE *out, *outtop, num[MAXNUMSIZE], *t;
LONG i;
if ( AC.tokens == 0 ) {
SBYTE **ppp = &(AC.tokens); /* to avoid a compiler warning */
SBYTE **pppp = &(AC.toptokens);
DoubleBuffer((void **)ppp,(void **)pppp,sizeof(SBYTE),"start tokens");
}
out = AC.tokens;
outtop = AC.toptokens - MAXNUMSIZE;
AC.dumnumflag = 0;
object = 0;
while ( *in ) {
if ( out > outtop ) {
LONG oldsize = (LONG)(out - AC.tokens);
SBYTE **ppp = &(AC.tokens); /* to avoid a compiler warning */
SBYTE **pppp = &(AC.toptokens);
DoubleBuffer((void **)ppp,(void **)pppp,sizeof(SBYTE),"expand tokens");
out = AC.tokens + oldsize;
outtop = AC.toptokens - MAXNUMSIZE;
}
switch ( FG.cTable[*in] ) {
case 0: /* a-zA-Z */
CHECKPOLY
s = in++;
while ( FG.cTable[*in] == 0 || FG.cTable[*in] == 1
|| *in == '_' ) in++;
dovariable: c = *in; *in = 0;
if ( object > 0 ) {
MesPrint("&Illegal position for %s",s);
if ( !error ) error = 1;
}
if ( out > AC.tokens && ( out[-1] == TWILDCARD || out[-1] == TNOT ) ) {
type = GetName(AC.varnames,s,&number,NOAUTO);
}
else {
type = GetName(AC.varnames,s,&number,WITHAUTO);
}
if ( type < 0 )
type = GetName(AC.exprnames,s,&number,NOAUTO);
switch ( type ) {
case CSYMBOL: *out++ = TSYMBOL; break;
case CINDEX:
if ( number >= (AM.IndDum-AM.OffsetIndex) ) {
if ( c != '?' ) {
MesPrint("&Generated indices should be of the type Nnumber_?");
error = 1;
}
else {
*in++ = c; c = *in; *in = 0;
AC.dumnumflag = 1;
}
}
*out++ = TINDEX;
break;
case CVECTOR: *out++ = TVECTOR; break;
case CFUNCTION:
#ifdef WITHMPI
/*
* In the preprocessor, random functions in #$var=... and #inside
* may cause troubles, because the program flow on a slave may be
* different from those on others. We set AC.RhsExprInModuleFlag in order
* to make the change of $-variable be done on the master and thus keep the
* consistency among the master and all slave processes. The previous value
* of AC.RhsExprInModuleFlag will be restored after #$var=... and #inside.
*/
if ( AP.PreAssignFlag || AP.PreInsideLevel ) {
switch ( number + FUNCTION ) {
case RANDOMFUNCTION:
case RANPERM:
AC.RhsExprInModuleFlag = 1;
}
}
#endif
*out++ = TFUNCTION;
break;
case CSET: *out++ = TSET; break;
case CEXPRESSION: *out++ = TEXPRESSION;
if ( leftright == LHSIDE ) {
if ( !error ) error = 1;
MesPrint("&Expression not allowed in LH-side of substitution: %s",s);
}
/*[06nov2003 mt]:*/
#ifdef WITHMPI
else { /*RHSide*/
/* NOTE: We always set AC.RhsExprInModuleFlag regardless of
* AP.PreAssignFlag or AP.PreInsideLevel because we have to detect
* RHS expressions even in those cases. */
AC.RhsExprInModuleFlag = 1;
}
if ( !AP.PreAssignFlag && !AP.PreInsideLevel )
Expressions[number].vflags |= ISINRHS;
#endif
/*:[06nov2003 mt]*/
if ( AC.exprfillwarning == 0 ) {
AC.exprfillwarning = 1;
}
break;
case CDELTA: *out++ = TDELTA; *in = c;
object = 1; continue;
case CDUBIOUS: *out++ = TDUBIOUS; break;
default: *out++ = TDUBIOUS;
if ( !error ) error = 1;
MesPrint("&Undeclared variable %s",s);
number = AddDubious(s);
break;
}
object = 1;
donumber: i = 0;
do { num[i++] = (SBYTE)(number & 0x7F); number >>= 7; } while ( number );
while ( --i >= 0 ) *out++ = num[i];
*in = c;
break;
case 1: /* 0-9 */
{
#ifdef WITHFLOAT
int spec;
UBYTE *in2;
#endif
CHECKPOLY
s = in;
while ( *s == '0' && FG.cTable[s[1]] == 1 ) s++;
in = s+1; i = 1;
while ( FG.cTable[*in] == 1 ) { in++; i++; }
if ( object > 0 ) {
c = *in; *in = 0;
MesPrint("&Illegal position for %s",s);
*in = c;
if ( !error ) error = 1;
}
if ( i == 1 && *in == '_' && ( *s == '5' || *s == '6'
|| *s == '7' ) ) {
in++; *out++ = TSGAMMA; *out++ = (SBYTE)(*s - '4');
object = 1;
break;
}
#ifdef WITHFLOAT
in2 = CheckFloat(in,&spec);
if ( in2 > in ) {
if ( spec == -1 ) {
MesPrint("&The floating point system has not been started: %s",in);
if ( !error ) error = 1;
}
else {
in = in2;
dofloat:
while ( out + (in-s) >= AC.toptokens ) {
LONG oldsize = (LONG)(out - AC.tokens);
SBYTE **ppp = &(AC.tokens); /* to avoid a compiler warning */
SBYTE **pppp = &(AC.toptokens);
DoubleBuffer((void **)ppp,(void **)pppp,sizeof(SBYTE),"more tokens");
out = AC.tokens + oldsize;
outtop = AC.toptokens - MAXNUMSIZE;
}
*out++ = TFLOAT;
while ( s < in ) *out++ = *s++;
}
}
else
#endif
{
*out++ = TNUMBER;
if ( ( i & 1 ) != 0 ) *out++ = (SBYTE)(*s++ - '0');
while ( out + (in-s)/2 >= AC.toptokens ) {
LONG oldsize = (LONG)(out - AC.tokens);
SBYTE **ppp = &(AC.tokens); /* to avoid a compiler warning */
SBYTE **pppp = &(AC.toptokens);
DoubleBuffer((void **)ppp,(void **)pppp,sizeof(SBYTE),"more tokens");
out = AC.tokens + oldsize;
outtop = AC.toptokens - MAXNUMSIZE;
}
while ( s < in ) { /* We store in base 100 */
*out++ = (SBYTE)(( *s - '0' ) * 10 + ( s[1] - '0' ));
s += 2;
}
}
object = 2;
}
break;
case 2: /* . $ _ ? # ' */
CHECKPOLY
if ( *in == '?' ) {
if ( leftright == LHSIDE ) {
if ( object == 1 ) { /* follows a name */
in++; *out++ = TWILDCARD;
if ( FG.cTable[in[0]] == 0 || in[0] == '[' || in[0] == '{' ) object = 0;
}
else if ( object == -1 ) { /* follows comma or ( */
in++; s = in;
while ( FG.cTable[*in] == 0 || FG.cTable[*in] == 1 ) in++;
c = *in; *in = 0;
if ( FG.cTable[*s] != 0 ) {
MesPrint("&Illegal name for argument list variable %s",s);
error = 1;
}
else {
i = AddWildcardName((UBYTE *)s);
*in = c;
*out++ = TWILDARG;
*out++ = (SBYTE)i;
}
object = 1;
}
else {
MesPrint("&Illegal position for ?");
error = 1;
in++;
}
}
else {
if ( object != -1 ) goto IllPos;
in++;
if ( FG.cTable[*in] == 0 || FG.cTable[*in] == 1 ) {
s = in;
while ( FG.cTable[*in] == 0 || FG.cTable[*in] == 1 ) in++;
c = *in; *in = 0;
i = GetWildcardName((UBYTE *)s);
if ( i <= 0 ) {
MesPrint("&Undefined argument list variable %s",s);
error = 1;
}
*in = c;
*out++ = TWILDARG;
*out++ = (SBYTE)i;
}
else {
if ( AC.vectorlikeLHS == 0 ) {
MesPrint("&Generated index ? only allowed in vector substitution",s);
error = 1;
}
*out++ = TGENINDEX;
}
object = 1;
}
}
else if ( *in == '.' ) {
if ( object == 1 ) { /* follows a name */
*out++ = TDOT;
object = 0;
in++;
}
#ifdef WITHFLOAT
else if ( object == 0 || object == -1 ) {
/*
Test for floating point number
*/
int spec;
s = CheckFloat(in,&spec);
if ( s > in ) {
if ( spec == -1 ) {
MesPrint("&The floating point system has not been started: %s",in);
if ( !error ) error = 1;
in++;
}
else {
UBYTE *a = s; s = in; in = a;
goto dofloat;
}
}
else goto IllPos;
}
#endif
else goto IllPos;
}
else if ( *in == '$' ) { /* $ variable */
in++;
s = in;
if ( FG.cTable[*in] == 0 ) {
while ( FG.cTable[*in] == 0 || FG.cTable[*in] == 1 ) in++;
if ( *in == '_' && AP.PreAssignFlag == 2 ) in++;
c = *in; *in = 0;
if ( object > 0 ) {
if ( object != 1 || leftright == RHSIDE ) {
MesPrint("&Illegal position for $%s",s);
if ( !error ) error = 1;
} /* else can be assignment in wildcard */
else {
if ( ( number = GetDollar(s) ) < 0 ) {
number = AddDollar(s,0,0,0);
}
}
}
else if ( ( number = GetDollar(s) ) < 0 ) {
MesPrint("&Undefined variable $%s",s);
if ( !error ) error = 1;
number = AddDollar(s,0,0,0);
}
*out++ = TDOLLAR;
object = 1;
if ( ( AC.exprfillwarning == 0 ) &&
( ( out > AC.tokens+1 ) && ( out[-2] != TWILDCARD ) ) ) {
AC.exprfillwarning = 1;
}
goto donumber;
}
else {
MesPrint("Illegal name for $ variable after %s",in);
if ( !error ) error = 1;
}
}
else if ( *in == '#' ) {
in++;
if ( object == 1 ) { /* follows a name */
*out++ = TCONJUGATE;
}
else {
MesPrint("&Illegal position for %#");
error = 1;
}
}
else goto IllPos;
break;
case 3: /* [ ] */
CHECKPOLY
if ( *in == '[' ) {
if ( object == 1 ) { /* after name */
t = out-1;
if ( *t == RPARENTHESIS ) {
*out++ = LBRACE; *out++ = LPARENTHESIS;
bracelevel++; explevel = bracelevel;
}
else {
while ( *t >= 0 && t > AC.tokens ) t--;
if ( *t == TEXPRESSION ) {
*out++ = LBRACE; *out++ = LPARENTHESIS;
bracelevel++; explevel = bracelevel;
}
else {*out++ = LBRACE; bracelevel++; }
}
object = 0;
}
else { /* name. find matching ] */
s = in;
in = SkipAName(in);
goto dovariable;
}
}
else {
if ( explevel > 0 && explevel == bracelevel ) {
*out++ = RPARENTHESIS; explevel = 0;
}
*out++ = RBRACE; object = 1; bracelevel--;
}
in++;
break;
case 4: /* ( ) = ; , */
if ( *in == '(' ) {
if ( funlevel >= AM.MaxParLevel ) {
MesPrint("&More than %d levels of parentheses",AM.MaxParLevel);
return(-1);
}
if ( object == 1 ) { /* After name -> function,vector */
AC.tokenarglevel[funlevel++] = TYPEISFUN;
*out++ = TFUNOPEN;
if ( polyflag ) {
if ( in[1] != ')' && in[1] != ',' ) {
*out++ = TNUMBER; *out++ = (SBYTE)(polyflag);
*out++ = TCOMMA;
*out++ = LPARENTHESIS;
}
else {
*out++ = LPARENTHESIS;
*out++ = TNUMBER; *out++ = (SBYTE)(polyflag);
}
polyflag = 0;
}
else if ( in[1] != ')' && in[1] != ',' ) {
*out++ = LPARENTHESIS;
}
}
else if ( object <= 0 ) {
CHECKPOLY
AC.tokenarglevel[funlevel++] = TYPEISSUB;
*out++ = LPARENTHESIS;
}
else {
polyflag = 0;
AC.tokenarglevel[funlevel++] = TYPEISMYSTERY;
MesPrint("&Illegal position for (: %s",in);
if ( error >= 0 ) error = -1;
}
object = -1;
}
else if ( *in == ')' ) {
funlevel--;
if ( funlevel < 0 ) {
/* if ( funflag == 0 ) { */
MesPrint("&There is an unmatched parenthesis");
if ( error >= 0 ) error = -1;
/* } */
}
else if ( object <= 0
&& ( AC.tokenarglevel[funlevel] != TYPEISFUN
|| out[-1] != TFUNOPEN ) ) {
MesPrint("&Illegal position for closing parenthesis.");
if ( error >= 0 ) error = -1;
if ( AC.tokenarglevel[funlevel] == TYPEISFUN ) object = 1;
else object = 3;
}
else {
if ( AC.tokenarglevel[funlevel] == TYPEISFUN ) {
if ( out[-1] == TFUNOPEN ) out--;
else {
if ( out[-1] != TCOMMA ) *out++ = RPARENTHESIS;
*out++ = TFUNCLOSE;
}
object = 1;
}
else if ( AC.tokenarglevel[funlevel] == TYPEISSUB ) {
*out++ = RPARENTHESIS;
object = 3;
}
}
}
else if ( *in == ',' ) {
if ( /* object > 0 && */ funlevel > 0 &&
AC.tokenarglevel[funlevel-1] == TYPEISFUN ) {
if ( out[-1] != TFUNOPEN && out[-1] != TCOMMA )
*out++ = RPARENTHESIS;
else { *out++ = TNUMBER; *out++ = 0; }
*out++ = TCOMMA;
if ( in[1] != ',' && in[1] != ')' )
*out++ = LPARENTHESIS;
else if ( in[1] == ')' ) {
*out++ = TNUMBER; *out++ = 0;
}
}
/*
else if ( object > 0 ) {
}
*/
else {
MesPrint("&Illegal position for comma: %s",in);
MesPrint("&Forgotten ; ?");
if ( error >= 0 ) error = -1;
}
object = -1;
}
else goto IllPos;
in++;
break;
case 5: /* + - * % / ^ : */
CHECKPOLY
if ( *in == ':' || *in == '%' ) goto IllPos;
if ( *in == '*' || *in == '/' || *in == '^' ) {
if ( object <= 0 ) {
MesPrint("&Illegal position for operator: %s",in);
if ( error >= 0 ) error = -1;
}
else if ( *in == '*' ) *out++ = TMULTIPLY;
else if ( *in == '/' ) *out++ = TDIVIDE;
else *out++ = TPOWER;
in++;
}
else {
i = 1;
while ( *in == '+' || *in == '-' ) {
if ( *in == '-' ) i = -i;
in++;
}
if ( i == 1 ) {
if ( out > AC.tokens && out[-1] != TFUNOPEN &&
out[-1] != LPARENTHESIS && out[-1] != TCOMMA
&& out[-1] != LBRACE )
*out++ = TPLUS;
}
else *out++ = TMINUS;
}
object = 0;
break;
case 6: /* Whitespace */
in++; break;
case 7: /* { | } */
CHECKPOLY
if ( *in == '{' ) {
if ( object > 0 ) {
MesPrint("&Illegal position for %s",in);
if ( !error ) error = 1;
}
s = in+1;
SKIPBRA2(in)
number = DoTempSet(s,in);
in++;
if ( number >= 0 ) {
*out++ = TSET;
i = 0;
do { num[i++] = (SBYTE)(number & 0x7F); number >>= 7; } while ( number );
while ( --i >= 0 ) *out++ = num[i];
}
else if ( error == 0 ) error = 1;
object = 1;
}
else goto IllPos;
break;
case 8: /* ! & < > */
CHECKPOLY
if ( *in != '!' || leftright == RHSIDE
|| object != 1 || out[-1] != TWILDCARD ) goto IllPos;
*out++ = TNOT;
if ( FG.cTable[in[1]] == 0 || in[1] == '[' || in[1] == '{' ) object = 0;
in++;
break;
default:
IllPos: MesPrint("&Illegal character at this position: %s",in);
if ( error >= 0 ) error = -1;
in++;
polyflag = 0;
break;
}
}
*out++ = TENDOFIT;
AC.endoftokens = out;
if ( funlevel > 0 || bracelevel != 0 ) {
if ( funlevel > 0 ) MesPrint("&Unmatched parentheses");
if ( bracelevel != 0 ) MesPrint("&Unmatched braces");
return(-1);
}
if ( AC.TokensWriteFlag ) WriteTokens(AC.tokens);
/*
Simplify fixed set elements
*/
if ( error == 0 && simp1token(AC.tokens) ) error = 1;
/*
Collect wildcards for the prototype. Simplify the leftover wildcards
*/
if ( error == 0 && leftright == LHSIDE && simpwtoken(AC.tokens) )
error = 1;
/*
Now prepare the set[n] objects in the RHS.
*/
if ( error == 0 && leftright == RHSIDE && simp4token(AC.tokens) )
error = 1;
/*
Simplify simple function arguments (and 1/fac_ and 1/invfac_)
*/
if ( error == 0 && simp2token(AC.tokens) ) error = 1;
/*
Next we try to remove composite denominators or exponents and
replace them by their internal functions. This may involve expanding
the buffer. The return code of 3a is negative if there is an error
and positive if indeed we need to do some work.
simp3btoken does the work
*/
numexp = 0;
if ( error == 0 && ( numexp = simp3atoken(AC.tokens,leftright) ) < 0 )
error = 1;
if ( numexp > 0 ) {
// Make some space at the beginning of AC.tokens, filled with TEMPTY.
SBYTE *tt;
out = AC.tokens;
while ( *out != TENDOFIT ) out++;
while ( out+numexp*9 > outtop ) {
LONG oldsize = (LONG)(out - AC.tokens);
SBYTE **ppp = &(AC.tokens); /* to avoid a compiler warning */
SBYTE **pppp = &(AC.toptokens);
DoubleBuffer((void **)ppp,(void **)pppp,sizeof(SBYTE),"out tokens");
out = AC.tokens + oldsize;
outtop = AC.toptokens - MAXNUMSIZE;
}
tt = out + numexp*9;
while ( out >= AC.tokens ) { *tt-- = *out--; }
while ( tt >= AC.tokens ) { *tt-- = TEMPTY; }
if ( error == 0 && simp3btoken(AC.tokens,leftright) ) error = 1;
if ( error == 0 && simp2token(AC.tokens) ) error = 1;
}
/*
In simp5token we test for special cases like sumvariables that are
already wildcards, etc.
*/
if ( error == 0 && simp5token(AC.tokens,leftright) ) error = 1;
/*
In simp6token we test for special cases like factorized expressions
that occur in the RHS in an improper way.
*/
if ( error == 0 && simp6token(AC.tokens,leftright) ) error = 1;
return(error);
}
/*
#] tokenize :
#[ WriteTokens :
*/
char *ttypes[] = { "\n", "S", "I", "V", "F", "set", "E", "dotp", "#",
"sub", "d_", "$", "dub", "(", ")", "?", "??", ".", "[", "]",
",", "((", "))", "*", "/", "^", "+", "-", "!", "end", "{{", "}}",
"N_?", "conj", "()", "#d", "^d", "_", "snum" };
void WriteTokens(SBYTE *in)
{
int numinline = 0, x, n = sizeof(ttypes)/sizeof(char *);
char outbuf[81], *s, *out, c;
out = outbuf;
while ( *in != TENDOFIT ) {
if ( *in < 0 ) {
if ( *in >= -n ) {
s = ttypes[-*in];
while ( *s ) { *out++ = *s++; numinline++; }
}
else {
*out++ = '-'; x = -*in; numinline++;
goto writenumber;
}
}
else {
x = *in;
writenumber:
s = out;
do {
*out++ = (char)(( x % 10 ) + '0');
numinline++;
x = x / 10;
} while ( x );
c = out[-1]; out[-1] = *s; *s = c;
}
if ( numinline > 70 ) {
*out = 0;
MesPrint("%s",outbuf);
out = outbuf; numinline = 0;
}
else {
*out++ = ' '; numinline++;
}
in++;
}
if ( numinline > 0 ) { *out = 0; MesPrint("%s",outbuf); }
}
/*
#] WriteTokens :
#[ simp1token :
Routine substitutes set elements if possible.
This means sets with a fixed argument like setname[3].
*/
int simp1token(SBYTE *s)
{
int error = 0, n, i, base;
WORD numsub;
SBYTE *fill = s, *start, *t, numtab[10];
SETS set;
while ( *s != TENDOFIT ) {
if ( *s == RBRACE ) {
start = fill-1;
while ( *start != LBRACE ) start--;
t = start - 1;
while ( *t >= 0 ) t--;
if ( *t == TSET && ( start[1] == TNUMBER || start[1] == TNUMBER1 ) ) {
base = start[1] == TNUMBER ? 100: 128;
start += 2;
numsub = *start++;
while ( *start >= 0 && start < fill )
{ numsub = base*numsub + *start++; }
if ( start == fill ) {
start = t;
t++; n = *t++; while ( *t >= 0 ) { n = 128*n + *t++; }
set = Sets+n;
if ( ( set->type != CRANGE )
&& ( numsub > 0 && numsub <= set->last-set->first ) ) {
fill = start;
n = SetElements[set->first+numsub-1];
switch (set->type) {
case CSYMBOL:
if ( n > MAXPOWER ) {
n -= 2*MAXPOWER;
if ( n < 0 ) { n = -n; *fill++ = TMINUS; }
*fill++ = TNUMBER1;
}
else *fill++ = TSYMBOL;
break;
case CINDEX:
if ( n < AM.OffsetIndex ) *fill++ = TNUMBER1;
else {
*fill++ = TINDEX;
n -= AM.OffsetIndex;
}
break;
case CVECTOR: *fill++ = TVECTOR;
n -= AM.OffsetVector; break;
case CFUNCTION: *fill++ = TFUNCTION;
n -= FUNCTION; break;
case CNUMBER: *fill++ = TNUMBER1; break;
case CDUBIOUS: *fill++ = TDUBIOUS; n = 1; break;
}
i = 0;
if ( n < 0 ) {
MesPrint("Value of n = %d",n);
}
do { numtab[i++] = (SBYTE)(n & 0x7F); n >>= 7; } while ( n );
while ( --i >= 0 ) *fill++ = numtab[i];
}
else {
MesPrint("&Illegal element %d in set",numsub);
error++;
}
s++; continue;
}
}
*fill++ = *s++;
}
else *fill++ = *s++;
}
*fill++ = TENDOFIT;
return(error);
}
/*
#] simp1token :
#[ simpwtoken :
Only to be called in the LHS.
Hunts down the wildcards and writes them to the wildcardbuffer.
Next it causes the ProtoType to be constructed.
All wildcards are simplified into the trailing TWILDCARD,
because the specifics are stored in the prototype.
These specifics also include the transfer of wildcard values
to $variables.
Types of wildcards:
a?, a?set, a?!set, a?set[i], A?set1?set2, ?a
After this we can strip the set information.
We still need the ? because of the wildcarding offset in code generation
*/
int simpwtoken(SBYTE *s)
{
int error = 0, first = 1, notflag;
WORD num, numto, numdollar, *w = AC.WildC, *wstart, *wtop;
SBYTE *fill = s, *t, *v, *s0 = s;
while ( *s != TENDOFIT ) {
if ( *s == TWILDCARD ) {
notflag = 0; t = fill;
while ( t > s0 && t[-1] >= 0 ) t--;
v = t; num = 0; *fill++ = *s++;
while ( *v >= 0 ) num = 128*num + *v++;
if ( t > s0 ) t--;
AC.NwildC += 4;
if ( AC.NwildC > 4*AM.MaxWildcards ) goto firsterr;
switch ( *t ) {
case TSYMBOL:
case TDUBIOUS:
*w++ = SYMTOSYM; *w++ = 4; *w++ = num; *w++ = num; break;
case TINDEX:
num += AM.OffsetIndex;
*w++ = INDTOIND; *w++ = 4; *w++ = num; *w++ = num; break;
case TVECTOR:
num += AM.OffsetVector;
*w++ = VECTOVEC; *w++ = 4; *w++ = num; *w++ = num; break;
case TFUNCTION:
num += FUNCTION;
*w++ = FUNTOFUN; *w++ = 4; *w++ = num; *w++ = num; break;
default:
MesPrint("&Illegal type of wildcard in LHS");
error = -1;
*w++ = SYMTOSYM; *w++ = 4; *w++ = num; *w++ = num; break;
break;
}
/*
Now the sets. The s pointer sits after the ?
*/
wstart = w;
if ( *s == TNOT && s[1] == TSET ) { notflag = 1; s++; }
if ( *s == TSET ) {
s++; num = 0; while ( *s >= 0 ) num = 128*num + *s++;
if ( notflag == 0 && *s == TWILDCARD && s[1] == TSET ) {
s += 2; numto = 0; while ( *s >= 0 ) numto = 128*numto + *s++;
if ( num < AM.NumFixedSets || numto < AM.NumFixedSets
|| Sets[num].type == CRANGE || Sets[numto].type == CRANGE ) {
MesPrint("&This type of set not allowed in this wildcard construction");
error = 1;
}
else {
AC.NwildC += 4;
if ( AC.NwildC > 4*AM.MaxWildcards ) goto firsterr;
*w++ = FROMSET; *w++ = 4; *w++ = num; *w++ = numto;
wstart = w;
}
}
else if ( notflag == 0 && *s == LBRACE && s[1] == TSYMBOL ) {
if ( num < AM.NumFixedSets || Sets[num].type == CRANGE ) {
MesPrint("&This type of set not allowed in this wildcard construction");
error = 1;
}
v = s; s += 2;
numto = 0; while ( *s >= 0 ) numto = 128*numto + *s++;
if ( *s == TWILDCARD ) s++; /* most common mistake */
if ( *s == RBRACE ) {
s++;
AC.NwildC += 8;
if ( AC.NwildC > 4*AM.MaxWildcards ) goto firsterr;
*w++ = SETTONUM; *w++ = 4; *w++ = num; *w++ = numto;
wstart = w;
*w++ = SYMTOSYM; *w++ = 4; *w++ = numto; *w++ = 0;
}
else if ( *s == TDOLLAR ) {
s++; numdollar = 0;
while ( *s >= 0 ) numdollar = 128*numdollar + *s++;
if ( *s == RBRACE ) {
s++;
AC.NwildC += 12;
if ( AC.NwildC > 4*AM.MaxWildcards ) goto firsterr;
*w++ = SETTONUM; *w++ = 4; *w++ = num; *w++ = numto;
wstart = w;
*w++ = SYMTOSYM; *w++ = 4; *w++ = numto; *w++ = 0;
*w++ = LOADDOLLAR; *w++ = 4; *w++ = numdollar;
*w++ = numdollar;
}
else { s = v; goto singlewild; }
}
else { s = v; goto singlewild; }
}
else {
singlewild: num += notflag * 2*WILDOFFSET;
AC.NwildC += 4;
if ( AC.NwildC > 4*AM.MaxWildcards ) goto firsterr;
*w++ = FROMSET; *w++ = 4; *w++ = num; *w++ = -WILDOFFSET;
wstart = w;
}
}
else if ( *s != TDOLLAR && *s != TENDOFIT && *s != RPARENTHESIS
&& *s != RBRACE && *s != TCOMMA && *s != TFUNCLOSE && *s != TMULTIPLY
&& *s != TPOWER && *s != TDIVIDE && *s != TPLUS && *s != TMINUS
&& *s != TPOWER1 && *s != TEMPTY && *s != TFUNOPEN && *s != TDOT ) {
MesPrint("&Illegal type of wildcard in LHS");
error = -1;
}
if ( *s == TDOLLAR ) {
s++; numdollar = 0;
while ( *s >= 0 ) numdollar = 128*numdollar + *s++;
AC.NwildC += 4;
if ( AC.NwildC > 4*AM.MaxWildcards ) goto firsterr;
wtop = w + 4;
if ( wstart < w ) {
while ( w > wstart ) { w[4] = w[0]; w--; }
}
*w++ = LOADDOLLAR; *w++ = 4; *w++ = numdollar; *w++ = numdollar;
w = wtop;
}
}
else if ( *s == TWILDARG ) {
*fill++ = *s++;
num = 0;
while ( *s >= 0 ) { num = 128*num + *s; *fill++ = *s++; }
AC.NwildC += 4;
if ( AC.NwildC > 4*AM.MaxWildcards ) {
firsterr: if ( first ) {
MesPrint("&More than %d wildcards",AM.MaxWildcards);
error = -1;
first = 0;
}
}
else { *w++ = ARGTOARG; *w++ = 4; *w++ = num; *w++ = -1; }
if ( *s == TDOLLAR ) {
s++; num = 0; while ( *s >= 0 ) num = 128*num + *s++;
AC.NwildC += 4;
if ( AC.NwildC > 4*AM.MaxWildcards ) goto firsterr;
*w++ = LOADDOLLAR; *w++ = 4; *w++ = num; *w++ = num;
}
}
else *fill++ = *s++;
}
*fill++ = TENDOFIT;
AC.WildC = w;
return(error);
}
/*
#] simpwtoken :
#[ simp2token :
Deals with function arguments.
The tokenizer has given function arguments extra parentheses.
We remove the double parentheses.
Next we remove the parentheses around the simple arguments.
It also replaces /fac_() by *invfac_() and /invfac_() by *fac_()
*/
int simp2token(SBYTE *s)
{
SBYTE *to, *fill, *t, *v, *w, *s0 = s, *vv;
int error = 0, n;
/*
Set substitutions
*/
fill = to = s;
while ( *s != TENDOFIT ) {
if ( *s == LPARENTHESIS && s[1] == LPARENTHESIS ) {
t = s+1; n = 0;
while ( n >= 0 ) {
t++;
if ( *t == LPARENTHESIS ) n++;
else if ( *t == RPARENTHESIS ) n--;
}
if ( t[1] == RPARENTHESIS ) {
*t = TEMPTY; s++;
}
*fill++ = *s++;
}
else if ( *s == TEMPTY ) s++;
else if ( *s == AM.facnum && ( fill > (s0+1) ) && fill[-2] == TDIVIDE
&& fill[-1] == TFUNCTION ) {
fill[-2] = TMULTIPLY; *fill++ = (SBYTE)(AM.invfacnum); s++;
}
else if ( *s == AM.invfacnum && ( fill > (s0+1) ) && fill[-2] == TDIVIDE
&& fill[-1] == TFUNCTION ) {
fill[-2] = TMULTIPLY; *fill++ = (SBYTE)(AM.facnum); s++;
}
else *fill++ = *s++;
}
*fill++ = TENDOFIT;
/*
Second round: try to locate 'simple' arguments and strip their brackets
We add (9-feb-2010) to the simple arguments integers of any size
*/
fill = s = to;
while ( *s != TENDOFIT ) {
if ( *s == LPARENTHESIS ) {
t = s; n = 0;
while ( n >= 0 ) {
t++;
if ( *t == LPARENTHESIS ) n++;
else if ( *t == RPARENTHESIS ) n--;
}
if ( t[1] == TFUNCLOSE && s[1] != TWILDARG ) { /* Check for last argument in sum */
v = fill - 1; n = 0;
while ( n >= 0 && v >= to ) {
if ( *v == TFUNOPEN ) n--;
else if ( *v == TFUNCLOSE ) n++;
v--;
}
if ( v > to ) {
while ( *v >= 0 ) v--;
if ( *v == TFUNCTION ) { v++;
n = 0; while ( *v >= 0 && v < fill ) n = 128*n + *v++;
if ( n == AM.sumnum || n == AM.sumpnum ) {
*fill++ = *s++; continue;
}
else if ( ( n == (FIRSTBRACKET-FUNCTION)
|| n == (TERMSINEXPR-FUNCTION)
|| n == (SIZEOFFUNCTION-FUNCTION)
|| n == (NUMFACTORS-FUNCTION)
|| n == (GCDFUNCTION-FUNCTION)
|| n == (DIVFUNCTION-FUNCTION)
|| n == (REMFUNCTION-FUNCTION)
|| n == (INVERSEFUNCTION-FUNCTION)
|| n == (MULFUNCTION-FUNCTION)
|| n == (FACTORIN-FUNCTION)
|| n == (FIRSTTERM-FUNCTION)
|| n == (CONTENTTERM-FUNCTION) )
&& fill[-1] == TFUNOPEN ) {
v = s+1;
if ( *v == TEXPRESSION ) {
v++;
n = 0; while ( *v >= 0 ) n = 128*n + *v++;
if ( v == t ) {
*t = TEMPTY; s++;
}
}
}
}
}
}
if ( ( fill > to )
&& ( ( fill[-1] == TFUNOPEN || fill[-1] == TCOMMA )
&& ( t[1] == TFUNCLOSE || t[1] == TCOMMA ) ) ) {
v = s + 1;
switch ( *v ) {
case TMINUS:
v++;
if ( *v == TVECTOR ) {
w = v+1; while ( *w >= 0 ) w++;
if ( w == t ) {
*t = TEMPTY; s++;
}
}
else {
if ( *v == TNUMBER || *v == TNUMBER1 ) {
if ( BITSINWORD == 16 ) { ULONG x; WORD base;
base = ( *v == TNUMBER ) ? 100: 128;
vv = v+1; x = 0; while ( *vv >= 0 ) { x = x*base + *vv++; }
if ( ( vv != t ) || ( ( vv - v ) > 4 ) || ( x > (MAXPOSITIVE+1) ) )
*fill++ = *s++;
else { *t = TEMPTY; s++; break; }
}
else if ( BITSINWORD == 32 ) { ULONG x; WORD base;
base = ( *v == TNUMBER ) ? 100: 128;
vv = v+1; x = 0; while ( *vv >= 0 ) { x = x*base + *vv++; }
if ( ( vv != t ) || ( ( vv - v ) > 6 ) || ( x > (MAXPOSITIVE+1) ) )
*fill++ = *s++;
else { *t = TEMPTY; s++; break; }
}
else {
if ( ( v+2 == t ) || ( v+3 == t && v[2] >= 0 ) )
{ *t = TEMPTY; s++; break; }
else *fill++ = *s++;
}
}
else if ( *v == LPARENTHESIS && t[-1] == RPARENTHESIS ) {
w = v; n = 0;
while ( n >= 0 ) {
w++;
if ( *w == LPARENTHESIS ) n++;
else if ( *w == RPARENTHESIS ) n--;
}
if ( w == ( t-1 ) ) { *t = TEMPTY; s++; }
else *fill++ = *s++;
}
else *fill++ = *s++;
break;
}
/* fall through */
case TSETNUM:
v++; while ( *v >= 0 ) v++;
goto tcommon;
case TSYMBOL:
if ( ( v[1] == COEFFSYMBOL || v[1] == NUMERATORSYMBOL
|| v[1] == DENOMINATORSYMBOL ) && v[2] < 0 ) {
*fill++ = *s++; break;
}
/* fall through */
case TSET:
case TVECTOR:
case TINDEX:
case TFUNCTION:
case TDOLLAR:
case TDUBIOUS:
case TSGAMMA:
tcommon: v++; while ( *v >= 0 ) v++;
if ( v == t || ( v[0] == TWILDCARD && v+1 == t ) )
{ *t = TEMPTY; s++; }
else *fill++ = *s++;
break;
case TGENINDEX:
v++;
if ( v == t ) { *t = TEMPTY; s++; }
else *fill++ = *s++;
break;
case TNUMBER:
case TNUMBER1:
if ( BITSINWORD == 16 ) { ULONG x; WORD base;
base = ( *v == TNUMBER ) ? 100: 128;
vv = v+1; x = 0; while ( *vv >= 0 ) { x = x*base + *vv++; }
if ( ( vv != t ) || ( ( vv - v ) > 4 ) || ( x > MAXPOSITIVE ) )
*fill++ = *s++;
else { *t = TEMPTY; s++; break; }
}
else if ( BITSINWORD == 32 ) { ULONG x; WORD base;
base = ( *v == TNUMBER ) ? 100: 128;
vv = v+1; x = 0; while ( *vv >= 0 ) { x = x*base + *vv++; }
if ( ( vv != t ) || ( ( vv - v ) > 6 ) || ( x > MAXPOSITIVE ) )
*fill++ = *s++;
else { *t = TEMPTY; s++; break; }
}
else {
if ( ( v+2 == t ) || ( v+3 == t && v[2] >= 0 ) )
{ *t = TEMPTY; s++; break; }
else *fill++ = *s++;
}
break;
case TWILDARG:
v++; while ( *v >= 0 ) v++;
if ( v == t ) { *t = TEMPTY; s++; }
else *fill++ = *s++;
break;
case TEXPRESSION:
/*
First establish that there is only the expression
in this argument.
*/
vv = s+1;
while ( vv < t ) {
if ( *vv != TEXPRESSION ) break;
vv++; while ( *vv >= 0 ) vv++;
}
if ( vv < t ) { *fill++ = *s++; break; }
/*
Find the function
*/
w = fill-1; n = 0;
while ( n >= 0 && w >= to ) {
if ( *w == TFUNOPEN ) n--;
else if ( *w == TFUNCLOSE ) n++;
w--;
}
w--; while ( w > to && *w >= 0 ) w--;
if ( *w != TFUNCTION ) { *fill++ = *s++; break; }
w++; n = 0;
while ( *w >= 0 ) { n = 128*n + *w++; }
if ( n == GCDFUNCTION-FUNCTION
|| n == DIVFUNCTION-FUNCTION
|| n == REMFUNCTION-FUNCTION
|| n == INVERSEFUNCTION-FUNCTION
|| n == MULFUNCTION-FUNCTION ) {
*t = TEMPTY; s++;
}
else *fill++ = *s++;
break;
default: *fill++ = *s++; break;
}
}
else *fill++ = *s++;
}
else if ( *s == TEMPTY ) s++;
else *fill++ = *s++;
}
*fill++ = TENDOFIT;
return(error);
}
/*
#] simp2token :
#[ simp3atoken :
We hunt for denominators and exponents that seem hidden.
For the denominators we have to recognize:
/fun /fun() /fun^power /fun()^power
/set[n] /set[n]() /set[n]^power /set[n]()^power
/symbol^power (power no number or symbol wildcard)
/dotpr^power (id)
/#^power (id)
/() /()^power
/vect /index /vect(anything) /vect(anything)^power
*/
int simp3atoken(SBYTE *s, int mode)
{
int error = 0, n, numexp = 0, denom, base, numprot, i;
SBYTE *t, c;
LONG num;
WORD *prot;
if ( mode == RHSIDE ) {
prot = AC.ProtoType;
numprot = prot[1] - SUBEXPSIZE;
prot += SUBEXPSIZE;
}
else { prot = 0; numprot = 0; }
while ( *s != TENDOFIT ) {
denom = 1;
if ( *s == TDIVIDE ) { denom = -1; s++; }
c = *s;
switch(c) {
case TSYMBOL:
case TNUMBER:
case TNUMBER1:
s++; while ( *s >= 0 ) s++; /* skip the object */
if ( *s == TWILDCARD ) s++; /* and the possible wildcard */
dosymbol:
if ( *s != TPOWER ) continue; /* No power -> done */
s++; /* Skip the power */
if ( *s == TMINUS ) s++; /* negative: no difference here */
if ( *s == TNUMBER || *s == TNUMBER1 ) {
base = *s == TNUMBER ? 100: 128; /* NUMBER = base 100 */
s++; /* Now we compose the power */
num = *s++; /* If the number is way too large */
while ( *s >= 0 ) { /* it may look like not too big */
if ( num > MAXPOWER ) break; /* Hence... */
num = base*num + *s++;
}
while ( *s >= 0 ) s++; /* Finish the number if needed */
if ( *s == TPOWER ) goto doublepower;
if ( num <= MAXPOWER ) continue; /* Simple case */
}
else if ( *s == TSYMBOL && c != TNUMBER && c != TNUMBER1 ) {
s++; n = 0; while ( *s >= 0 ) { n = 128*n + *s++; }
if ( *s == TWILDCARD ) { s++;
if ( *s == TPOWER ) goto doublepower;
continue; }
/*
Now we have to test whether n happens to be a wildcard
*/
if ( mode == RHSIDE ) {
n += 2*MAXPOWER;
for ( i = 0; i < numprot; i += 4 ) {
if ( prot[i+2] == n && prot[i] == SYMTOSYM ) break;
}
if ( i < numprot ) break;
}
if ( *s == TPOWER ) goto doublepower;
}
numexp++;
break;
case TINDEX:
s++; while ( *s >= 0 ) s++;
if ( *s == TWILDCARD ) s++;
doindex:
if ( denom < 0 || *s == TPOWER ) {
MesPrint("&Index to a power or in denominator is illegal");
error = 1;
}
break;
case TVECTOR:
s++; while ( *s >= 0 ) s++;
if ( *s == TWILDCARD ) s++;
dovector:
if ( *s == TFUNOPEN ) {
s++; n = 1;
for(;;) {
if ( *s == TFUNOPEN ) {
n++;
MesPrint("&Illegal vector index");
error = 1;
}
else if ( *s == TFUNCLOSE ) {
n--;
if ( n <= 0 ) break;
}
s++;
}
s++;
}
else if ( *s == TDOT ) goto dodot;
if ( denom < 0 || *s == TPOWER || *s == TPOWER1 ) numexp++;
break;
case TFUNCTION:
s++; while ( *s >= 0 ) s++;
if ( *s == TWILDCARD ) s++;
dofunction:
t = s;
if ( *t == TFUNOPEN ) {
t++; n = 1;
for(;;) {
if ( *t == TFUNOPEN ) n++;
else if ( *t == TFUNCLOSE ) { if ( --n <= 0 ) break; }
t++;
}
t++; s++;
}
if ( denom < 0 || *t == TPOWER || *t == TPOWER1 ) numexp++;
break;
#ifdef WITHFLOAT
case TFLOAT:
s++; while ( *s >= 0 ) s++;
if ( denom < 0 || *s == TPOWER || *s == TPOWER1 ) numexp++;
break;
#endif
case TEXPRESSION:
s++; while ( *s >= 0 ) s++;
t = s;
if ( *t == TFUNOPEN ) {
t++; n = 1;
for(;;) {
if ( *t == TFUNOPEN ) n++;
else if ( *t == TFUNCLOSE ) { if ( --n <= 0 ) break; }
t++;
}
t++;
}
if ( *t == LBRACE ) {
t++; n = 1;
for(;;) {
if ( *t == LBRACE ) n++;
else if ( *t == RBRACE ) { if ( --n <= 0 ) break; }
t++;
}
t++;
}
if ( denom < 0 || ( ( *t == TPOWER || *t == TPOWER1 )
&& t[1] == TMINUS ) ) numexp++;
break;
case TDOLLAR:
s++; while ( *s >= 0 ) s++;
if ( denom < 0 || ( ( *s == TPOWER || *s == TPOWER1 )
&& s[1] == TMINUS ) ) numexp++;
break;
case LPARENTHESIS:
s++; n = 1; t = s;
for(;;) {
if ( *t == LPARENTHESIS ) n++;
else if ( *t == RPARENTHESIS ) { if ( --n <= 0 ) break; }
t++;
}
t++;
if ( denom > 0 && ( *t == TPOWER || *t == TPOWER1 ) ) {
if ( ( t[1] == TNUMBER || t[1] == TNUMBER1 ) && t[2] >= 0
&& t[3] < 0 ) break;
numexp++;
}
else if ( denom < 0 && ( *t == TPOWER || *t == TPOWER1 ) ) {
if ( t[1] == TMINUS && ( t[2] == TNUMBER
|| t[2] == TNUMBER1 ) && t[3] >= 0
&& t[4] < 0 ) break;
numexp++;
}
else if ( denom < 0 || ( ( *t == TPOWER || *t == TPOWER1 )
&& ( t[1] == TMINUS || t[1] == LPARENTHESIS ) ) ) numexp++;
break;
case TSET:
s++; n = *s++; while ( *s >= 0 ) { n = 128*n + *s++; }
n = Sets[n].type;
switch ( n ) {
case CSYMBOL: goto dosymbol;
case CINDEX: goto doindex;
case CVECTOR: goto dovector;
case CFUNCTION: goto dofunction;
case CNUMBER: goto dosymbol;
case CMODEL:
if ( denom < 0 || *s == TPOWER ) {
MesPrint("&A model to a power or in denominator is illegal");
error = 1;
}
break;
case ANYTYPE:
if ( denom < 0 || *s == TPOWER ) {
MesPrint("&A set without type to a power or in denominator is illegal");
error = 1;
}
break;
default: error = 1; break;
}
break;
case TDOT:
dodot: s++;
if ( *s == TVECTOR ) { s++; while ( *s >= 0 ) s++; }
else if ( *s == TSET ) {
s++; n = *s++; while ( *s >= 0 ) { n = 128*n + *s++; }
if ( Sets[n].type != CVECTOR ) {
MesPrint("&Set in dotproduct is not a set of vectors");
error = 1;
}
if ( *s == LBRACE ) {
s++; n = 1;
for(;;) {
if ( *s == LBRACE ) n++;
else if ( *s == RBRACE ) { if ( --n <= 0 ) break; }
s++;
}
s++;
}
else {
MesPrint("&Set without argument in dotproduct");
error = 1;
}
}
else if ( *s == TSETNUM ) {
s++; n = *s++; while ( *s >= 0 ) { n = 128*n + *s++; }
if ( *s != TVECTOR ) goto nodot;
s++; n = *s++; while ( *s >= 0 ) { n = 128*n + *s++; }
if ( Sets[n].type != CVECTOR ) {
MesPrint("&Set in dotproduct is not a set of vectors");
error = 1;
}
}
else {
nodot: MesPrint("&Illegal second element in dotproduct");
error = 1;
s++; while ( *s >= 0 ) s++;
}
goto dosymbol;
default:
s++; while ( *s >= 0 ) s++;
break;
}
}
if ( error ) return(-1);
return(numexp);
doublepower:
MesPrint("&Dubious notation with object^power1^power2");
return(-1);
}
/*
#] simp3atoken :
#[ simp3btoken :
*/
int simp3btoken(SBYTE *s, int mode)
{
int error = 0, i, numprot, n, denom, base, inset = 0, dotp, sube = 0;
SBYTE *t, c, *fill, *ff, *ss;
LONG num;
WORD *prot;
// Work in a temporary buffer, to avoid clashes between input and output
SBYTE *tmptokens = (SBYTE*)Malloc1((AC.toptokens-AC.tokens)*sizeof(SBYTE), "simp3btoken scratch");
SBYTE* tmptoptokens = tmptokens + (AC.toptokens-AC.tokens);
fill = tmptokens;
if ( mode == RHSIDE ) {
prot = AC.ProtoType;
numprot = prot[1] - SUBEXPSIZE;
prot += SUBEXPSIZE;
}
else { prot = 0; numprot = 0; }
while ( *s == TEMPTY ) s++;
while ( *s != TENDOFIT ) {
// If we are near the end of the output buffer, we'd better reallocate
// both tmptokens and AC.tokens (which must fit the final result).
// Experimentally, fill can move by at most 5 per loop iteration, but
// this buffer of 20 of might not be enough for all cases!
while ( tmptoptokens - fill < 20 ) {
LONG tmppos;
tmppos = s - AC.tokens;
DoubleBuffer((void**)&(AC.tokens), (void**)&(AC.toptokens), sizeof(SBYTE), "simp3btoken double");
s = AC.tokens + tmppos;
tmppos = fill - tmptokens;
DoubleBuffer((void**)&tmptokens, (void**)&tmptoptokens, sizeof(SBYTE), "simp3btoken scratch double");
fill = tmptokens + tmppos;
}
if ( *s == TEMPTY ) { s++; continue; }
denom = 1;
if ( *s == TDIVIDE ) { denom = -1; *fill++ = *s++; }
ff = fill; ss = s; c = *s;
if ( c == TSETNUM ) {
*fill++ = *s++; while ( *s >= 0 ) *fill++ = *s++;
c = *s;
}
dotp = 0;
switch(c) {
case TSYMBOL:
case TNUMBER:
case TNUMBER1:
*fill++ = *s++;
while ( *s >= 0 ) *fill++ = *s++;
if ( *s == TWILDCARD ) *fill++ = *s++;
dosymbol:
t = s;
if ( *s != TPOWER ) continue;
*fill++ = *s++;
if ( *s == TMINUS ) *fill++ = *s++;
if ( *s == TPLUS ) s++;
if ( *s == TSETNUM ) {
*fill++ = *s++; while ( *s >= 0 ) *fill++ = *s++;
inset = 1;
}
else inset = 0;
if ( *s == TNUMBER || *s == TNUMBER1 ) {
base = *s == TNUMBER ? 100: 128;
*fill++ = *s++;
num = *s++; *fill++ = num;
while ( *s >= 0 ) {
if ( num > MAXPOWER ) break;
*fill++ = *s;
num = base*num + *s++;
}
while ( *s >= 0 ) *fill++ = *s++;
if ( num <= MAXPOWER ) continue;
goto putexp1;
}
else if ( *s == TSYMBOL && c != TNUMBER && c != TNUMBER1 ) {
*fill++ = *s++;
n = 0; while ( *s >= 0 ) { n = 128*n + *s; *fill++ = *s++; }
if ( *s == TWILDCARD ) { *fill++ = *s++;
if ( *s == TPOWER ) goto doublepower;
break; }
/*
Now we have to test whether n happens to be a wildcard
*/
if ( mode == RHSIDE && inset == 0 ) {
/* n += WILDOFFSET;*/
for ( i = 0; i < numprot; i += 4 ) {
if ( prot[i+2] == n && prot[i] == SYMTOSYM ) break;
}
if ( i < numprot ) break;
}
putexp1: fill = ff;
if ( denom < 0 ) fill[-1] = TMULTIPLY;
*fill++ = TFUNCTION; *fill++ = (SBYTE)(AM.expnum); *fill++ = TFUNOPEN;
if ( dotp ) *fill++ = LPARENTHESIS;
while ( ss < t ) *fill++ = *ss++;
if ( dotp ) *fill++ = RPARENTHESIS;
*fill++ = TCOMMA;
ss++; /* Skip TPOWER */
if ( *ss == TMINUS ) { denom = -denom; ss++; }
if ( denom < 0 ) {
*fill++ = LPARENTHESIS;
*fill++ = TMINUS;
while ( ss < s ) *fill++ = *ss++;
*fill++ = RPARENTHESIS;
}
else {
while ( ss < s ) *fill++ = *ss++;
}
*fill++ = TFUNCLOSE;
if ( *ss == TPOWER ) goto doublepower;
}
else { /* other objects can be composite */
goto dofunpower;
}
break;
case TINDEX:
*fill++ = *s++; while ( *s >= 0 ) *fill++ = *s++;
if ( *s == TWILDCARD ) *fill++ = *s++;
break;
case TVECTOR:
*fill++ = *s++; while ( *s >= 0 ) *fill++ = *s++;
if ( *s == TWILDCARD ) *fill++ = *s++;
dovector:
if ( *s == TFUNOPEN ) {
while ( *s != TFUNCLOSE ) *fill++ = *s++;
*fill++ = *s++;
}
else if ( *s == TDOT ) goto dodot;
t = s;
goto dofunpower;
#ifdef WITHFLOAT
case TFLOAT:
*fill++ = *s++; while ( *s >= 0 ) *fill++ = *s++;
t = s;
sube = 0;
goto dofunpower;
#endif
case TFUNCTION:
*fill++ = *s++; while ( *s >= 0 ) *fill++ = *s++;
if ( *s == TWILDCARD ) *fill++ = *s++;
dofunction:
t = s;
if ( *t == TFUNOPEN ) {
t++; n = 1;
for(;;) {
if ( *t == TFUNOPEN ) n++;
else if ( *t == TFUNCLOSE ) { if ( --n <= 0 ) break; }
t++;
}
t++; *fill++ = *s++;
}
sube = 0;
dofunpower:
if ( *t == TPOWER || *t == TPOWER1 ) {
if ( sube ) {
if ( ( t[1] == TNUMBER || t[1] == TNUMBER1 )
&& denom > 0 ) {
if ( t[2] >= 0 && t[3] < 0 ) { sube = 0; break; }
}
else if ( t[1] == TMINUS && denom < 0 &&
( t[2] == TNUMBER || t[2] == TNUMBER1 ) ) {
if ( t[2] >= 0 && t[3] < 0 ) { sube = 0; break; }
}
sube = 0;
}
fill = ff;
*fill++ = TFUNCTION; *fill++ = (SBYTE)(AM.expnum); *fill++ = TFUNOPEN;
*fill++ = LPARENTHESIS;
while ( ss < t ) *fill++ = *ss++;
t++;
*fill++ = RPARENTHESIS; *fill++ = TCOMMA;
if ( *t == TMINUS ) { t++; denom = -denom; }
*fill++ = LPARENTHESIS;
if ( denom < 0 ) *fill++ = TMINUS;
if ( *t == LPARENTHESIS ) {
*fill++ = *t++; n = 0;
while ( n >= 0 ) {
if ( *t == LPARENTHESIS ) n++;
else if ( *t == RPARENTHESIS ) n--;
*fill++ = *t++;
}
}
else if ( *t == TFUNCTION || *t == TDUBIOUS ) {
*fill++ = *t++; while ( *t >= 0 ) *fill++ = *t++;
if ( *t == TWILDCARD ) *fill++ = *t++;
if ( *t == TFUNOPEN ) {
*fill++ = *t++; n = 0;
while ( n >= 0 ) {
if ( *t == TFUNOPEN ) n++;
else if ( *t == TFUNCLOSE ) n--;
*fill++ = *t++;
}
}
}
else if ( *t == TSET ) {
*fill++ = *t++; n = 0;
while ( *t >= 0 ) { n = 128*n + *t; *fill++ = *t++; }
if ( *t == LBRACE ) {
if ( n < AM.NumFixedSets || Sets[n].type == CRANGE ) {
MesPrint("&This type of usage of sets is not allowed");
error = 1;
}
*fill++ = *t++; n = 0;
while ( n >= 0 ) {
if ( *t == LBRACE ) n++;
else if ( *t == RBRACE ) n--;
*fill++ = *t++;
}
}
}
else if ( *t == TEXPRESSION ) {
*fill++ = *t++; while ( *t >= 0 ) *fill++ = *t++;
if ( *t == TFUNOPEN ) {
*fill++ = *t++; n = 0;
while ( n >= 0 ) {
if ( *t == TFUNOPEN ) n++;
else if ( *t == TFUNCLOSE ) n--;
*fill++ = *t++;
}
}
if ( *t == LBRACE ) {
*fill++ = *t++; n = 0;
while ( n >= 0 ) {
if ( *t == LBRACE ) n++;
else if ( *t == RBRACE ) n--;
*fill++ = *t++;
}
}
}
else if ( *t == TVECTOR ) {
*fill++ = *t++; while ( *t >= 0 ) *fill++ = *t++;
if ( *t == TFUNOPEN ) {
*fill++ = *t++; n = 0;
while ( n >= 0 ) {
if ( *t == TFUNOPEN ) n++;
else if ( *t == TFUNCLOSE ) n--;
*fill++ = *t++;
}
}
else if ( *t == TDOT ) {
*fill++ = *t++;
if ( *t == TVECTOR || *t == TDUBIOUS ) {
*fill++ = *t++; while ( *t >= 0 ) *fill++ = *t++;
}
else if ( *t == TSET ) {
*fill++ = *t++; num = 0;
while ( *t >= 0 ) { num = 128*num + *t; *fill++ = *t++; }
if ( Sets[num].type != CVECTOR ) {
MesPrint("&Illegal set type in dotproduct");
error = 1;
}
if ( *t == LBRACE ) {
*fill++ = *t++; n = 0;
while ( n >= 0 ) {
if ( *t == LBRACE ) n++;
else if ( *t == RBRACE ) n--;
*fill++ = *t++;
}
}
}
else if ( *t == TSETNUM ) {
*fill++ = *t++;
while ( *t >= 0 ) { *fill++ = *t++; }
*fill++ = *t++;
while ( *t >= 0 ) { *fill++ = *t++; }
}
}
else {
MesPrint("&Illegal second element in dotproduct");
error = 1;
}
}
else {
*fill++ = *t++; while ( *t >= 0 ) *fill++ = *t++;
if ( *t == TWILDCARD ) *fill++ = *t++;
}
*fill++ = RPARENTHESIS; *fill++ = TFUNCLOSE;
if ( *t == TPOWER ) goto doublepower;
while ( fill > ff ) *--t = *--fill;
s = t;
}
else if ( denom < 0 ) {
fill = ff; ff[-1] = TMULTIPLY;
*fill++ = TFUNCTION; *fill++ = (SBYTE)(AM.denomnum);
*fill++ = TFUNOPEN; *fill++ = LPARENTHESIS;
while ( ss < t ) *fill++ = *ss++;
*fill++ = RPARENTHESIS; *fill++ = TFUNCLOSE;
while ( fill > ff ) *--t = *--fill;
s = t; denom = 1; sube = 0;
break;
}
sube = 0;
break;
case TEXPRESSION:
*fill++ = *s++; while ( *s >= 0 ) *fill++ = *s++;
t = s;
if ( *t == TFUNOPEN ) {
t++; n = 1;
for(;;) {
if ( *t == TFUNOPEN ) n++;
else if ( *t == TFUNCLOSE ) { if ( --n <= 0 ) break; }
t++;
}
t++;
}
if ( *t == LBRACE ) {
t++; n = 1;
for(;;) {
if ( *t == LBRACE ) n++;
else if ( *t == RBRACE ) { if ( --n <= 0 ) break; }
t++;
}
t++;
}
if ( t > s || denom < 0 || ( ( *t == TPOWER || *t == TPOWER1 )
&& t[1] == TMINUS ) ) goto dofunpower;
else goto dosymbol;
case TDOLLAR:
*fill++ = *s++; while ( *s >= 0 ) *fill++ = *s++;
goto dosymbol;
case LPARENTHESIS:
*fill++ = *s++; n = 1; t = s;
for(;;) {
if ( *t == LPARENTHESIS ) n++;
else if ( *t == RPARENTHESIS ) { if ( --n <= 0 ) break; }
t++;
}
t++; sube = 1;
goto dofunpower;
case TSET:
*fill++ = *s++; n = *s++; *fill++ = (SBYTE)n;
while ( *s >= 0 ) { *fill++ = *s; n = 128*n + *s++; }
n = Sets[n].type;
switch ( n ) {
case CSYMBOL: goto dosymbol;
case CINDEX: break;
case CVECTOR: goto dovector;
case CFUNCTION: goto dofunction;
case CNUMBER: goto dosymbol;
case CMODEL: break;
case ANYTYPE: break;
default: error = 1; break;
}
break;
case TDOT:
dodot: *fill++ = *s++;
if ( *s == TVECTOR ) {
*fill++ = *s++; while ( *s >= 0 ) *fill++ = *s++;
}
else if ( *s == TSET ) {
*fill++ = *s++; n = *s++; *fill++ = (SBYTE)n;
while ( *s >= 0 ) { *fill++ = *s; n = 128*n + *s++; }
if ( *s == LBRACE ) {
if ( n < AM.NumFixedSets || Sets[n].type == CRANGE ) {
MesPrint("&This type of usage of sets is not allowed");
error = 1;
}
*fill++ = *s++; n = 1;
for(;;) {
if ( *s == LBRACE ) n++;
else if ( *s == RBRACE ) { if ( --n <= 0 ) break; }
*fill++ = *s++;
}
*fill++ = *s++;
}
else {
MesPrint("&Set without argument in dotproduct");
error = 1;
}
}
else if ( *s == TSETNUM ) {
*fill++ = *s++; while ( *s >= 0 ) *fill++ = *s++;
if ( *s != TVECTOR ) goto nodot;
*fill++ = *s++; while ( *s >= 0 ) *fill++ = *s++;
}
else {
nodot: MesPrint("&Illegal second element in dotproduct");
error = 1;
*fill++ = *s++;
while ( *s >= 0 ) *fill++ = *s++;
}
dotp = 1;
goto dosymbol;
default:
*fill++ = *s++;
while ( *s >= 0 ) *fill++ = *s++;
break;
}
}
*fill = TENDOFIT;
// Now copy the modified tokens back to the original buffer
fill = tmptokens;
s = AC.tokens;
do {
*s++ = *fill;
} while ( *fill++ != TENDOFIT );
M_free(tmptokens, "simp3btoken scratch");
return(error);
doublepower:;
MesPrint("&Dubious notation with power of power");
M_free(tmptokens, "simp3btoken scratch");
return(-1);
}
/*
#] simp3btoken :
#[ simp4token :
Deal with the set[n] objects in the RHS.
*/
int simp4token(SBYTE *s)
{
int error = 0, n, nsym, settype;
WORD i, *w, *wstop, level;
SBYTE *const s0 = s;
SBYTE *fill = s, *s1, *s2, *s3, type, s1buf[10];
SBYTE *tbuf = s, *t, *t1;
while ( *s != TENDOFIT ) {
if ( *s != TSET ) {
if ( *s == TEMPTY ) s++;
else *fill++ = *s++;
continue;
}
if ( fill >= (s0+1) && fill[-1] == TWILDCARD ) { *fill++ = *s++; continue; }
if ( fill >= (s0+2) && fill[-1] == TNOT && fill[-2] == TWILDCARD ) { *fill++ = *s++; continue; }
s1 = s++; n = 0; while ( *s >= 0 ) { n = 128*n + *s++; }
i = Sets[n].type;
if ( *s != LBRACE ) { while ( s1 < s ) *fill++ = *s1++; continue; }
if ( n < AM.NumFixedSets || i == CRANGE ) {
MesPrint("&It is not allowed to refer to individual elements of built in or ranged sets");
error = 1;
}
s++;
if ( *s != TSYMBOL && *s != TDOLLAR ) {
MesPrint("&Set index in RHS is not a wildcard symbol or $-variable");
error = 1;
while ( s1 < s ) *fill++ = *s1++;
continue;
}
settype = ( *s == TDOLLAR );
s++; nsym = 0; s2 = s;
while ( *s >= 0 ) nsym = 128*nsym + *s++;
if ( *s != RBRACE ) {
MesPrint("&Improper set argument in RHS");
error = 1;
while ( s1 < s ) *fill++ = *s1++;
continue;
}
s++;
/*
Verify that nsym is a wildcard
*/
if ( !settype ) {
w = AC.ProtoType; wstop = w + w[1]; w += SUBEXPSIZE;
while ( w < wstop ) {
if ( *w == SYMTOSYM && w[2] == nsym ) break;
w += w[1];
}
if ( w >= wstop ) {
/*
It could still be a summation parameter!
*/
t = fill - 1;
while ( t >= tbuf ) {
if ( *t == TFUNCLOSE ) {
level = 1; t--;
while ( t >= tbuf ) {
if ( *t == TFUNCLOSE ) level++;
else if ( *t == TFUNOPEN ) {
level--;
if ( level == 0 ) break;
}
t--;
}
}
else if ( *t == RBRACE ) {
level = 1; t--;
while ( t >= tbuf ) {
if ( *t == RBRACE ) level++;
else if ( *t == LBRACE ) {
level--;
if ( level == 0 ) break;
}
t--;
}
}
else if ( *t == RPARENTHESIS ) {
level = 1; t--;
while ( t >= tbuf ) {
if ( *t == RPARENTHESIS ) level++;
else if ( *t == LPARENTHESIS ) {
level--;
if ( level == 0 ) break;
}
t--;
}
}
else if ( *t == TFUNOPEN ) {
t1 = t-1;
while ( *t1 > 0 && t1 > tbuf ) t1--;
if ( *t1 == TFUNCTION ) {
t1++; level = 0;
while ( *t1 > 0 ) level = level*128+*t1++;
if ( level == (SUMF1-FUNCTION)
|| level == (SUMF2-FUNCTION) ) {
t1 = t + 1;
if ( *t1 == LPARENTHESIS ) t1++;
if ( *t1 == TSYMBOL ) {
if ( ( t1[1] == COEFFSYMBOL
|| t1[1] == NUMERATORSYMBOL
|| t1[1] == DENOMINATORSYMBOL )
&& t1[2] < 0 ) {}
else {
t1++; level = 0;
while ( *t1 >= 0 && t1 < fill ) level = 128*level + *t1++;
if ( level == nsym && t1 < fill ) {
if ( t[1] == LPARENTHESIS
&& *t1 == RPARENTHESIS && t1[1] == TCOMMA ) break;
if ( t[1] != LPARENTHESIS && *t1 == TCOMMA ) break;
}
}
}
}
}
}
t--;
}
if ( t < tbuf ) {
fill--;
MesPrint("&Set index in RHS is not a wildcard symbol");
error = 1;
while ( s1 < s ) *fill++ = *s1++;
continue;
}
}
}
/*
Now replace by a set marker: TSETNUM,nsym,TYPE,setnumber
*/
switch ( i ) {
case CSYMBOL: type = TSYMBOL; break;
case CINDEX: type = TINDEX; break;
case CVECTOR: type = TVECTOR; break;
case CFUNCTION: type = TFUNCTION; break;
case CNUMBER: type = TNUMBER1; break;
case CDUBIOUS: type = TDUBIOUS; break;
default:
MesPrint("&Unknown set type in simp4token");
error = 1; type = CDUBIOUS; break;
}
s3 = s1buf; s1++;
while ( *s1 >= 0 ) *s3++ = *s1++;
*s3 = -1; s1 = s1buf;
if ( settype ) *fill++ = TSETDOL;
else *fill++ = TSETNUM;
while ( *s2 >= 0 ) *fill++ = *s2++;
*fill++ = type; while ( *s1 >= 0 ) *fill++ = *s1++;
}
*fill++ = TENDOFIT;
return(error);
}
/*
#] simp4token :
#[ simp5token :
Making sure that first argument of sumfunction is not a wildcard already
*/
int simp5token(SBYTE *s, int mode)
{
int error = 0, n, type;
WORD *w, *wstop;
if ( mode == RHSIDE ) {
while ( *s != TENDOFIT ) {
if ( *s == TFUNCTION ) {
s++; n = 0; while ( *s >= 0 ) n = 128*n + *s++;
if ( n == AM.sumnum || n == AM.sumpnum ) {
if ( *s != TFUNOPEN ) continue;
s++;
if ( *s != TSYMBOL && *s != TINDEX ) continue;
type = *s++;
n = 0; while ( *s >= 0 ) n = 128*n + *s++;
if ( type == TINDEX ) n += AM.OffsetIndex;
if ( *s != TCOMMA ) continue;
w = AC.ProtoType;
wstop = w + w[1];
w += SUBEXPSIZE;
while ( w < wstop ) {
if ( w[2] == n ) {
if ( ( type == TSYMBOL && ( w[0] == SYMTOSYM
|| w[0] == SYMTONUM || w[0] == SYMTOSUB ) ) || (
type == TINDEX && ( w[0] == INDTOIND
|| w[0] == INDTOSUB ) ) ) {
error = 1;
MesPrint("&Parameter of sum function is already a wildcard");
}
}
w += w[1];
}
}
}
else s++;
}
}
return(error);
}
/*
#] simp5token :
#[ simp6token :
Making sure that factorized expressions are used properly
*/
int simp6token(SBYTE *tokens, int mode)
{
/* EXPRESSIONS e = Expressions; */
int error = 0, n;
int level = 0, haveone = 0;
SBYTE *s = tokens, *ss;
LONG numterms;
WORD funnum = 0;
GETIDENTITY
if ( mode == RHSIDE ) {
while ( *s == TPLUS || *s == TMINUS ) s++;
numterms = 1;
while ( *s != TENDOFIT ) {
if ( *s == LPARENTHESIS ) level++;
else if ( *s == RPARENTHESIS ) level--;
else if ( *s == TFUNOPEN ) level++;
else if ( *s == TFUNCLOSE ) level--;
else if ( ( *s == TPLUS || *s == TMINUS ) && level == 0 ) {
/*
Special exception: x^-1 etc.
*/
if ( s[-1] != TPOWER && s[-1] != TPLUS && s[-1] != TMINUS ) {
numterms++;
}
}
else if ( *s == TEXPRESSION ) {
ss = s;
s++; n = 0; while ( *s >= 0 ) n = 128*n + *s++;
if ( Expressions[n].status == STOREDEXPRESSION ) {
POSITION position;
/*
#ifdef WITHPTHREADS
RENUMBER renumber;
#endif
*/
RENUMBER renumber;
WORD TMproto[SUBEXPSIZE];
TMproto[0] = EXPRESSION;
TMproto[1] = SUBEXPSIZE;
TMproto[2] = n;
TMproto[3] = 1;
{ int ie; for ( ie = 4; ie < SUBEXPSIZE; ie++ ) TMproto[ie] = 0; }
AT.TMaddr = TMproto;
PUTZERO(position);
/*
if ( (
#ifdef WITHPTHREADS
renumber =
#endif
GetTable(n,&position,0) ) == 0 )
*/
if ( ( renumber = GetTable(n,&position,0) ) == 0 )
{
error = 1;
MesPrint("&Problems getting information about stored expression %s(4)"
,EXPRNAME(n));
}
/*
#ifdef WITHPTHREADS
*/
if ( renumber->symb.lo != AN.dummyrenumlist )
M_free(renumber->symb.lo,"VarSpace");
M_free(renumber,"Renumber");
/*
#endif
*/
}
if ( ( ( AS.Oldvflags[n] & ISFACTORIZED ) != 0 ) && *s != LBRACE ) {
if ( level == 0 ) {
haveone = 1;
}
else if ( error == 0 ) {
if ( ss[-1] != TFUNOPEN || funnum != NUMFACTORS-FUNCTION ) {
MesPrint("&Illegal use of factorized expression(s) in RHS");
error = 1;
}
}
}
continue;
}
else if ( *s == TFUNCTION ) {
s++; funnum = 0; while ( *s >= 0 ) funnum = 128*funnum + *s++;
continue;
}
s++;
}
if ( haveone ) {
if ( numterms > 1 ) {
MesPrint("&Factorized expression in RHS in an expression of more than one term.");
error = 1;
}
else if ( AC.ToBeInFactors == 0 ) {
MesPrint("&Attempt to put a factorized expression inside an unfactorized expression.");
error = 1;
}
}
}
return(error);
}
/*
#] simp6token :
#] Compiler :
*/
|