1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140
|
/*
* Copyright (c) 2002-2006 Samit Basu
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "Interpreter.hpp"
#include <math.h>
#include <stdio.h>
#include "Exception.hpp"
#include "Math.hpp"
#include "Array.hpp"
#include "Struct.hpp"
#include "Parser.hpp"
#include "Scanner.hpp"
#include "Token.hpp"
#include "Module.hpp"
#include "File.hpp"
#include <signal.h>
#include "Class.hpp"
#include "Print.hpp"
#include "MemPtr.hpp"
#include <qeventloop.h>
#include <QtCore>
#include <fstream>
#include <stdarg.h>
#include "JITFactory.hpp"
#include "JITInfo.hpp"
#include "IEEEFP.hpp"
#include "Algorithms.hpp"
#include "GetSet.hpp"
#include "FuncPtr.hpp"
#include "AnonFunc.hpp"
#include "Stats.hpp"
#include <QtGui>
#include "DebugStream.hpp"
#include "CArray.hpp"
#ifdef _WIN32
#define PATHSEP ";"
#else
#define PATHSEP ":"
#endif
#ifdef _WIN32
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif
const int max_line_count = 1000000;
/**
* The database of compiled code segments
*/
QMap<int,JITInfo> m_codesegments;
void ClearJITCache() {
m_codesegments.clear();
}
/**
* The file system watcher -- watches for changes to the file system
* Only one interpreter thread should use this watcher at a time.
*/
//QFileSystemWatcher m_watch;
#define SaveEndInfo \
ArrayReference oldEndRef = endRef; \
int oldEndCount = endCount; \
int oldEndTotal = endTotal;
#define RestoreEndInfo \
endRef = oldEndRef; \
endCount = oldEndCount; \
endTotal = oldEndTotal; \
QString TildeExpand(QString path) {
if ((path.size() > 0) && (path[0] == '~')) {
path.remove(0,1);
return QDir::homePath() + path;
}
return path;
}
class CLIDisabler {
Interpreter *p;
bool CLIFlagSave;
public:
CLIDisabler(Interpreter *q) : p(q) {
CLIFlagSave = p->inCLI();
p->setInCLI(false);
}
~CLIDisabler() {
p->setInCLI(CLIFlagSave);
}
};
void Interpreter::setPath(QString path) {
if (path == m_userCachePath) return;
QStringList pathset(path.split(PATHSEP,QString::SkipEmptyParts));
m_userPath.clear();
for (int i=0;i<pathset.size();i++)
if (pathset[i] != ".") {
QDir tpath(TildeExpand(pathset[i]));
m_userPath << tpath.absolutePath();
}
setupWatcher();
// rescanPath();
updateFileTool();
m_userCachePath = path;
}
QString Interpreter::getTotalPath() {
QString retpath;
QStringList totalPath(QStringList() << m_basePath << m_userPath);
for (int i=0;i<totalPath.size()-1;i++)
retpath = retpath + totalPath[i] + PATHSEP;
if (totalPath.size() > 0)
retpath = retpath + totalPath[totalPath.size()-1];
return retpath;
}
QString Interpreter::getPath() {
QString retpath;
QStringList totalPath(m_userPath);
for (int i=0;i<totalPath.size()-1;i++)
retpath = retpath + totalPath[i] + PATHSEP;
if (totalPath.size() > 0)
retpath = retpath + totalPath[totalPath.size()-1];
return retpath;
}
void Interpreter::setLiveUpdateFlag(bool t) {
m_liveUpdateFlag = t;
if (t) {
// connect(&m_watch,SIGNAL(directoryChanged(const QString &)),
// this,SLOT(updateFileTool(const QString &)));
}
}
// static bool DirExists(const QString & path) {
// QDir tmp(path);
// return tmp.exists();
// }
void Interpreter::setupWatcher() {
if (!m_liveUpdateFlag) return;
// QStringList pathLists(m_watch.directories());
// if (!pathLists.isEmpty())
// m_watch.removePaths(pathLists);
// if (!m_userPath.isEmpty()) {
// for (int i=0;i<m_userPath.size();i++)
// if (DirExists(m_userPath[i]))
// m_watch.addPath(m_userPath[i]);
// }
// if (!m_basePath.isEmpty()) {
// for (int i=0;i<m_basePath.size();i++)
// if (DirExists(m_basePath[i]))
// m_watch.addPath(m_basePath[i]);
// }
// m_watch.addPath(QDir::currentPath());
}
void Interpreter::changeDir(QString path) {
if (QDir(path) == QDir::currentPath())
return;
if (!QDir::setCurrent(path))
throw Exception("Unable to change to specified directory: " + path);
if (m_liveUpdateFlag)
emit CWDChanged(QDir::currentPath());
setupWatcher();
rescanPath();
// updateFileTool();
}
void Interpreter::updateVariablesTool() {
if (!m_liveUpdateFlag) return;
StringVector varList(context->listAllVariables());
QList<QVariant> vars;
for (int i=0;i<varList.size();i++) {
QList<QVariant> entry;
// Icon
entry << QVariant();
entry << QVariant(varList[i]);
Array *dp = context->lookupVariableLocally(varList[i]);
if (dp) {
// class
if (dp->allReal())
entry << QVariant(dp->className());
else
entry << QVariant(dp->className() + " (complex)");
// value
entry << QVariant(SummarizeArrayCellEntry(*dp));
// size
entry << QVariant(dp->dimensions().toString());
// bytes min, max, range, mean, var, std
entry << QVariant(double(dp->bytes()));
entry += ComputeVariableStats(dp);
}
for (int i=entry.size();i<10;i++)
entry << QVariant();
vars << QVariant(entry);
}
emit updateVarView(QVariant(vars));
}
static bool InSpecificScope(Context *context, QString name, QString detail) {
return ((context->scopeName() == name) &&
(context->scopeDetailString() == detail));
}
static bool InKeyboardScope(Context *context) {
return InSpecificScope(context,"keyboard","keyboard");
}
static QString GetStackToolDescription(Context *context) {
int line = int(LineNumber(context->scopeTokenID()));
if (line > 0)
return QString(context->scopeDetailString() + QString("(%1)").arg(line));
else
return context->scopeDetailString();
}
void Interpreter::updateStackTool() {
QStringList stackInfo;
// Do a complete dump...
// Suppose we start with
int f_depth = context->scopeDepth();
context->restoreBypassedScopes();
int t_depth = context->scopeDepth();
for (int i=f_depth;i<t_depth;i++) {
if (!InKeyboardScope(context) && !context->scopeDetailString().isEmpty())
stackInfo << GetStackToolDescription(context);
context->bypassScope(1);
}
bool firstline = true;
for (int i=0;i<f_depth;i++) {
if (!InKeyboardScope(context) &&
!InSpecificScope(context,"docli","builtin") &&
!context->scopeDetailString().isEmpty()) {
if (firstline) {
stackInfo << QString("*") + GetStackToolDescription(context);
firstline = false;
} else
stackInfo << GetStackToolDescription(context);
}
context->bypassScope(1);
}
context->restoreBypassedScopes();
while (context->scopeDepth() > f_depth) context->bypassScope(1);
emit updateStackView(stackInfo);
}
void Interpreter::updateFileTool(const QString &) {
updateFileTool();
}
void Interpreter::updateFileTool() {
// Build the info to send to the file tool
QDir dir(QDir::currentPath());
dir.setFilter(QDir::Files|QDir::Dirs|QDir::NoDotAndDotDot);
QFileInfoList list(dir.entryInfoList());
QList<QVariant> files;
QList<QVariant> entry;
entry << QVariant(QString("dir"));
entry << QVariant(" .. (Parent Folder)");
entry << QVariant();
entry << QVariant();
entry << QVariant("Folder");
files << QVariant(entry);
for (int i=0;i<((int)list.size());i++) {
QList<QVariant> entry;
QFileInfo fileInfo(list.at(i));
if (fileInfo.isDir())
entry << QVariant(QString("dir"));
else
entry << QVariant(QString("file"));
entry << QVariant(fileInfo.fileName());
entry << QVariant(fileInfo.size());
entry << QVariant(fileInfo.lastModified());
if (fileInfo.isDir())
entry << QVariant(QString("Folder"));
else if (fileInfo.suffix().isEmpty())
entry << QVariant("File");
else
entry << QVariant(QString(fileInfo.suffix() + " File"));
files << QVariant(entry);
}
emit updateDirView(QVariant(files));
}
void Interpreter::rescanPath() {
if (m_disablerescan) return;
if (!context) return;
context->flushTemporaryGlobalFunctions();
for (int i=0;i<m_basePath.size();i++)
scanDirectory(m_basePath[i],false,"");
for (int i=0;i<m_userPath.size();i++)
scanDirectory(m_userPath[i],false,"");
// Scan the current working directory.
scanDirectory(QDir::currentPath(),true,"");
updateFileTool();
}
void Interpreter::setBasePath(QStringList pth) {
m_basePath = pth;
}
void Interpreter::setUserPath(QStringList pth) {
m_userPath = pth;
}
static QString mexExtension() {
#ifdef Q_OS_LINUX
return "fmxglx";
#endif
#ifdef Q_OS_MACX
return "fmxmac";
#endif
#ifdef Q_OS_WIN32
return "fmxw32";
#endif
return "fmx";
}
void Interpreter::scanDirectory(QString scdir, bool tempfunc,
QString prefix) {
QDir dir(scdir);
dir.setFilter(QDir::Files|QDir::Dirs|QDir::NoDotAndDotDot);
dir.setNameFilters(QStringList() << "*.m" << "*.p"
<< "@*" << "private" << "*."+mexExtension());
QFileInfoList list(dir.entryInfoList());
for (int i=0;i<((int)list.size());i++) {
QFileInfo fileInfo(list.at(i));
QString fileSuffix(fileInfo.suffix());
QString fileBaseName(fileInfo.baseName());
QString fileAbsoluteFilePath(fileInfo.absoluteFilePath());
if (fileSuffix == "m" || fileSuffix == "M")
if (prefix.isEmpty())
procFileM(fileBaseName,fileAbsoluteFilePath,tempfunc);
else
procFileM(prefix + ":" + fileBaseName,fileAbsoluteFilePath,tempfunc);
else if (fileSuffix == "p" || fileSuffix == "P")
if (prefix.isEmpty())
procFileP(fileBaseName,fileAbsoluteFilePath,tempfunc);
else
procFileP(prefix + ":" + fileBaseName,fileAbsoluteFilePath,tempfunc);
else if (fileBaseName[0] == '@')
scanDirectory(fileAbsoluteFilePath,tempfunc,fileBaseName);
else if (fileBaseName.toUpper() == "PRIVATE")
scanDirectory(fileAbsoluteFilePath,tempfunc,fileAbsoluteFilePath);
else
procFileMex(fileBaseName,fileAbsoluteFilePath,tempfunc);
}
}
void Interpreter::procFileM(QString fname, QString fullname, bool tempfunc) {
MFunctionDef *adef;
adef = new MFunctionDef();
adef->name = fname;
adef->fileName = fullname;
adef->temporaryFlag = tempfunc;
FuncPtr val;
if (context->lookupFunction(fname,val))
if (val->type() == FM_BUILT_IN_FUNCTION)
warningMessage("built in function " + fname + " will be shadowed by the script " + fullname);
context->insertFunction(adef, tempfunc);
}
void Interpreter::procFileP(QString fname, QString fullname, bool tempfunc) {
throw Exception("P-files are not supported in this version of FreeMat");
}
void Interpreter::procFileMex(QString fname, QString fullname, bool tempfunc) {
MexFunctionDef *adef;
adef = new MexFunctionDef(fullname);
adef->name = fname;
if (adef->LoadSuccessful())
context->insertFunction((MFunctionDef*)adef,tempfunc);
else
delete adef;
}
void Interpreter::RegisterGfxResults(ArrayVector m) {
mutex.lock();
gfx_buffer.push_back(m);
gfxBufferNotEmpty.wakeAll();
mutex.unlock();
}
void Interpreter::RegisterGfxError(QString msg) {
mutex.lock();
gfxError = msg;
gfxErrorOccured = true;
gfxBufferNotEmpty.wakeAll();
mutex.unlock();
}
ArrayVector Interpreter::doFunction(FuncPtr f, ArrayVector& m,
int narg_out, VariableTable *vtable) {
CLIDisabler dis(this);
PopContext saver(context,0);
context->pushScope(f->functionName(),f->detailedName(),false);
if (f->graphicsFunction) {
gfxErrorOccured = false;
QMutexLocker lock(&mutex);
emit doGraphicsCall(this,f,m,narg_out);
if (!gfxErrorOccured && gfx_buffer.empty()) {
gfxBufferNotEmpty.wait(&mutex);
} else {
dbout << "Wha??\n";
}
if (gfxErrorOccured) {
throw Exception(gfxError);
}
if (gfx_buffer.empty())
dbout << "Warning! graphics empty on return\n";
ArrayVector ret(gfx_buffer.front());
gfx_buffer.erase(gfx_buffer.begin());
return ret;
} else {
ArrayVector ret(f->evaluateFunc(this,m,narg_out,vtable));
if (context->scopeStepTrap() >= 1) {
tracetrap = 1;
tracecurrentline = 0;
warningMessage("dbstep beyond end of function " + context->scopeDetailString() +
" -- setting single step mode\n");
context->setScopeStepTrap(0);
}
return ret;
}
}
void Interpreter::setTerminalWidth(int ncols) {
mutex.lock();
m_ncols = ncols;
mutex.unlock();
}
int Interpreter::getTerminalWidth() {
return m_ncols;
}
QString TranslateString(QString x) {
return x.replace("\n","\r\n");
}
void Interpreter::diaryMessage(QString msg) {
QFile file(m_diaryFilename);
if (file.open(QIODevice::WriteOnly | QIODevice::Append)) {
QTextStream os(&file);
os << msg;
}
}
void Interpreter::outputMessage(QString msg) {
if (m_diaryState) diaryMessage(msg);
if (m_captureState)
m_capture += msg;
else
if (m_quietlevel < 2)
emit outputRawText(TranslateString(msg));
}
void Interpreter::outputMessage(const char* format,...) {
char buffer[4096];
va_list ap;
va_start(ap,format);
vsnprintf(buffer,4096,format,ap);
va_end(ap);
outputMessage(QString(buffer));
}
void Interpreter::errorMessage(QString msg) {
if (m_diaryState) diaryMessage("Error: " + msg + "\n");
if (m_captureState)
m_capture += "Error: " + msg + "\n";
else
if (m_quietlevel < 2)
emit outputRawText(TranslateString("Error: " + msg + "\r\n"));
}
void Interpreter::warningMessage(QString msg) {
static QString lastWarning;
static bool lastWarningRepeat = false;
if (!m_enableWarnings) return;
if (m_diaryState) diaryMessage("Warning: " + msg + "\n");
if (m_captureState)
m_capture += "Warning: " + msg + "\n";
else
if (m_quietlevel < 2) {
if (lastWarning != msg) {
emit outputRawText(TranslateString("Warning: " +msg + "\r\n"));
lastWarningRepeat = false;
lastWarning = msg;
} else {
if (!lastWarningRepeat) {
emit outputRawText(TranslateString("Warning: Last warning repeats... suppressing more of these\r\n"));
lastWarningRepeat = true;
}
}
}
}
static bool isMFile(QString arg) {
// Not completely right...
return (((arg[arg.size()-1] == 'm') ||
(arg[arg.size()-1] == 'p')) &&
(arg[arg.size()-2] == '.'));
}
QString TrimFilename(QString arg) {
int ndx = arg.lastIndexOf(QDir::separator());
if (ndx>=0)
arg.remove(0,ndx+1);
return arg;
}
QString TrimExtension(QString arg) {
if (arg.size() > 2 && arg[arg.size()-2] == '.')
arg.remove(arg.size()-2,arg.size());
return arg;
}
static QString PrivateMangleName(QString currentFunctionPath, QString fname) {
if (currentFunctionPath.isEmpty()) return "";
// First look to see if we are already a private function
QString separator("/");
int ndx1 = currentFunctionPath.lastIndexOf(separator + "private" + separator);
if (ndx1>=0) {
// The current function is already in a private directory
// In that case, try to find a private function in the same directory
currentFunctionPath.remove(ndx1+1,currentFunctionPath.size());
return currentFunctionPath + "private:" + fname;
}
int ndx;
ndx = currentFunctionPath.lastIndexOf(separator);
if (ndx>=0)
currentFunctionPath.remove(ndx+1,currentFunctionPath.size());
return currentFunctionPath + "private:" + fname;
}
static QString LocalMangleName(QString currentFunctionPath, QString fname) {
int ndx = currentFunctionPath.lastIndexOf("/");
if (ndx >= 0)
currentFunctionPath.remove(ndx,currentFunctionPath.size());
QString tmp = currentFunctionPath + "/" + fname;
return currentFunctionPath + "/" + fname;
}
static QString NestedMangleName(QString cfunc, QString fname) {
return cfunc + "/" + fname;
}
QString Interpreter::getVersionString() {
return QString("FreeMat v" FREEMAT_VERSION);
}
// Run the thread function
void Interpreter::run() {
if (m_threadFunc) {
try {
m_threadFuncRets = doFunction(m_threadFunc,m_threadFuncArgs,m_threadNargout);
} catch (Exception &e) {
m_threadErrorState = true;
lasterr = e.msg();
} catch (InterpreterQuitException &e) {
m_threadErrorState = true;
lasterr = "'quit' called in non-main thread";
} catch (InterpreterKillException &e) {
m_kill = false;
} catch (InterpreterRetallException &e) {
} catch (exception& e) {
m_threadErrorState = true;
lasterr = "thread crashed!! - you have encountered a bug in FreeMat - please file bug report describing what happened";
} catch (...) {
m_threadErrorState = true;
lasterr = "thread crashed!! - you have encountered a bug in FreeMat - please file bug report describing what happened";
}
}
}
void Interpreter::doCLI() {
// rescanPath();
emit CWDChanged(QDir::currentPath());
updateFileTool();
if (!m_skipflag)
sendGreeting();
try {
while (1) {
int scope_stackdepth = context->scopeDepth();
try {
evalCLI();
} catch (InterpreterRetallException) {
} catch (InterpreterReturnException &e) {
}
while (context->scopeDepth() > scope_stackdepth) context->popScope();
}
} catch (InterpreterQuitException &e) {
emit QuitSignal();
} catch (std::exception& e) {
qDebug() << "crash: " << e.what();
emit CrashedSignal();
} catch (...) {
emit CrashedSignal();
}
}
void Interpreter::sendGreeting() {
outputMessage(" " + getVersionString() + "\n");
outputMessage(" Copyright (c) 2002-2008 by Samit Basu\n");
outputMessage(" Licensed under the GNU Public License (GPL)\n");
outputMessage(" Type <help license> to find out more\n");
outputMessage(" <helpwin> for online help\n");
outputMessage(" <pathtool> to set or change your path\n");
outputMessage(" Use <dbauto on/off> to control stop-on-error behavior\n");
outputMessage(" Use ctrl-b to stop execution of a function/script\n");
outputMessage(" JIT is enabled by default - use jitcontrol to change it\n");
outputMessage(" Use <rootpath gui> to set/change where the FreeMat toolbox is installed\n");
outputMessage("");
}
bool Interpreter::inMFile() const {
return (isMFile(context->activeScopeName()));
}
void Interpreter::debugDump() {
int depth = context->scopeDepth();
qDebug() << "******************************\n";
for (int i=0;i<depth;i++) {
if (context->isScopeActive())
qDebug() << "In " << context->scopeName() << " (" << context->scopeDetailString() << ")*";
else
qDebug() << "In " << context->scopeName() << " (" << context->scopeDetailString() << ")";
context->bypassScope(1);
}
context->restoreScope(depth);
}
//DOCBLOCK debug_dbup
void Interpreter::dbup() {
// The stack should look like --
// base, foo, keyboard, dbup
// so to do a dbup, we have to save the top two of the
// stack, move foo to the backup stack, and then restore
//
// Consider the following. The stack looks like this:
// main: base foo1 keyboard foo2 keyboard dbup
// bypass: <empty>
//
// Now we do a dbup
// main: base foo1 keyboard keyboard dbup
// bypass foo2
//
// Suppose we
// We need the "keyboard" states on the stack because they
// capture the context updates for the command line routines.
//
if (InSpecificScope(context,"docli","builtin"))
return;
context->reserveScope();
while (InKeyboardScope(context))
context->bypassScope(1);
if (!InSpecificScope(context,"base","base") &&
!InSpecificScope(context,"docli","builtin")) {
// Bypass a single non-keyboard context
context->bypassScope(1);
}
while (InKeyboardScope(context))
context->bypassScope(1);
context->unreserveScope();
}
//DOCBLOCK debug_dbdown
void Interpreter::dbdown() {
// Save the one for the "dbdown" command
// Save the one for the "keyboard" command that we are currently in
context->reserveScope();
// Restore until we get a non-"keyboard" scope
context->restoreScope(1);
while (InKeyboardScope(context))
context->restoreScope(1);
context->unreserveScope();
dbdown_executed = true;
}
QString Interpreter::getLocalMangledName(QString fname) {
QString ret;
if (isMFile(context->activeScopeName()))
ret = LocalMangleName(context->activeScopeDetailString(),fname);
else
ret = fname;
return ret;
}
QString Interpreter::getPrivateMangledName(QString fname) {
QString ret;
if (isMFile(context->scopeName()))
ret = PrivateMangleName(context->scopeName(),fname);
else {
ret = QDir::currentPath() +
QString(QDir::separator()) +
QString("private:" + fname);
}
return ret;
}
QString Interpreter::getMFileName() {
if (isMFile(context->scopeName()))
return TrimFilename(TrimExtension(context->scopeName()));
// TESTME
// for (int i=cstack.size()-1;i>=0;i--)
// if (isMFile(cstack[i].cname))
// return TrimFilename(TrimExtension(cstack[i].cname));
return QString("");
}
// called by editor
QString Interpreter::getInstructionPointerFileName() {
if (!InCLI) return QString("");
ParentScopeLocker lock(context);
QString filename(context->scopeName());
if (isMFile(filename))
return filename;
return QString("");
}
Array Interpreter::DoBinaryOperator(const Tree & t, BinaryFunc fnc,
QString funcname) {
Array a(expression(t.first()));
Array b(expression(t.second()));
if (!(a.isUserClass() || b.isUserClass()))
return fnc(a,b);
return ClassBinaryOperator(a,b,funcname,this);
}
Array Interpreter::DoUnaryOperator(const Tree & t, UnaryFunc fnc,
QString funcname) {
Array a(expression(t.first()));
if (!a.isUserClass())
return fnc(a);
return ClassUnaryOperator(a,funcname,this);
}
void Interpreter::setPrintLimit(int lim) {
printLimit = lim;
}
int Interpreter::getPrintLimit() {
return(printLimit);
}
//DOCBLOCK variables_matrix
//Works
Array Interpreter::matrixDefinition(const Tree & t) {
ArrayMatrix m;
if (t.numChildren() == 0)
return EmptyConstructor();
for (int i=0;i<t.numChildren();i++) {
const Tree & s(t.child(i));
ArrayVector n;
for (int j=0;j<s.numChildren();j++)
multiexpr(s.child(j),n);
m.push_back(n);
}
// Check if any of the elements are user defined classes
bool anyuser = false;
for (int i=0;i<m.size() && !anyuser;i++)
for (int j=0;j<m[i].size() && !anyuser;j++)
if (m[i][j].isUserClass())
anyuser = true;
if (!anyuser)
return MatrixConstructor(m);
else
return ClassMatrixConstructor(m,this);
}
//DOCBLOCK variables_cell
//Works
Array Interpreter::cellDefinition(const Tree & t) {
ArrayMatrix m;
if (t.numChildren() == 0)
return Array(CellArray);
for (int i=0;i<t.numChildren();i++) {
const Tree & s(t.child(i));
ArrayVector n;
for (int j=0;j<s.numChildren();j++)
multiexpr(s.child(j),n);
m.push_back(n);
}
return CellConstructor(m);
}
Array Interpreter::ShortCutOr(const Tree & t) {
Array a(expression(t.first()));
Array retval;
if (!a.isScalar())
retval = DoBinaryOperator(t,Or,"or");
else {
// A is a scalar - is it true?
if (a.toClass(Bool).constRealScalar<bool>())
retval = a.toClass(Bool);
else
retval = DoBinaryOperator(t,Or,"or");
}
return retval;
}
Array Interpreter::ShortCutAnd(const Tree & t) {
context->setScopeTokenID(t.context());
Array a(expression(t.first()));
context->setScopeTokenID(t.context());
Array retval;
if (!a.isScalar()) {
retval = DoBinaryOperator(t,And,"and");
} else {
// A is a scalar - is it false?
if (!a.toClass(Bool).constRealScalar<bool>())
retval = a.toClass(Bool);
else
retval = DoBinaryOperator(t,And,"and");
}
return retval;
}
//Works
// Need to take care
ArrayVector Interpreter::handleReindexing(const Tree & t, const ArrayVector &p) {
if (t.numChildren() > 2)
if (p.size() > 1)
throw Exception("reindexing of function expressions not allowed when multiple values are returned by the function");
else {
Array r;
if (p.size() == 1)
r = p[0];
else
r = EmptyConstructor();
for (int index = 2;index < t.numChildren();index++)
deref(r,t.child(index));
return ArrayVector() << r;
}
else
return p;
}
void Interpreter::multiexpr(const Tree & t, ArrayVector &q, index_t lhsCount, bool output_optional) {
if (t.is(TOK_VARIABLE)) {
ArrayReference ptr(context->lookupVariable(t.first().text()));
if (!ptr.valid()) {
ArrayVector p;
functionExpression(t,int(lhsCount),output_optional,p);
q += handleReindexing(t,p);
return;
}
if (t.numChildren() == 1) {
q.push_back(*ptr);
return;
}
if (ptr->isUserClass() && !stopoverload) {
q += ClassRHSExpression(*ptr,t,this);
return;
}
Array r(*ptr);
for (int index = 1;index < t.numChildren()-1;index++)
deref(r,t.child(index));
SaveEndInfo;
endRef = &r;
const Tree & s(t.last());
if (s.is(TOK_PARENS)) {
ArrayVector m;
endTotal = s.numChildren();
if (s.numChildren() == 0)
q.push_back(r);
else {
for (int p = 0;p < s.numChildren(); p++) {
endCount = m.size();
multiexpr(s.child(p),m);
}
subsindex(m);
if (m.size() == 1)
q.push_back(r.get(m.front()));
else
q.push_back(r.get(m));
}
} else if (s.is(TOK_BRACES)) {
ArrayVector m;
endTotal = s.numChildren();
for (int p = 0;p < s.numChildren(); p++) {
endCount = m.size();
multiexpr(s.child(p),m);
}
subsindex(m);
if (m.size() == 1)
q += ArrayVectorFromCellArray(r.get(m.front()));
else
q += ArrayVectorFromCellArray(r.get(m));
} else if (s.is('.')) {
q += r.get(s.first().text());
} else if (s.is(TOK_DYN)) {
QString field;
try {
Array fname(expression(s.first()));
field = fname.asString();
} catch (Exception &e) {
throw Exception("dynamic field reference to structure requires a string argument");
}
q += r.get(field);
}
RestoreEndInfo;
} else if (!t.is(TOK_KEYWORD))
q.push_back(expression(t));
}
Array Interpreter::expression(const Tree & t) {
switch(t.token()) {
case TOK_VARIABLE:
return rhs(t);
case TOK_REAL:
case TOK_IMAG:
case TOK_REALF:
case TOK_IMAGF:
case TOK_STRING:
return t.array();
case TOK_REINDEX:
{
Array r = expression(t.first());
for (int index = 1;index < t.numChildren();index++)
deref(r,t.child(index));
return r;
}
case TOK_INCR_PREFIX:
{
Array dummy = rhs(t.first());
Array ret = Add(dummy,Array((double)(1)));
assignment(t.first(),false,ret);
return ret;
}
case TOK_DECR_PREFIX:
{
Array dummy = rhs(t.first());
Array ret = Subtract(dummy,Array((double)(1)));
assignment(t.first(),false,ret);
return ret;
}
case TOK_INCR_POSTFIX:
{
Array dummy = rhs(t.first());
Array ret = Add(dummy,Array((double)(1)));
assignment(t.first(),false,ret);
return dummy;
}
case TOK_DECR_POSTFIX:
{
Array dummy = rhs(t.first());
Array ret = Subtract(dummy,Array((double)(1)));
assignment(t.first(),false,ret);
return dummy;
}
case TOK_END:
if (!endRef.valid())
throw Exception("END keyword not allowed for undefined variables");
if (endTotal == 1)
return Array(double(endRef->length()));
else
return Array(double(endRef->dimensions()[endCount]));
case ':':
if (t.numChildren() == 0) {
return Array(QString(":"));
} else if (t.first().is(':')) {
return doubleColon(t);
} else {
return unitColon(t);
}
break;
case TOK_MATDEF:
return matrixDefinition(t);
break;
case TOK_CELLDEF:
return cellDefinition(t);
break;
case '+':
return DoBinaryOperator(t,Add,"plus");
break;
case '-':
return DoBinaryOperator(t,Subtract,"minus");
break;
case '*':
return DoBinaryOperator(t,Multiply,"mtimes");
break;
case '/':
return DoBinaryOperator(t,RightDivide,"mrdivide");
break;
case '\\':
return DoBinaryOperator(t,LeftDivide,"mldivide");
break;
case TOK_SOR:
return ShortCutOr(t);
break;
case '|':
return DoBinaryOperator(t,Or,"or");
break;
case TOK_SAND:
return ShortCutAnd(t);
break;
case '&':
return DoBinaryOperator(t,And,"and");
case '<':
return DoBinaryOperator(t,LessThan,"lt");
break;
case TOK_LE:
return DoBinaryOperator(t,LessEquals,"le");
break;
case '>':
return DoBinaryOperator(t,GreaterThan,"gt");
break;
case TOK_GE:
return DoBinaryOperator(t,GreaterEquals,"ge");
break;
case TOK_EQ:
return DoBinaryOperator(t,Equals,"eq");
break;
case TOK_NE:
return DoBinaryOperator(t,NotEquals,"ne");
break;
case TOK_DOTTIMES:
return DoBinaryOperator(t,DotMultiply,"times");
break;
case TOK_DOTRDIV:
return DoBinaryOperator(t,DotRightDivide,"rdivide");
break;
case TOK_DOTLDIV:
return DoBinaryOperator(t,DotLeftDivide,"ldivide");
break;
case TOK_UNARY_MINUS:
return DoUnaryOperator(t,Negate,"uminus");
break;
case TOK_UNARY_PLUS:
return DoUnaryOperator(t,Plus,"uplus");
break;
case '~':
return DoUnaryOperator(t,Not,"not");
break;
case '^':
return DoBinaryOperator(t,Power,"mpower");
break;
case TOK_DOTPOWER:
return DoBinaryOperator(t,DotPower,"power");
break;
case '\'':
return DoUnaryOperator(t,Hermitian,"ctranspose");
break;
case TOK_DOTTRANSPOSE:
return DoUnaryOperator(t,Transpose,"transpose");
break;
case '@':
return FunctionPointer(t);
default:
throw Exception("Unrecognized expression!");
}
}
Array Interpreter::FunctionPointer(const Tree & t) {
if (t.first().is(TOK_ANONYMOUS_FUNC)) {
return AnonFuncConstructor(this,t.first());
} else {
FuncPtr val;
if (!lookupFunction(t.first().text(),val))
throw Exception("unable to resolve " + t.first().text() +
" to a function call");
return FuncPtrConstructor(this,val);
}
}
//DOCBLOCK operators_colon
//Works
Array Interpreter::unitColon(const Tree & t) {
Array a, b;
a = expression(t.first());
b = expression(t.second());
if (!(a.isUserClass() || b.isUserClass()))
return UnitColon(a,b);
else
return ClassBinaryOperator(a,b,"colon",this);
}
void Interpreter::deleteHandleClass(StructArray *ap)
{
// We need to call the destructor on
Array b(*ap);
delete ap;
FuncPtr val;
if (b.isUserClass() && ClassResolveFunction(this,b,"delete",val))
{
val->updateCode(this);
ArrayVector args(b);
doFunction(val,args,1);
}
}
//Works
Array Interpreter::doubleColon(const Tree & t) {
Array a, b, c;
a = expression(t.first().first());
b = expression(t.first().second());
c = expression(t.second());
if (!(a.isUserClass() || b.isUserClass() || c.isUserClass()))
return DoubleColon(a,b,c);
else
return ClassTrinaryOperator(a,b,c,"colon",this);
}
/**
* This somewhat strange test is used by the switch statement.
* If x is a scalar, and we are a scalar, this is an equality
* test. If x is a string and we are a string, this is a
* strcmp test. If x is a scalar and we are a cell-array, this
* test is applied on an element-by-element basis, looking for
* any matches. If x is a string and we are a cell-array, then
* this is applied on an element-by-element basis also.
*/
bool Interpreter::testCaseStatement(const Tree & t, Array s) {
Array r(expression(t.first()));
bool caseMatched = TestForCaseMatch(s,r);
if (caseMatched)
block(t.second());
return caseMatched;
}
//DOCBLOCK flow_try
//Works
void Interpreter::tryStatement(const Tree & t) {
// Turn off autostop for this statement block
bool autostop_save = autostop;
autostop = false;
bool intryblock_save = intryblock;
intryblock = true;
// Get the state of the IDnum stack and the
// contextStack and the cnameStack
int stackdepth = context->scopeDepth();
try {
block(t.first());
} catch (Exception &e) {
while (context->scopeDepth() > stackdepth) context->popScope();
if (t.numChildren()>1) {
autostop = autostop_save;
block(t.second().first());
}
}
autostop = autostop_save;
intryblock = intryblock_save;
}
//DOCBLOCK flow_switch
//Works
void Interpreter::switchStatement(const Tree & t) {
Array switchVal;
// First, extract the value to perform the switch on.
switchVal = expression(t.first());
// Assess its type to determine if this is a scalar switch
// or a string switch.
if (!switchVal.isScalar() && !switchVal.isString())
throw Exception("Switch statements support scalar and string arguments only.");
int n=1;
while (n < t.numChildren() && t.child(n).is(TOK_CASE)) {
if (testCaseStatement(t.child(n),switchVal))
return;
n++;
}
if (t.last().is(TOK_OTHERWISE))
block(t.last().first());
}
//DOCBLOCK flow_if
//Works
void Interpreter::ifStatement(const Tree & t) {
bool condtest = RealAllNonZeros(expression(t.first()));
if (condtest) {
block(t.second());
return;
} else {
int n=2;
while (n < t.numChildren() && t.child(n).is(TOK_ELSEIF)) {
if (RealAllNonZeros(expression(t.child(n).first()))) {
block(t.child(n).second());
return;
}
n++;
}
}
if (t.last().is(TOK_ELSE))
block(t.last().first());
}
static bool compileJITBlock(Interpreter *interp, const Tree & t, JITInfo & ref, JITControlFlag jitflag) {
delete ref.JITFunction();
ref.setJITState(JITInfo::FAILED);
JITFuncBase *cg = JITFactory::GetJITFunc(interp);
if (!cg) return false;
bool success = false;
try {
if (!cg->compile(t,jitflag))
{
delete cg;
success = false;
ref.setJITState(JITInfo::FAILED);
return success;
}
success = true;
ref.setJITState(JITInfo::SUCCEEDED);
ref.setJITFunction(cg);
interp->incrementJITCounter();
dbout << "Block JIT compiled at line "
<< LineNumber(interp->getContext()->scopeTokenID())
<< " of " << interp->getContext()->scopeName() << "\n";
} catch (Exception &e) {
dbout << "JIT compile failed:" << e.msg() << " at line "
<< LineNumber(interp->getContext()->scopeTokenID())
<< " of " << interp->getContext()->scopeName() << "\n";
delete cg;
success = false;
ref.setJITState(JITInfo::FAILED);
}
return success;
}
bool Interpreter::tryJitCode(const Tree & t) {
// Try to compile this block to an instruction stream
if (jitcontrol) {
int UID = t.node().UID();
JITInfo & ref = m_codesegments[UID];
try{
if (ref.JITState() == JITInfo::UNTRIED) {
bool success = compileJITBlock(this,t,ref,jitcontrol);
if (success)
{
if (ref.JITFunction()->run() == CJIT_Success)
{
ref.setJITState(JITInfo::SUCCEEDED);
return true;
}
ref.setJITState(JITInfo::FAILED);
return false;
}
} else if (ref.JITState() == JITInfo::SUCCEEDED) {
int stat = ref.JITFunction()->run();
if (stat == CJIT_Success)
return true;
// If the prep stage failed, we can try to recompile
dbout << "Prep failed for JIT block retrying\n";
if (stat == CJIT_Prepfail)
{
bool success = compileJITBlock(this,t,ref,jitcontrol);
if (success)
{
if (ref.JITFunction()->run() == CJIT_Success)
{
return true;
}
}
}
}
}catch(Exception &e){
errorMessage(e.msg());
//errorMessage("Fatal Error. Please restart FreeMat.");
}
ref.setJITState(JITInfo::FAILED);
}
return false;
}
//DOCBLOCK flow_while
//Works
void Interpreter::whileStatement(const Tree & t) {
if (tryJitCode(t)) return;
const Tree & testCondition(t.first());
const Tree & codeBlock(t.second());
bool breakEncountered = false;
Array condVar(expression(testCondition));
bool conditionTrue = RealAllNonZeros(condVar);
context->enterLoop();
breakEncountered = false;
while (conditionTrue && !breakEncountered) {
try {
block(codeBlock);
} catch (InterpreterContinueException& e) {
} catch (InterpreterBreakException& e) {
breakEncountered = true;
} catch (InterpreterReturnException& e) {
context->exitLoop();
throw;
} catch (InterpreterRetallException& e) {
context->exitLoop();
throw;
}
if (!breakEncountered) {
condVar = expression(testCondition);
conditionTrue = RealAllNonZeros(condVar);
}
}
context->exitLoop();
}
//Helper functions for FOR loops. This template function
//handles the index variable with the correct type. Reducing
//the net loop time
class ContextLoopLocker {
Context* m_context;
public:
ContextLoopLocker(Context* a): m_context(a) {m_context->enterLoop();}
~ContextLoopLocker() {m_context->exitLoop();}
};
inline bool IsIntegerDataClass( const Array& a )
{
return (a.dataClass() >= Int8) && (a.dataClass() <= UInt64);
}
template <class T>
void ForLoopHelper(const Tree & codeBlock, const Array& indexSet,
index_t count, const QString& indexName, Interpreter *eval) {
for (index_t m=1;m<=count;m++) {
Array *vp = eval->getContext()->lookupVariableLocally( indexName );
if ((!vp) || (!vp->isScalar())) {
eval->getContext()->insertVariableLocally(indexName,Array());
vp = eval->getContext()->lookupVariableLocally(indexName);
}
*vp = indexSet.get(m);
try {
eval->block(codeBlock);
} catch (InterpreterContinueException &) {
} catch (InterpreterBreakException &) {
break;
}
}
}
void ForLoopIterator( const Tree & codeBlock, QString indexName,
Array& first, Array& last, Array& step, Interpreter *eval)
{
int nsteps;
if( !( first.isScalar() && last.isScalar() && step.isScalar() ) )
throw Exception("Loop parameters must be scalar.");
Array *vp = eval->getContext()->lookupVariableLocally( indexName );
if ((!vp) || (!vp->isScalar())) {
eval->getContext()->insertVariableLocally(indexName,Array());
vp = eval->getContext()->lookupVariableLocally(indexName);
}
bool bIntLoop = (IsIntegerDataClass(first) || IsIntegerDataClass(last) || IsIntegerDataClass(step));
if( bIntLoop ){
//integer loop path
Array temp1;
temp1 = DotRightDivide( Subtract( last, first ), step ); //( ( l - f ) / s )
nsteps = temp1.asInteger() + 1; //( ( l - f ) / s )+1
if( nsteps < 0 ) return;
for (int m=0;m<nsteps;m++) {
*vp = Add( first, DotMultiply( Array(m), step ) );
try {
eval->block(codeBlock);
}
catch (InterpreterContinueException &e) {
}
catch (InterpreterBreakException &e) {
break;
}
}
}
else{
//floating point loop path
bool bFloatLoop = ( first.dataClass() == Float || last.dataClass() == Float || step.dataClass() == Float );
double f = first.asDouble();
double l = last.asDouble();
double s = step.asDouble();
if( bFloatLoop )
nsteps = num_for_loop_iter_f(f, s, l);
else
nsteps = num_for_loop_iter(f, s, l);
for (double m=0;m<nsteps;m++) { //array variable should be of type double for correct typing of DotMultiply
*vp = Add( first, DotMultiply( Array(m), step ) );
try {
eval->block(codeBlock);
}
catch (InterpreterContinueException &) {}
catch (InterpreterBreakException &) {
break;
}
}
}
}
extern "C" EXPORT
float num_for_loop_iter_f( float first, float step, float last )
{
int signum = (step > 0) - (step < 0);
int nsteps = (int) floor( ( last - first ) / step ) + 1;
if( nsteps < 0 )
return 0;
float mismatch = signum*(first + nsteps*step - last);
if( (mismatch > 0) && ( mismatch < 3.*fepsf(last) ) && ( step != rint(step) ) ) //allow overshoot by 3 eps in some cases
nsteps++;
return nsteps;
}
extern "C" EXPORT
double num_for_loop_iter( double first, double step, double last )
{
int signum = (step > 0) - (step < 0);
int nsteps = (int) floor( ( last - first ) / step ) + 1;
if( nsteps < 0 )
return 0;
double mismatch = signum*(first + nsteps*step - last);
if( (mismatch > 0) && ( mismatch < 3.*feps(last) ) && ( step != rint(step) ) ) //allow overshoot by 3 eps in some cases
nsteps++;
return nsteps;
}
//DOCBLOCK flow_for
//Works
void Interpreter::forStatement(const Tree & t) {
if (tryJitCode(t)) return;
Array indexSet;
QString indexVarName;
/* Get the name of the indexing variable */
if( !t.first().is('=') )
throw Exception( "Incorrect format of for operator" );
indexVarName = t.first().first().text();
if( t.first().second().is(TOK_MATDEF) ||
t.first().second().is(TOK_VARIABLE) ) {
//case "for j=[1:10]"
//case "for j=K" skb
/* Evaluate the index set */
indexSet = expression(t.first().second());
/* Get the code block */
const Tree & codeBlock(t.second());
index_t elementCount = indexSet.length();
DataClass loopType(indexSet.dataClass());
ContextLoopLocker lock(context);
switch(loopType) {
case Invalid:
throw Exception("Invalid arrays not supported");
case Struct:
throw Exception("Structure arrays are not supported as for loop index sets");
case CellArray:
ForLoopHelper<Array>(codeBlock,indexSet,
elementCount,indexVarName,this);
break;
case Bool:
ForLoopHelper<logical>(codeBlock, indexSet,
elementCount,indexVarName,this);
break;
case UInt8:
ForLoopHelper<uint8>(codeBlock,indexSet,
elementCount,indexVarName,this);
break;
case Int8:
ForLoopHelper<int8>(codeBlock,indexSet,
elementCount,indexVarName,this);
break;
case UInt16:
ForLoopHelper<uint16>(codeBlock,indexSet,
elementCount,indexVarName,this);
break;
case Int16:
ForLoopHelper<int16>(codeBlock,indexSet,
elementCount,indexVarName,this);
break;
case UInt32:
ForLoopHelper<uint32>(codeBlock,indexSet,
elementCount,indexVarName,this);
break;
case Int32:
ForLoopHelper<int32>(codeBlock,indexSet,
elementCount,indexVarName,this);
break;
case UInt64:
ForLoopHelper<uint64>(codeBlock,indexSet,
elementCount,indexVarName,this);
break;
case Int64:
ForLoopHelper<int64>(codeBlock,indexSet,
elementCount,indexVarName,this);
break;
case Float:
ForLoopHelper<float>(codeBlock,indexSet,
elementCount,indexVarName,this);
break;
case Double:
ForLoopHelper<double>(codeBlock,indexSet,
elementCount,indexVarName,this);
break;
case StringArray:
ForLoopHelper<uint8>(codeBlock,indexSet,
elementCount,indexVarName,this);
break;
}
}
else if( t.first().second().token() == ':' ){
if (t.first().second().numChildren() == 0)
throw Exception( "Incorrect format of loop operator parameters" );
Array first, step, last;
const Tree & codeBlock(t.second());
ContextLoopLocker lock(context);
if (t.first().second().first().is(':')) {
first = expression(t.first().second().first().first());
step = expression(t.first().second().first().second());
last = expression(t.first().second().second());
ForLoopIterator( codeBlock, indexVarName, first, last, step, this);
//return doubleColon(t);
}
else {
first = expression(t.first().second().first());
last = expression(t.first().second().second());
Array tmp(BasicArray<double>(1));
ForLoopIterator( codeBlock, indexVarName, first, last, tmp, this);
//return unitColon(t);
}
}
}
//DOCBLOCK variables_global
void Interpreter::globalStatement(const Tree & t) {
for (int i=0;i<t.numChildren();i++) {
QString name = t.child(i).text();
context->addGlobalVariable(name);
if (!context->lookupVariable(name).valid())
context->insertVariable(name,EmptyConstructor());
}
}
//DOCBLOCK variables_persistent
void Interpreter::persistentStatement(const Tree & t) {
for (int i=0;i<t.numChildren();i++) {
QString name = t.child(i).text();
context->addPersistentVariable(name);
if (!context->lookupVariable(name).valid())
context->insertVariable(name,EmptyConstructor());
}
}
//DOCBLOCK flow_continue
//DOCBLOCK flow_break
//DOCBLOCK flow_return
//DOCBLOCK freemat_quit
//DOCBLOCK flow_retall
//DOCBLOCK flow_keyboard
void Interpreter::doDebugCycle() {
depth++;
PopContext saver(context,0);
{
context->pushScope("keyboard","keyboard");
PopContext saver2(context,0);
context->setScopeActive(false);
try {
evalCLI();
} catch (InterpreterContinueException& e) {
} catch (InterpreterBreakException& e) {
} catch (InterpreterReturnException& e) {
} catch (InterpreterRetallException& e) {
depth--;
throw;
}
}
depth--;
}
void Interpreter::displayArray(Array b) {
// Check for a user defined class
FuncPtr val;
if (b.isUserClass() && ClassResolveFunction(this,b,"display",val)) {
if (val->updateCode(this)) refreshBreakpoints();
ArrayVector args(b);
ArrayVector retvec(doFunction(val,args,1));
} else
PrintArrayClassic(b,printLimit,this);
}
//Works
void Interpreter::expressionStatement(const Tree & s, bool printIt) {
ArrayVector m;
if (!s.is(TOK_EXPR)) throw Exception("Unexpected statement type!");
const Tree & t(s.first());
// There is a special case to consider here - when a
// function call is made as a statement, we do not require
// that the function have an output.
Array b;
ArrayReference ptr;
if (t.is(TOK_VARIABLE)) {
ptr = context->lookupVariable(t.first().text());
if (!ptr.valid()) {
functionExpression(t,0,true,m);
m = handleReindexing(t,m);
bool emptyOutput = false;
if (m.size() == 0) {
b = EmptyConstructor();
emptyOutput = true;
} else
b = m[0];
if (printIt && (!emptyOutput)) {
outputMessage(QString("\nans = \n"));
displayArray(b);
}
} else {
multiexpr(t,m);
if (m.size() == 0)
b = EmptyConstructor();
else {
b = m[0];
if (printIt) {
outputMessage(QString("\nans = \n"));
for (int j=0;j<m.size();j++) {
char buffer[1000];
if (m.size() > 1) {
sprintf(buffer,"\n%d of %d:\n",j+1,m.size());
outputMessage(QString(buffer));
}
displayArray(m[j]);
}
}
}
}
} else {
b = expression(t);
if (printIt) {
outputMessage(QString("\nans = \n"));
displayArray(b);
}
}
context->insertVariable("ans",b);
}
void Interpreter::multiassign(ArrayReference r, const Tree & s, ArrayVector &data) {
SaveEndInfo;
endRef = r;
if (s.is(TOK_PARENS)) {
ArrayVector m;
endTotal = s.numChildren();
if (s.numChildren() == 0)
throw Exception("The expression A() is not legal unless A is a function");
for (int p = 0; p < s.numChildren(); p++) {
endCount = m.size();
multiexpr(s.child(p),m);
}
subsindex(m);
if (m.size() == 1)
r->set(m[0],data[0]);
else
r->set(m,data[0]);
data.pop_front();
} else if (s.is(TOK_BRACES)) {
ArrayVector m;
endTotal = s.numChildren();
for (int p = 0; p < s.numChildren(); p++) {
endCount = m.size();
multiexpr(s.child(p),m);
}
subsindex(m);
if (m.size() == 1)
SetCellContents(*r,m[0],data);
else
SetCellContents(*r,m,data);
} else if (s.is('.')) {
r->set(s.first().text(),data);
} else if (s.is(TOK_DYN)) {
QString field;
try {
Array fname(expression(s.first()));
field = fname.asString();
} catch (Exception &e) {
throw Exception("dynamic field reference to structure requires a string argument");
}
r->set(field,data);
}
RestoreEndInfo;
}
void Interpreter::assign(ArrayReference r, const Tree & s, Array &data) {
SaveEndInfo;
endRef = r;
if (s.is(TOK_PARENS)) {
ArrayVector m;
endTotal = s.numChildren();
if (s.numChildren() == 0)
throw Exception("The expression A() is not legal unless A is a function");
for (int p = 0; p < s.numChildren(); p++) {
endCount = m.size();
multiexpr(s.child(p),m);
}
subsindex(m);
if (m.size() == 1)
r->set(m[0],data);
else
r->set(m,data);
} else if (s.is(TOK_BRACES)) {
ArrayVector datav(data);
ArrayVector m;
endTotal = s.numChildren();
for (int p = 0; p < s.numChildren(); p++) {
endCount = m.size();
multiexpr(s.child(p),m);
}
subsindex(m);
if (m.size() == 1)
SetCellContents(*r,m[0],datav);
else
SetCellContents(*r,m,datav);
} else if (s.is('.')) {
ArrayVector datav(data);
r->set(s.first().text(),datav);
} else if (s.is(TOK_DYN)) {
QString field;
try {
Array fname(expression(s.first()));
field = fname.asString();
} catch (Exception &e) {
throw Exception("dynamic field reference to structure requires a string argument");
}
ArrayVector datav(data);
r->set(field,datav);
}
RestoreEndInfo;
}
ArrayReference Interpreter::createVariable(QString name) {
FuncPtr p;
PopContext saver(context,0);
// This is annoying.
// if (context->lookupFunction(name,p) && (name.size() > 1))
// warningMessage("Newly defined variable " + name + " shadows a function of the same name. Use clear " + name + " to recover access to the function");
// Are we in a nested scope?
if (!context->isCurrentScopeNested() || context->variableLocalToCurrentScope(name)) {
// if not, just create a local variable in the current scope, and move on.
context->insertVariable(name,EmptyConstructor());
} else {
// if so - walk up the scope chain until we are no longer nested
QString localScopeName = context->scopeName();
context->bypassScope(1);
while (context->currentScopeNests(localScopeName))
context->bypassScope(1);
context->restoreScope(1);
// We wre now pointing to the highest scope that contains the present
// (nested) scope. Now, we start walking down the chain looking for
// someone who accesses this variable
while (!context->currentScopeVariableAccessed(name) &&
context->scopeName() != localScopeName)
context->restoreScope(1);
// Either we are back in the local scope, or we are pointing to
// a scope that (at least theoretically) accesses a variable with
// the given name.
context->insertVariable(name,EmptyConstructor());
}
ArrayReference np(context->lookupVariable(name));
if (!np.valid())
throw Exception("error creating variable name " + name +
" with scope " + context->scopeName());
return np;
}
//Works
// The case of a(1,2).foo.goo{3} = rhs
// The tree looks like this:
// Variable
// Identifier
// ()
// etc
// .
// foo
// .
// goo
// {}
// 3
//
// We have to do:
// a1 = id data = id
// a2 = id(etc) stack[0] = id(etc)
// a3 = a2.foo stack[1] = stack[0].foo
// a4 = a3.goo stack[2] = stack[1].goo
// a3{3} = rhs data{3} = rhs
// a2.foo = a3
// id(etc) = a2;
//DOCBLOCK array_assign
void Interpreter::assignment(const Tree & var, bool printIt, Array &b) {
QString name(var.first().text());
ArrayReference ptr(context->lookupVariable(name));
if (!ptr.valid())
ptr = createVariable(name);
if (var.numChildren() == 1) {
(*ptr) = b;
} else if (ptr->isUserClass() &&
!inMethodCall(ptr->className()) &&
!stopoverload) {
ClassAssignExpression(ptr,var,b,this);
} else if (var.numChildren() == 2)
assign(ptr,var.second(),b);
else {
ArrayVector stack;
Array data(*ptr);
int varCount = var.numChildren();
for (int index=1;index<varCount-1;index++) {
if (!data.isEmpty()) {
try {
deref(data,var.child(index));
} catch (Exception &e) {
data = EmptyConstructor();
}
}
stack.push_back(data);
}
assign(&data,var.child(varCount-1),b);
Array rhs(data);
if (stack.size() > 0) {
stack.pop_back();
int ptr = 0;
while (stack.size() > 0) {
data = stack.back();
assign(&data,var.child(varCount-2-ptr),rhs);
rhs = data;
stack.pop_back();
ptr++;
}
}
assign(ptr,var.child(1),rhs);
}
if (printIt) {
outputMessage("\n");
outputMessage(name);
outputMessage(" = \n");
displayArray(*ptr);
}
}
void Interpreter::processBreakpoints(const Tree & t) {
for (int i=0;i<bpStack.size();i++) {
if ((bpStack[i].cname == context->scopeName()) &&
((LineNumber(context->scopeTokenID()) == bpStack[i].tokid))) {
doDebugCycle();
context->setScopeTokenID(t.context());
}
}
if (tracetrap > 0) {
if ((LineNumber(context->scopeTokenID()) != tracecurrentline)) {
tracetrap--;
if (tracetrap == 0)
doDebugCycle();
}
}
if (context->scopeStepTrap() > 0) {
if ((LineNumber(context->scopeTokenID())) !=
context->scopeStepCurrentLine()) {
context->setScopeStepTrap(context->scopeStepTrap()-1);
if (context->scopeStepTrap() == 0)
doDebugCycle();
}
}
}
void Interpreter::statementType(const Tree & t, bool printIt) {
// check the debug flag
context->setScopeTokenID(t.context());
processBreakpoints(t);
switch(t.token()) {
case '=':
{
Array b(expression(t.second()));
assignment(t.first(),printIt,b);
}
break;
case TOK_MULTI:
multiFunctionCall(t,printIt);
break;
case TOK_SPECIAL:
specialFunctionCall(t,printIt);
break;
case TOK_FOR:
forStatement(t);
break;
case TOK_WHILE:
whileStatement(t);
break;
case TOK_IF:
ifStatement(t);
break;
case TOK_BREAK:
if (context->inLoop())
throw InterpreterBreakException();
break;
case TOK_CONTINUE:
if (context->inLoop())
throw InterpreterContinueException();
break;
case TOK_DBSTEP:
dbstepStatement(t);
emit RefreshBPLists();
throw InterpreterReturnException();
break;
case TOK_DBTRACE:
dbtraceStatement(t);
emit RefreshBPLists();
throw InterpreterReturnException();
break;
case TOK_DBUP:
dbup();
break;
case TOK_DBDOWN:
dbdown();
break;
case TOK_RETURN:
throw InterpreterReturnException();
break;
case TOK_SWITCH:
switchStatement(t);
break;
case TOK_TRY:
tryStatement(t);
break;
case TOK_QUIT:
throw InterpreterQuitException();
break;
case TOK_RETALL:
throw InterpreterRetallException();
break;
case TOK_KEYBOARD:
doDebugCycle();
break;
case TOK_GLOBAL:
globalStatement(t);
break;
case TOK_PERSISTENT:
persistentStatement(t);
break;
case TOK_EXPR:
expressionStatement(t,printIt);
break;
case TOK_NEST_FUNC:
break;
default:
throw Exception("Unrecognized statement type");
}
}
//Trapping at the statement level is much better! - two
//problems... try/catch and multiline statements (i.e.,atell.m)
//The try-catch one is easy, I think... When a try occurs,
//we capture the stack depth... if an exception occurs, we
//unwind the stack to this depth..
//The second one is trickier - suppose we have a conditional
//statement
//if (a == 3)
// bfunc
//else
// cfunc
//end
//this is represented in the parse tree as a single construct...
//
//
//Works
void Interpreter::statement(const Tree & t) {
try {
if (t.is(TOK_QSTATEMENT))
statementType(t.first(),false);
else if (t.is(TOK_STATEMENT))
statementType(t.first(),m_quietlevel == 0);
else
throw Exception("Unexpected statement type!\n");
} catch (Exception& e) {
if (autostop && !InCLI) {
errorCount++;
e.printMe(this);
stackTrace();
doDebugCycle();
} else {
if (!e.wasHandled() && !InCLI && !intryblock) {
stackTrace();
e.markAsHandled();
}
throw;
}
}
}
//Works
void Interpreter::block(const Tree & t) {
try {
const TreeList statements(t.children());
for (TreeList::const_iterator i=statements.begin();
i!=statements.end();i++) {
if (m_kill)
throw InterpreterKillException();
if (m_interrupt) {
outputMessage("Interrupt (ctrl-b) encountered\n");
stackTrace();
m_interrupt = false;
doDebugCycle();
} else
statement(*i);
}
} catch (Exception &e) {
lasterr = e.msg();
throw;
}
}
// I think this is too complicated.... there should be an easier way
// Works
index_t Interpreter::countLeftHandSides(const Tree & t) {
Array lhs;
ArrayReference ptr(context->lookupVariable(t.first().text()));
if (!ptr.valid())
lhs = EmptyConstructor();
else
lhs = *ptr;
if (t.numChildren() == 1) return 1;
if (t.last().is(TOK_PARENS)) return 1;
for (int index = 1;index < t.numChildren()-1;index++) {
try {
deref(lhs,t.child(index));
} catch (Exception& e) {
lhs = EmptyConstructor();
}
}
if (t.last().is(TOK_BRACES)) {
const Tree & s(t.last());
ArrayVector m;
for (int p = 0; p < s.numChildren(); p++)
multiexpr(s.child(p),m);
subsindex(m);
if (m.size() == 0)
throw Exception("Expected indexing expression!");
if (m.size() == 1) {
// m[0] should have only one element...
if (IsColonOp(m[0]))
return (lhs.length());
return (IndexArrayFromArray(m[0]).length());
} else {
int i=0;
index_t outputCount=1;
while (i<m.size()) {
if (IsColonOp(m[i]))
outputCount *= lhs.dimensions()[i];
else {
outputCount *= IndexArrayFromArray(m[i]).length();
}
i++;
}
return (outputCount);
}
}
if (t.last().is('.'))
return std::max((index_t)1,lhs.length());
return 1;
}
Array Interpreter::AllColonReference(Array v, int index, int count) {
if (v.isUserClass()) return EmptyConstructor();
return Array(QString(":"));
}
//test
void Interpreter::specialFunctionCall(const Tree & t, bool printIt) {
ArrayVector m;
StringVector args;
for (int index=0;index < t.numChildren();index++)
args.push_back(t.child(index).text());
if (args.empty()) return;
ArrayVector n;
for (int i=1;i<args.size();i++)
n.push_back(Array(args[i]));
FuncPtr val;
if (!lookupFunction(args[0],val,n))
throw Exception("unable to resolve " + args[0] + " to a function call");
if (val->updateCode(this)) refreshBreakpoints();
m = doFunction(val,n,0);
}
void Interpreter::setBreakpoint(stackentry bp, bool enableFlag) {
FuncPtr val;
bool isFun = lookupFunction(bp.detail,val);
if (!isFun) {
warningMessage(QString("unable to find function ") +
bp.detail + " to set breakpoint");
return;
}
if (val->type() != FM_M_FUNCTION) {
warningMessage("function " + bp.detail +
" is not an m-file, and does not support breakpoints");
return;
}
// try {
// // ((MFunctionDef*)val)->SetBreakpoint(bp.tokid,enableFlag);
// } catch (Exception &e) {
// e.printMe(this);
// }
}
void Interpreter::addBreakpoint(stackentry bp) {
bpStack.push_back(bp);
refreshBreakpoints();
emit RefreshBPLists();
}
void Interpreter::refreshBreakpoints() {
for (int i=0;i<bpStack.size();i++)
setBreakpoint(bpStack[i],true);
}
//Some notes on the multifunction call... This one is pretty complicated, and the current logic is hardly transparent. Assume we have an expression of the form:
//
//[expr1 expr2 ... exprn] = fcall
//
//where fcall is a function call (obviously). Now, we want to determine how many output arguments fcall should have. There are several interesting cases to consider:
//
//expr_i is an existing numeric variable -- lhscount += 1
//
//expr_i is an existing cell array -- lhscount += size(expr_i)
//
//expr_i is an existing struct array -- lhscount += size(expr_i)
//
//expr_i does not exist -- lhscount += 1
//
//Where this will fail is in one case. If expr_i is a cell reference for a variable that does not exist, and has a sized argument, something like
//[eg{1:3}]
//in which case the lhscount += 3, even though eg does not exist.
// WORKS
void Interpreter::multiFunctionCall(const Tree & t, bool printIt) {
ArrayVector m;
TreeList s;
Array c;
index_t lhsCount;
if (!t.first().is(TOK_BRACKETS))
throw Exception("Illegal left hand side in multifunction expression");
s = t.first().children();
// We have to make multiple passes through the LHS part of the AST.
// The first pass is to count how many function outputs are actually
// being requested.
// Calculate how many lhs objects there are
lhsCount = 0;
for (int ind=0;ind<s.size();ind++)
lhsCount += countLeftHandSides(s[ind]);
multiexpr(t.second(),m,lhsCount);
int index;
for (index=0;(index<s.size()) && (m.size() > 0);index++) {
const Tree & var(s[index]);
QString name(var.first().text());
ArrayReference ptr(context->lookupVariable(name));
if (!ptr.valid())
ptr = createVariable(name);
if (ptr->isUserClass() &&
!inMethodCall(ptr->className()) &&
!stopoverload && (var.numChildren() > 1)) {
ClassAssignExpression(ptr,var,m.front(),this);
m.pop_front();
return;
}
if (var.numChildren() == 1) {
(*ptr) = m.front();
m.pop_front();
} else if (var.numChildren() == 2)
multiassign(ptr,var.second(),m);
else {
ArrayVector stack;
Array data(*ptr);
int varCount = var.numChildren();
for (int index=1;index<varCount-1;index++) {
if (!data.isEmpty()) {
try {
deref(data,var.child(index));
} catch (Exception &e) {
data = EmptyConstructor();
}
}
stack.push_back(data);
}
multiassign(&data,var.child(varCount-1),m);
Array rhs(data);
if (stack.size() > 0) {
stack.pop_back();
int ptr = 0;
while (stack.size() > 0) {
data = stack.back();
assign(&data,var.child(varCount-2-ptr),rhs);
rhs = data;
stack.pop_back();
ptr++;
}
}
assign(ptr,var.child(1),rhs);
}
if (printIt) {
outputMessage(name);
outputMessage(" = \n");
displayArray(*ptr);
}
}
if (index < s.size())
warningMessage("one or more outputs not assigned in call.");
}
int getArgumentIndex(StringVector list, QString t) {
bool foundArg = false;
QString q;
int i = 0;
while (i<list.size() && !foundArg) {
q = list[i];
if (q[0] == '&')
q.remove(0,1);
foundArg = (q == t);
if (!foundArg) i++;
}
if (foundArg)
return i;
else
return -1;
}
//DOCBLOCK functions_function
//DOCBLOCK functions_anonymous
//DOCBLOCK functions_keywords
//DOCBLOCK functions_varargin
//DOCBLOCK functions_varargout
//DOCBLOCK functions_script
//DOCBLOCK functions_special
void Interpreter::collectKeywords(const Tree & q, ArrayVector &keyvals,
TreeList &keyexpr, StringVector &keywords) {
// Search for the keyword uses -
// To handle keywords, we make one pass through the arguments,
// recording a list of keywords used and using ::expression to
// evaluate their values.
for (int index=0;index < q.numChildren();index++) {
if (q.child(index).is(TOK_KEYWORD)) {
keywords.push_back(q.child(index).first().text());
if (q.child(index).numChildren() > 1) {
keyvals.push_back(expression(q.child(index).second()));
keyexpr.push_back(q.child(index).second());
} else {
keyvals.push_back(Array(bool(true)));
keyexpr.push_back(Tree());
}
}
}
}
int* Interpreter::sortKeywords(ArrayVector &m, StringVector &keywords,
StringVector arguments, ArrayVector keyvals) {
// If keywords were used, we have to permute the
// entries of the arrayvector to the correct order.
int *keywordNdx = new int[keywords.size()];
int maxndx;
maxndx = 0;
// Map each keyword to an argument number
for (int i=0;i<keywords.size();i++) {
int ndx;
ndx = getArgumentIndex(arguments,keywords[i]);
if (ndx == -1)
throw Exception("out-of-order argument /" + keywords[i] + " is not defined in the called function!");
keywordNdx[i] = ndx;
if (ndx > maxndx) maxndx = ndx;
}
// Next, we have to determine how many "holes" there are
// in the argument list - we get the maximum list
int holes;
holes = maxndx + 1 - keywords.size();
// At this point, holes is the number of missing arguments
// If holes > m.size(), then the total number of arguments
// is just maxndx+1. Otherwise, its
// maxndx+1+(m.size() - holes)
int totalCount;
if (holes > m.size())
totalCount = maxndx+1;
else
totalCount = maxndx+1+(m.size() - holes);
// Next, we allocate a vector to hold the values
ArrayVector toFill;
for (int i=0;i<totalCount;i++)
toFill.push_back(Array());
// ArrayVector toFill(totalCount);
bool *filled = new bool[totalCount];
int *argTypeMap = new int[totalCount];
for (int i=0;i<totalCount;i++) {
filled[i] = false;
argTypeMap[i] = -1;
}
// Finally...
// Copy the keyword values in
for (int i=0;i<keywords.size();i++) {
toFill[keywordNdx[i]] = keyvals[i];
filled[keywordNdx[i]] = true;
argTypeMap[keywordNdx[i]] = i;
}
// Fill out the rest of the values from m
int n = 0;
int p = 0;
while (n < m.size()) {
if (!filled[p]) {
toFill[p] = m[n];
filled[p] = true;
argTypeMap[p] = -2;
n++;
}
p++;
}
// Finally, fill in empty matrices for the
// remaining arguments
for (int i=0;i<totalCount;i++)
if (!filled[i])
toFill[i] = EmptyConstructor();
// Clean up
delete[] filled;
delete[] keywordNdx;
// Reassign
m = toFill;
return argTypeMap;
}
// arguments is exactly what it should be - the vector of arguments
// m is vector of argument values
// keywords is the list of values passed as keywords
// keyexpr is the
void Interpreter::handlePassByReference(Tree q, StringVector arguments,
ArrayVector m,StringVector keywords,
TreeList keyexpr, int* argTypeMap) {
Tree p;
// M functions can modify their arguments
int maxsearch = m.size();
if (maxsearch > arguments.size()) maxsearch = arguments.size();
int qindx = 0;
for (int i=0;i<maxsearch;i++) {
// Was this argument passed out of order?
if ((keywords.size() > 0) && (argTypeMap[i] == -1)) continue;
if ((keywords.size() > 0) && (argTypeMap[i] >=0)) {
p = keyexpr[argTypeMap[i]];
} else {
p = q.second().child(qindx);
qindx++;
if (qindx >= q.second().numChildren())
qindx = q.second().numChildren()-1;
}
QString args(arguments[i]);
if (args[0] == '&') {
args.remove(0,1);
// This argument was passed by reference
if (!p.valid() || !(p.is(TOK_VARIABLE)))
throw Exception("Must have lvalue in argument passed by reference");
assignment(p,false,m[i]);
}
}
}
//Test
void Interpreter::functionExpression(const Tree & t,
int narg_out,
bool outputOptional,
ArrayVector &output) {
ArrayVector m, n;
StringVector keywords;
ArrayVector keyvals;
TreeList keyexpr;
FuncPtr funcDef;
int* argTypeMap;
// Because of the introduction of user-defined classes, we have to
// first evaluate the keywords and the arguments, before we know
// which function to call.
// First, check for arguments
if ((t.numChildren()>=2) && t.second().is(TOK_PARENS)) {
// Collect keywords
collectKeywords(t.second(),keyvals,keyexpr,keywords);
// Evaluate function arguments
try {
const Tree & s(t.second());
for (int p=0;p<s.numChildren();p++)
multiexpr(s.child(p),m);
} catch (Exception &e) {
// Transmute the error message about illegal use of ':'
// into one about undefined variables. Its crufty,
// but it works.
if (e.matches("Illegal use of the ':' operator"))
throw Exception("Undefined variable " + t.text());
else
throw;
}
}
// Now that the arguments have been evaluated, we have to
// find the dominant class
if (!lookupFunction(t.first().text(),funcDef,m))
throw Exception("Undefined function or variable " +
t.first().text());
if (funcDef->updateCode(this)) refreshBreakpoints();
if (funcDef->scriptFlag) {
if (t.numChildren()>=2)
throw Exception(QString("Cannot use arguments in a call to a script."));
if ((narg_out > 0) && !outputOptional)
throw Exception(QString("Cannot assign outputs in a call to a script."));
context->pushScope(((MFunctionDef*)funcDef)->fileName,
((MFunctionDef*)funcDef)->name,false);
context->setScopeActive(false);
block(((MFunctionDef*)funcDef)->code);
if (context->scopeStepTrap() >= 1) {
tracetrap = 1;
tracecurrentline = 0;
warningMessage("dbstep beyond end of script " + context->scopeDetailString() +
" -- setting single step mode\n");
context->setScopeStepTrap(0);
}
context->popScope();
} else {
// We can now adjust the keywords (because we know the argument list)
// Apply keyword mapping
if (!keywords.empty())
argTypeMap = sortKeywords(m,keywords,funcDef->arguments,keyvals);
else
argTypeMap = NULL;
if ((funcDef->inputArgCount() >= 0) &&
(m.size() > funcDef->inputArgCount()))
throw Exception(QString("Too many inputs to function ")+t.first().text());
if ((funcDef->outputArgCount() >= 0) &&
(narg_out > funcDef->outputArgCount() && !outputOptional))
throw Exception(QString("Too many outputs to function ")+t.first().text());
n = doFunction(funcDef,m,narg_out);
// Check for any pass by reference
if (t.hasChildren() && (funcDef->arguments.size() > 0))
handlePassByReference(t,funcDef->arguments,m,keywords,keyexpr,argTypeMap);
}
// Some routines (e.g., min and max) will return more outputs
// than were actually requested... so here we have to trim
// any elements received that we didn't ask for.
// preserve one output if we were called as an expression (for ans)
if (outputOptional) narg_out = (narg_out == 0) ? 1 : narg_out;
while (n.size() > narg_out)
n.pop_back();
output += n;
}
void Interpreter::toggleBP(QString fname, int lineNumber) {
if (isBPSet(fname,lineNumber)) {
QString fname_string(fname);
for (int i=0;i<bpStack.size();i++)
if ((bpStack[i].cname == fname_string) &&
(LineNumber(bpStack[i].tokid) == lineNumber)) {
deleteBreakpoint(bpStack[i].number);
return;
}
} else {
addBreakpoint(fname,lineNumber);
}
}
MFunctionDef* Interpreter::lookupFullPath(QString fname) {
StringVector allFuncs(context->listAllFunctions());
FuncPtr val;
for (int i=0;i<allFuncs.size();i++) {
bool isFun = context->lookupFunction(allFuncs[i],val);
if (!isFun || !val) return NULL;
if (val->type() == FM_M_FUNCTION) {
MFunctionDef *mptr;
mptr = (MFunctionDef *) val;
if (mptr->fileName == fname) return mptr;
}
}
return NULL;
}
static int bpList = 1;
// Add a breakpoint - name is used to track to a full filename
void Interpreter::addBreakpoint(QString name, int line) {
FuncPtr val;
// Map the name argument to a full file name.
QString fullFileName;
if (context->lookupFunction(name,val) && (val->type() == FM_M_FUNCTION))
fullFileName = ((MFunctionDef*) val)->fileName;
else
fullFileName = name;
// Get a list of all functions
StringVector allFuncs(context->listAllFunctions());
// We make one pass through the functions, and update
// those functions that belong to the given filename
for (int i=0;i<allFuncs.size();i++) {
bool isFun = context->lookupFunction(allFuncs[i],val);
if (!isFun || !val) throw Exception("Cannot add breakpoint to " + name + " : it does not appear to be a valid M file.");
if (val->type() == FM_M_FUNCTION) {
MFunctionDef *mptr = (MFunctionDef *) val;
if (mptr->fileName == fullFileName)
mptr->updateCode(this);
}
}
// Refresh the list of all functions
allFuncs = context->listAllFunctions();
// Search through the list for any function defined - for each function,
// record the line number closest to it
MemBlock<int> line_dist_block(allFuncs.size());
int *line_dist = &line_dist_block;
for (int i=0;i<allFuncs.size();i++) line_dist[i] = 2*max_line_count;
for (int i=0;i<allFuncs.size();i++) {
bool isFun = context->lookupFunction(allFuncs[i],val);
if (!isFun || !val) throw Exception("Cannot add breakpoint to " + name + " : it does not appear to be a valid M file.");
if (val->type() == FM_M_FUNCTION) {
MFunctionDef *mptr = (MFunctionDef *) val;
if (mptr->fileName == fullFileName) {
try {
int dline = mptr->ClosestLine(line);
line_dist[i] = dline;
} catch (Exception& e) {
}
}
}
}
// Second pass through it - find the function with a line number closest to the
// desired one, but not less than it
int best_func = -1;
int best_dist = 2*max_line_count;
for (int i=0;i<allFuncs.size();i++) {
if ((line_dist[i] >= line) && ((line_dist[i]-line) < best_dist)) {
best_func = i;
best_dist = line_dist[i]-line;
}
}
if (best_dist > max_line_count)
// warningMessage(QString("Cannot set breakpoint at line ")+line+" of file "+name + ". \r\nThis can be caused by an illegal line number, or a function that is not on the path or in the current directory.");
emit IllegalLineOrCurrentPath(name, line);
else {
addBreakpoint(stackentry(fullFileName,allFuncs[best_func],best_dist+line,bpList++));
}
}
bool Interpreter::isBPSet(QString fname, int lineNumber) {
for (int i=0;i<bpStack.size();i++)
if ((bpStack[i].cname == fname) &&
(LineNumber(bpStack[i].tokid) == lineNumber)) return true;
return false;
}
// called by editor
bool Interpreter::isInstructionPointer(QString fname, int lineNumber) {
if (!InCLI) return false;
ParentScopeLocker lock(context);
QString filename(context->scopeName());
int token(context->scopeTokenID());
return ((fname == filename) && ((lineNumber == LineNumber(token)) ||
((lineNumber == 1) && (LineNumber(token) == 0))));
}
void Interpreter::listBreakpoints() {
for (int i=0;i<bpStack.size();i++) {
QString buffer = QString("%1 %2 line %3\n").arg(bpStack[i].number)
.arg(bpStack[i].cname).arg(LineNumber(bpStack[i].tokid));
outputMessage(buffer);
}
}
void Interpreter::deleteBreakpoint(int number) {
for (int i=0;i<bpStack.size();i++)
if (bpStack[i].number == number) {
bpStack.remove(i);
emit RefreshBPLists();
return;
}
warningMessage("Unable to delete specified breakpoint (does not exist)");
emit RefreshBPLists();
return;
}
void Interpreter::stackTrace(int skiplevels) {
bool firstline = true;
int depth = context->scopeDepth();
context->bypassScope(skiplevels);
for (int i=0;i<(depth-skiplevels);i++) {
if ((context->scopeName() == "keyboard") &&
(context->scopeDetailString() == "keyboard")) {
context->bypassScope(1);
continue;
}
if (firstline) {
firstline = false;
} else
outputMessage(QString(" "));
outputMessage(QString("In ") + context->scopeName() + "("
+ context->scopeDetailString() + ")");
int line = int(LineNumber(context->scopeTokenID()));
if (line > 0)
outputMessage(QString(" at line " +
QString().setNum(LineNumber(context->scopeTokenID()))));
outputMessage("\r\n");
context->bypassScope(1);
}
context->restoreScope(depth);
}
bool Interpreter::inMethodCall(QString classname) {
if (context->scopeDetailString().isEmpty()) return false;
if (context->scopeDetailString()[0] != '@') return false;
return (context->scopeDetailString().mid(1,classname.size()) == classname);
}
bool Interpreter::lookupFunction(QString funcName, FuncPtr& val) {
ArrayVector dummy;
return(lookupFunction(funcName,val,dummy));
}
bool IsNestedName(QString basename) {
return (basename.lastIndexOf("/") >= 0);
}
QString StripNestLevel(QString basename) {
int ndx = basename.lastIndexOf("/");
if (ndx >= 0)
basename.remove(ndx,basename.size());
else
basename = "";
return basename;
}
// Look up a function by name. Use the arguments (if available) to assist
// in resolving method calls for objects
bool Interpreter::lookupFunction(QString funcName, FuncPtr& val,
ArrayVector &args, bool disableOverload) {
int passcount = 0;
while(passcount < 2) {
// This is the order for function dispatch according to the Matlab manual
// Nested functions - not explicitly listed in the Matlab manual, but
// I figure they have the highest priority in the current scope.
if (isMFile(context->scopeName()) &&
(context->lookupFunction(NestedMangleName(context->scopeDetailString(),funcName),val)))
return true;
if (InCLI && isMFile(context->activeScopeName()) &&
(context->lookupFunction(NestedMangleName(context->activeScopeDetailString(),funcName),val)))
return true;
// Not a nested function of the current scope. We have to look for nested
// functions of all parent scopes. Sigh.
if (context->isCurrentScopeNested()) {
QString basename = context->scopeDetailString();
while (!basename.isEmpty()) {
if (context->lookupFunction(NestedMangleName(basename,funcName),val))
return true;
basename = StripNestLevel(basename);
}
}
// Subfunctions
if (inMFile() &&
(context->lookupFunction(getLocalMangledName(funcName),val)))
return true;
// Private functions
// Not sure if you have to be an M-file in the current directory
// to access a private function...
if (context->lookupFunction(getPrivateMangledName(funcName),val))
return true;
// Class constructor functions
if (context->lookupFunction(ClassMangleName(funcName,funcName),val))
return true;
if (!(disableOverload || stopoverload)) {
// Look for a class method
// Are any of the arguments classes?
bool anyClasses = false;
int i=0;
while ((!anyClasses) && (i < args.size())) {
anyClasses = args[i].isUserClass();
if (!anyClasses) i++;
}
// Yes, try and resolve the call to a method
if (anyClasses && ClassResolveFunction(this,args[i],funcName,val))
return true;
}
if (context->lookupFunction(funcName,val)) return true;
if (passcount == 0)
rescanPath();
passcount++;
}
return false;
}
//DOCBLOCK variables_functionhandles
//DOCBLOCK variables_indexing
// This has a few shortcomings that prevent it from being
// 100% correct.
//
// 1. subsindex is not called for argument
// expressions of user-defined classes.
// 2. "end" no longer works.
//
// To fix "end", we should use a source transformation technique.
// The original tree looks like this
//
// variable
// -> t
// -> ()
// -> 2
// -> end
//
// This should be translated into:
//
// _t = end(t,2)
//
// This is done in Transform.cpp...
//
// This does not cover:
// Function pointers
// subsindex
//
//
//
//
void Interpreter::deref(Array &r, const Tree & s) {
SaveEndInfo;
endRef = &r;
if (s.is(TOK_PARENS)) {
ArrayVector m;
endTotal = s.numChildren();
if (s.numChildren() == 0) {
r = r;
} else {
for (int p = 0; p < s.numChildren(); p++) {
endCount = m.size();
multiexpr(s.child(p),m);
}
subsindex(m);
if (m.size() == 1)
r = r.get(m[0]);
else
r = r.get(m);
}
} else if (s.is(TOK_BRACES)) {
ArrayVector m;
endTotal = s.numChildren();
for (int p = 0; p < s.numChildren(); p++) {
endCount = m.size();
multiexpr(s.child(p),m);
}
subsindex(m);
if (m.size() == 1)
r = ArrayFromCellArray(r.get(m[0]));
else
r = ArrayFromCellArray(r.get(m));
} else if (s.is('.')) {
r = r.get(s.first().text()).front();
} else if (s.is(TOK_DYN)) {
QString field;
try {
Array fname(expression(s.first()));
field = fname.asString();
} catch (Exception &e) {
throw Exception("dynamic field reference to structure requires a string argument");
}
r = r.get(field).front();
}
RestoreEndInfo;
}
Array Interpreter::rhs(const Tree & t) {
ArrayReference ptr(context->lookupVariable(t.first().text()));
if (!ptr.valid()) {
ArrayVector m;
functionExpression(t,1,false,m);
m = handleReindexing(t,m);
if (m.size() >= 1)
return m[0];
else
return EmptyConstructor();
}
if (t.numChildren() == 1)
return *ptr;
if (ptr->isUserClass() && !stopoverload) {
ArrayVector m(ClassRHSExpression(*ptr,t,this));
if (m.size() >= 1)
return m[0];
else
return EmptyConstructor();
}
Array r(*ptr);
for (int index = 1;index < t.numChildren();index++)
deref(r,t.child(index));
return r;
}
int Interpreter::getErrorCount() {
int retval = errorCount;
errorCount = 0;
return retval;
}
Interpreter::Interpreter(Context* aContext) {
errorCount = 0;
lasterr = QString("");
context = aContext;
depth = 0;
printLimit = 1000;
autostop = false;
intryblock = false;
jitcontrol = JITOff;
jitcount = 0;
stopoverload = false;
m_skipflag = false;
m_noprompt = false;
m_liveUpdateFlag = false;
tracetrap = 0;
tracecurrentline = 0;
endRef = NULL;
m_interrupt = false;
m_kill = false;
m_diaryState = false;
m_diaryFilename = "diary";
m_captureState = false;
m_capture = "";
m_profile = false;
m_quietlevel = 0;
m_enableWarnings = true;
m_disablerescan = false;
context->pushScope("base","base",false);
}
Interpreter::~Interpreter() {
delete context;
}
bool Interpreter::getDisableRescan() {
return m_disablerescan;
}
void Interpreter::setDisableRescan(bool flag) {
m_disablerescan = flag;
}
bool Interpreter::getStopOverload() {
return stopoverload;
}
void Interpreter::setStopOverload(bool flag) {
stopoverload = flag;
}
// stackentry& Interpreter::activeDebugStack() {
// if (cstack.isEmpty()) throw Exception("Debug stack is corrupted -- please file a bug report that reproduces this problem!");
// if (cstack.size() < 2) return cstack[0];
// return cstack[cstack.size()-2];
// }
// const stackentry& Interpreter::activeDebugStack() const {
// if (cstack.isEmpty()) throw Exception("Debug stack is corrupted -- please file a bug report that reproduces this problem!");
// if (cstack.size() < 2) return cstack[0];
// return cstack[cstack.size()-2];
// }
// We want dbstep(n) to cause us to advance n statements and then
// stop. we execute statement-->set step trap,
void Interpreter::dbstepStatement(const Tree & t) {
int lines = 1;
if (t.hasChildren()) {
Array lval(expression(t.first()));
lines = lval.asInteger();
}
// Get the current function
FuncPtr val;
if (context->scopeName() == "base") return;
ParentScopeLocker lock(context);
if (!lookupFunction(context->scopeDetailString(),val)) {
warningMessage(QString("unable to find function ") + context->scopeDetailString() + " to single step");
return;
}
context->setScopeStepTrap(lines);
context->setScopeStepCurrentLine(LineNumber(context->scopeTokenID()));
}
void Interpreter::dbtraceStatement(const Tree & t) {
int lines = 1;
if (t.hasChildren()) {
Array lval(expression(t.first()));
lines = lval.asInteger();
}
// Get the current function
FuncPtr val;
if (context->scopeDetailString() == "base") return;
ParentScopeLocker lock(context);
if (!lookupFunction(context->scopeDetailString(),val)) {
warningMessage(QString("unable to find function ") + context->scopeDetailString() + " to single step");
return;
}
tracetrap = lines;
tracecurrentline = LineNumber(context->scopeTokenID());
}
// static QString EvalPrep(QString line) {
// QString buf1 = line;
// if (buf1.endsWith('\n'))
// buf1.chop(1);
// if (buf1.endsWith('\r'))
// buf1.chop(1);
// if (buf1.size() > 20)
// buf1 = buf1.left(20) + "...";
// return buf1;
// }
void Interpreter::ExecuteLine(QString txt) {
mutex.lock();
cmd_buffer.push_back(txt);
bufferNotEmpty.wakeAll();
mutex.unlock();
if (m_diaryState) diaryMessage(txt);
}
//PORT
void Interpreter::evaluateString(QString line, bool propogateExceptions) {
Tree b;
Tree t;
m_interrupt = false;
Scanner S(line,"");
Parser P(S);
try{
b = P.process();
t = b;
if (!t.is(TOK_SCRIPT))
throw Exception("Function definition unexpected!");
t = t.first();
} catch(Exception &e) {
if (propogateExceptions)
throw;
errorCount++;
e.printMe(this);
return;
}
try {
block(t);
} catch(Exception &e) {
if (propogateExceptions) throw;
errorCount++;
e.printMe(this);
}
}
QString Interpreter::getLastErrorString() {
return lasterr;
}
void Interpreter::setLastErrorString(QString txt) {
lasterr = txt;
}
void Interpreter::setGreetingFlag(bool skip) {
m_skipflag = skip;
}
void Interpreter::setNoPromptFlag(bool noprompt) {
m_noprompt = noprompt;
}
bool NeedsMoreInput(Interpreter *eval, QString txt) {
// Check for ... or an open []
try {
Scanner S(txt,"");
while (!S.next().is(TOK_EOF))
S.consume();
if (S.inContinuationState() || S.inBracket()) return true;
} catch (Exception &e) {
}
try {
Scanner S(txt,"");
Parser P(S);
P.process();
return false;
} catch (Exception &e) {
if (e.msg().left(13) == "Expecting end") {
return true;
}
}
return false;
}
void Interpreter::sleepMilliseconds(unsigned long msecs) {
QThread::msleep(msecs);
}
QString Interpreter::getLine(QString prompt) {
if (!m_noprompt) emit SetPrompt(prompt);
if (m_diaryState) diaryMessage(prompt);
QString retstring;
emit EnableRepaint();
mutex.lock();
if (cmd_buffer.isEmpty())
bufferNotEmpty.wait(&mutex);
retstring = cmd_buffer.front();
cmd_buffer.erase(cmd_buffer.begin());
mutex.unlock();
emit DisableRepaint();
return retstring;
}
// This is a "generic" CLI routine. The user interface (non-debug)
// version of this is "docli"
void Interpreter::evalCLI() {
QString prompt;
bool rootCLI;
setupWatcher();
while(1) {
QString fname;
int line = 0;
if ((depth == 0) || (context->scopeDepth() < 2)) {
prompt = "--> ";
rootCLI = true;
} else {
int bypasscount = 0;
while (InKeyboardScope(context)) {
bypasscount++;
context->bypassScope(1);
}
fname = context->scopeName();
line = LineNumber(context->scopeTokenID());
QString scopename = context->scopeDetailString();
if (scopename == "builtin")
scopename = context->scopeName();
if (scopename == "docli")
scopename = "base";
prompt = QString("[%1,%2]--> ").arg(scopename).arg(line);
context->restoreScope(bypasscount);
rootCLI = false;
}
if (rootCLI) {
tracetrap = 0;
context->setScopeStepTrap(0);
}
if (m_captureState)
m_capture += prompt;
else {
if (!m_noprompt) emit SetPrompt(prompt);
if (m_diaryState) diaryMessage(prompt);
}
if (m_liveUpdateFlag) {
updateVariablesTool();
updateStackTool();
emit ShowActiveLine(fname,line);
}
QString cmdset;
QString cmdline;
emit EnableRepaint();
mutex.lock();
while ((cmdset.isEmpty() ||
NeedsMoreInput(this,cmdset)) && (!m_interrupt)) {
if (cmd_buffer.isEmpty())
bufferNotEmpty.wait(&mutex);
cmdline = cmd_buffer.front();
cmd_buffer.erase(cmd_buffer.begin());
cmdset += cmdline;
if (m_captureState)
m_capture += cmdline;
}
mutex.unlock();
emit DisableRepaint();
if (m_interrupt) {
m_interrupt = false;
continue;
}
int scope_stackdepth = context->scopeDepth();
setInCLI(true);
dbdown_executed = false;
evaluateString(cmdset,false);
if (!dbdown_executed) {
while (context->scopeDepth() > scope_stackdepth) context->popScope();
}
}
}
//
// Convert a list of variable into indexing expressions
// - for user defined classes, we call subsindex for
// - the object
Array Interpreter::subsindex(const Array &m) {
if (m.isUserClass() && !stopoverload) {
Array t(ClassUnaryOperator(m,"subsindex",this));
return Add(t.toClass(Double),Array(index_t(1)));
}
return m;
}
void Interpreter::subsindex(ArrayVector& m) {
for (int p=0;p<((int)m.size());p++)
m[p] = subsindex(m[p]);
}
|