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
|
/************************************************************************
************************************************************************
FAUST compiler
Copyright (C) 2003-2018 GRAME, Centre National de Creation Musicale
Copyright (C) 2024-2024 INRIA
---------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
************************************************************************
************************************************************************/
#undef TRACE
/**
* \file eval.cpp
* Implementation of the Block diagram evaluator.
*
* A strict lambda-calculus evaluator for block diagram expressions.
*
**/
#include <stdio.h>
#include <cstdlib>
#include "boxIdentity.hh"
#include "boxModulationImplanter.hh"
#include "compatibility.hh"
#include "environment.hh"
#include "errormsg.hh"
#include "eval.hh"
#include "exception.hh"
#include "global.hh"
#include "labels.hh"
#include "names.hh"
#include "patternmatcher.hh"
#include "ppbox.hh"
#include "propagate.hh"
#include "property.hh"
#include "simplify.hh"
#include "xtended.hh"
using namespace std;
// History
// 23/05/2005 : New environment management
//-------------- prototypes ---------------------------------------------------------
static Tree a2sb(Tree exp);
static Tree eval(Tree exp, Tree visited, Tree localValEnv);
static Tree realeval(Tree exp, Tree visited, Tree localValEnv);
static Tree revEvalList(Tree lexp, Tree visited, Tree localValEnv);
static Tree applyList(Tree fun, Tree larg);
static Tree iteratePar(Tree var, int num, Tree body, Tree visited, Tree localValEnv);
static Tree iterateSeq(Tree id, int num, Tree body, Tree visited, Tree localValEnv);
static Tree iterateSum(Tree id, int num, Tree body, Tree visited, Tree localValEnv);
static Tree iterateProd(Tree id, int num, Tree body, Tree visited, Tree localValEnv);
static Tree larg2par(Tree larg);
static int eval2int(Tree exp, Tree visited, Tree localValEnv);
static double eval2double(Tree exp, Tree visited, Tree localValEnv);
static string evalLabel(const char* l, Tree visited, Tree localValEnv);
static Tree evalIdDef(Tree id, Tree visited, Tree env);
static Tree evalCase(Tree rules, Tree env);
static Tree evalRuleList(Tree rules, Tree env);
static Tree evalRule(Tree rule, Tree env);
static Tree evalPatternList(Tree patterns, Tree env);
static Tree evalPattern(Tree pattern, Tree env);
static Tree patternSimplification(Tree pattern);
static bool isBoxNumeric(Tree in, Tree& out);
static Tree vec2list(const vector<Tree>& v);
static void list2vec(Tree l, vector<Tree>& v);
static Tree listn(int n, Tree e);
static Tree boxSimplification(Tree box);
static void setNumericProperty(Tree t, Tree num);
static bool getNumericProperty(Tree t, Tree& num);
//------------------
// Public Interface
//------------------
/**
* Eval "process" from a list of definitions.
*
* Strict evaluation of a block diagram expression by applying beta reduction.
* @param eqlist a list of Faust definitions forming the global environment
* @return the process block diagram in normal form
*/
Tree evalprocess(Tree eqlist)
{
// Init stack overflow detector
gGlobal->gStackOverflowDetector = stackOverflowDetector(MAX_STACK_SIZE);
Tree b = a2sb(eval(boxIdent(gGlobal->gProcessName.c_str()), gGlobal->nil,
pushMultiClosureDefs(eqlist, gGlobal->nil, gGlobal->nil)));
if (gGlobal->gSimplifyDiagrams) {
b = boxSimplification(b);
}
return b;
}
/* Eval a documentation expression. */
Tree evaldocexpr(Tree docexpr, Tree eqlist)
{
// Init stack overflow detector
gGlobal->gStackOverflowDetector = stackOverflowDetector(MAX_STACK_SIZE);
return a2sb(
eval(docexpr, gGlobal->nil, pushMultiClosureDefs(eqlist, gGlobal->nil, gGlobal->nil)));
}
/**
* Simplify a block-diagram pattern by computing its numerical sub-expressions
* \param pattern an evaluated block-diagram
* \return a simplified pattern
*
*/
/* uncomment for debugging output */
// #define DEBUG
Tree simplifyPattern(Tree value)
{
Tree num;
if (!getNumericProperty(value, num)) {
if (!isBoxNumeric(value, num)) {
num = value;
}
setNumericProperty(value, num);
}
return num;
}
//------------------------
// Private Implementation
//------------------------
/**
* Transform unused (unapplied) closures into symbolic boxes.
*
* @param exp the expression to transform
* @return an expression where abstractions have been replaced by symbolic boxes
*/
static Tree real_a2sb(Tree exp);
static Tree a2sb(Tree exp)
{
Tree result;
Tree id;
if (gGlobal->gSymbolicBoxProperty->get(exp, result)) {
return result;
}
result = real_a2sb(exp);
if (result != exp && getDefNameProperty(exp, id)) {
// Propagate definition name property when needed
setDefNameProperty(result, id);
}
gGlobal->gSymbolicBoxProperty->set(exp, result);
return result;
}
static Tree real_a2sb(Tree exp)
{
Tree abstr, visited, unusedEnv, localValEnv, var, name, body;
if (isClosure(exp, abstr, unusedEnv, visited, localValEnv)) {
if (isBoxIdent(abstr)) {
// Special case introduced with access and components
Tree result = a2sb(eval(abstr, visited, localValEnv));
// Propagate definition name property when needed
if (getDefNameProperty(exp, name)) {
setDefNameProperty(result, name);
}
return result;
} else if (isBoxAbstr(abstr, var, body)) {
// Here we have remaining abstraction that we will try to
// transform in a symbolic box by applying it to a slot
Tree slot = boxSlot(++gGlobal->gBoxSlotNumber);
stringstream s;
s << boxpp(var);
setDefNameProperty(slot, s.str());
// Apply the abstraction to the slot
Tree result =
boxSymbolic(slot, a2sb(eval(body, visited, pushValueDef(var, slot, localValEnv))));
// Propagate definition name property when needed
if (getDefNameProperty(exp, name)) {
setDefNameProperty(result, name);
}
return result;
} else if (isBoxEnvironment(abstr)) {
return abstr;
} else {
evalerror(FAUSTfilename, -1,
"a2sb : internal error : not an abstraction inside closure (1)", exp);
// Never reached since evalerror throws an exception
return 0;
}
} else if (isBoxPatternMatcher(exp)) {
// Here we have remaining PM rules that we will try to
// transform in a symbolic box by applying it to a slot
Tree slot = boxSlot(++gGlobal->gBoxSlotNumber);
stringstream s;
s << "PM" << gGlobal->gBoxSlotNumber;
setDefNameProperty(slot, s.str());
// Apply the PM rules to the slot and transfoms the result in a symbolic box
Tree result = boxSymbolic(slot, a2sb(applyList(exp, cons(slot, gGlobal->nil))));
// Propagate definition name property when needed
if (getDefNameProperty(exp, name)) {
setDefNameProperty(result, name);
}
return result;
} else if (isBoxWaveform(exp)) {
// A waveform is always in Normal Form, nothing to evaluate
return exp;
} else {
// It is a constructor : transform each branches
unsigned int ar = exp->arity();
tvec B(ar);
bool modified = false;
for (unsigned int i = 0; i < ar; i++) {
Tree b = exp->branch(i);
Tree m = a2sb(b);
B[i] = m;
if (b != m) {
modified = true;
}
}
Tree r = (modified) ? tree(exp->node(), B) : exp;
return r;
}
}
/**
* Set the value of box in the environment env.
*
* @param box the block diagram we have evaluated
* @param env the evaluation environment
* @param value the evaluated block diagram
*/
static void setEvalProperty(Tree box, Tree env, Tree value)
{
setProperty(box, tree(gGlobal->EVALPROPERTY, env), value);
}
/**
* Retrieve the value of box in the environment env.
*
* @param box the expression we want to retrieve the value
* @param env the lexical environment
* @param value the returned value if any
* @return true if a value already exist
*/
static bool getEvalProperty(Tree box, Tree env, Tree& value)
{
return getProperty(box, tree(gGlobal->EVALPROPERTY, env), value);
}
/**
* Eval a block diagram expression.
*
* Wrap the realeval function in order to propagate the name property
* @param exp the expression to evaluate
* @param visited list of visited definitions to detect recursive definitions
* @param localValEnv the local environment
* @return a block diagram in normal form
*/
static Tree eval(Tree exp, Tree visited, Tree localValEnv)
{
Tree id;
Tree result;
// cerr << "eval : " << boxpp(exp) << " in env " << envpp(localValEnv) << endl;
if (!getEvalProperty(exp, localValEnv, result)) {
gGlobal->gLoopDetector.detect(cons(exp, localValEnv));
gGlobal->gStackOverflowDetector.detect();
result = realeval(exp, visited, localValEnv);
setEvalProperty(exp, localValEnv, result);
if (getDefNameProperty(exp, id)) {
setDefNameProperty(result, id); // propagate definition name property
}
}
return result;
}
/**
* Check numerical tuple.
*
* Check if a box is a parallel construction of numbers
* @param box the expression to analyse
* @param L the resulting flat list of numbers
* @return true if box is a parallel construction of numbers
*/
static bool isNumericalTuple(Tree box, siglist& L)
{
Tree l, r;
if (isBoxInt(box) || isBoxReal(box)) {
L.push_back(box);
return true;
} else if (isBoxPar(box, l, r) && isNumericalTuple(l, L)) {
return isNumericalTuple(r, L);
} else {
return false;
}
}
/**
* Eval a block diagram expression.
*
* Strict evaluation of a block diagram expression by applying beta reduction.
* @param exp the expression to evaluate
* @param visited list of visited definition to detect recursive definitions
* @param localValEnv the local environment
* @return a block diagram in normal form
*/
static Tree realeval(Tree exp, Tree visited, Tree localValEnv)
{
Tree fun;
Tree arg;
Tree var, num, chan, body, ldef, ins, outs, routes;
Tree label;
Tree cur, lo, hi, step;
Tree e1, e2, exp2, notused, visited2, lenv2;
Tree rules;
Tree id;
xtended* xt = (xtended*)getUserData(exp);
// cerr << "realeval : " << boxpp(exp) << " in env " << envpp(localValEnv) << endl;
// Constants
//-----------
if (xt || isBoxInt(exp) || isBoxReal(exp) || isBoxWire(exp) || isBoxCut(exp) ||
isBoxPrim0(exp) || isBoxPrim1(exp) || isBoxPrim2(exp) || isBoxPrim3(exp) ||
isBoxPrim4(exp) || isBoxPrim5(exp) || isBoxFFun(exp) || isBoxFConst(exp) ||
isBoxFVar(exp) || isBoxWaveform(exp)) {
return exp;
// Block-diagram constructors
//---------------------------
} else if (isBoxSeq(exp, e1, e2)) {
Tree a1 = eval(e1, visited, localValEnv);
Tree a2 = eval(e2, visited, localValEnv);
Tree re = boxSeq(a1, a2);
xtended* xxt = (xtended*)getUserData(a2);
siglist lsig;
// Try a numerical simplification of expressions of type 2,3:+
if (isNumericalTuple(a1, lsig) &&
(xxt || isBoxWire(a2) || isBoxPrim1(a2) || isBoxPrim2(a2))) {
// check that re is well typed before trying to simplify it
int n, m;
getBoxType(re, &n, &m);
Tree lres = boxPropagateSig(gGlobal->nil, a2, lsig);
if (isList(lres) && isNil(tl(lres))) {
Tree r = simplify(hd(lres));
if (isNum(r)) {
return r;
}
}
}
// No numerical simplification
return re;
} else if (isBoxPar(exp, e1, e2)) {
return boxPar(eval(e1, visited, localValEnv), eval(e2, visited, localValEnv));
} else if (isBoxRec(exp, e1, e2)) {
return boxRec(eval(e1, visited, localValEnv), eval(e2, visited, localValEnv));
} else if (isBoxSplit(exp, e1, e2)) {
return boxSplit(eval(e1, visited, localValEnv), eval(e2, visited, localValEnv));
} else if (isBoxMerge(exp, e1, e2)) {
return boxMerge(eval(e1, visited, localValEnv), eval(e2, visited, localValEnv));
// Modules
//--------
} else if (isBoxAccess(exp, body, var)) {
Tree val = eval(body, visited, localValEnv);
if (isClosure(val, exp2, notused, visited2, lenv2)) {
// It is a closure, we have an environment to access
return eval(closure(var, notused, visited2, lenv2), visited, localValEnv);
} else {
evalerror(getDefFileProp(exp), getDefLineProp(exp), "no environment to access", exp);
}
//////////////////////en chantier////////////////////////////
} else if (isBoxModifLocalDef(exp, body, ldef)) {
Tree val = eval(body, visited, localValEnv);
if (isClosure(val, exp2, notused, visited2, lenv2)) {
// We rebuild the closure using a copy of the original environment
// modified with some new definitions
Tree lenv3 = copyEnvReplaceDefs(lenv2, ldef, visited2, localValEnv);
return eval(closure(exp2, notused, visited2, lenv3), visited, localValEnv);
} else {
evalerror(getDefFileProp(exp), getDefLineProp(exp), "not a closure", val);
evalerror(getDefFileProp(exp), getDefLineProp(exp), "no environment to access", exp);
}
///////////////////////////////////////////////////////////////////
} else if (isBoxComponent(exp, label)) {
const char* fname = tree2str(label);
Tree eqlst = gGlobal->gReader.expandList(gGlobal->gReader.getList(fname));
Tree res = closure(boxIdent("process"), gGlobal->nil, gGlobal->nil,
pushMultiClosureDefs(eqlst, gGlobal->nil, gGlobal->nil));
setDefNameProperty(res, label);
return res;
} else if (isBoxLibrary(exp, label)) {
const char* fname = tree2str(label);
Tree eqlst = gGlobal->gReader.expandList(gGlobal->gReader.getList(fname));
Tree res = closure(boxEnvironment(), gGlobal->nil, gGlobal->nil,
pushMultiClosureDefs(eqlst, gGlobal->nil, gGlobal->nil));
setDefNameProperty(res, label);
return res;
// User interface elements
//------------------------
} else if (isBoxButton(exp, label)) {
const char* l1 = tree2str(label);
string s1 = evalLabel(l1, visited, localValEnv);
Tree l2 = tree(s1.c_str());
Tree w = boxButton(l2);
return w;
} else if (isBoxCheckbox(exp, label)) {
const char* l1 = tree2str(label);
string s1 = evalLabel(l1, visited, localValEnv);
Tree l2 = tree(s1.c_str());
Tree w = boxCheckbox(l2);
return w;
} else if (isBoxVSlider(exp, label, cur, lo, hi, step)) {
const char* l1 = tree2str(label);
string s1 = evalLabel(l1, visited, localValEnv);
Tree l2 = tree(s1.c_str());
Tree w = (boxVSlider(l2, tree(eval2double(cur, visited, localValEnv)),
tree(eval2double(lo, visited, localValEnv)),
tree(eval2double(hi, visited, localValEnv)),
tree(eval2double(step, visited, localValEnv))));
return w;
} else if (isBoxHSlider(exp, label, cur, lo, hi, step)) {
const char* l1 = tree2str(label);
string s1 = evalLabel(l1, visited, localValEnv);
Tree l2 = tree(s1.c_str());
Tree w = (boxHSlider(l2, tree(eval2double(cur, visited, localValEnv)),
tree(eval2double(lo, visited, localValEnv)),
tree(eval2double(hi, visited, localValEnv)),
tree(eval2double(step, visited, localValEnv))));
return w;
} else if (isBoxNumEntry(exp, label, cur, lo, hi, step)) {
const char* l1 = tree2str(label);
string s1 = evalLabel(l1, visited, localValEnv);
Tree l2 = tree(s1.c_str());
Tree w = (boxNumEntry(l2, tree(eval2double(cur, visited, localValEnv)),
tree(eval2double(lo, visited, localValEnv)),
tree(eval2double(hi, visited, localValEnv)),
tree(eval2double(step, visited, localValEnv))));
return w;
} else if (isBoxSoundfile(exp, label, chan)) {
const char* l1 = tree2str(label);
string l2 = evalLabel(l1, visited, localValEnv);
return boxSoundfile(tree(l2.c_str()), tree(eval2int(chan, visited, localValEnv)));
} else if (isBoxVGroup(exp, label, arg)) {
const char* l1 = tree2str(label);
string l2 = evalLabel(l1, visited, localValEnv);
return boxVGroup(tree(l2.c_str()), eval(arg, visited, localValEnv));
} else if (isBoxHGroup(exp, label, arg)) {
const char* l1 = tree2str(label);
string l2 = evalLabel(l1, visited, localValEnv);
return boxHGroup(tree(l2.c_str()), eval(arg, visited, localValEnv));
} else if (isBoxTGroup(exp, label, arg)) {
const char* l1 = tree2str(label);
string l2 = evalLabel(l1, visited, localValEnv);
return boxTGroup(tree(l2.c_str()), eval(arg, visited, localValEnv));
} else if (isBoxHBargraph(exp, label, lo, hi)) {
const char* l1 = tree2str(label);
string l2 = evalLabel(l1, visited, localValEnv);
return boxHBargraph(tree(l2.c_str()), tree(eval2double(lo, visited, localValEnv)),
tree(eval2double(hi, visited, localValEnv)));
} else if (isBoxMetadata(exp, e1, e2)) {
gGlobal->gMetaDataSet[hd(e2)].insert(tl(e2));
return eval(e1, visited, localValEnv);
} else if (isBoxVBargraph(exp, label, lo, hi)) {
const char* l1 = tree2str(label);
string l2 = evalLabel(l1, visited, localValEnv);
return boxVBargraph(tree(l2.c_str()), tree(eval2double(lo, visited, localValEnv)),
tree(eval2double(hi, visited, localValEnv)));
// Lambda calculus
//----------------
} else if (isBoxIdent(exp)) {
return evalIdDef(exp, visited, localValEnv);
} else if (isBoxWithLocalDef(exp, body, ldef)) {
Tree expandedldef = gGlobal->gReader.expandList(ldef);
return eval(body, visited, pushMultiClosureDefs(expandedldef, visited, localValEnv));
} else if (isBoxAppl(exp, fun, arg)) {
return applyList(eval(fun, visited, localValEnv), revEvalList(arg, visited, localValEnv));
} else if (isBoxAbstr(exp)) {
// it is an abstraction : return a closure
return closure(exp, gGlobal->nil, visited, localValEnv);
} else if (isBoxEnvironment(exp)) {
// environment : return also a closure
return closure(exp, gGlobal->nil, visited, localValEnv);
} else if (isClosure(exp, exp2, notused, visited2, lenv2)) {
if (isBoxAbstr(exp2)) {
// a 'real' closure
return closure(exp2, gGlobal->nil, setUnion(visited, visited2), lenv2);
} else if (isBoxEnvironment(exp2)) {
// a 'real' closure
return closure(exp2, gGlobal->nil, setUnion(visited, visited2), lenv2);
} else {
// it was a suspended evaluation
return eval(exp2, setUnion(visited, visited2), lenv2);
}
// Algorithmic constructions
//--------------------------
} else if (isBoxIPar(exp, var, num, body)) {
int n = eval2int(num, visited, localValEnv);
return iteratePar(var, n, body, visited, localValEnv);
} else if (isBoxISeq(exp, var, num, body)) {
int n = eval2int(num, visited, localValEnv);
return iterateSeq(var, n, body, visited, localValEnv);
} else if (isBoxISum(exp, var, num, body)) {
int n = eval2int(num, visited, localValEnv);
return iterateSum(var, n, body, visited, localValEnv);
} else if (isBoxIProd(exp, var, num, body)) {
int n = eval2int(num, visited, localValEnv);
return iterateProd(var, n, body, visited, localValEnv);
// Static
} else if (isBoxInputs(exp, body)) {
int ins1, outs1;
Tree b = a2sb(eval(body, visited, localValEnv));
if (getBoxType(b, &ins1, &outs1)) {
return boxInt(ins1);
} else {
stringstream error;
error << "ERROR : can't evaluate : " << *exp << endl;
throw faustexception(error.str());
}
} else if (isBoxOutputs(exp, body)) {
int ins1, outs1;
Tree b = a2sb(eval(body, visited, localValEnv));
if (getBoxType(b, &ins1, &outs1)) {
return boxInt(outs1);
} else {
stringstream error;
error << "ERROR : can't evaluate : " << *exp << endl;
throw faustexception(error.str());
}
} else if (isBoxSlot(exp)) {
return exp;
} else if (isBoxSymbolic(exp)) {
return exp;
// Pattern matching extension
//---------------------------
} else if (isBoxCase(exp, rules)) {
return evalCase(rules, localValEnv);
} else if (isBoxPatternVar(exp, id)) {
return exp;
// return evalIdDef(id, visited, localValEnv);
} else if (isBoxPatternMatcher(exp)) {
return exp;
} else if (isBoxRoute(exp, ins, outs, routes)) {
// Evaluate the route description
Tree v1 = a2sb(eval(ins, visited, localValEnv));
Tree v2 = a2sb(eval(outs, visited, localValEnv));
Tree vr = a2sb(eval(routes, visited, localValEnv));
// Check that we have constant numerical descriptions
int i1, o1, i2, o2, i3, o3;
getBoxType(v1, &i1, &o1);
getBoxType(v2, &i2, &o2);
getBoxType(vr, &i3, &o3);
if ((i1 == 0) & (o1 == 1) & (i2 == 0) & (o2 == 1) & (i3 == 0) & (o3 > 1) &
((o3 % 2) == 0)) {
// We are in good shape
Tree ls1 = boxPropagateSig(gGlobal->nil, v1, makeSigInputList(0));
Tree ls2 = boxPropagateSig(gGlobal->nil, v2, makeSigInputList(0));
Tree lsr = boxPropagateSig(gGlobal->nil, vr, makeSigInputList(0));
// All these lists should be list of constant numerical signals
// that we need to convert back to box expressions
vector<int> w1, w2, wr;
if (sigList2vecInt(ls1, w1) && sigList2vecInt(ls2, w2) && sigList2vecInt(lsr, wr)) {
// Convert wr into a parallel boxes of ints b
Tree b = boxInt(wr[o3 - 1]);
for (int j = o3 - 2; j >= 0; j--) {
b = boxPar(boxInt(wr[j]), b);
}
return boxRoute(boxInt(w1[0]), boxInt(w2[0]), b);
} else {
evalerror(getDefFileProp(exp), getDefLineProp(exp),
"invalid route expression, parameters should be numbers", exp);
}
} else {
evalerror(getDefFileProp(exp), getDefLineProp(exp),
"invalid route expression, first two parameters should be blocks producing a "
"value, third "
"parameter a list of input/output pairs",
exp);
}
} else if (isBoxModulation(exp, var, body)) {
// Evaluation of a box modulation expression. The body will be rewriten to introduce
// modulation circuits.
// Var is a pair (label, modulation circuit). The modulation circuit can be NIL if was not
// specified
faustassert(isList(var));
// Evaluate the label and the modulation circuit to be used
Tree ulabel = hd(var);
string s1 = evalLabel(tree2str(ulabel), visited, localValEnv);
Tree elabel = tree(s1.c_str());
Tree mcircuit = tl(var);
Tree emcircuit =
isNil(mcircuit) ? boxPrim2(sigMul) : a2sb(eval(mcircuit, visited, localValEnv));
// Check the number of inputs (<= 2) and outputs (1) of the modulation circuit
int inum, onum;
if (getBoxType(emcircuit, &inum, &onum)) {
if (inum > 2) {
evalerror(getDefFileProp(exp), getDefLineProp(exp),
"invalid modulation circuit, should have no more than 2 inputs", exp);
} else if (onum != 1) {
evalerror(getDefFileProp(exp), getDefLineProp(exp),
"invalid modulation circuit, it should have exactly 1 output", exp);
}
} else {
evalerror(getDefFileProp(exp), getDefLineProp(exp),
"invalid modulation circuit, should be a block diagram", exp);
}
// If needed, create a new slot named after the evaluated label
Tree slot;
if (inum == 2) {
// we need a slot for the modulation signal
slot = boxSlot(++gGlobal->gBoxSlotNumber);
stringstream s;
s << *elabel;
setDefNameProperty(slot, s.str());
} else {
// no need for a slot
slot = gGlobal->nil;
}
// Create a path from the evaluated label
Tree mpath = superNormalizePath(cons(elabel, gGlobal->nil));
// Fully evaluate the body we are going to rewrite
Tree ebody = a2sb(eval(body, visited, localValEnv));
// rewrite the body
BoxModulationImplanter bm(mpath, slot, inum, emcircuit);
// bm.trace(true);
Tree mbody = bm.self(ebody);
if (mbody == ebody) {
stringstream error;
error << "WARNING : no modulation of: '" << *elabel
<< "' took place in: " << boxpp(ebody) << endl;
gWarningMessages.push_back(error.str());
}
// if we have a slot, we need to wrap the modulation body in a symbolic box
if (inum == 2) {
return boxSymbolic(slot, mbody);
} else {
return mbody;
}
} else {
cerr << "ASSERT : eval doesn't intercept : " << *exp << endl;
faustassert(false);
}
return nullptr;
}
/* Deconstruct a (BDA) op pattern */
static inline bool isBoxPatternOp(Tree box, Node& n, Tree& t1, Tree& t2)
{
if (isBoxPar(box, t1, t2) || isBoxSeq(box, t1, t2) || isBoxSplit(box, t1, t2) ||
isBoxMerge(box, t1, t2) || isBoxRec(box, t1, t2)) {
n = box->node();
return true;
} else {
return false;
}
}
static void setNumericProperty(Tree t, Tree num)
{
setProperty(t, gGlobal->NUMERICPROPERTY, num);
}
static bool getNumericProperty(Tree t, Tree& num)
{
return getProperty(t, gGlobal->NUMERICPROPERTY, num);
}
static bool isBoxNumeric(Tree in, Tree& out)
{
int numInputs, numOutputs;
double x;
int i;
Tree v, abstr, genv, vis, lenv, var, body;
if (isBoxInt(in, &i) || isBoxReal(in, &x)) {
out = in;
return true;
} else if (isClosure(in, abstr, genv, vis, lenv) && isBoxAbstr(abstr, var, body)) {
return false;
} else {
v = a2sb(in);
if (getBoxType(v, &numInputs, &numOutputs) && (numInputs == 0) && (numOutputs == 1)) {
// Potential numerical expression
Tree lsignals = boxPropagateSig(gGlobal->nil, v, makeSigInputList(numInputs));
Tree res = simplify(hd(lsignals));
if (isSigReal(res, &x)) {
out = boxReal(x);
return true;
}
if (isSigInt(res, &i)) {
out = boxInt(i);
return true;
}
}
return false;
}
}
static Tree patternSimplification(Tree pattern)
{
Node n(0);
Tree v, t1, t2;
if (isBoxNumeric(pattern, v)) {
return v;
} else if (isBoxPatternOp(pattern, n, t1, t2)) {
return tree(n, patternSimplification(t1), patternSimplification(t2));
} else {
return pattern;
}
}
/**
* Eval a block diagram to a double.
*
* Eval a block diagram that represent a double constant. This function first eval
* a block diagram to its normal form, then check it represent a numerical value (a
* block diagram of type : 0->1) then do a symbolic propagation and try to convert the
* resulting signal to a double.
* @param exp the expression to evaluate
* @param globalDefEnv the global environment
* @param visited list of visited definition to detect recursive definitions
* @param localValEnv the local environment
* @return a block diagram in normal form
*/
static double eval2double(Tree exp, Tree visited, Tree localValEnv)
{
Tree diagram = a2sb(eval(exp, visited, localValEnv)); // For getBoxType
int numInputs, numOutputs;
getBoxType(diagram, &numInputs, &numOutputs);
if ((numInputs > 0) || (numOutputs != 1)) {
evalerror(FAUSTfilename, FAUSTlineno, "not a constant expression of type : (0->1)", exp);
// Never reached since evalerror throws an exception
return 1;
} else {
Tree lsignals = boxPropagateSig(gGlobal->nil, diagram, makeSigInputList(numInputs));
Tree val = simplify(hd(lsignals));
return tree2double(val);
}
}
/**
* Eval a block diagram to an int.
*
* Eval a block diagram that represent an integer constant. This function first eval
* a block diagram to its normal form, then check it represent a numerical value (a
* block diagram of type : 0->1) then do a symbolic propagation and try to convert the
* resulting signal to an int.
* @param exp the expression to evaluate
* @param globalDefEnv the global environment
* @param visited list of visited definition to detect recursive definitions
* @param localValEnv the local environment
* @return a block diagram in normal form
*/
static int eval2int(Tree exp, Tree visited, Tree localValEnv)
{
Tree diagram = a2sb(eval(exp, visited, localValEnv)); // pour getBoxType()
int numInputs, numOutputs;
getBoxType(diagram, &numInputs, &numOutputs);
if ((numInputs > 0) || (numOutputs != 1)) {
evalerror(FAUSTfilename, FAUSTlineno, "not a constant expression of type : (0->1)", exp);
// Never reached since evalerror throws an exception
return 1;
} else {
Tree lsignals = boxPropagateSig(gGlobal->nil, diagram, makeSigInputList(numInputs));
Tree val = simplify(hd(lsignals));
return tree2int(val);
}
}
static bool isDigitChar(char c)
{
return (c >= '0') & (c <= '9');
}
static bool isIdentChar(char c)
{
return ((c >= 'a') & (c <= 'z')) || ((c >= 'A') & (c <= 'Z')) || ((c >= '0') & (c <= '9')) ||
(c == '_');
}
static const char* Formats[] = {"%d", "%1d", "%2d", "%3d", "%4d"};
static void writeIdentValue(string& dst, const string& format, const string& ident, Tree visited,
Tree localValEnv)
{
int f = std::atoi(format.c_str());
int n = eval2int(boxIdent(ident.c_str()), visited, localValEnv);
int i = std::min(4, std::max(f, 0));
char val[256];
snprintf(val, 250, Formats[i], n);
dst += val;
}
/**
* evallabel replace "...%2i..." occurences in label with value of i
*/
static string evalLabel(const char* src, Tree visited, Tree localValEnv)
{
// cerr << "Eval Label : " << src;
int state = 0; // current state
string dst; // label once evaluated
string ident; // current identifier
string format; // current format
while (state != -1) {
if (state == 0) {
if (*src == 0) {
state = -1;
} else if (*src == '%') {
ident = "";
format = "";
state = 1;
src++;
} else {
dst += *src++;
state = 0;
}
} else if (state == 1) {
if (*src == 0) {
// End and no identifier, stops
dst += '%';
dst += format;
state = -1;
} else if (isDigitChar(*src)) {
format += *src++;
state = 1;
} else if (isIdentChar(*src)) {
ident += *src++;
state = 2;
} else if (*src == '{') {
src++;
state = 3;
} else {
// Punctuation character and no identifier, stops
dst += '%';
dst += format;
state = 0;
}
} else if (state == 2) {
if (isIdentChar(*src)) {
ident += *src++;
state = 2;
} else {
writeIdentValue(dst, format, ident, visited, localValEnv);
state = 0;
}
} else if (state == 3) {
if (isIdentChar(*src)) {
ident += *src++;
state = 3;
} else if (*src == '}') {
writeIdentValue(dst, format, ident, visited, localValEnv);
src++;
state = 0;
} else {
// End and no identifier, stops
dst += '%';
dst += format;
state = -1;
}
} else {
cerr << "ASSERT : evallabel, undefined state : " << state << endl;
faustassert(false);
}
}
return dst;
}
/**
* Iterate a parallel construction.
*
* Iterate a parallel construction such that :
* par(i,10,E) --> E(i<-0),(E(i<-1),...,E(i<-9))
* @param id the formal parameter of the iteration
* @param num the number of iterations
* @param body the body expression of the iteration
* @param visited list of visited definition to detect recursive definitions
* @param localValEnv the local environment
* @return a block diagram in normal form
*/
static Tree iteratePar(Tree id, int num, Tree body, Tree visited, Tree localValEnv)
{
if (num == 0) {
// zero iteration: return neutral circuit (0->0) for parallel composition
return boxRoute(boxInt(0), boxInt(0), boxPar(boxInt(0), boxInt(0)));
} else {
Tree res = eval(body, visited, pushValueDef(id, tree(num - 1), localValEnv));
for (int i = num - 2; i >= 0; i--) {
res = boxPar(eval(body, visited, pushValueDef(id, tree(i), localValEnv)), res);
}
return res;
}
}
/**
* @brief Compute the neutral element for sequential composition modeled after body.
*
* @param id
* @param body
* @param visited
* @param localValEnv
* @return a bus: _,...,_
*/
static Tree neutralExpSeq(Tree id, Tree body, Tree visited, Tree localValEnv)
{
// We need to find the number of inputs and outputs of body
// We eval body with binding id to 0
Tree res = a2sb(eval(body, visited, pushValueDef(id, tree(0), localValEnv)));
int ins, outs;
getBoxType(res, &ins, &outs);
if (ins != outs) {
stringstream error;
error << "ERROR in seq() expressions. The iterated function must have the same number of "
"inputs and outputs. "
"Here "
<< boxpp(res) << " has " << ins << " inputs and " << outs << " outputs" << endl;
throw faustexception(error.str());
} else if (outs > 0) {
Tree bus = boxWire();
for (int j = 1; j < outs; j++) {
bus = boxPar(bus, boxWire());
}
return bus;
} else {
return boxRoute(boxInt(0), boxInt(0), boxPar(boxInt(0), boxInt(0)));
}
}
/**
* Iterate a sequential construction.
*
* Iterate a sequential construction such that :
* seq(i,10,E) --> E(i<-0):(E(i<-1):...:E(i<-9))
* @param id the formal parameter of the iteration
* @param num the number of iterations
* @param body the body expression of the iteration
* @param visited list of visited definition to detect recursive definitions
* @param localValEnv the local environment
* @return a block diagram in normal form
*/
static Tree iterateSeq(Tree id, int num, Tree body, Tree visited, Tree localValEnv)
{
if (num == 0) {
Tree neutral = neutralExpSeq(id, body, visited, localValEnv);
return neutral;
} else {
Tree res = eval(body, visited, pushValueDef(id, tree(num - 1), localValEnv));
for (int i = num - 2; i >= 0; i--) {
res = boxSeq(eval(body, visited, pushValueDef(id, tree(i), localValEnv)), res);
}
return res;
}
}
/**
* Iterate an addition construction.
*
* Iterate an addition construction such that :
* par(i,10,E) --> E(i<-0)+E(i<-1)+...+E(i<-9)
* @param id the formal parameter of the iteration
* @param num the number of iterations
* @param body the body expression of the iteration
* @param visited list of visited definition to detect recursive definitions
* @param localValEnv the local environment
* @return a block diagram in normal form
*/
static Tree iterateSum(Tree id, int num, Tree body, Tree visited, Tree localValEnv)
{
if (num == 0) {
return boxRoute(boxInt(0), boxInt(0), boxPar(boxInt(0), boxInt(0)));
} else {
Tree res = eval(body, visited, pushValueDef(id, tree(0), localValEnv));
for (int i = 1; i < num; i++) {
res = boxSeq(boxPar(res, eval(body, visited, pushValueDef(id, tree(i), localValEnv))),
boxPrim2(sigAdd));
}
return res;
}
}
/**
* Iterate a product construction.
*
* Iterate a product construction such that :
* par(i,10,E) --> E(i<-0)*E(i<-1)*...*E(i<-9)
* @param id the formal parameter of the iteration
* @param num the number of iterations
* @param body the body expression of the iteration
* @param visited list of visited definition to detect recursive definitions
* @param localValEnv the local environment
* @return a block diagram in normal form
*/
static Tree iterateProd(Tree id, int num, Tree body, Tree visited, Tree localValEnv)
{
if (num == 0) {
return boxRoute(boxInt(0), boxInt(0), boxPar(boxInt(0), boxInt(0)));
} else {
Tree res = eval(body, visited, pushValueDef(id, tree(0), localValEnv));
for (int i = 1; i < num; i++) {
res = boxSeq(boxPar(res, eval(body, visited, pushValueDef(id, tree(i), localValEnv))),
boxPrim2(sigMul));
}
return res;
}
}
/**
* Compute the sum of outputs of a list of boxes. The sum is
* valid if all the boxes have a valid boxType.
*
* @param boxlist the list of boxes
* @param outputs sum of outputs of the boxes
* @return true if outputs is valid, false otherwise
*/
#if 1
static bool boxlistOutputs(Tree boxlist, int* outputs)
{
int ins, outs;
*outputs = 0;
while (!isNil(boxlist)) {
Tree b = a2sb(hd(boxlist)); // For getBoxType, suppose list of evaluated boxes
if (getBoxType(b, &ins, &outs)) {
*outputs += outs;
} else {
// Arbitrary output arity set to 1
// when can't be determined
*outputs += 1;
}
boxlist = tl(boxlist);
}
return isNil(boxlist);
}
#else
static bool boxlistOutputs(Tree boxlist, int* outputs)
{
int ins, outs;
*outputs = 0;
while (!isNil(boxlist) && getBoxType(hd(boxlist), &ins, &outs)) {
*outputs += outs;
boxlist = tl(boxlist);
}
return isNil(boxlist);
}
#endif
/**
* Repeat a wire n times.
*/
static Tree nwires(int n)
{
Tree l = gGlobal->nil;
while (n--) {
l = cons(boxWire(), l);
}
return l;
}
/**
* Apply a function to a list of arguments.
* Apply a function F to a list of arguments (a,b,c,...).
* F can be either a closure over an abstraction, or a
* pattern matcher. If it is not the case then we have :
* F(a,b,c,...) ==> (a,b,c,...):F
*
* @param fun the function to apply
* @param larg the list of arguments
* @return the resulting expression in normal form
*/
static Tree applyList(Tree fun, Tree larg)
{
Tree abstr;
Tree globalDefEnv;
Tree visited;
Tree localValEnv;
Tree envList;
Tree originalRules;
Tree revParamList;
Tree id;
Tree body;
PM::Automaton* automat;
int state;
prim2 p2;
if (isNil(larg)) {
return fun;
}
if (isBoxError(fun) || isBoxError(larg)) {
return boxError();
}
if (isBoxPatternMatcher(fun, automat, state, envList, originalRules, revParamList)) {
Tree result;
int state2;
vector<Tree> envVect;
list2vec(envList, envVect);
state2 = PM::apply_pattern_matcher(automat, state, hd(larg), result, envVect);
if (state2 >= 0 && isNil(result)) {
// We need to continue the pattern matching
return applyList(boxPatternMatcher(automat, state2, vec2list(envVect), originalRules,
cons(hd(larg), revParamList)),
tl(larg));
} else if (state2 < 0) {
stringstream error;
error << "ERROR : pattern matching failed, no rule of " << boxpp(boxCase(originalRules))
<< " matches argument list " << boxpp(reverse(cons(hd(larg), revParamList)))
<< endl;
throw faustexception(error.str());
} else {
// Pattern Matching was succesful
// the result is a closure that we need to evaluate.
if (isClosure(result, body, globalDefEnv, visited, localValEnv)) {
// why ??? return simplifyPattern(eval(body, nil, localValEnv));
// return eval(body, nil, localValEnv);
return applyList(eval(body, gGlobal->nil, localValEnv), tl(larg));
} else {
cerr << "ERROR : wrong result from pattern matching (not a closure) : "
<< boxpp(result) << endl;
return boxError();
}
}
}
if (!isClosure(fun, abstr, globalDefEnv, visited, localValEnv)) {
// principle : f(a,b,c,...) ==> (a,b,c,...):f
int ins, outs;
// Check arity of function
Tree efun = a2sb(fun);
if (!getBoxType(efun, &ins, &outs)) { // we leave it that way for now
// We can't determine the input arity of the expression,
// hope for the best
return boxSeq(larg2par(larg), fun);
}
// Check arity of arg list
if (!boxlistOutputs(larg, &outs)) {
// We don't know yet the output arity of larg. Therefore we can't
// do any arity checking nor add _ to reach the required number of arguments
return boxSeq(larg2par(larg), fun);
}
if (outs > ins) {
stringstream error;
error << "ERROR : too much arguments : " << outs << ", instead of : " << ins << endl;
error << "when applying : " << boxpp(fun) << endl << "to : " << boxpp(larg) << endl;
throw faustexception(error.str());
}
if ((outs == 1) && ((isBoxPrim2(fun, &p2) && (p2 != sigPrefix)) ||
(getUserData(fun) && ((xtended*)getUserData(fun))->isSpecialInfix()))) {
// special case : /(3) ==> _,3 : /
Tree larg2 = concat(nwires(ins - outs), larg);
return boxSeq(larg2par(larg2), fun);
} else {
Tree larg2 = concat(larg, nwires(ins - outs));
return boxSeq(larg2par(larg2), fun);
}
}
// Here fun is a closure, we can test the content of abstr
if (isBoxEnvironment(abstr)) {
evalerrorbox(FAUSTfilename, -1, "an environment can't be used as a function", fun);
}
if (isBoxIdent(abstr)) {
// We have an unevaluated Ident inside a closure
Tree fff = eval(abstr, visited, localValEnv);
return applyList(fff, larg);
}
if (!isBoxAbstr(abstr, id, body)) {
evalerror(FAUSTfilename, -1, "(internal) not an abstraction inside closure (2)", fun);
}
// Here abstr is an abstraction, we can test the content of abstr.
// Try to synthetise a name from the function name and the argument name
{
Tree arg = eval(hd(larg), visited, localValEnv);
Tree narg;
if (isBoxNumeric(arg, narg)) {
arg = narg;
}
Tree f = eval(body, visited, pushValueDef(id, arg, localValEnv));
Tree fname;
if (getDefNameProperty(fun, fname)) {
stringstream s;
s << tree2str(fname);
if (!gGlobal->gSimpleNames) {
s << "(" << boxpp(arg) << ")";
}
setDefNameProperty(f, s.str());
}
return applyList(f, tl(larg));
}
}
/**
* Eval a list of expressions returning the list of results in reverse order.
*
* @param lexp list of expressions to evaluate
* @param globalDefEnv the global environment
* @param visited list of visited definition to detect recursive definitions
* @param localValEnv the local environment
* @return list of evaluated expressions in reverse order
*/
static Tree revEvalList(Tree lexp, Tree visited, Tree localValEnv)
{
Tree result = gGlobal->nil;
while (!isNil(lexp)) {
result = cons(eval(hd(lexp), visited, localValEnv), result);
lexp = tl(lexp);
}
return result;
}
/**
* Transform a list of expressions in a parallel construction.
*
* @param larg list of expressions
* @return parallel construction
*/
static Tree larg2par(Tree larg)
{
if (isNil(larg)) {
evalerror(FAUSTfilename, -1, "empty list of arguments", larg);
}
if (isNil(tl(larg))) {
return hd(larg);
}
return boxPar(hd(larg), larg2par(tl(larg)));
}
/**
* Search the environment for the definition of a symbol
* ID and evaluate it. Detects recursive definitions using
* a set of visited IDxENV. Associates the symbol as a definition name
* property of the definition.
*
* @param id the symbol ID to search
* @param visited set of visited symbols (used for recursive definition detection)
* @param lenv the environment where to search
* @return the evaluated definition of ID
*/
static Tree evalIdDef(Tree id, Tree visited, Tree lenv)
{
Tree def = nullptr;
Tree name = nullptr;
// Search the environment env for a definition of symbol id
while (!isNil(lenv) && !getProperty(lenv, id, def)) {
faustassert(lenv->arity() > 0);
lenv = lenv->branch(0);
}
// Check that the definition exists
if (isNil(lenv)) {
if (hasDefProp(id)) {
stringstream error;
error << "ERROR : " << *id << " is defined here : " << getDefFileProp(id) << ":"
<< getDefLineProp(id) << endl;
throw faustexception(error.str());
} else {
evalerror(getUseFileProp(id), getUseLineProp(id), "undefined symbol", id);
}
}
// Check that it is not a recursive definition
Tree p = cons(id, lenv);
// Set the definition name property
faustassert(def);
if (!getDefNameProperty(def, name)) {
// iIf the definition has no name use the identifier
stringstream s;
s << boxpp(id);
// XXXXXX setDefNameProperty(def, s.str());
}
// Return the evaluated definition
return eval(def, addElement(p, visited), gGlobal->nil);
}
/**
* Creates a list of n elements.
*
* @param n number of elements
* @param e element to be repeated
* @return [e e e ...] n times
*/
static Tree listn(int n, Tree e)
{
return (n <= 0) ? gGlobal->nil : cons(e, listn(n - 1, e));
}
/**
* A property to store the pattern matcher corresponding to a set of rules
* in a specific environement
*/
static void setPMProperty(Tree t, Tree env, Tree pm)
{
setProperty(t, tree(gGlobal->PMPROPERTYNODE, env), pm);
}
static bool getPMProperty(Tree t, Tree env, Tree& pm)
{
return getProperty(t, tree(gGlobal->PMPROPERTYNODE, env), pm);
}
/**
* Eval a case expression containing a list of pattern matching rules.
* Creates a boxPatternMatcher containing a pm autamaton a state
* and a list of environments.
*
* @param rules the list of rules
* @param env the environment uused to evaluate the patterns and closure the rhs
* @return a boxPatternMatcher ready to be applied
*/
static Tree evalCase(Tree rules, Tree env)
{
Tree pm;
if (!getPMProperty(rules, env, pm)) {
PM::Automaton* a = PM::make_pattern_matcher(evalRuleList(rules, env));
pm = boxPatternMatcher(a, 0, listn(len(rules), pushEnvBarrier(env)), rules, gGlobal->nil);
setPMProperty(rules, env, pm);
}
return pm;
}
/**
* Evaluates each rule of the list.
*/
static Tree evalRuleList(Tree rules, Tree env)
{
if (isNil(rules)) {
return gGlobal->nil;
} else {
return cons(evalRule(hd(rules), env), evalRuleList(tl(rules), env));
}
}
/**
* Evaluates the list of patterns and closure the rhs.
*/
static Tree evalRule(Tree rule, Tree env)
{
return cons(evalPatternList(left(rule), env), right(rule));
}
/**
* Evaluates each pattern of the list.
*/
static Tree evalPatternList(Tree patterns, Tree env)
{
if (isNil(patterns)) {
return gGlobal->nil;
} else {
return cons(evalPattern(hd(patterns), env), evalPatternList(tl(patterns), env));
}
}
/**
* Evaluates a pattern and simplify it to numerical value
* if possible.
*/
static Tree evalPattern(Tree pattern, Tree env)
{
Tree p = eval(pattern, gGlobal->nil, env);
return patternSimplification(p);
}
static void list2vec(Tree l, vector<Tree>& v)
{
while (!isNil(l)) {
v.push_back(hd(l));
l = tl(l);
}
}
static Tree vec2list(const vector<Tree>& v)
{
Tree l = gGlobal->nil;
int n = (int)v.size();
while (n--) {
l = cons(v[n], l);
}
return l;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Further simplification : replace bloc-diagrams that denote constant number by this number
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static Tree numericBoxSimplification(Tree box);
static Tree insideBoxSimplification(Tree box);
/**
* boxSimplification(box) : simplify a block-diagram by replacing expressions
* denoting a constant number by this number.
*/
static Tree boxSimplification(Tree box)
{
Tree simplified;
if (gGlobal->gSimplifiedBoxProperty->get(box, simplified)) {
return simplified;
} else {
simplified = numericBoxSimplification(box);
// Transfers name property if any
Tree name;
if (getDefNameProperty(box, name)) {
setDefNameProperty(simplified, name);
}
// Attach simplified expression as a property of original box
gGlobal->gSimplifiedBoxProperty->set(box, simplified);
return simplified;
}
}
/**
* Try to do a numeric simplification of a block-diagram.
*/
static Tree numericBoxSimplification(Tree box)
{
int ins, outs;
Tree result;
int i;
double x;
if (!getBoxType(box, &ins, &outs)) {
stringstream error;
error << "ERROR : file " << __FILE__ << ':' << __LINE__
<< ", can't compute the box type of : ";
error << *box << endl;
throw faustexception(error.str());
}
if (ins == 0 && outs == 1) {
// This box can potentially denote a number
if (isBoxInt(box, &i) || isBoxReal(box, &x)) {
result = box;
} else {
// Propagate signals to discover if it simplifies to a number
int i1;
double x1;
Tree lsignals = boxPropagateSig(gGlobal->nil, box, makeSigInputList(0));
Tree s = simplify(hd(lsignals));
if (isSigReal(s, &x1)) {
result = boxReal(x1);
} else if (isSigInt(s, &i1)) {
result = boxInt(i1);
} else {
result = insideBoxSimplification(box);
}
}
} else {
// This box can't denote a number
result = insideBoxSimplification(box);
}
return result;
}
/**
* Simplify inside a block-diagram : S[A*B] => S[A]*S[B]
*/
static Tree insideBoxSimplification(Tree box)
{
int i;
double r;
prim0 p0;
prim1 p1;
prim2 p2;
prim3 p3;
prim4 p4;
prim5 p5;
Tree t1, t2, ff, label, cur, min, max, step, type, name, file, slot, body;
Tree ins, outs, routes;
xtended* xt = (xtended*)getUserData(box);
// Extended Primitives
if (xt) {
return box;
}
// Numbers and Constants
else if (isBoxInt(box, &i)) {
return box;
} else if (isBoxReal(box, &r)) {
return box;
}
else if (isBoxFConst(box, type, name, file)) {
return box;
}
else if (isBoxFVar(box, type, name, file)) {
return box;
}
// Wire and Cut
else if (isBoxCut(box)) {
return box;
}
else if (isBoxWire(box)) {
return box;
}
// Primitives
else if (isBoxPrim0(box, &p0)) {
return box;
}
else if (isBoxPrim1(box, &p1)) {
return box;
}
else if (isBoxPrim2(box, &p2)) {
return box;
}
else if (isBoxPrim3(box, &p3)) {
return box;
}
else if (isBoxPrim4(box, &p4)) {
return box;
}
else if (isBoxPrim5(box, &p5)) {
return box;
}
else if (isBoxFFun(box, ff)) {
return box;
}
// User Interface Widgets
else if (isBoxButton(box, label)) {
return box;
}
else if (isBoxCheckbox(box, label)) {
return box;
}
else if (isBoxVSlider(box, label, cur, min, max, step)) {
return box;
}
else if (isBoxHSlider(box, label, cur, min, max, step)) {
return box;
}
else if (isBoxNumEntry(box, label, cur, min, max, step)) {
return box;
}
else if (isBoxVBargraph(box, label, min, max)) {
return box;
}
else if (isBoxHBargraph(box, label, min, max)) {
return box;
}
// User Interface Groups
else if (isBoxVGroup(box, label, t1)) {
return boxVGroup(label, boxSimplification(t1));
}
else if (isBoxHGroup(box, label, t1)) {
return boxHGroup(label, boxSimplification(t1));
}
else if (isBoxTGroup(box, label, t1)) {
return boxTGroup(label, boxSimplification(t1));
}
// Slots and Symbolic Boxes
else if (isBoxSlot(box)) {
return box;
}
else if (isBoxSymbolic(box, slot, body)) {
Tree b = boxSimplification(body);
return boxSymbolic(slot, b);
}
// Block Diagram Composition Algebra
else if (isBoxSeq(box, t1, t2)) {
Tree s1 = boxSimplification(t1);
Tree s2 = boxSimplification(t2);
return boxSeq(s1, s2);
}
else if (isBoxPar(box, t1, t2)) {
Tree s1 = boxSimplification(t1);
Tree s2 = boxSimplification(t2);
return boxPar(s1, s2);
}
else if (isBoxSplit(box, t1, t2)) {
Tree s1 = boxSimplification(t1);
Tree s2 = boxSimplification(t2);
return boxSplit(s1, s2);
}
else if (isBoxMerge(box, t1, t2)) {
Tree s1 = boxSimplification(t1);
Tree s2 = boxSimplification(t2);
return boxMerge(s1, s2);
}
else if (isBoxRec(box, t1, t2)) {
Tree s1 = boxSimplification(t1);
Tree s2 = boxSimplification(t2);
return boxRec(s1, s2);
}
else if (isBoxMetadata(box, t1, t2)) {
Tree s1 = boxSimplification(t1);
return boxMetadata(s1, t2);
}
else if (isBoxWaveform(box)) {
// A waveform is always in Normal Form, nothing to simplify
return box;
}
else if (isBoxRoute(box, ins, outs, routes)) {
Tree s1 = boxSimplification(ins);
Tree s2 = boxSimplification(outs);
return boxRoute(s1, s2, routes);
}
cerr << "ASSERT : in file " << __FILE__ << ':' << __LINE__
<< ", unrecognised box expression : " << *box << endl;
faustassert(false);
return nullptr;
}
|