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
|
/*
Copyright 2011-2014 by
Laboratoire d'Informatique de Paris 6, equipe PEQUAN,
UPMC Universite Paris 06 - CNRS - UMR 7606 - LIP6, Paris, France
and by
Centre de recherche INRIA Sophia Antipolis Mediterranee, equipe APICS,
Sophia Antipolis, France.
Contributors Ch. Lauter, S. Chevillard
christoph.lauter@ens-lyon.org
sylvain.chevillard@ens-lyon.org
This software is a computer program whose purpose is to provide an
environment for safe floating-point code development. It is
particularly targeted to the automated implementation of
mathematical floating-point libraries (libm). Amongst other features,
it offers a certified infinity norm, an automatic polynomial
implementer and a fast Remez algorithm.
This software is governed by the CeCILL-C license under French law and
abiding by the rules of distribution of free software. You can use,
modify and/ or redistribute the software under the terms of the CeCILL-C
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty and the software's author, the holder of the
economic rights, and the successive licensors have only limited
liability.
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
herefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL-C license and that you accept its terms.
This program is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "expression.h"
#include "execute.h"
#include "chain.h"
#include "assignment.h"
#include <string.h>
#include <stdlib.h>
/* A small wrapper around evaluateThing to be sure that the returned
object has no memory references
*/
node *evaluateThingDeepCopy(node *thing) {
node *tempNode, *res;
tempNode = evaluateThing(thing);
res = deepCopyThing(tempNode);
freeThing(tempNode);
return res;
}
/* Declare tryMatch immediately as it is recursively used almost
everywhere */
int tryMatch(chain **, node *, node *);
/* Add association identifier -> thing to list
If identifier exists already:
- And is associated to a thing that is equal to thing, return success (1)
- And is associated to a different thing, return failure (0)
Otherwise: create the association identifier -> thing and add it to the
list, return success (1)
*/
int associateThing(chain **associations, char *identifier, node *thing) {
chain *curr;
entry *newEntry;
for (curr = *associations; curr != NULL; curr = curr->next) {
if (!strcmp(identifier, ((char *) (((entry *) (curr->value))->name)))) {
/* Identifier exists already in list */
if (isEqualThing(thing, ((node *) (((entry *) (curr->value))->value)))) {
/* But it is associated to the same thing */
return 1;
} else {
/* But it is associated to another thing */
return 0;
}
}
}
newEntry = (entry *) safeMalloc(sizeof(entry));
newEntry->name = (char *) safeCalloc(strlen(identifier) + 1,sizeof(char));
strcpy(newEntry->name, identifier);
newEntry->value = copyThing(thing);
*associations = addElement(*associations, newEntry);
return 1;
}
int tryCombineAssociations(chain **associations, chain *assoc1, chain *assoc2) {
int okay;
chain *myAssociations = NULL;
chain *curr;
if ((assoc1 == NULL) && (assoc2 == NULL)) {
*associations = NULL;
return 1;
}
if (assoc1 == NULL) {
*associations = copyChain(assoc2,copyEntryOnVoid);
return 1;
}
if (assoc2 == NULL) {
*associations = copyChain(assoc1,copyEntryOnVoid);
return 1;
}
okay = 1;
myAssociations = copyChain(assoc1,copyEntryOnVoid);
for (curr=assoc2; curr != NULL; curr = curr->next) {
if (!associateThing(&myAssociations,
((char *) ((entry *) (curr->value))->name),
((node *) ((entry *) (curr->value))->value))) {
okay = 0;
break;
}
}
if (okay) {
*associations = myAssociations;
} else {
if (myAssociations != NULL) {
freeChain(myAssociations, freeEntryOnVoid);
}
}
return okay;
}
node *headFunction(node *tree) {
node *copy;
switch (tree->nodeType) {
case MEMREF:
return headFunction(getMemRefChild(tree));
break;
case VARIABLE:
case CONSTANT:
case ADD:
case SUB:
case MUL:
case DIV:
case POW:
case PI_CONST:
case LIBRARYCONSTANT:
copy = NULL;
break;
case NEG:
copy = allocateNode();
copy->nodeType = tree->nodeType;
copy->child1 = makeVariable();
break;
case UNARY_BASE_FUNC:
copy = allocateNode();
copy->nodeType = UNARY_BASE_FUNC;
copy->baseFun = tree->baseFun;
copy->child1 = makeVariable();
break;
case LIBRARYFUNCTION:
copy = allocateNode();
copy->nodeType = LIBRARYFUNCTION;
copy->libFun = tree->libFun;
copy->libFunDeriv = tree->libFunDeriv;
copy->child1 = makeVariable();
break;
case PROCEDUREFUNCTION:
copy = allocateNode();
copy->nodeType = PROCEDUREFUNCTION;
copy->libFunDeriv = tree->libFunDeriv;
copy->child1 = makeVariable();
copy->child2 = copyThing(tree->child2);
break;
default:
sollyaFprintf(stderr,"Error: headFunction: unknown identifier in the tree\n");
exit(1);
}
return copy;
}
int tryMatchExtendedPureTree(chain **associations, node *thingToMatch, node *possibleMatcher) {
chain *leftAssoc, *rightAssoc, *recursionAssoc;
int okay, recursion, matchRecursion;
node *currentThingToMatch, *currentPossibleMatcher, *headSymbol, *newHeadSymbol, *tempNode;
char *currentIdentifier;
/* Special case: possibleMatcher is default.
Everthing that is correctly typed (and we know it is) matches
without creating any binding.
*/
if (accessThruMemRef(possibleMatcher)->nodeType == DEFAULT) {
return 1;
}
/* The free variable matches the free variable */
if ((accessThruMemRef(possibleMatcher)->nodeType == VARIABLE) &&
(accessThruMemRef(thingToMatch)->nodeType == VARIABLE)) {
return 1;
}
/* Special case: possibleMatcher is an apply
of the free variable on a function
*/
if (accessThruMemRef(possibleMatcher)->nodeType == APPLY) {
/* Check if the child of the apply is the free variable */
if (accessThruMemRef(accessThruMemRef(possibleMatcher)->child1)->nodeType != VARIABLE) {
if (variablename == NULL) return 0;
if (accessThruMemRef(accessThruMemRef(possibleMatcher)->child1)->nodeType != TABLEACCESS) return 0;
if (strcmp(variablename,accessThruMemRef(accessThruMemRef(possibleMatcher)->child1)->string)) return 0;
}
/* Check if recursion is possible on the thing to match */
switch (accessThruMemRef(thingToMatch)->nodeType) {
case VARIABLE:
case CONSTANT:
case PI_CONST:
case LIBRARYCONSTANT:
case ADD:
case SUB:
case MUL:
case DIV:
case POW:
return 0;
break;
default:
break;
}
/* If we are here, we are sure that recursion is possible
on the one, first and only child of the thing to match.
*/
okay = tryMatchExtendedPureTree(associations, thingToMatch, ((node *) (accessThruMemRef(possibleMatcher)->arguments->value)));
if (okay) {
if (variablename != NULL) {
printMessage(1,SOLLYA_MSG_FREE_VAR_INTERPRETED_AS_IDENTITY_FUNCTION,"Warning: the identifier \"%s\" is bound to the current free variable. In a functional context it will be considered as the identity function.\n",
variablename);
} else {
printMessage(1,SOLLYA_MSG_FREE_VAR_INTERPRETED_AS_IDENTITY_FUNCTION,"Warning: \"_x_\" is the free variable. In a functional context it will be considered as the identity function.\n");
}
}
return okay;
}
/* Special case: possibleMatcher is a free variable to bind
Check if it is possible equal to the mathematical free
variable. If not, create an association
*/
if (accessThruMemRef(possibleMatcher)->nodeType == TABLEACCESS) {
if ((variablename != NULL) &&
(!strcmp(variablename, accessThruMemRef(possibleMatcher)->string))) {
/* Here, the free variable to bind is actually equal
to the free mathematical variable.
We have a match if the thing to match is
also equal to the free mathematical variable.
Otherwise, we have no match.
In none of the cases, we have to establish an
association nor call ourselves for recursion.
*/
return (accessThruMemRef(thingToMatch)->nodeType == VARIABLE);
}
/* Here, the free variable is not the mathematical one.
We have to establish an association and return success
(unless the association fails).
*/
return associateThing(associations, accessThruMemRef(possibleMatcher)->string, thingToMatch);
}
/* Second special case: possibleMatcher is a free variable in an
application context (e.g. f(thing))
First check, if recursion is actually possible on the thing to match.
Recursion is not possible:
- On a variable
- On a constant
- On pi
- On a library constant
- On a binary function (as we have only one variable on the
possible matcher).
Second check if it is equal to the free mathematical variable. If
it is, consider the free mathematic variable as the identity
function (emitting a warning) and return the match result of
child tree.
Otherwise, perform matching logic for the case when f(thing)
makes sense.
*/
if (accessThruMemRef(possibleMatcher)->nodeType == TABLEACCESSWITHSUBSTITUTE) {
/* Check if recursion is possible on the thing to match */
switch (accessThruMemRef(thingToMatch)->nodeType) {
case VARIABLE:
case CONSTANT:
case PI_CONST:
case LIBRARYCONSTANT:
case ADD:
case SUB:
case MUL:
case DIV:
case POW:
return 0;
break;
default:
break;
}
/* If we are here, we are sure that recursion is possible
on the one, first and only child of the thing to match.
Now check if the identifier is equal to the free
mathematical variable.
*/
if ((variablename != NULL) &&
(!strcmp(variablename, accessThruMemRef(possibleMatcher)->string))) {
/* Here, the free variable to bind is actually equal
to the free mathematical variable.
*/
okay = tryMatchExtendedPureTree(associations, thingToMatch, ((node *) (accessThruMemRef(possibleMatcher)->arguments->value)));
if (okay) {
if (variablename != NULL) {
printMessage(1,SOLLYA_MSG_FREE_VAR_INTERPRETED_AS_IDENTITY_FUNCTION,"Warning: the identifier \"%s\" is bound to the current free variable. In a functional context it will be considered as the identity function.\n",
variablename);
} else {
printMessage(1,SOLLYA_MSG_FREE_VAR_INTERPRETED_AS_IDENTITY_FUNCTION,"Warning: \"_x_\" is the free variable. In a functional context it will be considered as the identity function.\n");
}
}
return okay;
}
/* Here, the free variable is not the mathematical one.
We know that recursion is possible on the one, first and
only child of the thing to match.
*/
okay = 0;
recursion = 1;
currentThingToMatch = accessThruMemRef(thingToMatch)->child1; /* No copy, just the ptr */
currentPossibleMatcher = (node *) (accessThruMemRef(possibleMatcher)->arguments->value); /* No copy, just the ptr */
currentIdentifier = accessThruMemRef(possibleMatcher)->string; /* No copy, just the ptr */
headSymbol = headFunction(thingToMatch); /* This one performs mallocs */
if (headSymbol == NULL) {
/* This case should never happen */
return 0;
}
do {
recursionAssoc = NULL;
matchRecursion = tryMatchExtendedPureTree(&recursionAssoc, currentThingToMatch, currentPossibleMatcher);
if (matchRecursion) {
/* We have a match on recursion. This means we just have to check if we can associate
the current identifier with the headSymbol */
recursion = 0;
okay = associateThing(&recursionAssoc, currentIdentifier, headSymbol);
if (okay) {
*associations = copyChainWithoutReversal(recursionAssoc, copyEntryOnVoid);
}
} else {
/* Here, we do not match on recursion. We have to check now if
we can extend the head symbol to one level below and check
again.
There is no recursion possible:
- if the current thing to match is one of
-- variable
-- constant
-- pi
-- libraryconstant
-- binary function
- if the current possible matcher is a free variable in an application context
*/
switch (accessThruMemRef(currentThingToMatch)->nodeType) {
case VARIABLE:
case CONSTANT:
case PI_CONST:
case LIBRARYCONSTANT:
case ADD:
case SUB:
case MUL:
case DIV:
case POW:
recursion = 0;
break;
default:
break;
}
if (recursion) {
if (accessThruMemRef(currentPossibleMatcher)->nodeType == TABLEACCESSWITHSUBSTITUTE) recursion = 0;
if (recursion) {
/* Here, recursion is possible
Compute a new head symbol.
Move currentThingToMatch to the one, first and only
child of the current thing to match.
*/
tempNode = headFunction(currentThingToMatch);
if (tempNode == NULL) {
/* This case should never happen */
recursion = 0;
} else {
newHeadSymbol = substitute(headSymbol, tempNode);
free_memory(tempNode);
free_memory(headSymbol);
headSymbol = newHeadSymbol;
currentThingToMatch = accessThruMemRef(currentThingToMatch)->child1;
}
}
}
}
if (recursionAssoc != NULL) freeChain(recursionAssoc, freeEntryOnVoid);
} while (recursion);
free_memory(headSymbol);
return okay;
}
/* Here, possibleMatcher is itself a pure tree, i.e. a function without
free variables to bind.
To start with, we cannot have a match if the head symbols are not
the same.
*/
if (accessThruMemRef(possibleMatcher)->nodeType != accessThruMemRef(thingToMatch)->nodeType) return 0;
/* Here, the head symbols are always the same */
switch (accessThruMemRef(possibleMatcher)->nodeType) {
case VARIABLE:
/* The free mathematical variable matches the free mathematical variable */
return 1;
break;
case CONSTANT:
/* Constants match if they are the same. We have to be careful with NaNs */
if (mpfr_nan_p(*(accessThruMemRef(possibleMatcher)->value)) && mpfr_nan_p(*(accessThruMemRef(thingToMatch)->value))) return 1;
if (mpfr_equal_p(*(accessThruMemRef(possibleMatcher)->value),*(accessThruMemRef(thingToMatch)->value))) return 1;
return 0;
break;
case ADD:
case SUB:
case MUL:
case DIV:
case POW:
/* Binary functions match if both sub-expressions match and if we can combine ("unify")
both association lists.
*/
leftAssoc = NULL;
rightAssoc = NULL;
if (!tryMatchExtendedPureTree(&leftAssoc, accessThruMemRef(thingToMatch)->child1, accessThruMemRef(possibleMatcher)->child1)) return 0;
if (!tryMatchExtendedPureTree(&rightAssoc, accessThruMemRef(thingToMatch)->child2, accessThruMemRef(possibleMatcher)->child2)) return 0;
okay = tryCombineAssociations(associations, leftAssoc, rightAssoc);
if (leftAssoc != NULL) freeChain(leftAssoc, freeEntryOnVoid);
if (rightAssoc != NULL) freeChain(rightAssoc, freeEntryOnVoid);
return okay;
break;
case NEG:
case UNARY_BASE_FUNC:
/* Unary functions: we have a match if we have a match on recursion
Possible associations are established on recursion.
*/
if (accessThruMemRef(possibleMatcher)->baseFun != accessThruMemRef(thingToMatch)->baseFun) return 0;
return tryMatchExtendedPureTree(associations, accessThruMemRef(thingToMatch)->child1, accessThruMemRef(possibleMatcher)->child1);
break;
case PI_CONST:
/* Pi matches pi */
return 1;
break;
case LIBRARYFUNCTION:
/* Library functions to not match if they are not bound to the same code
or if they are at different differentiation levels
*/
if ((accessThruMemRef(possibleMatcher)->libFun != accessThruMemRef(thingToMatch)->libFun) ||
(accessThruMemRef(possibleMatcher)->libFunDeriv != accessThruMemRef(thingToMatch)->libFunDeriv)) return 0;
/* Otherwise they match when recursion matches */
return tryMatchExtendedPureTree(associations, accessThruMemRef(thingToMatch)->child1, accessThruMemRef(possibleMatcher)->child1);
break;
case LIBRARYCONSTANT:
/* Library constants match iff they are bound to the same code */
return (accessThruMemRef(possibleMatcher)->libFun == accessThruMemRef(thingToMatch)->libFun);
break;
case PROCEDUREFUNCTION:
/* Procedure functions do not match if they are not based on the same
procedure or if they are at different differentiation levels
*/
if ((!isEqualThing(accessThruMemRef(possibleMatcher)->child2, accessThruMemRef(thingToMatch)->child2)) ||
(accessThruMemRef(possibleMatcher)->libFunDeriv != accessThruMemRef(thingToMatch)->libFunDeriv)) return 0;
/* Otherwise they match when recursion matches */
return tryMatchExtendedPureTree(associations, accessThruMemRef(thingToMatch)->child1, accessThruMemRef(possibleMatcher)->child1);
break;
default:
sollyaFprintf(stderr,"Error: tryMatchExtendedPureTree: unknown identifier (%d) in the tree\n",accessThruMemRef(possibleMatcher)->nodeType);
exit(1);
}
/* We should never get here */
return 0;
}
int tryMatchRange(chain **associations, mpfr_t a, mpfr_t b, node *possibleMatcher) {
chain *myAssociations;
node *tempNode;
int okay;
if (accessThruMemRef(possibleMatcher)->nodeType != RANGE) return 0;
if (!((accessThruMemRef(accessThruMemRef(possibleMatcher)->child1)->nodeType == CONSTANT) || (accessThruMemRef(accessThruMemRef(possibleMatcher)->child1)->nodeType == TABLEACCESS) || (accessThruMemRef(accessThruMemRef(possibleMatcher)->child1)->nodeType == DEFAULT))) return 0;
if (!((accessThruMemRef(accessThruMemRef(possibleMatcher)->child2)->nodeType == CONSTANT) || (accessThruMemRef(accessThruMemRef(possibleMatcher)->child2)->nodeType == TABLEACCESS) || (accessThruMemRef(accessThruMemRef(possibleMatcher)->child2)->nodeType == DEFAULT))) return 0;
if (variablename != NULL) {
if (accessThruMemRef(accessThruMemRef(possibleMatcher)->child1)->nodeType == TABLEACCESS) {
if (!strcmp(accessThruMemRef(accessThruMemRef(possibleMatcher)->child1)->string,variablename)) return 0;
}
if (accessThruMemRef(accessThruMemRef(possibleMatcher)->child2)->nodeType == TABLEACCESS) {
if (!strcmp(accessThruMemRef(accessThruMemRef(possibleMatcher)->child2)->string,variablename)) return 0;
}
}
if (accessThruMemRef(accessThruMemRef(possibleMatcher)->child1)->nodeType == CONSTANT) {
if ((!mpfr_equal_p(*(accessThruMemRef(accessThruMemRef(possibleMatcher)->child1)->value),a)) &&
(!(mpfr_nan_p(*(accessThruMemRef(accessThruMemRef(possibleMatcher)->child1)->value)) && mpfr_nan_p(a)))) return 0;
}
if (accessThruMemRef(accessThruMemRef(possibleMatcher)->child2)->nodeType == CONSTANT) {
if ((!mpfr_equal_p(*(accessThruMemRef(accessThruMemRef(possibleMatcher)->child2)->value),b)) &&
(!(mpfr_nan_p(*(accessThruMemRef(accessThruMemRef(possibleMatcher)->child2)->value)) && mpfr_nan_p(b)))) return 0;
}
/* Here, we know that the possible Matcher is a range, that its
childs are either constants equal to the bounds of the thing to
match or that they are free variables different from the free
mathematical variable or default */
myAssociations = NULL;
okay = 1;
if (accessThruMemRef(accessThruMemRef(possibleMatcher)->child1)->nodeType == TABLEACCESS) {
tempNode = makeConstant(a);
okay = associateThing(&myAssociations, accessThruMemRef(accessThruMemRef(possibleMatcher)->child1)->string, tempNode);
free_memory(tempNode);
}
if (okay && (accessThruMemRef(accessThruMemRef(possibleMatcher)->child2)->nodeType == TABLEACCESS)) {
tempNode = makeConstant(b);
okay = associateThing(&myAssociations, accessThruMemRef(accessThruMemRef(possibleMatcher)->child2)->string, tempNode);
free_memory(tempNode);
}
if (okay) {
*associations = myAssociations;
} else {
if (myAssociations != NULL) freeChain(myAssociations, freeEntryOnVoid);
}
return okay;
}
int isIntegerElement(int *res, node *thing) {
mpfr_t a, b;
int i, okay;
if (!isPureTree(thing)) return 0;
if (!isConstant(thing)) return 0;
okay = 0; i = 0;
mpfr_init2(a, tools_precision);
if (evaluateThingToConstant(a, thing, NULL, 0, 0)) {
if (mpfr_integer_p(a)) {
i = mpfr_get_si(a, GMP_RNDN);
mpfr_init2(b, 8 * sizeof(i) + 5);
mpfr_set_si(b, i, GMP_RNDN);
if (mpfr_cmp(a, b) == 0) {
okay = 1;
}
mpfr_clear(b);
}
}
mpfr_clear(a);
if (okay) {
*res = i;
}
return okay;
}
int formConsecutiveIntegers(node *thing1, node *thing2) {
mpfr_t a, b, c, d;
int i, j, okay;
if (!isPureTree(thing1)) return 0;
if (!isPureTree(thing2)) return 0;
if (!isConstant(thing1)) return 0;
if (!isConstant(thing2)) return 0;
okay = 0;
mpfr_init2(a, tools_precision);
if (evaluateThingToConstant(a, thing1, NULL, 0, 0)) {
if (mpfr_integer_p(a)) {
i = mpfr_get_si(a, GMP_RNDN);
mpfr_init2(b, 8 * sizeof(i) + 5);
mpfr_set_si(b, i, GMP_RNDN);
if (mpfr_cmp(a, b) == 0) {
mpfr_init2(c, tools_precision);
if (evaluateThingToConstant(c, thing2, NULL, 0, 0)) {
if (mpfr_integer_p(c)) {
j = mpfr_get_si(c, GMP_RNDN);
mpfr_init2(d, 8 * sizeof(i) + 5);
mpfr_set_si(d, j, GMP_RNDN);
if (mpfr_cmp(c, d) == 0) {
okay = (i + 1 == j);
}
mpfr_clear(d);
}
}
mpfr_clear(c);
}
mpfr_clear(b);
}
}
mpfr_clear(a);
return okay;
}
chain *normalizeFinalEllipticList(chain *list) {
node **thingArray;
int i, len, shortenedLen, mayShorten;
chain *curr;
chain *copy;
if (list == NULL) return NULL;
len = lengthChain(list);
shortenedLen = len;
thingArray = (node **) safeCalloc(len,sizeof(node *));
for (i=0, curr=list; curr != NULL; curr = curr->next, i++)
thingArray[i] = copyThing((node *) (curr->value));
while (shortenedLen >= 2) {
mayShorten = 0;
if ((accessThruMemRef(thingArray[shortenedLen - 2])->nodeType != TABLEACCESS) &&
(accessThruMemRef(thingArray[shortenedLen - 1])->nodeType != TABLEACCESS)) {
if (formConsecutiveIntegers(thingArray[shortenedLen - 2],
thingArray[shortenedLen - 1])) {
mayShorten = 1;
} else {
if (isEqualThing(thingArray[shortenedLen - 2],
thingArray[shortenedLen - 1]) ||
((accessThruMemRef(thingArray[shortenedLen - 2])->nodeType == CONSTANT) &&
(accessThruMemRef(thingArray[shortenedLen - 1])->nodeType == CONSTANT) &&
mpfr_nan_p(*(accessThruMemRef(thingArray[shortenedLen - 2])->value)) &&
mpfr_nan_p(*(accessThruMemRef(thingArray[shortenedLen - 2])->value)))) {
mayShorten = 1;
}
}
}
if (mayShorten) {
shortenedLen--;
} else {
break;
}
}
copy = NULL;
for (i=shortenedLen-1;i>=0;i--) {
copy = addElement(copy,copyThing(thingArray[i]));
}
for (i=0;i<len;i++) freeThing(thingArray[i]);
safeFree(thingArray);
return copy;
}
int tryMatchList(chain **associations, node *thingToMatch, node *possibleMatcher) {
int okay, freeLists, lenPossibleMatcherList, lenThingToMatchList;
chain *currThingToMatch, *currPossibleMatcher, *thingToMatchList, *possibleMatcherList;
chain *myAssociations, *elementAssociations, *myNewAssociations;
chain *lastThingToMatch;
mpfr_t c, d;
int i, integerBase, integerSequence;
node *implicitElement;
/* Make compiler happy */
lastThingToMatch = NULL;
/* End of compiler happiness */
/* All possibilities for empty lists */
if (isEmptyList(possibleMatcher)) {
if (isEmptyList(thingToMatch)) return 1;
return 0;
}
if (isEmptyList(thingToMatch)) return 0;
/* Check that we have got lists */
if (!(isPureList(thingToMatch) || isPureFinalEllipticList(thingToMatch))) return 0;
if (!(isPureList(possibleMatcher) || isPureFinalEllipticList(possibleMatcher))) return 0;
/* Here, both the possible matcher and the thing to match are lists.
Check if both are of the same type of lists.
*/
if (accessThruMemRef(thingToMatch)->nodeType != accessThruMemRef(possibleMatcher)->nodeType) return 0;
/* Here, both lists are of the same type
Now check if normalization is needed for final elliptic lists.
*/
okay = 1;
myAssociations = NULL;
freeLists = 0;
if (isPureFinalEllipticList(thingToMatch) &&
(lengthChain(accessThruMemRef(thingToMatch)->arguments) != lengthChain(accessThruMemRef(possibleMatcher)->arguments))) {
thingToMatchList = normalizeFinalEllipticList(accessThruMemRef(thingToMatch)->arguments);
possibleMatcherList = normalizeFinalEllipticList(accessThruMemRef(possibleMatcher)->arguments);
freeLists = 1;
} else {
thingToMatchList = accessThruMemRef(thingToMatch)->arguments;
possibleMatcherList = accessThruMemRef(possibleMatcher)->arguments;
freeLists = 0;
}
/* After normalization, there can be no match if the lists are not
of the same length (for lists) or if the possibleMatcherList is
shorter than the thingToMatchList
*/
lenThingToMatchList = lengthChain(thingToMatchList);
lenPossibleMatcherList = lengthChain(possibleMatcherList);
okay = (lenThingToMatchList == lenPossibleMatcherList) ||
(isPureFinalEllipticList(thingToMatch) &&
(lenThingToMatchList <= lenPossibleMatcherList));
if (okay) {
/* Matching is possible because the lists are of appropriate
lengths. Now check if each element pair of the lists
matches. Keep attention to the associations.
*/
myAssociations = NULL;
for (currThingToMatch = thingToMatchList, currPossibleMatcher = possibleMatcherList;
(currThingToMatch != NULL) && (currPossibleMatcher != NULL);
currThingToMatch = currThingToMatch->next, currPossibleMatcher = currPossibleMatcher->next) {
lastThingToMatch = currThingToMatch;
elementAssociations = NULL;
okay = tryMatch(&elementAssociations, (node *) (currThingToMatch->value), (node *) (currPossibleMatcher->value));
if (okay) {
myNewAssociations = NULL;
okay = tryCombineAssociations(&myNewAssociations, myAssociations, elementAssociations);
if (myAssociations != NULL) freeChain(myAssociations, freeEntryOnVoid);
myAssociations = myNewAssociations;
}
if (elementAssociations != NULL) freeChain(elementAssociations, freeEntryOnVoid);
if (!okay) break;
}
}
if (okay && isPureFinalEllipticList(thingToMatch) && (currPossibleMatcher != NULL)) {
/* Here, the explicit possible matcher list is longer and both
list are final elliptic.
We have to continue on the possible matcher list until it is
empty (explicitly speaking). For each of its elements, we have
to "invent", i.e. generate, an implicit element of the
thing-to-match list. This generation is based on the type of
the last explicit element in the thing-to-match list, stored
on variable lastThingToMatch->value.
We start by performing a test that allows us to decide if
we have a sequence of integers or another type continued to
infinity.
*/
integerSequence = 0;
integerBase = 0;
mpfr_init2(c, tools_precision);
if (evaluateThingToConstant(c, (node *) (lastThingToMatch->value), NULL, 0, 0)) {
if (mpfr_integer_p(c)) {
integerBase = mpfr_get_si(c, GMP_RNDN);
mpfr_init2(d, 8 * sizeof(i) + 5);
mpfr_set_si(d, integerBase, GMP_RNDN);
if (mpfr_cmp(c, d) == 0) {
integerSequence = 1;
}
mpfr_clear(d);
}
}
mpfr_clear(c);
/* Now check all remaining elements of the possible matcher */
for (i = 1; (currPossibleMatcher != NULL); currPossibleMatcher = currPossibleMatcher->next, i++) {
/* Start by generating the implicit element of the sequence */
if (integerSequence) {
mpfr_init2(c, 8 * sizeof(int) + 1 + 5);
mpfr_set_si(c,integerBase,GMP_RNDN); /* exact */
mpfr_add_si(c,c,i,GMP_RNDN); /* exact */
implicitElement = makeConstant(c);
mpfr_clear(c);
} else {
/* We just have to repeat the last element of the sequence
We avoid mallocs here. We'll have to account for that later.
*/
implicitElement = (node *) (lastThingToMatch->value);
}
/* Now perform the match check */
elementAssociations = NULL;
okay = tryMatch(&elementAssociations, implicitElement, (node *) (currPossibleMatcher->value));
if (okay) {
myNewAssociations = NULL;
okay = tryCombineAssociations(&myNewAssociations, myAssociations, elementAssociations);
if (myAssociations != NULL) freeChain(myAssociations, freeEntryOnVoid);
myAssociations = myNewAssociations;
}
if (elementAssociations != NULL) freeChain(elementAssociations, freeEntryOnVoid);
/* If we have an integer sequence, we malloc'ed for the
implicit element. So we have to free it.
*/
if (integerSequence) {
freeThing(implicitElement);
}
if (!okay) break;
}
}
/* Free the list if it was us who allocated them */
if (freeLists) {
freeChain(thingToMatchList,freeThingOnVoid);
freeChain(possibleMatcherList,freeThingOnVoid);
}
if (okay) {
*associations = myAssociations;
} else {
if (myAssociations != NULL) freeChain(myAssociations, freeEntryOnVoid);
}
return okay;
}
int tryMatchPrepend(chain **associations, node *thingToMatch, node *possibleMatcher) {
int okay;
chain *myAssociations, *headAssoc, *tailAssoc;
node *tailList, *tempNode;
if ((accessThruMemRef(thingToMatch)->nodeType != LIST) && (accessThruMemRef(thingToMatch)->nodeType != FINALELLIPTICLIST)) return 0;
myAssociations = NULL;
headAssoc = NULL;
okay = tryMatch(&headAssoc, (node *) (accessThruMemRef(thingToMatch)->arguments->value), accessThruMemRef(possibleMatcher)->child1);
if (okay) {
tempNode = makeTail(copyThing(thingToMatch));
tailList = evaluateThingDeepCopy(tempNode);
freeThing(tempNode);
tailAssoc = NULL;
okay = tryMatch(&tailAssoc, tailList, accessThruMemRef(possibleMatcher)->child2);
if (okay) {
okay = tryCombineAssociations(&myAssociations, headAssoc, tailAssoc);
}
if (tailAssoc != NULL) freeChain(tailAssoc, freeEntryOnVoid);
freeThing(tailList);
}
if (headAssoc != NULL) freeChain(headAssoc, freeEntryOnVoid);
if (okay) {
*associations = myAssociations;
} else {
if (myAssociations != NULL) freeChain(myAssociations, freeEntryOnVoid);
}
return okay;
}
int tryMatchAppend(chain **associations, node *thingToMatch, node *possibleMatcher) {
int okay, len, i;
chain *myAssociations, *headAssoc, *tailAssoc, *curr;
node *headList, *tailElement, **elementArray;
if (accessThruMemRef(thingToMatch)->nodeType != LIST) return 0;
myAssociations = NULL;
if (accessThruMemRef(thingToMatch)->arguments->next != NULL) {
/* The list of things to match has more than one element. Its is
easy to compute the head list and the tailElement.
*/
len = lengthChain(accessThruMemRef(thingToMatch)->arguments);
elementArray = (node **) safeCalloc(len,sizeof(node *));
for (i=0, curr=accessThruMemRef(thingToMatch)->arguments; curr != NULL; curr = curr->next, i++) {
elementArray[i] = (node *) (curr->value);
}
tailElement = elementArray[len-1];
curr = NULL;
for (i=len-2;i>=0;i--) {
curr = addElement(curr,copyThing(elementArray[i]));
}
headList = makeList(curr);
safeFree(elementArray);
} else {
/* The list of things to match has only one element, so the head
list is the empty list. The tail element is easy to get, too.
*/
tailElement = (node *) (accessThruMemRef(thingToMatch)->arguments->value);
headList = makeEmptyList();
}
headAssoc = NULL;
okay = tryMatch(&headAssoc, headList, accessThruMemRef(possibleMatcher)->child1);
if (okay) {
tailAssoc = NULL;
okay = tryMatch(&tailAssoc, tailElement, accessThruMemRef(possibleMatcher)->child2);
if (okay) {
okay = tryCombineAssociations(&myAssociations, headAssoc, tailAssoc);
}
if (tailAssoc != NULL) freeChain(tailAssoc, freeEntryOnVoid);
}
if (headAssoc != NULL) freeChain(headAssoc, freeEntryOnVoid);
freeThing(headList);
if (okay) {
*associations = myAssociations;
} else {
if (myAssociations != NULL) freeChain(myAssociations, freeEntryOnVoid);
}
return okay;
}
int tryEvaluateRecursiveConcatMatcherToString(char **concatenatedString, node *tree) {
char *buf, *bufLeft, *bufRight, *curr;
int okayLeft, okayRight, okay;
if (accessThruMemRef(tree)->nodeType == STRING) {
buf = (char *) safeCalloc(strlen(accessThruMemRef(tree)->string) + 1, sizeof(char));
strcpy(buf,accessThruMemRef(tree)->string);
*concatenatedString = buf;
return 1;
}
if (accessThruMemRef(tree)->nodeType == CONCAT) {
okayLeft = tryEvaluateRecursiveConcatMatcherToString(&bufLeft, accessThruMemRef(tree)->child1);
okayRight = tryEvaluateRecursiveConcatMatcherToString(&bufRight, accessThruMemRef(tree)->child2);
okay = 0;
if (okayLeft && okayRight) {
buf = (char *) safeCalloc(strlen(bufLeft) + strlen(bufRight) + 1, sizeof(char));
*concatenatedString = buf;
for (curr=bufLeft; *curr != '\0'; curr++, buf++) {
*buf = *curr;
}
for (curr=bufRight; *curr != '\0'; curr++, buf++) {
*buf = *curr;
}
okay = 1;
}
if (okayLeft) safeFree(bufLeft);
if (okayRight) safeFree(bufRight);
return okay;
}
return 0;
}
int tryCutPrefix(char **rest, char *mainString, char *prefix) {
int okay;
char *ptrStr, *ptrPref, *buf;
okay = 1;
for (ptrStr = mainString, ptrPref = prefix;
(*ptrStr != '\0') && (*ptrPref != '\0');
ptrStr++, ptrPref++) {
if (*ptrStr != *ptrPref) {
okay = 0;
break;
}
}
if ((*ptrStr == '\0') && (*ptrPref != '\0')) {
okay = 0;
}
if (okay) {
buf = (char *) safeCalloc(strlen(ptrStr) + 1, sizeof(char));
strcpy(buf, ptrStr);
*rest = buf;
}
return okay;
}
char *revertString(char *buf) {
char *revBuf;
int len, i;
len = strlen(buf);
revBuf = (char *) safeCalloc(len + 1, sizeof(char));
for (i=0;i<len;i++) {
revBuf[len - 1 - i] = buf[i];
}
return revBuf;
}
int tryCutPostfix(char **rest, char *mainString, char *postfix) {
char *revMainString, *revPostfix, *revRest;
int okay;
revMainString = revertString(mainString);
revPostfix = revertString(postfix);
okay = tryCutPrefix(&revRest, revMainString, revPostfix);
if (okay) {
*rest = revertString(revRest);
safeFree(revRest);
}
safeFree(revMainString);
safeFree(revPostfix);
return okay;
}
int tryMatchConcatOnString(chain **associations, char *stringToMatch, node *possibleMatcher) {
int okayFullEvaluate, okay, okayLeftEvaluate, okayRightEvaluate, okayCut;
char *stringFullEvaluate, *stringLeftEvaluate, *stringRightEvaluate, *restString;
chain *myAssociations;
node *restStringThing;
myAssociations = NULL;
okayFullEvaluate = tryEvaluateRecursiveConcatMatcherToString(&stringFullEvaluate, possibleMatcher);
if (okayFullEvaluate) {
okay = !strcmp(stringToMatch,stringFullEvaluate);
safeFree(stringFullEvaluate);
return okay;
}
if (accessThruMemRef(possibleMatcher)->nodeType != CONCAT) return 0;
okayLeftEvaluate = tryEvaluateRecursiveConcatMatcherToString(&stringLeftEvaluate, accessThruMemRef(possibleMatcher)->child1);
okayRightEvaluate = tryEvaluateRecursiveConcatMatcherToString(&stringRightEvaluate, accessThruMemRef(possibleMatcher)->child2);
if (okayLeftEvaluate || okayRightEvaluate) {
okay = 0;
if (okayLeftEvaluate) {
okayCut = tryCutPrefix(&restString,stringToMatch,stringLeftEvaluate);
} else {
okayCut = tryCutPostfix(&restString,stringToMatch,stringRightEvaluate);
}
if (okayCut) {
restStringThing = makeString(restString);
myAssociations = NULL;
if (okayLeftEvaluate) {
okay = tryMatch(&myAssociations,restStringThing,accessThruMemRef(possibleMatcher)->child2);
} else {
okay = tryMatch(&myAssociations,restStringThing,accessThruMemRef(possibleMatcher)->child1);
}
freeThing(restStringThing);
safeFree(restString);
}
if (okayLeftEvaluate) safeFree(stringLeftEvaluate);
if (okayRightEvaluate) safeFree(stringRightEvaluate);
if (okay) {
*associations = myAssociations;
} else {
if (myAssociations != NULL) freeChain(myAssociations, freeEntryOnVoid);
}
return okay;
}
return 0;
}
int tryEvaluateRecursiveConcatMatcherToList(node **concatenatedList, node *tree) {
node *evalLeft, *evalRight;
int okayLeft, okayRight, okay;
chain *tempChain, *tempChain2;
switch (accessThruMemRef(tree)->nodeType) {
case EMPTYLIST:
case LIST:
case FINALELLIPTICLIST:
*concatenatedList = copyThing(tree);
return 1;
break;
case PREPEND:
evalRight = NULL;
okayRight = tryEvaluateRecursiveConcatMatcherToList(&evalRight, accessThruMemRef(tree)->child2);
if (okayRight) {
okay = 0;
switch (accessThruMemRef(evalRight)->nodeType) {
case EMPTYLIST:
*concatenatedList = makeList(addElement(NULL, copyThing(accessThruMemRef(tree)->child1)));
okay = 1;
break;
case LIST:
tempChain = copyChainWithoutReversal(accessThruMemRef(evalRight)->arguments, copyThingOnVoid);
tempChain = addElement(tempChain, copyThing(accessThruMemRef(tree)->child1));
*concatenatedList = makeList(tempChain);
okay = 1;
break;
case FINALELLIPTICLIST:
tempChain = copyChainWithoutReversal(accessThruMemRef(evalRight)->arguments, copyThingOnVoid);
tempChain = addElement(tempChain, copyThing(accessThruMemRef(tree)->child1));
*concatenatedList = makeFinalEllipticList(tempChain);
okay = 1;
break;
default:
okay = 0;
}
freeThing(evalRight);
return okay;
}
return 0;
break;
case APPEND:
evalLeft = NULL;
okayLeft = tryEvaluateRecursiveConcatMatcherToList(&evalLeft, accessThruMemRef(tree)->child1);
if (okayLeft) {
okay = 0;
switch (accessThruMemRef(evalLeft)->nodeType) {
case EMPTYLIST:
*concatenatedList = makeList(addElement(NULL, copyThing(accessThruMemRef(tree)->child2)));
okay = 1;
break;
case LIST:
tempChain = copyChain(accessThruMemRef(evalLeft)->arguments, copyThingOnVoid);
tempChain = addElement(tempChain, copyThing(accessThruMemRef(tree)->child2));
tempChain2 = copyChain(tempChain, copyThingOnVoid);
freeChain(tempChain, freeThingOnVoid);
*concatenatedList = makeList(tempChain2);
okay = 1;
break;
default:
okay = 0;
}
freeThing(evalLeft);
return okay;
}
return 0;
break;
case CONCAT:
evalLeft = NULL;
okayLeft = tryEvaluateRecursiveConcatMatcherToList(&evalLeft, accessThruMemRef(tree)->child1);
if (okayLeft) {
okay = 0;
evalRight = NULL;
okayRight = tryEvaluateRecursiveConcatMatcherToList(&evalRight, accessThruMemRef(tree)->child2);
if (okayRight) {
switch (accessThruMemRef(evalLeft)->nodeType) {
case EMPTYLIST:
switch (accessThruMemRef(evalRight)->nodeType) {
case EMPTYLIST:
case LIST:
case FINALELLIPTICLIST:
*concatenatedList = copyThing(evalRight);
okay = 1;
break;
default:
okay = 0;
break;
}
break;
case LIST:
switch (accessThruMemRef(evalRight)->nodeType) {
case EMPTYLIST:
*concatenatedList = copyThing(evalLeft);
okay = 1;
break;
case LIST:
tempChain = concatChains(copyChainWithoutReversal(accessThruMemRef(evalLeft)->arguments, copyThingOnVoid),
copyChainWithoutReversal(accessThruMemRef(evalRight)->arguments, copyThingOnVoid));
*concatenatedList = makeList(tempChain);
okay = 1;
break;
case FINALELLIPTICLIST:
tempChain = concatChains(copyChainWithoutReversal(accessThruMemRef(evalLeft)->arguments, copyThingOnVoid),
copyChainWithoutReversal(accessThruMemRef(evalRight)->arguments, copyThingOnVoid));
*concatenatedList = makeFinalEllipticList(tempChain);
okay = 1;
break;
default:
okay = 0;
break;
}
break;
default:
okay = 0;
break;
}
freeThing(evalRight);
}
freeThing(evalLeft);
return okay;
}
return 0;
break;
default:
return 0;
}
return 0;
}
int tryCutPostfixList(chain **associations, node **restList, node *mainList, node *postfix) {
int lenMainList, lenPostfix, i, k, okay, offset;
node **mainListArray;
chain *curr, *possibleMatchList, *possibleRestList;
node *possibleThingToMatch, *possibleRest, *tempNode, *myMainList;
chain *myAssociations;
node *postfixFirstElem, *mainListLastElem;
chain *mainListCurr;
int mainListInt, postfixInt;
if ((accessThruMemRef(mainList)->nodeType == FINALELLIPTICLIST) &&
(accessThruMemRef(postfix)->nodeType != FINALELLIPTICLIST)) return 0;
if ((accessThruMemRef(postfix)->nodeType == FINALELLIPTICLIST) &&
(accessThruMemRef(mainList)->nodeType != FINALELLIPTICLIST)) return 0;
switch (accessThruMemRef(postfix)->nodeType) {
case EMPTYLIST:
*restList = copyThing(mainList);
*associations = NULL;
return 1;
break;
case LIST:
if (accessThruMemRef(mainList)->nodeType == LIST) {
lenMainList = lengthChain(accessThruMemRef(mainList)->arguments);
lenPostfix = lengthChain(accessThruMemRef(postfix)->arguments);
if (lenMainList >= lenPostfix) {
mainListArray = (node **) safeCalloc(lenMainList, sizeof(node *));
for (i=0, curr = accessThruMemRef(mainList)->arguments; curr != NULL; curr = curr->next, i++) {
mainListArray[i] = (node *) (curr->value);
}
possibleMatchList = NULL;
for (i=lenMainList-1, k=0; (i >= 0) && (k < lenPostfix); i--, k++) {
possibleMatchList = addElement(possibleMatchList, copyThing(mainListArray[i]));
}
possibleThingToMatch = makeList(possibleMatchList);
if (lenMainList == lenPostfix) {
possibleRest = makeEmptyList();
} else {
possibleRestList = NULL;
for (i=lenMainList-lenPostfix-1;i>=0;i--) {
possibleRestList = addElement(possibleRestList, copyThing(mainListArray[i]));
}
possibleRest = makeList(possibleRestList);
}
safeFree(mainListArray);
okay = tryMatch(associations, possibleThingToMatch, postfix);
freeThing(possibleThingToMatch);
if (okay) {
*restList = possibleRest;
} else {
freeThing(possibleRest);
}
return okay;
} else {
return 0;
}
} else {
return 0;
}
break;
case FINALELLIPTICLIST:
if (accessThruMemRef(mainList)->nodeType == FINALELLIPTICLIST) {
lenMainList = lengthChain(accessThruMemRef(mainList)->arguments);
possibleRest = makeEmptyList();
myMainList = copyThing(mainList);
offset = 1;
postfixFirstElem = (node *) (((chain *) (accessThruMemRef(postfix)->arguments))->value);
for (mainListCurr = accessThruMemRef(mainList)->arguments;
mainListCurr->next != NULL;
mainListCurr = mainListCurr->next);
mainListLastElem = (node *) (mainListCurr->value);
if (isIntegerElement(&mainListInt, mainListLastElem) &&
isIntegerElement(&postfixInt, postfixFirstElem) &&
(postfixInt > mainListInt)) {
offset = postfixInt - mainListInt + 2;
}
okay = 0;
for (i=1; i<=lenMainList+offset; i++) {
myAssociations = NULL;
okay = tryMatch(&myAssociations, myMainList, postfix);
if (okay) {
*associations = myAssociations;
*restList = copyThing(possibleRest);
break;
} else {
if (myAssociations != NULL) freeChain(myAssociations, freeEntryOnVoid);
tempNode = makeAppend(possibleRest, makeHead(copyThing(myMainList)));
possibleRest = evaluateThingDeepCopy(tempNode);
freeThing(tempNode);
tempNode = makeTail(myMainList);
myMainList = evaluateThingDeepCopy(tempNode);
freeThing(tempNode);
}
}
freeThing(possibleRest);
freeThing(myMainList);
return okay;
} else {
return 0;
}
break;
default:
return 0;
break;
}
return 0;
}
int tryCutPrefixList(chain **associations, node **restList, node *mainList, node *prefix) {
node *myRestList, *possibleMatchingPrefix, *tempNode;
int lenPrefix, i, okay;
switch (accessThruMemRef(prefix)->nodeType) {
case EMPTYLIST:
*restList = copyThing(mainList);
*associations = NULL;
return 1;
break;
case LIST:
switch (accessThruMemRef(mainList)->nodeType) {
case LIST:
case FINALELLIPTICLIST:
lenPrefix = lengthChain(accessThruMemRef(prefix)->arguments);
possibleMatchingPrefix = makeEmptyList();
myRestList = copyThing(mainList);
for (i=0; i<lenPrefix; i++) {
tempNode = makeAppend(possibleMatchingPrefix, makeHead(copyThing(myRestList)));
possibleMatchingPrefix = evaluateThingDeepCopy(tempNode);
freeThing(tempNode);
tempNode = makeTail(myRestList);
myRestList = evaluateThingDeepCopy(tempNode);
freeThing(tempNode);
}
okay = tryMatch(associations, possibleMatchingPrefix, prefix);
if (okay) {
*restList = myRestList;
} else {
freeThing(myRestList);
}
freeThing(possibleMatchingPrefix);
return okay;
break;
default:
return 0;
break;
}
return 0;
break;
default:
return 0;
}
return 0;
}
int tryMatchConcatOnList(chain **associations, node *thingToMatch, node *possibleMatcher) {
int okayFullEvaluate, okay, okayLeft, okayRight;
node *listFullEvaluate, *leftEval, *rightEval;
chain *myAssociations;
node *restList;
chain *cutAssociations, *restAssociations;
myAssociations = NULL;
listFullEvaluate = NULL;
okayFullEvaluate = tryEvaluateRecursiveConcatMatcherToList(&listFullEvaluate, possibleMatcher);
if (okayFullEvaluate) {
myAssociations = NULL;
okay = tryMatch(&myAssociations, thingToMatch, listFullEvaluate);
freeThing(listFullEvaluate);
if (okay) {
*associations = myAssociations;
} else {
if (myAssociations != NULL) freeChain(myAssociations, freeEntryOnVoid);
}
return okay;
}
if (accessThruMemRef(possibleMatcher)->nodeType != CONCAT) return 0;
leftEval = NULL;
rightEval = NULL;
okayLeft = tryEvaluateRecursiveConcatMatcherToList(&leftEval, accessThruMemRef(possibleMatcher)->child1);
okayRight = tryEvaluateRecursiveConcatMatcherToList(&rightEval, accessThruMemRef(possibleMatcher)->child2);
if (okayLeft || okayRight) {
myAssociations = NULL;
okay = 1;
if (okayLeft && (accessThruMemRef(leftEval)->nodeType == FINALELLIPTICLIST)) okay = 0;
if (okay) {
if (okayRight) {
cutAssociations = NULL;
restList = NULL;
okay = tryCutPostfixList(&cutAssociations, &restList, thingToMatch, rightEval);
if (okay) {
restAssociations = NULL;
okay = tryMatch(&restAssociations,restList,accessThruMemRef(possibleMatcher)->child1);
if (okay) {
myAssociations = NULL;
okay = tryCombineAssociations(&myAssociations, restAssociations, cutAssociations);
if (restAssociations != NULL) freeChain(restAssociations, freeEntryOnVoid);
}
if (cutAssociations != NULL) freeChain(cutAssociations, freeEntryOnVoid);
freeThing(restList);
}
} else {
cutAssociations = NULL;
restList = NULL;
okay = tryCutPrefixList(&cutAssociations, &restList, thingToMatch, leftEval);
if (okay) {
restAssociations = NULL;
okay = tryMatch(&restAssociations,restList,accessThruMemRef(possibleMatcher)->child2);
if (okay) {
myAssociations = NULL;
okay = tryCombineAssociations(&myAssociations, restAssociations, cutAssociations);
if (restAssociations != NULL) freeChain(restAssociations, freeEntryOnVoid);
}
if (cutAssociations != NULL) freeChain(cutAssociations, freeEntryOnVoid);
freeThing(restList);
}
}
}
if (okayLeft) freeThing(leftEval);
if (okayRight) freeThing(rightEval);
if (okay) {
*associations = myAssociations;
} else {
if (myAssociations != NULL) freeChain(myAssociations, freeEntryOnVoid);
}
return okay;
}
return 0;
}
int tryMatchStructure(chain **associations, node *thingToMatch, node *possibleMatcher) {
int okay, found, okayRecurse, okayCombine;
chain *myAssociations, *currPossibleMatcher, *currThingToMatch, *recurseAssoc, *myCombinedAssoc;
char *currPossibleMatcherName, *currThingToMatchName;
node *currPossibleMatcherMatcher, *currThingToMatchThing;
if (accessThruMemRef(thingToMatch)->nodeType != STRUCTURE) return 0;
if (accessThruMemRef(possibleMatcher)->nodeType != STRUCTURE) return 0;
okay = 1;
myAssociations = NULL;
for (currPossibleMatcher=accessThruMemRef(possibleMatcher)->arguments;
currPossibleMatcher != NULL;
currPossibleMatcher=currPossibleMatcher->next) {
currPossibleMatcherName = (char *) (((entry *) (currPossibleMatcher->value))->name);
currPossibleMatcherMatcher = (node *) (((entry *) (currPossibleMatcher->value))->value);
for (found = 0, currThingToMatch=accessThruMemRef(thingToMatch)->arguments;
(!found) && (currThingToMatch != NULL);
currThingToMatch=currThingToMatch->next) {
currThingToMatchName = (char *) (((entry *) (currThingToMatch->value))->name);
currThingToMatchThing = (node *) (((entry *) (currThingToMatch->value))->value);
if (!strcmp(currThingToMatchName,currPossibleMatcherName)) found = 1;
}
if (found) {
recurseAssoc = NULL;
okayRecurse = tryMatch(&recurseAssoc, currThingToMatchThing, currPossibleMatcherMatcher);
if (okayRecurse) {
myCombinedAssoc = NULL;
okayCombine = tryCombineAssociations(&myCombinedAssoc, recurseAssoc, myAssociations);
if (okayCombine) {
if (myAssociations != NULL) freeChain(myAssociations, freeEntryOnVoid);
myAssociations = myCombinedAssoc;
} else {
okay = 0;
}
if (recurseAssoc != NULL) freeChain(recurseAssoc, freeEntryOnVoid);
} else {
okay = 0;
}
} else {
okay = 0;
}
if (!okay) break;
}
if (okay) {
*associations = myAssociations;
} else {
if (myAssociations != NULL) freeChain(myAssociations, freeEntryOnVoid);
}
return okay;
}
int tryMatchInner(chain **associations, node *thingToMatch, node *possibleMatcher) {
/* Default case: a match for everything, no association to perform */
if (accessThruMemRef(possibleMatcher)->nodeType == DEFAULT) return 1;
/* Base symbols: a match is obtained if the thing to match is equal
to the matcher */
if (isCorrectlyTypedBaseSymbol(possibleMatcher)) {
if (isEqualThing(thingToMatch, possibleMatcher)) {
return 1;
}
return 0;
}
/* Extended pure trees (functions with possible variables to bind to):
run specialized code if the thing to match is a pure tree (a function
without variables)
*/
if (isExtendedPureTree(possibleMatcher)) {
if (!isPureTree(thingToMatch) && !(accessThruMemRef(possibleMatcher)->nodeType == TABLEACCESS)) return 0;
return tryMatchExtendedPureTree(associations, thingToMatch, possibleMatcher);
}
/* Match ranges */
if (accessThruMemRef(possibleMatcher)->nodeType == RANGE) {
if (!isRange(thingToMatch)) return 0;
return tryMatchRange(associations, *(accessThruMemRef(accessThruMemRef(thingToMatch)->child1)->value), *(accessThruMemRef(accessThruMemRef(thingToMatch)->child2)->value), possibleMatcher);
}
/* Match lists */
if (isMatchableList(possibleMatcher)) {
if (!(isEmptyList(thingToMatch) ||
isPureList(thingToMatch) ||
isPureFinalEllipticList(thingToMatch))) return 0;
return tryMatchList(associations, thingToMatch, possibleMatcher);
}
/* Match the prepend operator .: */
if (isMatchablePrepend(possibleMatcher)) {
if (!(isPureList(thingToMatch) || isPureFinalEllipticList(thingToMatch))) return 0;
return tryMatchPrepend(associations, thingToMatch, possibleMatcher);
}
/* Match the append operator :. */
if (isMatchableAppend(possibleMatcher)) {
if (!isPureList(thingToMatch)) return 0;
return tryMatchAppend(associations, thingToMatch, possibleMatcher);
}
/* Match the concat operator @ for strings and lists */
if (isMatchableConcat(possibleMatcher)) {
if (!(isPureList(thingToMatch) ||
isPureFinalEllipticList(thingToMatch) ||
isEmptyList(thingToMatch) ||
isString(thingToMatch))) return 0;
if (isString(thingToMatch)) {
return tryMatchConcatOnString(associations, accessThruMemRef(thingToMatch)->string, possibleMatcher);
} else {
return tryMatchConcatOnList(associations, thingToMatch, possibleMatcher);
}
}
/* Match literate structures */
if (isMatchableStructure(possibleMatcher)) {
if (!isStructure(thingToMatch)) return 0;
if (associationContainsDoubleEntries(accessThruMemRef(thingToMatch)->arguments)) return 0;
return tryMatchStructure(associations, thingToMatch, possibleMatcher);
}
return 0;
}
int tryMatch(chain **associations, node *thingToMatch, node *possibleMatcher) {
int okay;
chain *myAssociations = NULL;
okay = tryMatchInner(&myAssociations, thingToMatch, possibleMatcher);
if (okay) {
*associations = myAssociations;
} else {
if (myAssociations != NULL) {
freeChain(myAssociations, freeEntryOnVoid);
}
}
return okay;
}
|