1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
|
# Classes for data expressions and sort expressions, used for code generation
# Basically reflects the parse tree of a specification, but allows for more
# flexibility.
import string
from string import Template
import copy
import re
import logging
# The following lists identifiers that must be
# escaped by a namespace when the corresponding function
# is called, to work around a bug in GCC 4.4
IDS_WITH_NAMESPACE = ['set_comprehension', 'bag_comprehension']
LOG = logging.getLogger("data")
# Uncomment to add lots of debug information
#LOG.setLevel(logging.DEBUG)
# create console handler and set level to debug
CH = logging.StreamHandler()
LOG.addHandler(CH)
def indent(n):
result = ""
i = 0
while i < n:
result += " "
i += 1
return result
def add_namespace(function_symbol_name, function_symbol_namespace, other_namespace = "undefined"):
global IDS_WITH_NAMESPACE
if (function_symbol_namespace == other_namespace or is_standard_function(function_symbol_name) or function_symbol_namespace == "undefined") and not (str(function_symbol_name) in IDS_WITH_NAMESPACE):
return function_symbol_name
else:
return "sort_%s::%s" % (function_symbol_namespace, function_symbol_name)
# Remove trailing _ from a string
def remove_underscore(s):
return s[:-1] if s.endswith('_') else s
# Escape an initial @ sign is present. Needed for doxygen code extraction
def escape(x):
x = str(x)
return '\\' + x if x.startswith('@') else x
def fcode(x, spec):
if isinstance(x, str):
return x
else:
return x.code(spec)
# Merge elements of a list of lists into a single list.
# Elements in the result are sorted and unique.
def merge(args):
result = []
for arg in args:
for x in arg:
if x not in result:
result.append(x)
return sorted(result)
# The elements of l1 that do not occur in l2
def difference(l1, l2):
result = []
for x in l1:
if x not in l2:
result.append(x)
return result
def is_standard_function(s):
return str(s) in ["equal_to", "not_equal_to", "if_", "less", "less_equal", "greater", "greater_equal"]
def target_sort(sort_expr):
if isinstance(sort_expr, sort_arrow):
return sort_expr.codomain
else:
return sort_expr
class identifier():
def __init__(self, string):
self.string = string
def __str__(self):
return self.string
def __eq__(self, other):
if hasattr(other, "string"):
return self.string == other.string
else:
return self.string == other
def __hash__(self):
return hash(self.string)
class label():
def __init__(self, id):
assert(isinstance(id, identifier))
self.identifier = id
def __str__(self):
return self.identifier.__str__()
def __eq__(self, other):
if hasattr(other, "identifier"):
return self.identifier == other.identifier
else:
return NotImplemented
def __hash__(self):
return hash(self.identifier)
class variable_declaration():
def __init__(self, id, sort_expr):
assert(isinstance(id, identifier))
assert(isinstance(sort_expr, sort_expression))
self.id = id
self.sort_expression = sort_expr
def __str__(self):
return "{0} : {1}".format(self.id, self.sort_expression)
def __eq__(self, other):
if hasattr(other, "id") and hasattr(other, "sort_expression"):
return self.id == other.id and self.sort_expression == other.sort_expression
else:
return NotImplemented
def sort_parameters(self, spec):
return self.sort_expression.sort_parameters(spec)
def code(self, spec):
return " variable v{0}(\"{0}\",{1})".format(self.id, self.sort_expression.code(spec))
class variable_declaration_list():
def __init__(self, elements):
assert(all(map(lambda x: isinstance(x, variable_declaration), elements)))
self.elements = elements
def __str__(self):
return ", ".join([str(e) for e in self.elements])
def __eq__(self, other):
if hasattr(other, "elements"):
return self.elements == other.elements
else:
return NotImplemented
def push_back(self, element):
if element not in self.elements:
self.elements.append(element)
def find_variable(self, variable):
for e in self.elements:
if e.id == variable.id:
return e
def code(self, spec):
return "".join(["{0};\n".format(e.code(spec)) for e in self.elements])
class function_declaration():
def __init__(self, id, sort_expr, l):
assert(isinstance(id, identifier))
assert(isinstance(sort_expr, sort_expression))
assert(isinstance(l, label))
self.id = id
self.sort_expression = sort_expr
self.label = l
self.namespace = ""
self.original_namespace = ""
def __eq__(self, other):
if hasattr(other, "id") and hasattr(other, "label") and hasattr(other, "namespace") and hasattr(other, "original_namespace"):
return self.id == other.id and self.label == other.label and self.namespace == other.namespace and self.original_namespace == other.original_namespace
else:
return NotImplemented
def set_namespace(self, string):
self.namespace = string
if self.original_namespace == "":
self.original_namespace = string
def projection_arguments(self, projection_arguments):
if isinstance(self.sort_expression, sort_arrow):
arguments = self.sort_expression.domain.projection_arguments()
for (a, idx) in arguments:
value = (self.id, self.label, idx)
try:
projection_arguments[a].append(value)
except:
projection_arguments[a] = [value]
return projection_arguments
def sort_parameters(self, spec):
return self.sort_expression.sort_parameters(spec)
def __str__(self):
return "{0} : {1}".format(self.id, self.sort_expression)
def generator_code(self, spec, function_declarations):
if self.namespace <> self.original_namespace or self.namespace <> function_declarations.namespace or self.namespace <> spec.namespace:
return ""
sort_params = self.sort_expression.sort_parameters(spec)
# Determine whether this function is overloaded
functions = filter(lambda x: (x.id == self.id) and (x.label == self.label) and (x.namespace == self.namespace), function_declarations.elements)
extra_parameters = []
if len(functions) > 1:
try:
extra_parameters.append(self.sort_expression.domain.code(spec))
except:
pass # in case sort_expression has no domain
return " result.push_back({0}({1}));\n".format(add_namespace(self.label, self.namespace), ", ".join([s.code(spec) for s in sort_params] + extra_parameters))
class function_declaration_list():
def __init__(self, elements):
assert(all(map(lambda x: isinstance(x, function_declaration), elements)))
self.elements = elements
self.namespace = ""
self.original_namespace = ""
def __eq__(self, other):
return self.elements == other.elements and self.namespace == other.namespace and self.original_namespace == other.original_namespace
def empty(self):
return len(self.elements) == 0
def push_back(self, element):
assert(isinstance(element, function_declaration))
self.elements += [element]
def set_namespace(self, string):
self.namespace = string
if self.original_namespace == "":
self.original_namespace = string
for e in self.elements:
e.set_namespace(string)
def sort_parameters(self, spec):
return merge([e.sort_parameters(spec) for e in self.elements])
def find_function(self, function, argumentcount):
if function.id == "==":
return function_declaration(function.id, sort_expression(), label(identifier("equal_to")))
elif function.id == "!=":
return function_declaration(function.id, sort_expression(), label(identifier("not_equal_to")))
elif function.id == "if":
return function_declaration(function.id, sort_expression(), label(identifier("if_")))
elif function.id == "<":
return function_declaration(function.id, sort_expression(), label(identifier("less")))
elif function.id == ">":
return function_declaration(function.id, sort_expression(), label(identifier("greater")))
elif function.id == "<=":
return function_declaration(function.id, sort_expression(), label(identifier("less_equal")))
elif function.id == ">=":
return function_declaration(function.id, sort_expression(), label(identifier("greater_equal")))
else:
for e in self.elements:
if e.id == function.id:
if argumentcount == -1:
return e
elif argumentcount == 0 and not isinstance(e.sort_expression, sort_arrow):
return e
elif isinstance(e.sort_expression, sort_arrow) and len(e.sort_expression.domain.elements) == argumentcount:
return e
return None
def merge_declarations(self, declarations, is_supertype):
for d in declarations.elements:
if is_supertype and any(map(lambda x: (d.id == x.id) and (d.label == x.label) and (x.namespace == self.namespace), self.elements)):
d.set_namespace(self.namespace)
self.elements.append(d)
def __str__(self):
return "".join(["{0}\n".format(e) for e in self.elements])
def generator_code(self, spec, is_constructor_declaration = False):
code = ""
for e in self.elements:
if not is_constructor_declaration or not spec.sort_specification.is_alias(target_sort(e.sort_expression)):
code += e.generator_code(spec, self)
return code
def projection_code(self, spec):
projection_arguments = {}
for e in self.elements:
if (e.namespace == e.original_namespace and e.namespace == spec.namespace):
projection_arguments = e.projection_arguments(projection_arguments)
code = []
for p in projection_arguments:
# Filter duplicates
case_arguments = projection_arguments[p]
unique_case_arguments = []
for (id, label, idx) in case_arguments:
if not (id, label, idx) in unique_case_arguments:
unique_case_arguments.append((id, label, idx))
assertions = ["is_{0}_application(e)".format(label) for (id, label, idx) in unique_case_arguments]
# First group by index
index_table = {}
for (id, label, idx) in unique_case_arguments:
try:
index_table[idx].append((id, label))
except:
index_table[idx] = [(id, label)]
# If all projection project out the argument with the same index, do not generate a condition;
# otherwise we generate an if statement for the different cases.
projection = []
if len(index_table) == 1:
projection.append("return atermpp::aterm_cast<const application >(e)[{0}];".format(index_table.keys()[0]))
else:
projection_case = ''' if ({0})
{
return atermpp::aterm_cast<const application >(e)[%s];\n" % (i)
}'''.format(" || ".join(["is_{0}_application(e)".format(c[1] for c in index_table[i])]), i)
projection.append(projection_case)
function = ''' ///\\brief Function for projecting out argument
/// {0} from an application
/// \\param e A data expression
/// \\pre {0} is defined for e
/// \\return The argument of e that corresponds to {0}
inline
data_expression {1}(const data_expression& e)
{{
assert({2});
{3}
}}'''.format(escape(p), p, " || ".join(assertions), "\n".join(projection))
code.append(function)
return "{0}\n\n".format("\n\n".join(code))
def code(self, spec, is_constructor_declaration = False):
assert(isinstance(spec, specification))
# First we merge function declarations with the same function symbol/label,
# hence we get function symbols with multiple sorts. Then for each of these
# we generate the code
class multi_function_declaration():
def __init__(self, id, namespace, sort_expressions, l):
assert(isinstance(id, identifier))
assert(isinstance(sort_expressions, sort_expression_list))
assert(isinstance(l, label))
self.id = id
self.namespace = namespace
self.sort_expression_list = sort_expressions
self.label = l
def __eq__(self, other):
return self.id == other.id and self.namespace == other.namespace and self.sort_expression_list == other.sort_expression_list and self.label == other.label
def __str__(self):
return "{0} : { {1} }".format(self.id, self.sort_expression_list)
def function_name(self, fullname, name):
CODE_TEMPLATE = Template('''
/// \\brief Generate identifier ${namestring}
/// \\return Identifier ${namestring}
inline
core::identifier_string const& ${namecode}_name()
{
static core::identifier_string ${namecode}_name = core::identifier_string("${name}");
return ${namecode}_name;
}
''')
return CODE_TEMPLATE.substitute(namestring=escape(fullname), namecode=name, name=fullname)
def function_constructor(self, fullname, name, sortparams, sort):
CODE_TEMPLATE = Template('''
/// \\brief Constructor for function symbol ${namestring}
${sortparameterstring}
/// \\return Function symbol ${functionname}
inline
function_symbol ${const}${functionname}(${sortparameters})
{
${static}function_symbol ${functionname}(${functionname}_name(), ${sortname});
return ${functionname};
}
''')
return self.function_name(fullname, name) + CODE_TEMPLATE.substitute(
namestring=escape(fullname),
sortparameterstring='\n '.join(map(lambda x: '/// \\param {0} A sort expression'.format(escape(x.code())), sortparams)),
functionname=escape(name),
sortparameters = ', '.join(map(lambda x: 'const sort_expression& {0}'.format(x.code()), sortparams)),
sortname=sort,
const='const& ' if sortparams == [] else '',
static='static ' if sortparams == [] else '')
def polymorphic_function_constructor(self, fullname, name, sortparams):
CODE_TEMPLATE = Template('''
///\\brief Constructor for function symbol ${namestring}
${sortparameterstring}
///\\return Function symbol ${functionname}
inline
function_symbol ${functionname}(${parameters})
{
${targetsort}
function_symbol ${functionname}(${functionname}_name(), ${sortname});
return ${functionname};
}
''')
# There is polymorphism at play, hence we need to compute the
# allowed sorts first.
# That is, we have parameters for all domain elements
# based on those, we compute the corresponding target sort
domain_sort_ids = [sort_identifier(identifier("s%s" % i)) for i in range(len(self.sort_expression_list.elements[0].domain.elements))]
target_sort_id = sort_identifier(identifier("target_sort"))
new_sort = sort_arrow(domain(False, domain_sort_ids), target_sort_id)
# If all codomains are equal we have a unique target sort, otherwise
# this is detemined from the domain_sort_ids
first_codomain = self.sort_expression_list.elements[0].codomain
simple = False
if all(map(lambda x: x.codomain == first_codomain, self.sort_expression_list.elements)):
target_sort = 'sort_expression {0}({1});'.format(target_sort_id, first_codomain.code(spec))
simple = True
else:
CASE_TEMPLATE = Template(''' ${elsestr}if (${condition})
{
target_sort = ${sort};
}''')
TARGET_SORT_TEMPLATE = Template('''sort_expression ${target_sort_id};
${cases}
else
{
throw mcrl2::runtime_error("cannot compute target sort for ${functionname} with domain sorts \" + ${sortmsg});
}
''')
cases = []
for (i,sort) in enumerate(self.sort_expression_list.elements):
cases.append(CASE_TEMPLATE.substitute(
elsestr = '' if i == 0 else 'else ',
condition = ' && '.join(map(lambda (j,domsort): '{0} == {1}'.format(domain_sort_ids[j].code(spec), domsort.code(spec)), enumerate(sort.domain.sorts()))),
sort = sort.codomain.code(spec)
))
target_sort = TARGET_SORT_TEMPLATE.substitute(
target_sort_id = target_sort_id.code(spec),
cases = '\n'.join(cases),
functionname = self.label,
sortmsg = " + \", \" + ".join(['to_string({0})'.format(domain_sort_ids[j].code(spec)) for j in range(len(sort.domain.elements))])
)
parameters = []
if domain_sort_ids != [] and sortparams != [] and simple:
parameters += map(lambda x: 'const sort_expression& ', sortparams)
else:
parameters += map(lambda x: 'const sort_expression& {0}'.format(x.code()), sortparams)
parameters += map(lambda x: 'const sort_expression& {0}'.format(x.code()), domain_sort_ids)
return self.function_name(fullname, name) + CODE_TEMPLATE.substitute(
namestring = escape(fullname),
sortparameterstring = '\n '.join(map(lambda x: '/// \\param {0} A sort expression'.format(escape(x.code())), sortparams + domain_sort_ids)),
functionname = name,
parameters = ', '.join(parameters),
targetsort = target_sort,
sortname = new_sort.code(spec)
)
def function_recogniser(self, fullname, name, sortparams):
CODE_TEMPLATE = Template('''
/// \\brief Recogniser for function ${namestring}
/// \\param e A data expression
/// \\return true iff e is the function symbol matching ${namestring}
inline
bool is_${functionname}_function_symbol(const atermpp::aterm_appl& e)
{
if (is_function_symbol(e))
{
return function_symbol(e)${getnamef} == ${functionname}${getname}();
}
return false;
}
''')
return CODE_TEMPLATE.substitute(
namestring = escape(fullname),
functionname = name,
getnamef = '' if sortparams == [] else '.name()',
getname = '' if sortparams == [] else '_name'
)
def polymorphic_function_recogniser(self, fullname, name, sortparams):
CODE_TEMPLATE = Template('''
/// \\brief Recogniser for function ${namestring}
/// \\param e A data expression
/// \\return true iff e is the function symbol matching ${namestring}
inline
bool is_${functionname}_function_symbol(const atermpp::aterm_appl& e)
{
if (is_function_symbol(e))
{
function_symbol f(e);
return f.name() == ${functionname}_name()${condition};
}
return false;
}
''')
condition = ''
if sortparams == '':
domain_size = len(self.sort_expression_list.elements[0].domain.elements)
cases = []
for s in self.sort_expression_list.elements:
d = s.domain
assert(len(d.elements) == domain_size)
cases.append('f == {0}({1})'.format(name, d.code(spec)))
condition = ' && function_sort(f.sort()).domain().size() == {0} && ({1})'.format(domain_size, ' || '.join(cases))
return CODE_TEMPLATE.substitute(
namestring = escape(fullname),
functionname = name,
condition = condition
)
def function_application(self, fullname, name, sort_params, data_params, polymorphic):
CODE_TEMPLATE = Template('''
/// \\brief Application of function symbol ${namestring}
${sortparameterstring}
${dataparameterstring}
/// \\return Application of ${namestring} to a number of arguments
inline
application ${functionname}(${parameters})
{
return ${nsfunctionname}(${actsortparameters})(${actdataparameters});
}
''')
formal_sort_params = map(lambda x: 'const sort_expression& {0}'.format(fcode(x, spec)), sort_params)
formal_data_params = map(lambda x: 'const data_expression& {0}'.format(fcode(x, spec)), data_params)
domain_params = map(lambda x: '{0}.sort()'.format(x), data_params) if polymorphic else []
return CODE_TEMPLATE.substitute(
namestring = escape(fullname),
sortparameterstring = '' if sort_params == '' else '\n '.join(map(lambda x: '/// \\param {0} A sort expression'.format(fcode(x, spec)), sort_params)),
dataparameterstring = '' if data_params == '' else '\n '.join(map(lambda x: '/// \\param {0} A data expression'.format(fcode(x, spec)), data_params)),
functionname = name,
parameters = ', '.join(formal_sort_params + formal_data_params),
nsfunctionname = add_namespace(name, self.namespace),
actsortparameters = ', '.join(map(lambda x: fcode(x, spec), sort_params + domain_params)),
actdataparameters = ', '.join(map(lambda x: fcode(x, spec), data_params))
)
def function_application_recogniser(self, fullname, name):
CODE_TEMPLATE = Template('''
/// \\brief Recogniser for application of ${namestring}
/// \\param e A data expression
/// \\return true iff e is an application of function symbol ${functionname} to a
/// number of arguments
inline
bool is_${functionname}_application(const atermpp::aterm_appl& e)
{
if (is_application(e))
{
return is_${functionname}_function_symbol(application(e).head());
}
return false;
}
''')
return CODE_TEMPLATE.substitute(
namestring = escape(fullname),
functionname = name,
)
def function_application_code(self, sort, polymorphic = False):
if not isinstance(sort, sort_arrow):
return ''
return self.function_application(self.id, self.label, self.sort_expression_list.sort_parameters(spec), sort.domain.parameters(spec), polymorphic) + \
self.function_application_recogniser(self.id, self.label)
def code(self, spec):
assert(isinstance(spec, specification))
if self.namespace <> spec.namespace:
return ''
if len(self.sort_expression_list.elements) == 1:
sort = self.sort_expression_list.elements[0] # as len is 1
return self.function_constructor(self.id, self.label, self.sort_expression_list.sort_parameters(spec), sort.code(spec)) + \
self.function_recogniser(self.id, self.label, self.sort_expression_list.sort_parameters(spec)) + \
self.function_application_code(sort)
else:
assert self.sort_expression_list.elements > 1
return self.polymorphic_function_constructor(self.id, self.label, self.sort_expression_list.sort_parameters(spec)) + \
self.polymorphic_function_recogniser(self.id, self.label, self.sort_expression_list.formal_parameters_code(spec)) + \
self.function_application_code(self.sort_expression_list.elements[0], True)
class multi_function_declaration_list():
def __init__(self, elements):
assert(all(map(lambda x: isinstance(x, multi_function_declaration), elements)))
self.elements = elements
def __eq__(self, other):
return self.elements == other.elements
def push_back(self, element):
assert(isinstance(element, multi_function_declaration))
self.elements += [element]
def push_back_function_declaration(self, element):
assert(isinstance(element, function_declaration))
for e in self.elements:
if (e.id == element.id) and (e.label == element.label):
if all(map(lambda x: str(x) <> str(element.sort_expression), e.sort_expression_list.elements)):
e.sort_expression_list.push_back(element.sort_expression)
return
self.elements += [multi_function_declaration(element.id, element.namespace, sort_expression_list([element.sort_expression]), element.label)]
def __str__(self):
return "".join(["{0}\n".format(e) for e in self.elements])
def code(self, spec):
assert(isinstance(spec, specification))
code = ""
for e in self.elements:
code += "%s" % (e.code(spec))
return code
# Add all the elements to a multi_function_declaration_list,
# and let it do the code generation
multi_function_declarations = multi_function_declaration_list([])
for e in self.elements:
if (e.namespace == self.namespace):
multi_function_declarations.push_back_function_declaration(e)
code = multi_function_declarations.code(spec)
return code
class equation_declaration():
def __init__(self, lhs, rhs, condition = None):
assert(isinstance(lhs, data_expression))
assert(isinstance(rhs, data_expression))
self.namespace = ""
self.original_namespace = ""
if condition != None: assert(isinstance(condition, data_expression))
self.lhs = lhs
self.rhs = rhs
self.condition = condition
def __eq__(self, other):
return self.condition == other.condition and self.lhs == other.lhs and self.rhs == other.rhs and self.condition == other.condition and self.namespace == other.namespace and self.original_namespace == other.original_namespace
def set_namespace(self, string):
self.namespace = string
if self.original_namespace == "":
self.original_namespace = string
def sort_parameters(self, spec, function_spec, variable_spec):
condition_params = []
if self.condition:
condition_params = self.condition.sort_parameters(spec, function_spec, variable_spec)
return merge([condition_params, self.lhs.sort_parameters(spec, function_spec, variable_spec), self.rhs.sort_parameters(spec, function_spec, variable_spec)])
def has_lambda(self):
if self.condition and self.condition.has_lambda():
return True
return self.lhs.has_lambda() or self.rhs.has_lambda()
def has_forall(self):
if self.condition and self.condition.has_forall():
return True
return self.lhs.has_forall() or self.rhs.has_forall()
def has_exists(self):
if self.condition and self.condition.has_exists():
return True
return self.lhs.has_exists() or self.rhs.has_exists()
def __str__(self):
if self.condition:
return "{0} = {1}".format(self.lhs, self.rhs)
else:
return "{0} -> {1} = {2}".format(self.condition, self.lhs, self.rhs)
def determinise_variable_or_function_symbol(self, function_spec, variable_spec):
condition = None
if self.condition <> None:
condition = self.condition.determinise_variable_or_function_symbol(function_spec, variable_spec)
lhs = self.lhs.determinise_variable_or_function_symbol(function_spec, variable_spec)
rhs = self.rhs.determinise_variable_or_function_symbol(function_spec, variable_spec)
return equation_declaration(lhs, rhs, condition)
def code(self, spec, function_spec, variable_spec):
variables = []
if self.condition:
variables = self.condition.free_variables()
variables = merge([variables, self.lhs.free_variables(), self.rhs.free_variables()])
if len(variables) == 0:
variables_string = "variable_list()"
else:
variables_string = "atermpp::make_vector({0})".format(", ".join(sorted([v.code(spec, function_spec, variable_spec) for v in variables])))
if self.condition:
return "result.push_back(data_equation({0}, {1}, {2}, {3}));".format(variables_string, self.condition.code(spec, function_spec, variable_spec), self.lhs.code(spec, function_spec, variable_spec), self.rhs.code(spec, function_spec, variable_spec))
else:
return "result.push_back(data_equation({0}, {1}, {2}));".format(variables_string, self.lhs.code(spec, function_spec, variable_spec), self.rhs.code(spec, function_spec, variable_spec))
class equation_declaration_list():
def __init__(self, elements):
assert(all(map(lambda x: isinstance(x, equation_declaration), elements)))
self.elements = elements
self.namespace = ""
self.original_namespace = ""
def __eq__(self, other):
return self.elements == other.elements and self.namespace == other.namespace and self.original_namespace == other.original_namespace
def push_back(self, element):
assert(isinstance(element, equation_declaration))
self.elements.append(element)
def set_namespace(self, string):
self.namespace = string
if self.original_namespace == "":
self.original_namespace = string
for e in self.elements:
e.set_namespace(string)
def sort_parameters(self, spec, function_spec, variable_spec):
return merge([e.sort_parameters(spec, function_spec, variable_spec) for e in self.elements])
def determinise_variable_or_function_symbol(self, function_spec, variable_spec):
result = equation_declaration_list([])
result.namespace = self.namespace
result.original_namespace = self.original_namespace
for e in self.elements:
result.push_back(e.determinise_variable_or_function_symbol(function_spec, variable_spec))
return result
def has_lambda(self):
return any(map(lambda x: x.has_lambda(), self.elements))
def has_forall(self):
return any(map(lambda x: x.has_forall(), self.elements))
def has_exists(self):
return any(map(lambda x: x.has_exists(), self.elements))
def __str__(self):
return "".join(["{0}\n".format(e) for e in self.elements])
def code(self, spec, function_spec, variable_spec):
self = self.determinise_variable_or_function_symbol(function_spec, variable_spec)
sort_parameters = self.sort_parameters(spec, function_spec, variable_spec)
formal_parameters_code = map(lambda x: x.formal_parameter_code(), sort_parameters)
code = " /// \\brief Give all system defined equations for %s\n" % (escape(spec.get_namespace()))
for s in sort_parameters:
code += " /// \\param %s A sort expression\n" % (escape(s.code(spec)))
code += " /// \\return All system defined equations for sort %s\n" % (escape(spec.get_namespace()))
code += " inline\n"
code += " data_equation_vector %s_generate_equations_code(%s)\n" % (spec.get_namespace(), string.join(formal_parameters_code, ", "))
code += " {\n"
code += "%s\n" % (variable_spec.code(spec))
code += " data_equation_vector result;\n"
code += "%s" % (spec.sort_specification.structured_sort_equation_code())
for e in self.elements:
code += " %s\n" % (e.code(spec, function_spec, variable_spec))
code += " return result;\n"
code += " }\n"
return code
class data_expression():
def __init__(self):
pass
def __str__(self):
raise Exception("String operation for data_expression is not implemented")
def has_lambda(self):
return False
def has_forall(self):
return False
def has_exists(self):
return False
class data_application(data_expression):
def __init__(self, head, arguments):
assert(isinstance(head, data_expression))
assert(isinstance(arguments, data_expression_list))
self.head = head
self.arguments = arguments
def __eq__(self, other):
return self.head == other.head and self.arguments == other.arguments
def sort_parameters(self, spec, function_spec, variable_spec):
return merge([self.head.sort_parameters(spec, function_spec, variable_spec), self.arguments.sort_parameters(spec, function_spec, variable_spec)])
def determinise_variable_or_function_symbol(self, function_spec, variable_spec):
return data_application(self.head.determinise_variable_or_function_symbol(function_spec, variable_spec), self.arguments.determinise_variable_or_function_symbol(function_spec, variable_spec))
def free_variables(self):
return merge([self.head.free_variables(), self.arguments.free_variables()])
def has_lambda(self):
return self.head.has_lambda() or self.arguments.has_lambda()
def has_forall(self):
return self.head.has_forall() or self.arguments.has_forall()
def has_exists(self):
return self.head.has_exists() or self.arguments.has_exists()
def __str__(self):
return "{0}({1})".format(self.head, self.arguments)
def code(self, spec, function_spec, variable_spec, indentlog = 0):
LOG.debug(indent(indentlog) + "Generating code for {0}".format(self))
if isinstance(self.head, function_symbol):
# FIXME: Extremely ugly workaround for set complement
if self.head.id == "!" and len(self.arguments.elements) == 1 and isinstance(self.arguments.elements[0], data_application) and isinstance(self.arguments.elements[0].head, data_variable):
head_code = "sort_bool_::not_"
# FIXME: Extremely ugly workaround for bag join
elif self.head.id == "+" and len(self.arguments.elements) == 2 and isinstance(self.arguments.elements[0], data_application) and isinstance(self.arguments.elements[0].head, data_variable):
head_code = "sort_nat::plus"
else:
head_code = self.head.code(spec, function_spec, variable_spec, indentlog + 2, len(self.arguments.elements))
# make generated code a bit more readable
head_code = head_code[:head_code.find("(")]
else:
head_code = self.head.code(spec, function_spec, variable_spec, indentlog + 2)
sort_parameters_code = []
# FIXME: workaround for complement and join
if isinstance(self.head, function_symbol) and (head_code <> "sort_bool_::not_" and head_code <> "sort_nat::plus"):
sort_parameters = self.head.sort_parameters(spec, function_spec, variable_spec)
sort_parameters_code += map(lambda x: x.code(spec), sort_parameters)
sort_parameters_code += [self.arguments.code(spec, function_spec, variable_spec, indentlog + 2)]
result = "%s(%s)" % (head_code, string.join(sort_parameters_code, ", "))
LOG.debug(indent(indentlog) + "Yields {0}".format(result))
return result
class data_variable_or_function_symbol(data_expression):
def __init__(self, identifier):
self.id = identifier
def __eq__(self, other):
return self.id == other.id
def __str__(self):
return str(self.id)
def determinise_variable_or_function_symbol(self, function_spec, variable_spec):
result = function_spec.find_function(self, -1)
if result:
return function_symbol(self.id)
else:
return data_variable(self.id)
class data_variable(data_expression):
def __init__(self, id):
assert(isinstance(id, identifier))
self.id = id
def __eq__(self, other):
if hasattr(other, "id"):
return self.id == other.id
else:
return NotImplemented
def sort_parameters(self, spec, function_spec, variable_spec):
variable = variable_spec.find_variable(self)
if variable == None:
return []
return variable.sort_expression.sort_parameters(spec)
def determinise_variable_or_function_symbol(self, function_spec, variable_spec):
return self
def free_variables(self):
return [self]
def __str__(self):
return str(self.id)
def code(self, spec, function_spec, variable_spec, indentlog = 0):
LOG.debug(indent(indentlog) + "Generating code for {0}".format(self))
result = "v{0}".format(self.id)
LOG.debug(indent(indentlog) + "Yields {0}".format(result))
return result
class function_symbol(data_expression):
def __init__(self, id):
assert(isinstance(id, identifier))
self.id = id
def __eq__(self, other):
return self.id == other.id
def sort_parameters(self, spec, function_spec, variable_spec):
return function_spec.find_function(self, -1).sort_expression.sort_parameters(spec)
def determinise_variable_or_function_symbol(self, function_spec, variable_spec):
return self
def free_variables(self):
result = set()
return result
def __str__(self):
return str(self.id)
def code(self, spec, function_spec, variable_spec, indentlog=0, argumentcount = -1):
LOG.debug(indent(indentlog) + "Generating code for {0} with argumentcount {1} and indent {2}".format(self, argumentcount, indentlog))
f = function_spec.find_function(self, argumentcount)
sort_parameters_code = [s.code(spec) for s in f.sort_parameters(spec)]
result = "%s(%s)" % (add_namespace(f.label, f.namespace, function_spec.namespace), string.join(sort_parameters_code, ", "))
LOG.debug(indent(indentlog) + "Yields {0}".format(result))
return result
class lambda_abstraction(data_expression):
def __init__(self, var_declaration, expression):
assert(isinstance(var_declaration, variable_declaration))
assert(isinstance(expression, data_expression))
self.variable_declaration = var_declaration
self.expression = expression
def __eq__(self, other):
return self.variable_declaration == other.variable_declaration and self.expression == other.expression
def sort_parameters(self, spec, function_spec, variable_spec):
return merge(self.variable_declaration.sort_parameters(spec), self.expression.sort_parameters(spec, function_spec, variable_spec))
def determinise_variable_or_function_symbol(self, function_spec, variable_spec):
variable_spec.declarations.push_back(self.variable_declaration)
return lambda_abstraction(self.variable_declaration, self.expression.determinise_variable_or_function_symbol(function_spec, variable_spec))
def free_variables(self):
return difference(self.expression.free_variables(), [data_variable(self.variable_declaration.id)])
def has_lambda(self):
return True
def has_forall(self):
return self.expression.has_forall()
def has_exists(self):
return self.expression.has_exists()
def __str__(self):
return "lambda({0}, {1})".format(self.variable_declaration, self.expression)
def code(self, spec, function_spec, variable_spec, indentlog = 0):
LOG.debug(indent(indentlog) + "Generating code for {0}".format(self))
result = "lambda(atermpp::make_vector(%s), %s)" % (data_variable(self.variable_declaration.id).code(spec, function_spec, variable_spec, indentlog+2), self.expression.code(spec, function_spec, variable_spec, indentlog+2))
LOG.debug(indent(indentlog) + "Yields {0}".format(result))
return result
class forall(data_expression):
def __init__(self, var_declaration, expression):
assert(isinstance(var_declaration, variable_declaration))
assert(isinstance(expression, data_expression))
self.variable_declaration = var_declaration
self.expression = expression
def __eq__(self, other):
return self.variable_declaration == other.variable_declaration and self.expression == other.expression
def sort_parameters(self, spec, function_spec, variable_spec):
return merge([self.variable_declaration.sort_parameters(spec), self.expression.sort_parameters(spec, function_spec, variable_spec)])
def determinise_variable_or_function_symbol(self, function_spec, variable_spec):
variable_spec.declarations.push_back(self.variable_declaration)
return forall(self.variable_declaration, self.expression.determinise_variable_or_function_symbol(function_spec, variable_spec))
def free_variables(self):
return difference(self.expression.free_variables(), [data_variable(self.variable_declaration.id)])
def has_lambda(self):
return self.expression.has_lambda()
def has_forall(self):
return True
def has_exists(self):
return self.expression.has_exists()
def __str__(self):
return "forall({0}, {1})".format(self.variable_declaration, self.expression)
def code(self, spec, function_spec, variable_spec, indentlog = 0):
LOG.debug(indent(indentlog) + "Generating code for {0}".format(self))
result = "forall(atermpp::make_vector(%s), %s)" % (data_variable(self.variable_declaration.id).code(spec, function_spec, variable_spec, indentlog+2), self.expression.code(spec, function_spec, variable_spec, indentlog+2))
LOG.debug(indent(indentlog) + "Yields {0}".format(result))
return result
class exists(data_expression):
def __init__(self, var_declaration, expression):
assert(isinstance(var_declaration, variable_declaration))
assert(isinstance(expression, data_expression))
self.variable_declaration = var_declaration
self.expression = expression
def __eq__(self, other):
return self.variable_declaration == other.variable_declaration and self.expression == other.expression
def sort_parameters(self, spec, function_spec, variable_spec):
return merge(self.variable_declaration.sort_parameters(spec), self.expression.sort_parameters(spec, function_spec, variable_spec))
def determinise_variable_or_function_symbol(self, function_spec, variable_spec):
variable_spec.declarations.push_back(self.variable_declaration)
return exists(self.variable_declaration, self.expression.determinise_variable_or_function_symbol(function_spec, variable_spec))
def free_variables(self):
return difference(self.expression.free_variables(), [data_variable(self.variable_declaration.id)])
def has_lambda(self):
return self.expression.has_lambda()
def has_forall(self):
return self.expression.has_forall()
def has_exists(self):
return True
def __str__(self):
return "exists({0}, {1})".format(self.variable_declaration, self.expression)
def code(self, spec, function_spec, variable_spec, indentlog=0):
LOG.debug(indent(indentlog) + "Generating code for {0}".format(self))
result = "exists(atermpp::make_vector(%s), %s)" % (data_variable(self.variable_declaration.id).code(spec, function_spec, variable_spec, indentlog+2), self.expression.code(spec, function_spec, variable_spec, indentlog+2))
LOG.debug(indent(indentlog) + "Yields {0}".format(result))
return result
class data_expression_list():
def __init__(self, elements):
assert(all(map(lambda x: isinstance(x, data_expression), elements)))
self.elements = elements
def __eq__(self, other):
return self.elements == other.elements
def push_back(self, element):
assert(isinstance(element, data_expression))
self.elements += [element]
def determinise_variable_or_function_symbol(self, function_spec, variable_spec):
result = data_expression_list([])
for e in self.elements:
result.push_back(e.determinise_variable_or_function_symbol(function_spec, variable_spec))
return result
def sort_parameters(self, spec, function_spec, variable_spec):
return merge([e.sort_parameters(spec, function_spec, variable_spec) for e in self.elements])
def free_variables(self):
return merge([e.free_variables() for e in self.elements])
def has_lambda(self):
return any(map(lambda x: x.has_lambda(), self.elements))
def has_forall(self):
return any(map(lambda x: x.has_forall(), self.elements))
def has_exists(self):
return any(map(lambda x: x.has_exists(), self.elements))
def __str__(self):
return ", ".join([str(e) for e in self.elements])
def code(self, spec, function_spec, variable_spec, indentlog):
return ", ".join([e.code(spec, function_spec, variable_spec, indentlog+2) for e in self.elements])
class sort_expression_list():
def __init__(self, elements):
assert(all(map(lambda x: isinstance(x, sort_expression), elements)))
self.elements = elements
def __eq__(self, other):
return self.elements == other.elements
def push_back(self, element):
assert(isinstance(element, sort_expression))
self.elements += [element]
def sort_parameters(self, spec):
return merge([e.sort_parameters(spec) for e in self.elements])
def formal_parameters_code(self, spec):
return ", ".join([p.formal_parameter_code() for p in self.sort_parameters(spec)])
def actual_parameters_code(self, spec):
return ", ".join([p.actual_parameter_code() for p in self.sort_parameters(spec)])
def __str__(self):
return ", ".join(self.elements)
def code(self):
return "atermpp::make_vector({0})".format(", ".join([e.code() for e in self.elements()]))
class sort_expression():
def __init__(self):
pass
def sort_parameters(self, spec):
return []
class sort_identifier(sort_expression):
def __init__(self, id):
assert(isinstance(id, identifier))
self.name = id
def __eq__(self, other):
if hasattr(other, "name"):
return self.name == other.name
else:
return NotImplemented
def sort_parameters(self, spec):
if self in spec.sort_parameters():
return [self]
else:
return []
def __str__(self):
return str(self.name)
def formal_parameter_code(self):
return "const sort_expression& {0}".format(self.name).lower()
def actual_parameter_code(self):
return str(self.name).lower()
def code(self, spec = None):
#assert(isinstance(spec, specification))
if (spec != None and spec.sort_specification.has_sort(self)):
return spec.sort_specification.get_sort(self).inline_code(spec)
else:
return str(self.name).lower()
class domain(sort_expression):
def __init__(self, labelled, elements):
assert(isinstance(labelled, bool))
self.labelled = labelled
self.elements = elements
def __eq__(self, other):
if hasattr(other, "labelled") and hasattr(other, "elements"):
return self.labelled == other.labelled and self.elements == other.elements
else:
return NotImplemented
def sorts(self):
if self.labelled:
return [e[0] for e in self.elements]
else:
return self.elements
def __labels(self):
return [e[1] for e in self.elements]
def push_back(self, element):
self.elements.append(element)
def projection_arguments(self):
if not self.labelled:
return []
else:
return [(e, i) for (i, e) in enumerate(self.__labels())]
def sort_parameters(self, spec):
return merge([p.sort_parameters(spec) for p in self.sorts()])
def parameters(self, spec):
return ['arg{0}'.format(i) for i in range(0,len(self.elements))]
def formal_parameters_code(self, spec):
return ', '.join(map(lambda x: 'const data_expression& {0}'.format(str(x)), self.parameters(spec)))
def actual_parameters_code(self, spec):
return ', '.join(self.parameters(spec))
def assertions_code(self):
return ""
def __str__(self):
if self.labelled:
return " # ".join(["{0} <\"{1}\">".format(e[0], e[1]) for e in self.elements])
else:
return " # ".join(map(str, self.elements))
def code(self, spec):
return ", ".join([e.code(spec) for e in self.sorts()])
class sort_arrow(sort_expression):
def __init__(self, dom, codom):
assert(isinstance(dom, domain))
assert(isinstance(codom, sort_expression))
self.domain = dom
self.codomain = codom
def __eq__(self, other):
return self.domain == other.domain and self.codomain == other.codomain
def sort_parameters(self, spec):
return merge([self.codomain.sort_parameters(spec), self.domain.sort_parameters(spec)])
def __str__(self):
return "{0} -> {1}".format(self.domain, self.codomain)
def code(self, spec):
assert(isinstance(spec, specification))
return "make_function_sort(%s, %s)" % (self.domain.code(spec), self.codomain.code(spec))
class sort_container(sort_expression):
def __init__(self, container, element_sort):
assert(isinstance(container, identifier))
assert(isinstance(element_sort, sort_expression))
self.container = container
self.element_sort = element_sort
def __eq__(self, other):
if hasattr(other, "container") and hasattr(other, "element_sort"):
return self.container == other.container and self.element_sort == other.element_sort
else:
return NotImplemented
def sort_parameters(self, spec):
return self.element_sort.sort_parameters(spec)
def __str__(self):
return "{0}({1})".format(self.container, self.element_sort)
def code(self, spec):
assert(isinstance(spec, specification))
if (spec.sort_specification.has_sort(self)):
sort = spec.sort_specification.get_sort(self)
result = "%s(%s)" % (sort.label, self.element_sort.code(spec))
if sort.original_namespace <> spec.namespace and sort.original_namespace <> "undefined":
result = "sort_%s::%s" % (sort.original_namespace, result)
return result
else:
return "%s(%s)" % (str(self.container).lower(), self.element_sort.code(spec))
class structured_sort_declaration():
def __init__(self, id, l, arguments = None):
assert(isinstance(id, identifier))
assert(isinstance(l, label))
assert(arguments == None or isinstance(arguments, domain))
self.id = id
self.label = l
self.arguments = arguments
self.namespace = ""
self.original_namespace = ""
def __eq__(self, other):
return self.id == other.id and self.label == other.label and self.arguments == other.arguments and self.namespace == other.namespace and self.original_namespace == other.original_namespace
def merge_structured_sort(self, function_spec, sort_expr):
if self.arguments == None:
s = sort_expr
else:
s = sort_arrow(self.arguments, sort_expr)
f = function_declaration(self.id, s, self.label)
# f.set_namespace should not be used here, as this function may be
# introduced in an included specification, and hence we need to preserve
# original_namespace
f.original_namespace = self.original_namespace
f.namespace = self.namespace
constructor_spec = function_spec.constructor_specification
constructor_spec.declarations.push_back(f)
function_spec.constructor_specification = constructor_spec
return function_spec
def set_namespace(self, string):
self.namespace = string
if self.original_namespace == "":
self.original_namespace = string
def __str__(self):
if self.arguments:
return "{0}({1})".format(self.id, self.arguments)
else:
return str(self.id)
def struct_constructor_arguments(self, spec):
return "atermpp::make_vector({0})".format(", ".join(["structured_sort_constructor_argument(\"%s\", %s)" % (a[1], a[0].code(spec)) for a in self.arguments.elements]))
def code(self, spec):
if self.arguments == None:
return "structured_sort_constructor(\"%s\", \"%s\")" % (self.id, self.label)
else:
return "structured_sort_constructor(\"%s\", %s, \"%s\")" % (self.id, self.struct_constructor_arguments(spec), self.label)
class structured_sort_declaration_list():
def __init__(self, elements):
assert(all(map(lambda x: isinstance(x, structured_sort_declaration), elements)))
self.elements = elements
self.namespace = ""
self.original_namespace = ""
def __eq__(self, other):
return self.elements == other.elements and self.namespace == other.namespace and self.original_namespace == other.original_namespace
def merge_structured_sort(self, function_spec, sort_expr):
for e in self.elements:
function_spec = e.merge_structured_sort(function_spec, sort_expr)
return function_spec
def push_back(self, element):
assert(isinstance(element, structured_sort_declaration))
self.elements += [element]
def set_namespace(self, string):
self.namespace = string
if self.original_namespace == "":
self.original_namespace = string
for e in self.elements:
e.set_namespace(string)
def __str__(self):
return " | ".join(map(str, self.elements))
def code(self, spec):
code = ""
for e in self.elements:
code += " constructors.push_back(%s);\n" % (e.code(spec))
return code
class structured_sort_specification():
def __init__(self, elements):
assert(isinstance(elements, structured_sort_declaration_list))
self.elements = elements
self.namespace = ""
self.original_namespace = ""
def __eq__(self, other):
if hasattr(other, "elements") and hasattr(other, "namespace") and hasattr(other, "original_namespace"):
return self.elements == other.elements and self.namespace == other.namespace and self.original_namespace == other.original_namespace
else:
return NotImplemented
def merge_structured_sort(self, function_spec, sort_expr):
return self.elements.merge_structured_sort(function_spec, sort_expr)
def set_namespace(self, string):
self.namespace = string
if self.original_namespace == "":
self.original_namespace = string
self.elements.set_namespace(string)
def __str__(self):
return "struct {0}".format(self.elements)
def code(self, spec):
return self.elements.code(spec)
class sort_declaration():
def __init__(self, sort_expr, l, alias = None):
assert(isinstance(sort_expr, sort_expression))
assert(alias == None or isinstance(alias, structured_sort_specification))
assert(isinstance(l, label))
self.sort_expression = sort_expr
self.label = l
self.alias = alias
self.namespace = ""
self.original_namespace = ""
def merge_structured_sort(self, function_spec):
if self.alias <> None:
function_spec = self.alias.merge_structured_sort(function_spec, self.sort_expression)
return function_spec
def set_namespace(self, string):
self.namespace = string
if self.original_namespace == "":
self.original_namespace = string
if self.alias <> None:
self.alias.set_namespace(string)
def name(self):
return self.sort_expression
def defines_container(self):
return isinstance(self.sort_expression, sort_container)
def defines_struct(self):
return self.alias <> None
def __str__(self):
if self.alias:
return "{0} = {1}".format(self.sort_expression, self.alias)
else:
return str(self.sort_expression)
def inline_code(self, spec):
if (self.namespace == spec.namespace) or (self.namespace == "") or self.namespace == "undefined":
return "%s()" % (self.label)
else:
return "sort_%s::%s()" % (self.namespace, self.label)
def structured_sort_constructor_code(self):
if self.alias == None:
return ""
else:
param = ""
if isinstance(self.sort_expression, sort_container):
param = str(self.sort_expression.element_sort).lower()
sort = "%s(%s)" % (self.label, param)
result = " function_symbol_vector %s_constructors = detail::%s_struct(%s).constructor_functions(%s);\n" % (self.label, self.label, param, sort)
return result + " result.insert(result.end(), %s_constructors.begin(), %s_constructors.end());\n" % (self.label, self.label)
def structured_sort_mapping_code(self):
if self.alias == None:
return ""
else:
param = ""
if isinstance(self.sort_expression, sort_container):
param = str(self.sort_expression.element_sort).lower()
sort = "%s(%s)" % (self.label, param)
result = " function_symbol_vector %s_mappings = detail::%s_struct(%s).comparison_functions(%s);\n" % (self.label, self.label, param, sort)
result += " result.insert(result.end(), %s_mappings.begin(), %s_mappings.end());\n" % (self.label, self.label)
# result += " %s_mappings = detail::%s_struct(%s).projection_functions(%s);\n" % (self.label, self.label, param, sort)
# result += " result.insert(result.end(), %s_mappings.begin(), %s_mappings.end());\n" % (self.label, self.label)
# result += " %s_mappings = detail::%s_struct(%s).recogniser_functions(%s);\n" % (self.label, self.label, param, sort)
# result += " result.insert(result.end(), %s_mappings.begin(), %s_mappings.end());\n" % (self.label, self.label)
return result
def structured_sort_equation_code(self):
if self.alias == None:
return ""
else:
param = ""
if isinstance(self.sort_expression, sort_container):
param = str(self.sort_expression.element_sort).lower()
sort = "%s(%s)" % (self.label, param)
result = " data_equation_vector %s_equations = detail::%s_struct(%s).constructor_equations(%s);\n" % (self.label, self.label, param, sort)
result += " result.insert(result.end(), %s_equations.begin(), %s_equations.end());\n" % (self.label, self.label)
result += " %s_equations = detail::%s_struct(%s).comparison_equations(%s);\n" % (self.label, self.label, param, sort)
result += " result.insert(result.end(), %s_equations.begin(), %s_equations.end());\n" % (self.label, self.label)
# result += " %s_equations = detail::%s_struct(%s).projection_equations(%s);\n" % (self.label, self.label, param, sort)
# result += " result.insert(result.end(), %s_equations.begin(), %s_equations.end());\n" % (self.label, self.label)
# result += " %s_equations = detail::%s_struct(%s).recogniser_equations(%s);\n" % (self.label, self.label, param, sort)
# result += " result.insert(result.end(), %s_equations.begin(), %s_equations.end());\n" % (self.label, self.label)
return result
def sort_name(self, id, label):
code = ""
code += " inline\n"
code += " core::identifier_string const& %s_name()\n" % (label)
code += " {\n"
code += " static core::identifier_string %s_name = core::identifier_string(\"%s\");\n" % (label, id)
code += " return %s_name;\n" % (label)
code += " }\n\n"
return code
def container_name(self, id, label):
code = ""
code += " inline\n"
code += " core::identifier_string const& %s_name()\n" % (label)
code += " {\n"
code += " static core::identifier_string %s_name = core::identifier_string(\"%s\");\n" % (label, label)
code += " return %s_name;\n" % (label)
code += " }\n\n"
return code
def sort_expression_constructors(self, id, label):
code = ""
code += self.sort_name(id, label)
code += " /// \\brief Constructor for sort expression %s\n" % (escape(id))
code += " /// \\return Sort expression %s\n" % (escape(id))
code += " inline\n"
code += " basic_sort const& %s()\n" % (label)
code += " {\n"
code += " static basic_sort %s = basic_sort(%s_name());\n" % (label, label)
code += " return %s;\n" % (label)
code += " }\n\n"
code += " /// \\brief Recogniser for sort expression %s\n" % (escape(str(id)))
code += " /// \\param e A sort expression\n"
code += " /// \\return true iff e == %s()\n" % (escape(label))
code += " inline\n"
code += " bool is_%s(const sort_expression& e)\n" % (label)
code += " {\n"
code += " if (is_basic_sort(e))\n"
code += " {\n"
code += " return basic_sort(e) == %s();\n" % (label)
code += " }\n"
code += " return false;\n"
code += " }\n"
return code
def sort_expression_constructors_container_sort(self, id, label, parameter):
code = ""
# code += self.container_name(id, label)
code += " /// \\brief Constructor for sort expression %s(%s)\n" % (escape(id), escape(parameter))
code += " /// \\param %s A sort expression\n" % (escape(str(parameter).lower()))
code += " /// \\return Sort expression %s(%s)\n" % (escape(label), escape(str(parameter).lower()))
code += " inline\n"
code += " container_sort %s(const sort_expression& %s)\n" % (label, str(parameter).lower())
code += " {\n"
code += " container_sort %s(%s_container(), %s);\n" % (label, label, str(parameter).lower())
code += " return %s;\n" % (label)
code += " }\n\n"
code += " /// \\brief Recogniser for sort expression %s(%s)\n" % (escape(id), escape(str(parameter).lower()))
code += " /// \\param e A sort expression\n"
code += " /// \\return true iff e is a container sort of which the name matches\n"
code += " /// %s\n" % (escape(label))
code += " inline\n"
code += " bool is_%s(const sort_expression& e)\n" % (label)
code += " {\n"
code += " if (is_container_sort(e))\n"
code += " {\n"
code += " return container_sort(e).container_name() == %s_container();\n" % (label)
code += " }\n"
code += " return false;\n"
code += " }\n"
return code
def code(self, spec):
if isinstance(self.sort_expression, sort_identifier):
code = self.sort_expression_constructors(self.sort_expression, self.label)
else:
assert(isinstance(self.sort_expression, sort_container))
code = self.sort_expression_constructors_container_sort(self.sort_expression.container, self.label, self.sort_expression.element_sort)
if self.alias <> None:
code += "\n"
code += " namespace detail {\n\n"
code += " /// \\brief Declaration for sort %s as structured sort\n" % (escape(self.label))
if isinstance(self.sort_expression, sort_container):
param = str(self.sort_expression.element_sort).lower()
code += " /// \\param %s A sort expression\n" % (escape(param))
else:
param = ""
code += " /// \\return The structured sort representing %s\n" % (escape(self.label))
code += " inline\n"
code += " structured_sort %s_struct(const sort_expression& %s)\n" % (self.label, param)
code += " {\n"
code += " structured_sort_constructor_vector constructors;\n"
code += "%s" % (self.alias.code(spec))
code += " return structured_sort(constructors);\n"
code += " }\n\n"
code += " } // namespace detail\n"
return code
class sort_declaration_list():
def __init__(self, elements):
assert(all(map(lambda x: isinstance(x, sort_declaration), elements)))
self.elements = elements
self.namespace = ""
self.original_namespace = ""
def merge_structured_sorts(self, function_spec):
for e in self.elements:
function_spec = e.merge_structured_sort(function_spec)
return function_spec
def push_back(self, element):
assert(isinstance(element, sort_declaration))
self.elements += [element]
def is_alias(self, sort_expr):
assert(isinstance(sort_expr, sort_expression))
return any(map(lambda x: x.sort_expression == sort_expr and x.alias <> None, self.elements))
def set_namespace(self, string):
self.namespace = string
if self.original_namespace == "":
self.original_namespace = string
for e in self.elements:
e.set_namespace(string)
def merge_declarations(self, declarations, is_supertype):
self.elements += declarations.elements
def defines_container(self):
return any(map(lambda x: x.defines_container(), self.elements))
def defines_struct(self):
return any(map(lambda x: x.defines_struct(), self.elements))
def __str__(self):
return "".join(["{0}\n".format(e) for e in self.elements])
def structured_sort_constructor_code(self):
code = ""
for e in self.elements:
if e.namespace == self.namespace:
code += "%s" % (e.structured_sort_constructor_code())
return code
def structured_sort_mapping_code(self):
code = ""
for e in self.elements:
if e.namespace == self.namespace:
code += "%s" % (e.structured_sort_mapping_code())
return code
def structured_sort_equation_code(self):
code = ""
for e in self.elements:
if e.namespace == self.namespace:
code += "%s" % (e.structured_sort_equation_code())
return code
def code(self, spec):
code = ""
for e in self.elements:
if e.namespace == self.namespace:
code += "%s\n" % (e.code(spec))
return code
class equation_specification():
def __init__(self, declarations):
assert(isinstance(declarations, equation_declaration_list))
self.declarations = declarations
self.namespace = ""
self.original_namespace = ""
def set_namespace(self, string):
self.namespace = string
if self.original_namespace == "":
self.original_namespace = string
self.declarations.set_namespace(string)
def has_lambda(self):
return self.declarations.has_lambda()
def has_forall(self):
return self.declarations.has_forall()
def has_exists(self):
return self.declarations.has_exists()
def __str__(self):
return "eqn {0}".format(self.declarations)
def code(self, spec, function_spec, variable_spec):
return self.declarations.code(spec, function_spec, variable_spec)
class variable_specification():
def __init__(self, declarations):
assert(isinstance(declarations, variable_declaration_list))
self.declarations = declarations
def find_variable(self, variable):
return self.declarations.find_variable(variable)
def __str__(self):
return "var {0}".format(self.declarations)
def code(self, spec):
return self.declarations.code(spec)
class mapping_specification():
def __init__(self, declarations):
assert(isinstance(declarations, function_declaration_list))
self.declarations = declarations
self.namespace = ""
self.original_namespace = ""
def set_namespace(self, string):
self.namespace = string
if self.original_namespace == "":
self.original_namespace = string
self.declarations.set_namespace(string)
def find_function(self, function, argumentcount):
return self.declarations.find_function(function, argumentcount)
def __str__(self):
return "map {0}".format(self.declarations)
def merge_specification(self, spec, is_supertype):
self.declarations.merge_declarations(spec.declarations, is_supertype)
def code(self, spec):
if not self.declarations.empty():
assert(isinstance(spec, specification))
sort_parameters = string.join(map(lambda x: "const sort_expression& %s" % (str(x).lower()), self.declarations.sort_parameters(spec)), ", ")
assert(self.namespace == spec.namespace)
namespace_string = self.namespace
if namespace_string == "undefined":
namespace_string = spec.get_namespace()
code = ""
code += self.declarations.code(spec)
code += " /// \\brief Give all system defined mappings for %s\n" % (escape(namespace_string))
for s in self.declarations.sort_parameters(spec):
code += " /// \\param %s A sort expression\n" % (escape(str(s).lower()))
code += " /// \\return All system defined mappings for %s\n" % (escape(namespace_string))
code += " inline\n"
code += " function_symbol_vector %s_generate_functions_code(%s)\n" % (namespace_string, sort_parameters)
code += " {\n"
code += " function_symbol_vector result;\n"
code += self.declarations.generator_code(spec) + (spec.sort_specification.structured_sort_mapping_code())
code += " return result;\n"
code += " }\n"
return code
else:
return ""
class constructor_specification():
def __init__(self, declarations):
assert(isinstance(declarations, function_declaration_list))
self.declarations = declarations
self.namespace = ""
self.original_namespace = ""
def set_namespace(self, string):
self.namespace = string
if self.original_namespace == "":
self.original_namespace = string
self.declarations.set_namespace(string)
def find_function(self, function, argumentcount):
return self.declarations.find_function(function, argumentcount)
def __str__(self):
return "cons {0}".format(self.declarations)
def merge_specification(self, spec, is_supertype):
self.declarations.merge_declarations(spec.declarations, is_supertype)
def code(self, spec):
if not self.declarations.empty():
assert(isinstance(spec, specification))
sort_parameters = string.join(map(lambda x: "const sort_expression& %s" % (str(x).lower()), self.declarations.sort_parameters(spec)), ", ")
assert(self.namespace == spec.namespace)
namespace_string = self.namespace
if namespace_string == "undefined":
namespace_string = spec.get_namespace()
code = self.declarations.code(spec)
code += " /// \\brief Give all system defined constructors for %s\n" % (escape(namespace_string))
for s in self.declarations.sort_parameters(spec):
code += " /// \\param %s A sort expression\n" % (escape(str(s).lower()))
code += " /// \\return All system defined constructors for %s\n" % (escape(namespace_string))
code += " inline\n"
code += " function_symbol_vector %s_generate_constructors_code(%s)\n" % (namespace_string,sort_parameters)
code += " {\n"
code += " function_symbol_vector result;\n"
add_constructors_code = self.declarations.generator_code(spec) + (spec.sort_specification.structured_sort_constructor_code())
if add_constructors_code == "":
for s in self.declarations.sort_parameters(spec):
code += " static_cast< void >(%s); // suppress unused variable warnings\n" % (str(s).lower())
else:
code += "%s" % (spec.sort_specification.structured_sort_constructor_code())
code += "%s\n" % (self.declarations.generator_code(spec, True))
code += " return result;\n"
code += " }\n"
return code
else:
return ""
class function_specification():
def __init__(self, mapping_spec, constructor_spec = constructor_specification(function_declaration_list([]))):
assert(isinstance(mapping_spec, mapping_specification))
assert(isinstance(constructor_spec, constructor_specification))
self.constructor_specification = constructor_spec
self.mapping_specification = mapping_spec
self.namespace = ""
self.original_namespace = ""
def merge_specification(self, spec, is_supertype):
self.constructor_specification.merge_specification(spec.constructor_specification, is_supertype)
self.mapping_specification.merge_specification(spec.mapping_specification, is_supertype)
def set_namespace(self, string):
self.namespace = string
if self.original_namespace == "":
self.original_namespace = string
if self.constructor_specification <> None:
self.constructor_specification.set_namespace(string)
self.mapping_specification.set_namespace(string)
def find_function(self, function, argumentcount):
result = self.constructor_specification.find_function(function, argumentcount)
if not result:
result = self.mapping_specification.find_function(function, argumentcount)
return result
def __str__(self):
return "{0}\n{1}".format(self.constructor_specification, self.mapping_specification)
def code(self, spec):
assert(isinstance(spec, specification))
# This assumes that no functions occur with the same name, but one is
# constructor and one is mapping.
code = ""
code += self.constructor_specification.code(spec)
code += self.mapping_specification.code(spec)
# We need deepcopy here to prevent undesired duplication
merged_declarations = copy.deepcopy(self.constructor_specification.declarations)
for e in self.mapping_specification.declarations.elements:
merged_declarations.push_back(copy.deepcopy(e))
code += merged_declarations.projection_code(spec)
return code
class sort_specification():
def __init__(self, declarations):
assert(isinstance(declarations, sort_declaration_list))
self.declarations = declarations
self.namespace = ""
self.original_namespace = ""
def is_alias(self, sort_expr):
assert(isinstance(sort_expr, sort_expression))
return self.declarations.is_alias(sort_expr)
def merge_structured_sorts(self, function_spec):
return self.declarations.merge_structured_sorts(function_spec)
def set_namespace(self, string):
self.namespace = string
if self.original_namespace == "":
self.original_namespace = string
self.declarations.set_namespace(string)
def has_sort(self, sort):
assert(isinstance(sort, sort_expression))
for d in self.declarations.elements:
if str(d.sort_expression) == str(sort):
return True
return False
def get_sort(self, sort):
assert(self.has_sort(sort))
for d in self.declarations.elements:
if str(d.sort_expression) == str(sort):
return d
def defines_container(self):
return self.declarations.defines_container()
def defines_struct(self):
return self.declarations.defines_struct()
def merge_specification(self, spec, is_supertype):
self.declarations.merge_declarations(spec.declarations, is_supertype)
def __str__(self):
return "sort {0}".format(self.declarations)
def structured_sort_constructor_code(self):
return self.declarations.structured_sort_constructor_code()
def structured_sort_mapping_code(self):
return self.declarations.structured_sort_mapping_code()
def structured_sort_equation_code(self):
return self.declarations.structured_sort_equation_code()
def code(self,spec):
return self.declarations.code(spec)
class specification():
def __init__(self, sort_spec, function_spec, variable_spec, equation_spec):
assert(isinstance(sort_spec, sort_specification))
assert(isinstance(function_spec, function_specification))
assert(isinstance(variable_spec, variable_specification))
assert(isinstance(equation_spec, equation_specification))
self.sort_specification = sort_spec
self.function_specification = function_spec
self.variable_specification = variable_spec
self.equation_specification = equation_spec
self.includes = None
self.uses = None
self.subtypes = None
self.namespace = ""
self.original_namespace = ""
self.originfile = ""
def set_includes(self, includes):
assert(isinstance(includes, include_list))
self.includes = includes
def set_using(self, uses):
assert(isinstance(uses, using_list))
self.uses = uses
def set_subtypes(self, subtypes):
self.subtypes = subtypes
def sort_parameters(self):
if self.uses != None:
return self.uses.sort_parameters()
else:
return []
def has_lambda(self):
return self.equation_specification.has_lambda()
def has_forall(self):
return self.equation_specification.has_forall()
def has_exists(self):
return self.equation_specification.has_exists()
def defines_container(self):
return self.sort_specification.defines_container()
def defines_struct(self):
return self.sort_specification.defines_struct()
def set_origin_file(self, f):
self.origin_file = f
def get_namespace(self):
if self.namespace <> "undefined" and self.namespace <> "":
return self.namespace
else:
return self.origin_file[:len(self.origin_file)-5]
def set_namespace(self):
if (len(self.sort_specification.declarations.elements) == 0):
s = "undefined"
else:
s = "{0}".format(self.sort_specification.declarations.elements[0].label)
self.namespace = s
if self.original_namespace == "":
self.original_namespace = s
self.sort_specification.set_namespace(s)
self.function_specification.set_namespace(s)
self.equation_specification.set_namespace(s)
def merge_specification(self, spec):
assert(isinstance(spec, specification))
self.sort_specification.merge_specification(spec.sort_specification, self.subtypes <> None)
self.function_specification.merge_specification(spec.function_specification, self.subtypes <> None)
def __str__(self):
res = []
if self.includes:
res.append(str(self.includes))
res.append(str(self.sort_specification))
res.append(str(self.function_specification))
res.append(str(self.variable_specification))
res.append(str(self.equation_specification))
return "\n".join(res)
def code(self):
# Add structured sorts to constructor declarations
self.function_specification = self.sort_specification.merge_structured_sorts(self.function_specification)
code = ""
code += "// Author(s): Jeroen Keiren\n"
code += "// Copyright: see the accompanying file COPYING or copy at\n"
code += "// https://svn.win.tue.nl/trac/MCRL2/browser/trunk/COPYING\n"
code += "//\n"
code += "// Distributed under the Boost Software License, Version 1.0.\n"
code += "// (See accompanying file LICENSE_1_0.txt or copy at\n"
code += "// http://www.boost.org/LICENSE_1_0.txt)\n"
code += "//\n"
code += "/// \\file mcrl2/data/%s.h\n" % (remove_underscore(self.get_namespace()))
code += "/// \\brief The standard sort %s.\n" % (self.get_namespace())
code += "///\n"
code += "/// This file was generated from the data sort specification\n"
code += "/// mcrl2/data/build/%s.spec.\n" % (remove_underscore(self.get_namespace()))
code += "\n"
code += "#ifndef MCRL2_DATA_%s_H\n" % (self.get_namespace().upper())
code += "#define MCRL2_DATA_%s_H\n\n" % (self.get_namespace().upper())
code += "#include \"mcrl2/utilities/exception.h\"\n"
code += "#include \"mcrl2/data/basic_sort.h\"\n"
code += "#include \"mcrl2/data/function_sort.h\"\n"
code += "#include \"mcrl2/data/function_symbol.h\"\n"
code += "#include \"mcrl2/data/application.h\"\n"
code += "#include \"mcrl2/data/data_equation.h\"\n"
code += "#include \"mcrl2/atermpp/container_utility.h\"\n"
code += "#include \"mcrl2/data/standard.h\"\n"
# code += "#include \"mcrl2/data/data_specification.h\"\n"
if self.has_lambda():
code += "#include \"mcrl2/data/lambda.h\"\n"
if self.has_forall():
code += "#include \"mcrl2/data/forall.h\"\n"
if self.has_exists():
code += "#include \"mcrl2/data/exists.h\"\n"
if self.defines_container():
code += "#include \"mcrl2/data/container_sort.h\"\n"
if self.defines_struct():
code += "#include \"mcrl2/data/structured_sort.h\"\n"
if self.includes != None:
code += self.includes.code()
code += "\n"
code += "namespace mcrl2 {\n\n"
code += " namespace data {\n\n"
if self.namespace <> "undefined":
code += " /// \\brief Namespace for system defined sort %s\n" % (escape(self.get_namespace()))
code += " namespace sort_%s {\n\n" % (self.get_namespace())
code += self.sort_specification.code(self)
code += self.function_specification.code(self)
code += self.equation_specification.code(self, self.function_specification, self.variable_specification)
code += "\n"
dependent_sorts = set([])
auxiliary_sorts = set([])
for e in self.sort_specification.declarations.elements:
if str(e).startswith("@") and e.original_namespace == self.namespace:
auxiliary_sorts.add(" specification.add_system_defined_sort(%s);\n" % (e.inline_code(self)))
if self.namespace <> "undefined":
code += " } // namespace sort_%s\n\n" % (self.get_namespace())
code += " } // namespace data\n\n"
code += "} // namespace mcrl2\n\n"
code += "#endif // MCRL2_DATA_%s_H\n" % (self.get_namespace().upper())
code = string.replace(code, "__", "_")
p = re.compile('sort_([A-Za-z0-9]*)_([ ]|:)')
code = p.sub(r'sort_\1\2', code)
p = re.compile('is_([A-Za-z0-9]*)_\(')
code = p.sub(r'is_\1(',code)
return code
class include_list():
def __init__(self, elements):
assert(all(map(lambda x: isinstance(x, include), elements)))
self.elements = elements
def push_back(self, element):
assert(isinstance(element, include))
self.elements += [element]
def __str__(self):
return "".join(["{0}\n".format(e) for e in self.elements])
def code(self):
s = ""
for e in self.elements:
s += "%s\n" % (e.code())
return s
class include():
def __init__(self, id):
assert(isinstance(id, identifier))
self.identifier = id
def __str__(self):
return "#include {0}".format(self.identifier)
def code(self):
return "#include \"mcrl2/data/{0}.h\"".format(str(self.identifier)[:-5])
class subtype_list():
def __init__(self, elements):
assert(all(map(lambda x: isinstance(x, subtype), elements)))
self.elements = elements
def push_back(self, element):
assert(isinstance(element, subtype))
self.elements += [element]
def __str__(self):
return "".join(["{0}\n".format(e) for e in self.elements])
def code(self):
s = ""
for e in self.elements:
s += "%s\n" % (e.code())
return s
class subtype():
def __init__(self, id):
assert(isinstance(id, identifier))
self.identifier = id
def __str__(self):
return "#supertypeof {0}".format(self.identifier)
class using_list():
def __init__(self, elements):
assert(all(map(lambda x: isinstance(x, using), elements)))
self.elements = elements
def push_back(self, element):
assert(isinstance(element, using))
self.elements += [element]
def sort_parameters(self):
return map(lambda x: x.sort_parameters(), self.elements)
def __str__(self):
return ", ".join(map(str, self.elements))
def code(self):
code = ""
for e in self.elements:
code += "%s\n" % (e.code())
return code
class using():
def __init__(self, identifier):
self.identifier = identifier
def __str__(self):
return str(self.identifier)
def sort_parameters(self):
return sort_identifier(self.identifier)
def code(self):
return str(self)
|