/usr/src/castle-game-engine-5.2.0/castlescript/castlescript.pas is in castle-game-engine-src 5.2.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | {
Copyright 2001-2014 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" 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.
----------------------------------------------------------------------------
}
{ Base CastleScript structures: values, functions, expressions.
It is designed to be extendable, so you can add new TCasScriptValue
descendants and new TCasScriptFunction descendants, and register
their handlers in FunctionHandlers instance (TCasScriptFunctionHandlers).
Using structures here you can also build CastleScript expressions
by Pascal code (that is, you don't have to parse them). For example
this is an expression that calculates @code(sin(3) + 10 + 1):
@longcode(#
Expr := TCasScriptAdd.Create([
TCasScriptSin.Create([TCasScriptFloat.Create(false, 3)]),
TCasScriptFloat.Create(false, 10),
TCasScriptFloat.Create(false, 1)
]);
#)
You can then call @code(Expr.Execute) to calculate such expression.
To make a variable in the expression, just create and remember a
TCasScriptFloat instance first, and then change it's value freely between
@code(Expr.Execute) calls. For example
@longcode(#
MyVariable := TCasScriptFloat.Create(false, 3);
Expr := TCasScriptAdd.Create([
TCasScriptSin.Create([MyVariable]),
TCasScriptFloat.Create(false, 10),
TCasScriptFloat.Create(false, 1)
]);
Writeln((Expr.Execute as TKamStringFloat).Value); // calculate "sin(3) + 10 + 1"
MyVariable.Value := 4;
Writeln((Expr.Execute as TKamStringFloat).Value); // calculate "sin(4) + 10 + 1"
MyVariable.Value := 5;
Writeln((Expr.Execute as TKamStringFloat).Value); // calculate "sin(5) + 10 + 1"
#)
Note that generally each TCasScriptExpression owns it's children
expressions, so they will be automatically freed when parent is freed.
Also, the values returned by Execute are owned by expression.
So you can simply free whole thing by @code(Expr.Free).
If you're want to parse CastleScript expression from a text
file, see CastleScriptParser.
}
unit CastleScript;
interface
uses SysUtils, Math, Contnrs, CastleUtils, CastleClassUtils, Classes, FGL;
type
{ }
TCasScriptValue = class;
ECasScriptError = class(Exception);
ECasScriptAssignError = class(ECasScriptError);
ECasScriptAnyMathError = class(ECasScriptError);
{ @deprecated Deprecated name for ECasScriptAssignError. }
EKamAssignValueError = ECasScriptAssignError deprecated;
TCasScriptMessage = procedure (const S: string) of object;
{ Various information that may be useful for implementing some
function handlers, but that should be supplied from outside of
CastleScript. }
TCasScriptEnvironment = class
private
FBaseUrl: string;
public
{ Base URL to resolve relative URLs. Similar to TX3DNode.BaseUrl. }
property BaseUrl: string read FBaseUrl write FBaseUrl;
end;
TCasScriptExpression = class
private
FEnvironment: TCasScriptEnvironment;
protected
{ More internal version of Execute.
This doesn't necessarily check floating-point exceptions.
Execute actually calls CoreExecute and then ClearExceptions.
Also this doesn't try to convert EIntError and EMathError
to ECasScriptAnyMathError. This is done by Execute.
When one CastleScript CoreExecute calls another function,
it can use CoreExecute instead of Execute. This way only one
ClearExceptions will be needed for whole expression execution,
instead of doing ClearExceptions after each function handler. }
function CoreExecute: TCasScriptValue; virtual; abstract;
public
(*Execute and calculate this expression.
Returned value is owned by this object. Which should be comfortable
for you usually, as you do not have to worry about freeing it.
Also, it allows us to make various optimizations to avoid
creating/destroying lots of temporary TCasScriptExpression
instances during calculation of complex expression.
The disadvantage of this is that returned object value is valid
only until you executed this same expression again,
or until you freed this expression. If you need to remember the
execute result for longer, you have to copy it somewhere.
For example you can do
@longCode(#
{ This will always work, thanks to virtual TCasScriptValue.Create
and AssignValue methods. }
Copy := TCasScriptValue(ReturnedValue.ClassType).Create;
Copy.AssignValue(ReturnedValue);
#)
@raises(ECasScriptError
Execute is guaranteed to raise an ECasScriptError exception if some
calculation fails because of invalid arguments.
This means that when you run CastleScript expression provided
by the user, you only have to catch ECasScriptError
to be safe from errors produced by user.
No need to catch something more general like Exception class.
Also it's guaranteed that no hanging floating-point errors
are left. Normally, there is no guarantee that
floating-point errors are raised immediately, they may
be raised at the next fp operation (this is needed for fp operations
to proceed in parallel, and be much faster).
For executing CastleScript, Execute calls Math.ClearExceptions(true)
to make sure that all floating point errors are caught.
This ensures that we can safely execute even invalid expressions
(like 'ln(-3)') and get reliable exceptions.
Floating-point errors of course also result in ECasScriptError descendants.
More specifically, EIntError and EMathError result
in ECasScriptAnyMathError.)
*)
function Execute: TCasScriptValue;
{ Try to execute expression, or return @nil if a mathematical error occurred
within expression. "Math error within expression" means that
a ECasScriptAnyMathError exception occurred while calculating expression.
This is useful to secure you against math arguments errors ('ln(-3)',
'sqrt(-3)') but still raises normal exception on other ECasScriptError
errors (like invalid argument type for function). }
function TryExecuteMath: TCasScriptValue;
{ Execute expression, return the result as a simple float value.
It assumes that the expression is written to always return float.
To easily create such expression, use @link(ParseFloatExpression). }
function AsFloat(const ADefaultValue: Float = 0): Float;
{ Call Free, but only if this is not TCasScriptValue with
OwnedByParentExpression = false. (This cannot be implemented
cleanly, as virtual procedure, since it must work when Self is @nil,
and then virtual method table is not available of course.) }
procedure FreeByParentExpression;
{ Environment (outside information) for this expression.
May be @nil. This object is not owned by TCasScriptExpression,
will not be freed by TCasScriptExpression and such. }
property Environment: TCasScriptEnvironment read FEnvironment write FEnvironment;
end;
TCasScriptExpressionList = class(specialize TFPGObjectList<TCasScriptExpression>)
public
procedure AddArray(const A: array of TCasScriptExpression);
procedure AddList(const Source: TCasScriptExpressionList);
procedure FreeContentsByParentExpression;
end;
TCasScriptValue = class(TCasScriptExpression)
private
FOwnedByParentExpression: boolean;
FName: string;
FValueAssigned: boolean;
FWriteable: boolean;
protected
function CoreExecute: TCasScriptValue; override;
public
constructor Create(const AWriteable: boolean); virtual;
{ Is this value writeable.
If not, this will not be allowed to change by CastleScript assignment
and such functions. Note that Writeable = @false will not prevent
you from changing value internally, by AssignValue or changin
Value property directly (that would be too uncomfortable). }
property Writeable: boolean read FWriteable write FWriteable;
property OwnedByParentExpression: boolean
read FOwnedByParentExpression write FOwnedByParentExpression
default true;
{ Name of this value, or '' if not named.
Named value can be recognized in expressions by CastleScriptParser. }
property Name: string read FName write FName;
{ Assign value from Source to Self.
@raises(ECasScriptAssignError if assignment is not possible
because types don't match.) }
procedure AssignValue(Source: TCasScriptValue); virtual; abstract;
{ Set to @true on each assign to Value. You can reset it at any time
to @false.
This allows the caller to know which variables were
assigned during script execution, which is useful if changes to
CastleScript variables should be propagated to some other things
after the script finished execution. This is essential for behavior
in VRML/X3D Script node.
Descendants note: you have to set this to @true in SetValue. }
property ValueAssigned: boolean read FValueAssigned write FValueAssigned
default false;
end;
TCasScriptValueClass = class of TCasScriptValue;
TCasScriptValueClassArray = array of TCasScriptValueClass;
TCasScriptValueList = class(specialize TFPGObjectList<TCasScriptValue>)
public
procedure AddArray(const A: array of TCasScriptValue);
end;
{ This is a very special CastleScript value, used to represent user-defined
function parameter. This poses itself as a TCasScriptValue descendant,
and it has working AssignValue and everything else. This way it can
be used in "Variables" list for various CastleScriptParser functions.
Except it's cheating: it doesn't actually store the value.
Instead, it has SourceValue property that is used when doing
AssignValue. So AssignValue is handled by SourceValue.AssignValue,
and Execute is handled by SourceValue.Execute, and so reading/writing
this works.
The advantage: the exact type of function parameter is not known,
and still we can parse the function expression. This is crucial
for parser implementation: when parsing you need to create
TCasScriptParameterValue instance, but you don't know actual
type of parameter that will be passed here. }
TCasScriptParameterValue = class(TCasScriptValue)
private
FSourceValue: TCasScriptValue;
protected
function CoreExecute: TCasScriptValue; override;
public
property SourceValue: TCasScriptValue read FSourceValue write FSourceValue;
procedure AssignValue(Source: TCasScriptValue); override;
end;
TCasScriptFloat = class;
TCasScriptFunction = class;
TCasScriptInteger = class(TCasScriptValue)
private
class procedure HandleAdd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleSubtract(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleNegate(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleMultiply(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleDivide(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleModulo(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandlePower(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleMax(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleMin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleSqr(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleSgn(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleAbs(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleRandom(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleGreater(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleLesser(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleGreaterEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleLesserEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure ConvertFromInt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure ConvertFromFloat(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure ConvertFromBool(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure ConvertFromString(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
private
FPromoteToFloat: TCasScriptFloat;
FValue: Int64;
procedure SetValue(const AValue: Int64);
public
{ Comfortable constructor to set initial Value.
Note that the inherited constructor (without AValue parameter)
is also fine to use, it will set value to zero. }
constructor Create(const AWriteable: boolean; const AValue: Int64);
constructor Create(const AWriteable: boolean); override;
destructor Destroy; override;
property Value: Int64 read FValue write SetValue;
procedure AssignValue(Source: TCasScriptValue); override;
{ Returns this integer promoted to float.
This object is kept and owned by this TCasScriptInteger instance,
so it's valid as long as this TCasScriptInteger instance is valid.
This allows you to safely use this (since you may have to return
PromoteToFloat as return value of some Execute expressions,
so it desirable that it's valid object reference). }
function PromoteToFloat: TCasScriptFloat;
end;
TCasScriptFloat = class(TCasScriptValue)
private
class procedure HandleAdd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleSubtract(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleMultiply(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleDivide(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleNegate(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleModulo(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleSin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleCos(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleTan(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleCotan(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleArcSin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleArcCos(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleArcTan(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleArcCotan(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleSinh(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleCosh(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleTanh(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleCotanh(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleLog2(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleLn(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleLog(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandlePower2(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleExp(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandlePower(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleSqr(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleSqrt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleMax(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleMin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleSgn(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleAbs(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleRandom(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleCeil(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleFloor(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleRound(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleGreater(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleLesser(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleGreaterEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleLesserEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure ConvertFromInt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure ConvertFromFloat(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure ConvertFromBool(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure ConvertFromString(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
private
FValue: Float;
procedure SetValue(const AValue: Float);
public
{ Comfortable constructor to set initial Value.
Note that the inherited constructor (without AValue parameter)
is also fine to use, it will set value to zero. }
constructor Create(const AWriteable: boolean; const AValue: Float);
constructor Create(const AWriteable: boolean); override;
property Value: Float read FValue write SetValue;
procedure AssignValue(Source: TCasScriptValue); override;
end;
TCasScriptBoolean = class(TCasScriptValue)
private
class procedure HandleOr(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleAnd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleNot(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleGreater(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleLesser(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleGreaterEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleLesserEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure ConvertFromInt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure ConvertFromFloat(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure ConvertFromBool(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure ConvertFromString(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
private
FValue: boolean;
procedure SetValue(const AValue: boolean);
public
{ Comfortable constructor to set initial Value.
Note that the inherited constructor (without AValue parameter)
is also fine to use, it will set value to false. }
constructor Create(const AWriteable: boolean; const AValue: boolean);
constructor Create(const AWriteable: boolean); override;
property Value: boolean read FValue write SetValue;
procedure AssignValue(Source: TCasScriptValue); override;
end;
TCasScriptString = class(TCasScriptValue)
private
class procedure HandleAdd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleGreater(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleLesser(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleGreaterEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleLesserEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure ConvertFromInt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure ConvertFromFloat(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure ConvertFromBool(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure ConvertFromString(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleWriteln(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
class procedure HandleCharacterFromCode(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
private
FValue: string;
procedure SetValue(const AValue: string);
public
{ Comfortable constructor to set initial Value.
Note that the inherited constructor (without AValue parameter)
is also fine to use, it will set value to ''. }
constructor Create(const AWriteable: boolean; const AValue: string);
constructor Create(const AWriteable: boolean); override;
property Value: string read FValue write SetValue;
procedure AssignValue(Source: TCasScriptValue); override;
end;
TCasScriptRegisteredHandler = class;
TCasScriptSearchArgumentClassesCache = record
IsCache: boolean;
QueryHandlersByArgument: TObjectList;
QueryArgumentClasses: TCasScriptValueClassArray;
Answer: boolean;
AnswerArgumentIndex: Integer;
AnswerHandler: TCasScriptRegisteredHandler;
end;
TCasScriptFunction = class(TCasScriptExpression)
private
FArgs: TCasScriptExpressionList;
LastExecuteResult: TCasScriptValue;
ParentOfLastExecuteResult: boolean;
{ This is as returned by SearchFunctionClass }
HandlersByArgument: TObjectList;
{ Helper variables for Execute implementation.
Initialized in CheckArguments, to optimize: profiling shows that when
they are intialized in Execute, this takes quite a lot of Execute time. }
ExecuteArguments: array of TCasScriptValue;
ExecuteArgumentClasses: TCasScriptValueClassArray;
{ Caches for SearchArgumentClasses, used to speed up Execute }
Cache1: TCasScriptSearchArgumentClassesCache;
Cache2: TCasScriptSearchArgumentClassesCache;
protected
{ Used by constructor to check are args valid.
Also, right now this gets FunctionHandlersByArgument (this way we don't
have to search it at each TCasScriptFunction.Execute call,
so TCasScriptFunction.Execute may work much faster).
@raises(ECasScriptFunctionArgumentsError on invalid Args passed to
function.) }
procedure CheckArguments; virtual;
function CoreExecute: TCasScriptValue; override;
public
{ Constructor initializing Args from given TCasScriptExpressionList.
AArgs list contents is copied, i.e. AArgs refence is not
stored or freed by TCasScriptFunction. But items on AArags are not copied
recursively, we copy references from AArags items, and so we become
their owners.
@raises(ECasScriptFunctionArgumentsError if you specified invalid
number of arguments for this function.)
}
constructor Create(AArgs: TCasScriptExpressionList); overload;
constructor Create(const AArgs: array of TCasScriptExpression); overload;
destructor Destroy; override;
{ Long function name for user. This is possibly with spaces,
parenthesis and other funny characters. It will be used in
error messages and such to describe this function.
Default implementation in this class simply returns ShortName.
This should be suitable for most "norma" functions. }
class function Name: string; virtual;
{ Short function name, for the parser.
This is the name of the function for use in expressions
like "function_name(arg_1, arg_2 ... , arg_n)".
This can be empty string ('') if no such name for this function exists,
then the logic to parse this function expressions must be somehow
built in the parser (for example, operators use this: they are
just normal functions, TCasScriptFunction, with ShortName = ''
and special support in the parser). }
class function ShortName: string; virtual; abstract;
{ Function name when used as an infix operator.
Empty string ('') if no such name for this function.
This is returned by default implementation of this in this class.
This does require cooperation from the parser to actually work,
that is you cannot simply define new operators by
registering new TCasScriptFunction with InfixOperatorName <> ''.
For now.
Note that at least one of ShortName and InfixOperatorName
must not be empty.
The only exception is the TCasScriptNegate function, that is neither
infix operator nor a usual function that must be specified
as "function_name(arguments)". So this is an exception,
and if there will be a need, I shall fix this (probably
by introducing some third field, like PrefixOperatorName ?)
Note 2 things:
@orderedList(
@item(
Function that can be used as infix operator (i.e. has
InfixOperatorName <> '') is not necessary binary operator,
i.e. InfixOperatorName <> '' does not determine the value of
ArgsCount. This way I was able to define infix operators
+, -, * etc. that take any number of arguments and operators
like ^ and > that always take 2 arguments.)
@item(
Function may have both ShortName <> '' and InfixOperatorName <> ''.
E.g. TCasScriptPower can be used as "Power(3, 1.5)" or "3 ^ 1.5".)
) }
class function InfixOperatorName: string; virtual;
{ Specify which arguments are calculated before function handler
is called.
If = -1 (default value returned by implementation
in this class) then all arguments are greedily calculated,
which simply means that all arguments are calculated before
executing function handler. This is the usual and expected
behavior of normal functions. It's also a prerequisite
for most of overloaded things to work, since we need to know
types of @italic(calculated) arguments (TCasScriptValue classes)
before we choose overloaded handler for function.
If this is >= 0, then arguments with index >= of this will not
be calculated before handler execution.
Since their type is unknown, they will match any type in
handler's ArgumentClasses.
Your handler will receive @nil in their places, and is responsible
for calling their Execute on it's own if needed.
This is particularly suited for implementing control-flow
instructions, like "if" and "while", as normal functions
inside CastleScript. For example, "if" will have
GreedyArgumentsCalculation = 1, so the first argument (condition)
will be calculated, but the execution of 2nd or 3rd argument
("then" code or "else" code) will be left to the handler. }
class function GreedyArgumentsCalculation: Integer; virtual;
{ Which arguments should be assignable by this function.
Default implementation in TCasScriptFunction just returns @false
always. If you're making a function that changes it's argument
(like assignment operator, or vector_set, array_set and such)
you want to override this.
This is actually checked by CheckArguments, called from
constructors. }
class function ArgumentMustBeAssignable(const Index: Integer): boolean; virtual;
{ Function arguments. Don't modify this list after function is created
(although you can modify values inside arguments). }
property Args: TCasScriptExpressionList read FArgs;
end;
TCasScriptFunctionClass = class of TCasScriptFunction;
{ Calculate result on given function arguments Arguments.
Place result in AResult.
The current function is also passed here, although usually you don't need it
(you already get a list of calculated Arguments, and you should
register different procedures for different TCasScriptFunction classes,
so you know what operation on arguments should be done).
For functions when GreedyArgumentsCalculation >= 0, it may be useful
to directly access AFunction.Args.
If needed, previous value of AResult should be freed and new created.
If current AResult is <> nil and it's of appropriate class,
you may also reuse it and only change it's fields
(this is helpful, to avoid many creations/destroying
of class instances while calculating an expression many times).
CreateValueIfNeeded may be helpful for implementing this. }
TCasScriptFunctionHandler = procedure (
AFunction: TCasScriptFunction;
const Arguments: array of TCasScriptValue;
var AResult: TCasScriptValue;
var ParentOfResult: boolean) of object;
TCasScriptSequence = class(TCasScriptFunction)
private
class procedure HandleSequence(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
public
class function Name: string; override;
class function ShortName: string; override;
class function InfixOperatorName: string; override;
end;
{ CastleScript assignment operator. This is a special function,
that must have TCasScriptValue (with Writeable = true) as it's 1st argument. }
TCasScriptAssignment = class(TCasScriptFunction)
private
class procedure HandleAssignment(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
public
class function Name: string; override;
class function ShortName: string; override;
class function InfixOperatorName: string; override;
class function ArgumentMustBeAssignable(const Index: Integer): boolean; override;
end;
TCasScriptIf = class(TCasScriptFunction)
private
class procedure HandleIf(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
public
class function ShortName: string; override;
class function GreedyArgumentsCalculation: Integer; override;
end;
TCasScriptWhen = class(TCasScriptFunction)
private
class procedure HandleWhen(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
public
class function ShortName: string; override;
class function GreedyArgumentsCalculation: Integer; override;
end;
TCasScriptWhile = class(TCasScriptFunction)
private
class procedure HandleWhile(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
public
class function ShortName: string; override;
class function GreedyArgumentsCalculation: Integer; override;
end;
TCasScriptFor = class(TCasScriptFunction)
private
class procedure HandleFor(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
public
class function ShortName: string; override;
class function GreedyArgumentsCalculation: Integer; override;
class function ArgumentMustBeAssignable(const Index: Integer): boolean; override;
end;
TCasScriptRegisteredHandler = class
private
FHandler: TCasScriptFunctionHandler;
FFunctionClass: TCasScriptFunctionClass;
FArgumentClasses: TCasScriptValueClassArray;
FVariableArgumentsCount: boolean;
public
constructor Create(
AHandler: TCasScriptFunctionHandler;
AFunctionClass: TCasScriptFunctionClass;
const AArgumentClasses: TCasScriptValueClassArray;
const AVariableArgumentsCount: boolean);
property Handler: TCasScriptFunctionHandler read FHandler;
property FunctionClass: TCasScriptFunctionClass read FFunctionClass;
property ArgumentClasses: TCasScriptValueClassArray read FArgumentClasses;
{ Is the handler able to receive any number of arguments.
If yes, then the last argument class
may be repeated any number of times (but must occur
at least once). That is, the ArgumentClasses array
dictates the required arguments, and more arguments are allowed.
Note that this means that at least one argument
must be allowed (we have to know the argument class that can
be repeated at the end), otherwise the handler will not be able to receive
variable number of arguments anyway. }
property VariableArgumentsCount: boolean read FVariableArgumentsCount;
end;
{ This specifies for each type combination (array of TCasScriptValue classes)
and for each function (TCasScriptFunction class) how they should
be handled. You can think of this as a table that has a handler
for each possible TCasScriptValue sequence and TCasScriptFunction
combination.
The idea is to allow programmer to extend KambiScipt by
@orderedList(
@item(Defining new types of values for CastleScript:
add new TCasScriptValue class, and create handlers for known
functions to handle this type.
It may be comfortable to place these handlers as private methods
within your new TCasScriptValue descendant, although this is your
private decision.)
@item(Defining new functions for CastleScript:
add new TCasScriptFunction class, and create handlers for known
values to be handled by this function.
It may be comfortable to place these handlers as private methods
within your new TCasScriptFunction descendant, although this is your
private decision.)
)
You have a guarantee that every registered here Handler will be called
only with AFunction of registstered type and all Arguments
matching the array of registered types and satisfying
VariableArgumentsCount setting.
As a bonus, this also provides a list of all usable function classes.
That's because you have to register at least one handler for each
TCasScriptFunction descendant to make this function actually usable,
so we know about it here. }
TCasScriptFunctionHandlers = class
private
{ This is a list of another TObjectList lists.
Each nested list has only TCasScriptRegisteredHandler items.
It always has at least one item.
Each nested list has only equal FunctionClass values. }
FHandlersByFunction: TObjectList;
function SearchFunctionClass(
FunctionClass: TCasScriptFunctionClass;
out FunctionIndex: Integer;
out HandlersByArgument: TObjectList): boolean; overload;
function SearchFunctionClass(
FunctionClass: TCasScriptFunctionClass;
out HandlersByArgument: TObjectList): boolean; overload;
function SearchArgumentClasses(
HandlersByArgument: TObjectList;
const ArgumentClasses: TCasScriptValueClassArray;
out ArgumentIndex: Integer;
out Handler: TCasScriptRegisteredHandler): boolean; overload;
function SearchArgumentClasses(
HandlersByArgument: TObjectList;
const ArgumentClasses: TCasScriptValueClassArray;
out Handler: TCasScriptRegisteredHandler): boolean; overload;
{ This uses Cache to speed up SearchArgumentClasses.
The cache remembers last HandlersByArgument, ArgumentClasses,
and answer for them (if @true). Use this if you suspect that
SearchArgumentClasses will be called many times with the same
HandlersByArgument, ArgumentClasses --- then this will use cache
to give answer much faster. }
function SearchArgumentClasses(
HandlersByArgument: TObjectList;
const ArgumentClasses: TCasScriptValueClassArray;
out Handler: TCasScriptRegisteredHandler;
var Cache: TCasScriptSearchArgumentClassesCache): boolean; overload;
public
constructor Create;
destructor Destroy; override;
procedure RegisterHandler(
AHandler: TCasScriptFunctionHandler;
AFunctionClass: TCasScriptFunctionClass;
const AArgumentClasses: array of TCasScriptValueClass;
const AVariableArgumentsCount: boolean);
{ Search for function class with matching ShortName.
Returns @nil if not found. }
function SearchFunctionShortName(const AShortName: string): TCasScriptFunctionClass;
end;
ECasScriptFunctionArgumentsError = class(ECasScriptError);
ECasScriptFunctionNoHandler = class(ECasScriptError);
{ CastleScript user function definition.
Not to be confused with TCasScriptFunction: TCasScriptFunction is
an internal, built-in function or operator. This class represents
functions defined by user. }
TCasScriptUserFunction = class
private
FName: string;
FParameters: TCasScriptValueList;
FBody: TCasScriptExpression;
public
constructor Create;
destructor Destroy; override;
property Name: string read FName write FName;
{ List of function parameters.
Note that they are also referenced inside function Expression,
so you simply change them to set value of this parameter within
whole function body.
These are always fresh variables, not referenced anywhere outside
of Body. This means that they are owned (always, regardless of
OwnedByParentExpression) by this class.
They must always be of TCasScriptParameterValue class. }
property Parameters: TCasScriptValueList read FParameters;
{ Function body. }
property Body: TCasScriptExpression read FBody write FBody;
end;
TCasScriptUserFunctionList = class(specialize TFPGObjectList<TCasScriptUserFunction>)
function IndexOf(const FunctionName: string): Integer;
end;
ECasScriptMissingFunction = class(ECasScriptError);
TCasScriptProgram = class
private
FFunctions: TCasScriptUserFunctionList;
FEnvironment: TCasScriptEnvironment;
public
constructor Create;
destructor Destroy; override;
property Functions: TCasScriptUserFunctionList read FFunctions;
{ Execute a user-defined function (from Functions list of this program).
@unorderedList(
@item(Looks for given FunctionName.
IgnoreMissingFunction says what to do in case of missing function:
if true, it will be simply ignored (ExecuteFunction will
silently do nothng). If false (default)
then we will raise exception ECasScriptMissingFunction.)
@item(Sets function parameters to given values
(number of parameters must match, otherwise ECasScriptError).
Values from your parameters are set as our parameters
TCasScriptParameterValue.SourceValue, so script can read and write
your values.)
@item(Finally executes function body.)
)
}
procedure ExecuteFunction(const FunctionName: string;
const Parameters: array of TCasScriptValue;
const IgnoreMissingFunction: boolean = false);
{ Environment (outside information) for this program.
This will be created and is owned by this TCasScriptProgram instance.
You should take care to copy this reference to all expressions
within this program (for example ParseProgram does this),
this way all expressions share the same Environment instance. }
property Environment: TCasScriptEnvironment read FEnvironment write FEnvironment;
end;
var
FunctionHandlers: TCasScriptFunctionHandlers;
{ Make sure Value is assigned and of NeededClass.
If Value is not assigned, or is not exactly of NeededClass,
it will be freed and new will be created. }
procedure CreateValueIfNeeded(var Value: TCasScriptValue;
var ParentOfValue: boolean;
NeededClass: TCasScriptValueClass);
var
{ Global method to output messages done by CastleScript @code(writeln())
function. If not assigned, we will use OnWarning. }
OnScriptMessage: TCasScriptMessage;
implementation
uses CastleScriptCoreFunctions, CastleWarnings;
{ FPC 2.2.2 has bug http://bugs.freepascal.org/view.php?id=12214
that strongly hits calculating invalid expressions.
This results in calls like
gen_function "ln(x)" -10 10 0.1
gen_function "sqrt(x)" -10 10 0.1
to fail after a couple of "break" lines with
gen_function: Exception EInvalidOp (at address 0x080488B5) :
Invalid floating point operation
An unhandled exception occurred at $080488B5 :
EInvalidOp : Invalid floating point operation
$080488B5 main, line 127 of gen_function.lpr
I tried to make more elegant workarounds by doing dummy fp
operations at the end of function calculation or Execute, to cause
the exception, but it just looks like EInvalidOp is never cleared by
try..except block.
The only workaround seems to be to use Set8087CW to mask exceptions,
and then compare with NaN to make proper Execute implementation.
Tests show that FPC 2.2.3 (fixes_2_2) also has this bug, and so 2.2.4
will probably have this too...
Also 2.3.1 (current trunk on 2009-03-03) still has it...
It's easier to just define it always, for now. }
{$define WORKAROUND_EXCEPTIONS_FOR_SCRIPT_EXPRESSIONS}
{ TCasScriptExpression ------------------------------------------------------- }
procedure TCasScriptExpression.FreeByParentExpression;
begin
if (Self <> nil) and
( (not (Self is TCasScriptValue)) or
TCasScriptValue(Self).OwnedByParentExpression ) then
Free;
end;
function TCasScriptExpression.Execute: TCasScriptValue;
begin
try
Result := CoreExecute;
{ Force raising pending exceptions by FP calculations }
ClearExceptions(true);
except
{ Convert EIntError and EMathError to ECasScriptAnyMathError }
on E: EIntError do
raise ECasScriptAnyMathError.CreateFmt('Integer error %s: %s',
[E.ClassName, E.Message]);
on E: EMathError do
raise ECasScriptAnyMathError.CreateFmt('Math error %s: %s',
[E.ClassName, E.Message]);
end;
{$ifdef WORKAROUND_EXCEPTIONS_FOR_SCRIPT_EXPRESSIONS}
{$I norqcheckbegin.inc}
if (Result is TCasScriptFloat) and
( IsNan(TCasScriptFloat(Result).Value) or
IsInfinite(TCasScriptFloat(Result).Value) ) then
raise ECasScriptAnyMathError.Create('Floating point error');
{$I norqcheckend.inc}
{$endif}
end;
function TCasScriptExpression.TryExecuteMath: TCasScriptValue;
begin
try
Result := Execute;
except
on ECasScriptAnyMathError do
Result := nil;
end;
end;
function TCasScriptExpression.AsFloat(const ADefaultValue: Float): Float;
var
Res: TCasScriptValue;
begin
try
Res := Execute;
except
on E: ECasScriptError do
begin
OnWarning(wtMajor, 'CastleScript', 'Error when executing CastleScript expression: ' + E.Message);
Result := ADefaultValue;
Exit;
end;
end;
if Res is TCasScriptFloat then
Result := TCasScriptFloat(Res).Value else
begin
OnWarning(wtMajor, 'CastleScript', 'CastleScript expression result is not float');
Result := ADefaultValue;
end;
end;
{ TCasScriptExpressionList -------------------------------------------------- }
procedure TCasScriptExpressionList.AddArray(const A: array of TCasScriptExpression);
var
OldCount: Integer;
begin
OldCount := Count;
Count := Count + High(A) + 1;
if High(A) <> -1 then
System.Move(A[0], List^[OldCount], SizeOf(Pointer) * (High(A) + 1));
end;
procedure TCasScriptExpressionList.AddList(const Source: TCasScriptExpressionList);
var
OldCount: Integer;
begin
OldCount := Count;
Count := Count + Source.Count;
if Source.Count <> 0 then
System.Move(Source.List^[0], List^[OldCount], SizeOf(Pointer) * Source.Count);
end;
procedure TCasScriptExpressionList.FreeContentsByParentExpression;
var
I: Integer;
begin
for I := 0 to Count - 1 do
begin
Items[I].FreeByParentExpression;
FPGObjectList_NilItem(Self, I);
end;
end;
{ TCasScriptValue ------------------------------------------------------------ }
constructor TCasScriptValue.Create(const AWriteable: boolean);
begin
inherited Create;
FOwnedByParentExpression := true;
FWriteable := AWriteable;
end;
function TCasScriptValue.CoreExecute: TCasScriptValue;
begin
{ Since we own Execute result, we can simply return self here. }
Result := Self;
end;
{ TCasScriptValueList ------------------------------------------------------- }
procedure TCasScriptValueList.AddArray(const A: array of TCasScriptValue);
var
OldCount: Integer;
begin
OldCount := Count;
Count := Count + High(A) + 1;
if High(A) <> -1 then
System.Move(A[0], List^[OldCount], SizeOf(Pointer) * (High(A) + 1));
end;
{ TCasScriptParameterValue --------------------------------------------------- }
function TCasScriptParameterValue.CoreExecute: TCasScriptValue;
begin
Result := SourceValue.CoreExecute;
end;
procedure TCasScriptParameterValue.AssignValue(Source: TCasScriptValue);
begin
SourceValue.AssignValue(Source);
end;
{ TCasScriptInteger ---------------------------------------------------------- }
constructor TCasScriptInteger.Create(const AWriteable: boolean; const AValue: Int64);
begin
Create(AWriteable);
Value := AValue;
end;
constructor TCasScriptInteger.Create(const AWriteable: boolean);
begin
inherited Create(AWriteable);
end;
destructor TCasScriptInteger.Destroy;
begin
FPromoteToFloat.FreeByParentExpression;
inherited;
end;
function TCasScriptInteger.PromoteToFloat: TCasScriptFloat;
begin
if FPromoteToFloat = nil then
FPromoteToFloat := TCasScriptFloat.Create(false, Value) else
FPromoteToFloat.Value := Value;
Result := FPromoteToFloat;
end;
class procedure TCasScriptInteger.HandleAdd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
{ The function allows only >= 1 arguments, and this handler is
registered only for TCasScriptInteger values, so we can safely take
the first arg as TCasScriptInteger. }
TCasScriptInteger(AResult).Value := TCasScriptInteger(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptInteger(AResult).Value :=
TCasScriptInteger(AResult).Value + TCasScriptInteger(Arguments[I]).Value;
end;
class procedure TCasScriptInteger.HandleSubtract(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
TCasScriptInteger(AResult).Value := TCasScriptInteger(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptInteger(AResult).Value :=
TCasScriptInteger(AResult).Value - TCasScriptInteger(Arguments[I]).Value;
end;
class procedure TCasScriptInteger.HandleMultiply(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
TCasScriptInteger(AResult).Value := TCasScriptInteger(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptInteger(AResult).Value :=
TCasScriptInteger(AResult).Value * TCasScriptInteger(Arguments[I]).Value;
end;
class procedure TCasScriptInteger.HandleDivide(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
TCasScriptInteger(AResult).Value := TCasScriptInteger(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptInteger(AResult).Value :=
TCasScriptInteger(AResult).Value div TCasScriptInteger(Arguments[I]).Value;
end;
class procedure TCasScriptInteger.HandleNegate(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
TCasScriptInteger(AResult).Value := - TCasScriptInteger(Arguments[0]).Value;
end;
class procedure TCasScriptInteger.HandleModulo(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
TCasScriptInteger(AResult).Value :=
TCasScriptInteger(Arguments[0]).Value mod
TCasScriptInteger(Arguments[1]).Value;
end;
class procedure TCasScriptInteger.HandlePower(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
if (TCasScriptInteger(Arguments[0]).Value < 0) or
(TCasScriptInteger(Arguments[1]).Value < 0) then
raise ECasScriptError.Create('Power function on integer operands expects both arguments to be >= 0');
TCasScriptInteger(AResult).Value := NatNatPower(
TCasScriptInteger(Arguments[0]).Value,
TCasScriptInteger(Arguments[1]).Value );
end;
class procedure TCasScriptInteger.HandleSqr(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
TCasScriptInteger(AResult).Value := Sqr( TCasScriptInteger(Arguments[0]).Value );
end;
class procedure TCasScriptInteger.HandleMax(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
TCasScriptInteger(AResult).Value := TCasScriptInteger(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptInteger(AResult).Value := Max(
TCasScriptInteger(AResult).Value, TCasScriptInteger(Arguments[I]).Value);
end;
class procedure TCasScriptInteger.HandleMin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
TCasScriptInteger(AResult).Value := TCasScriptInteger(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptInteger(AResult).Value := Min(
TCasScriptInteger(AResult).Value, TCasScriptInteger(Arguments[I]).Value);
end;
class procedure TCasScriptInteger.HandleSgn(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
TCasScriptInteger(AResult).Value := Sign( TCasScriptInteger(Arguments[0]).Value );
end;
class procedure TCasScriptInteger.HandleAbs(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
TCasScriptInteger(AResult).Value := Abs( TCasScriptInteger(Arguments[0]).Value );
end;
class procedure TCasScriptInteger.HandleRandom(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
TCasScriptInteger(AResult).Value := Random( TCasScriptInteger(Arguments[0]).Value );
end;
class procedure TCasScriptInteger.HandleGreater(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptInteger(Arguments[0]).Value >
TCasScriptInteger(Arguments[1]).Value;
end;
class procedure TCasScriptInteger.HandleLesser(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptInteger(Arguments[0]).Value <
TCasScriptInteger(Arguments[1]).Value;
end;
class procedure TCasScriptInteger.HandleGreaterEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptInteger(Arguments[0]).Value >=
TCasScriptInteger(Arguments[1]).Value;
end;
class procedure TCasScriptInteger.HandleLesserEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptInteger(Arguments[0]).Value <=
TCasScriptInteger(Arguments[1]).Value;
end;
class procedure TCasScriptInteger.HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptInteger(Arguments[0]).Value =
TCasScriptInteger(Arguments[1]).Value;
end;
class procedure TCasScriptInteger.HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptInteger(Arguments[0]).Value <>
TCasScriptInteger(Arguments[1]).Value;
end;
class procedure TCasScriptInteger.ConvertFromInt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
if ParentOfResult then
AResult.FreeByParentExpression else
AResult := nil;
AResult := Arguments[0];
Assert(AResult is TCasScriptInteger);
ParentOfResult := false;
end;
class procedure TCasScriptInteger.ConvertFromFloat(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
F: Float;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
{ Can't use Int function, as it returns float value }
F := TCasScriptFloat(Arguments[0]).Value;
if F >= 0 then
TCasScriptInteger(AResult).Value := Floor(F) else
TCasScriptInteger(AResult).Value := Ceil(F);
end;
class procedure TCasScriptInteger.ConvertFromBool(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
BoolTo01: array [boolean] of Int64 = (0, 1);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
TCasScriptInteger(AResult).Value := BoolTo01[TCasScriptBoolean(Arguments[0]).Value];
end;
class procedure TCasScriptInteger.ConvertFromString(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
try
TCasScriptInteger(AResult).Value := StrToInt64(TCasScriptString(Arguments[0]).Value);
except
on E: EConvertError do
{ Change EConvertError to ECasScriptError }
raise ECasScriptError.CreateFmt('Error when converting string "%s" to integer: %s',
[TCasScriptString(Arguments[0]).Value, E.Message]);
end;
end;
procedure TCasScriptInteger.AssignValue(Source: TCasScriptValue);
begin
if Source is TCasScriptInteger then
Value := TCasScriptInteger(Source).Value else
raise ECasScriptAssignError.CreateFmt('Assignment from %s to %s not possible', [Source.ClassName, ClassName]);
end;
procedure TCasScriptInteger.SetValue(const AValue: Int64);
begin
FValue := AValue;
ValueAssigned := true;
end;
{ TCasScriptFloat ------------------------------------------------------- }
constructor TCasScriptFloat.Create(const AWriteable: boolean; const AValue: Float);
begin
Create(AWriteable);
Value := AValue;
end;
constructor TCasScriptFloat.Create(const AWriteable: boolean);
begin
inherited Create(AWriteable);
end;
class procedure TCasScriptFloat.HandleAdd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
{ The function allows only >= 1 arguments, and this handler is
registered only for TCasScriptFloat values, so we can safely take
the first arg as TCasScriptFloat. }
TCasScriptFloat(AResult).Value := TCasScriptFloat(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptFloat(AResult).Value :=
TCasScriptFloat(AResult).Value + TCasScriptFloat(Arguments[I]).Value;
end;
class procedure TCasScriptFloat.HandleSubtract(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := TCasScriptFloat(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptFloat(AResult).Value :=
TCasScriptFloat(AResult).Value - TCasScriptFloat(Arguments[I]).Value;
end;
class procedure TCasScriptFloat.HandleMultiply(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := TCasScriptFloat(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptFloat(AResult).Value :=
TCasScriptFloat(AResult).Value * TCasScriptFloat(Arguments[I]).Value;
end;
class procedure TCasScriptFloat.HandleDivide(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := TCasScriptFloat(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptFloat(AResult).Value :=
TCasScriptFloat(AResult).Value / TCasScriptFloat(Arguments[I]).Value;
end;
class procedure TCasScriptFloat.HandleNegate(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := - TCasScriptFloat(Arguments[0]).Value;
end;
class procedure TCasScriptFloat.HandleModulo(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value :=
TCasScriptFloat(Arguments[0]).Value -
Floor( TCasScriptFloat(Arguments[0]).Value /
TCasScriptFloat(Arguments[1]).Value )
* TCasScriptFloat(Arguments[1]).Value;
end;
class procedure TCasScriptFloat.HandleSin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := Sin( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleCos(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := Cos( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleTan(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := Tan( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleCotan(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := CastleCoTan( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleArcSin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := ArcSin( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleArcCos(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := ArcCos( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleArcTan(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := ArcTan( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleArcCotan(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := ArcCot( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleSinh(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := SinH( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleCosh(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := CosH( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleTanh(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := TanH( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleCotanh(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := 1 / TanH( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleLog2(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := Log2( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleLn(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := Ln( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleLog(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := Logn( TCasScriptFloat(Arguments[0]).Value,
TCasScriptFloat(Arguments[1]).Value );
end;
class procedure TCasScriptFloat.HandlePower2(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := Power(2, TCasScriptFloat(Arguments[0]).Value);
end;
class procedure TCasScriptFloat.HandleExp(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := Exp( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandlePower(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := Power(
TCasScriptFloat(Arguments[0]).Value,
TCasScriptFloat(Arguments[1]).Value );
end;
class procedure TCasScriptFloat.HandleSqr(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := Sqr( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleSqrt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := Sqrt( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleMax(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := TCasScriptFloat(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptFloat(AResult).Value := Max(
TCasScriptFloat(AResult).Value, TCasScriptFloat(Arguments[I]).Value);
end;
class procedure TCasScriptFloat.HandleMin(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := TCasScriptFloat(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptFloat(AResult).Value := Min(
TCasScriptFloat(AResult).Value, TCasScriptFloat(Arguments[I]).Value);
end;
class procedure TCasScriptFloat.HandleSgn(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
TCasScriptInteger(AResult).Value := Sign( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleAbs(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := Abs( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleRandom(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := Random();
end;
class procedure TCasScriptFloat.HandleCeil(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
TCasScriptInteger(AResult).Value := Ceil( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleFloor(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
TCasScriptInteger(AResult).Value := Floor( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleRound(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptInteger);
TCasScriptInteger(AResult).Value := Round( TCasScriptFloat(Arguments[0]).Value );
end;
class procedure TCasScriptFloat.HandleGreater(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptFloat(Arguments[0]).Value >
TCasScriptFloat(Arguments[1]).Value;
end;
class procedure TCasScriptFloat.HandleLesser(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptFloat(Arguments[0]).Value <
TCasScriptFloat(Arguments[1]).Value;
end;
class procedure TCasScriptFloat.HandleGreaterEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptFloat(Arguments[0]).Value >=
TCasScriptFloat(Arguments[1]).Value;
end;
class procedure TCasScriptFloat.HandleLesserEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptFloat(Arguments[0]).Value <=
TCasScriptFloat(Arguments[1]).Value;
end;
class procedure TCasScriptFloat.HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptFloat(Arguments[0]).Value =
TCasScriptFloat(Arguments[1]).Value;
end;
class procedure TCasScriptFloat.HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptFloat(Arguments[0]).Value <>
TCasScriptFloat(Arguments[1]).Value;
end;
class procedure TCasScriptFloat.ConvertFromInt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := TCasScriptInteger(Arguments[0]).Value;
end;
class procedure TCasScriptFloat.ConvertFromFloat(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
if ParentOfResult then
AResult.FreeByParentExpression else
AResult := nil;
AResult := Arguments[0];
Assert(AResult is TCasScriptFloat);
ParentOfResult := false;
end;
class procedure TCasScriptFloat.ConvertFromBool(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
BoolTo01: array [boolean] of Float = (0, 1);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
TCasScriptFloat(AResult).Value := BoolTo01[TCasScriptBoolean(Arguments[0]).Value];
end;
class procedure TCasScriptFloat.ConvertFromString(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptFloat);
try
TCasScriptFloat(AResult).Value := StrToFloat(TCasScriptString(Arguments[0]).Value);
except
on E: EConvertError do
{ Change EConvertError to ECasScriptError }
raise ECasScriptError.CreateFmt('Error when converting string "%s" to float: %s',
[TCasScriptString(Arguments[0]).Value, E.Message]);
end;
end;
procedure TCasScriptFloat.AssignValue(Source: TCasScriptValue);
begin
if Source is TCasScriptFloat then
Value := TCasScriptFloat(Source).Value else
{ This allows for type promotion integer->float at assignment. }
if Source is TCasScriptInteger then
Value := TCasScriptInteger(Source).Value else
raise ECasScriptAssignError.CreateFmt('Assignment from %s to %s not possible', [Source.ClassName, ClassName]);
end;
procedure TCasScriptFloat.SetValue(const AValue: Float);
begin
FValue := AValue;
ValueAssigned := true;
end;
{ TCasScriptBoolean ---------------------------------------------------------- }
constructor TCasScriptBoolean.Create(const AWriteable: boolean; const AValue: boolean);
begin
Create(AWriteable);
Value := AValue;
end;
constructor TCasScriptBoolean.Create(const AWriteable: boolean);
begin
inherited Create(AWriteable);
end;
class procedure TCasScriptBoolean.HandleOr(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value := TCasScriptBoolean(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptBoolean(AResult).Value :=
TCasScriptBoolean(AResult).Value or TCasScriptBoolean(Arguments[I]).Value;
end;
class procedure TCasScriptBoolean.HandleAnd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value := TCasScriptBoolean(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptBoolean(AResult).Value :=
TCasScriptBoolean(AResult).Value and TCasScriptBoolean(Arguments[I]).Value;
end;
class procedure TCasScriptBoolean.HandleNot(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value := not TCasScriptBoolean(Arguments[0]).Value;
end;
class procedure TCasScriptBoolean.HandleGreater(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptBoolean(Arguments[0]).Value >
TCasScriptBoolean(Arguments[1]).Value;
end;
class procedure TCasScriptBoolean.HandleLesser(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptBoolean(Arguments[0]).Value <
TCasScriptBoolean(Arguments[1]).Value;
end;
class procedure TCasScriptBoolean.HandleGreaterEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptBoolean(Arguments[0]).Value >=
TCasScriptBoolean(Arguments[1]).Value;
end;
class procedure TCasScriptBoolean.HandleLesserEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptBoolean(Arguments[0]).Value <=
TCasScriptBoolean(Arguments[1]).Value;
end;
class procedure TCasScriptBoolean.HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptBoolean(Arguments[0]).Value =
TCasScriptBoolean(Arguments[1]).Value;
end;
class procedure TCasScriptBoolean.HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptBoolean(Arguments[0]).Value <>
TCasScriptBoolean(Arguments[1]).Value;
end;
class procedure TCasScriptBoolean.ConvertFromInt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value := TCasScriptInteger(Arguments[0]).Value <> 0;
end;
class procedure TCasScriptBoolean.ConvertFromFloat(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value := TCasScriptFloat(Arguments[0]).Value <> 0;
end;
class procedure TCasScriptBoolean.ConvertFromBool(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
if ParentOfResult then
AResult.FreeByParentExpression else
AResult := nil;
AResult := Arguments[0];
Assert(AResult is TCasScriptBoolean);
ParentOfResult := false;
end;
class procedure TCasScriptBoolean.ConvertFromString(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
S: string;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
S := LowerCase(TCasScriptString(Arguments[0]).Value);
if S = 'false' then
TCasScriptBoolean(AResult).Value := false else
if S = 'true' then
TCasScriptBoolean(AResult).Value := true else
raise ECasScriptError.CreateFmt('Error when converting string "%s" to boolean: invalid value, must be "false" or "true"',
[TCasScriptString(Arguments[0]).Value]);
end;
procedure TCasScriptBoolean.AssignValue(Source: TCasScriptValue);
begin
if Source is TCasScriptBoolean then
Value := TCasScriptBoolean(Source).Value else
raise ECasScriptAssignError.CreateFmt('Assignment from %s to %s not possible', [Source.ClassName, ClassName]);
end;
procedure TCasScriptBoolean.SetValue(const AValue: Boolean);
begin
FValue := AValue;
ValueAssigned := true;
end;
{ TCasScriptString ---------------------------------------------------------- }
constructor TCasScriptString.Create(const AWriteable: boolean; const AValue: string);
begin
Create(AWriteable);
Value := AValue;
end;
constructor TCasScriptString.Create(const AWriteable: boolean);
begin
inherited Create(AWriteable);
end;
class procedure TCasScriptString.HandleAdd(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
I: Integer;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptString);
{ The function allows only >= 1 arguments, and this handler is
registered only for TCasScriptString values, so we can safely take
the first arg as TCasScriptString. }
TCasScriptString(AResult).Value := TCasScriptString(Arguments[0]).Value;
for I := 1 to Length(Arguments) - 1 do
TCasScriptString(AResult).Value :=
TCasScriptString(AResult).Value + TCasScriptString(Arguments[I]).Value;
end;
class procedure TCasScriptString.HandleGreater(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptString(Arguments[0]).Value >
TCasScriptString(Arguments[1]).Value;
end;
class procedure TCasScriptString.HandleLesser(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptString(Arguments[0]).Value <
TCasScriptString(Arguments[1]).Value;
end;
class procedure TCasScriptString.HandleGreaterEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptString(Arguments[0]).Value >=
TCasScriptString(Arguments[1]).Value;
end;
class procedure TCasScriptString.HandleLesserEq(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptString(Arguments[0]).Value <=
TCasScriptString(Arguments[1]).Value;
end;
class procedure TCasScriptString.HandleEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptString(Arguments[0]).Value =
TCasScriptString(Arguments[1]).Value;
end;
class procedure TCasScriptString.HandleNotEqual(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptBoolean);
TCasScriptBoolean(AResult).Value :=
TCasScriptString(Arguments[0]).Value <>
TCasScriptString(Arguments[1]).Value;
end;
class procedure TCasScriptString.ConvertFromInt(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptString);
TCasScriptString(AResult).Value := IntToStr(TCasScriptInteger(Arguments[0]).Value);
end;
class procedure TCasScriptString.ConvertFromFloat(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptString);
TCasScriptString(AResult).Value := FloatToStr(TCasScriptFloat(Arguments[0]).Value);
end;
class procedure TCasScriptString.ConvertFromBool(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
BoolTo01: array [boolean] of string = ('false', 'true');
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptString);
TCasScriptString(AResult).Value := BoolTo01[TCasScriptBoolean(Arguments[0]).Value];
end;
class procedure TCasScriptString.ConvertFromString(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
if ParentOfResult then
AResult.FreeByParentExpression else
AResult := nil;
AResult := Arguments[0];
Assert(AResult is TCasScriptString);
ParentOfResult := false;
end;
class procedure TCasScriptString.HandleWriteln(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
S: string;
begin
if ParentOfResult then
AResult.FreeByParentExpression else
AResult := nil;
AResult := Arguments[0];
ParentOfResult := false;
S := TCasScriptString(Arguments[0]).Value;
if Assigned(OnScriptMessage) then
OnScriptMessage(S) else
OnWarning(wtMinor, 'CastleScript', 'Writeln: '+ S);
end;
class procedure TCasScriptString.HandleCharacterFromCode(AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
CharCode: Int64;
begin
CreateValueIfNeeded(AResult, ParentOfResult, TCasScriptString);
CharCode := TCasScriptInteger(Arguments[0]).Value;
if Between(CharCode, Low(Byte), High(Byte)) then
TCasScriptString(AResult).Value := Chr(CharCode) else
TCasScriptString(AResult).Value := '';
end;
procedure TCasScriptString.AssignValue(Source: TCasScriptValue);
begin
if Source is TCasScriptString then
Value := TCasScriptString(Source).Value else
raise ECasScriptAssignError.CreateFmt('Assignment from %s to %s not possible', [Source.ClassName, ClassName]);
end;
procedure TCasScriptString.SetValue(const AValue: String);
begin
FValue := AValue;
ValueAssigned := true;
end;
{ TCasScriptFunction --------------------------------------------------------- }
constructor TCasScriptFunction.Create(AArgs: TCasScriptExpressionList);
begin
inherited Create;
FArgs := TCasScriptExpressionList.Create(false);
FArgs.AddList(AArgs);
CheckArguments;
end;
constructor TCasScriptFunction.Create(const AArgs: array of TCasScriptExpression);
begin
inherited Create;
FArgs := TCasScriptExpressionList.Create(false);
FArgs.AddArray(AArgs);
CheckArguments;
end;
procedure TCasScriptFunction.CheckArguments;
var
I: Integer;
begin
for I := 0 to Args.Count - 1 do
if ArgumentMustBeAssignable(I) and
not ( (Args[I] is TCasScriptValue) and
TCasScriptValue(Args[I]).Writeable ) then
raise ECasScriptFunctionArgumentsError.CreateFmt('Argument %d of function %s must be a writeable operand (but is not)',
[I, Name]);
if not FunctionHandlers.SearchFunctionClass(
TCasScriptFunctionClass(Self.ClassType), HandlersByArgument) then
raise ECasScriptFunctionNoHandler.CreateFmt('No handler defined for function "%s"', [Name]);
SetLength(ExecuteArguments, Args.Count);
SetLength(ExecuteArgumentClasses, Args.Count);
end;
destructor TCasScriptFunction.Destroy;
begin
if FArgs <> nil then
begin
FArgs.FreeContentsByParentExpression;
FreeAndNil(FArgs);
end;
if ParentOfLastExecuteResult then
LastExecuteResult.FreeByParentExpression;
LastExecuteResult := nil;
inherited;
end;
class function TCasScriptFunction.Name: string;
begin
Result := ShortName;
end;
class function TCasScriptFunction.InfixOperatorName: string;
begin
Result := '';
end;
class function TCasScriptFunction.GreedyArgumentsCalculation: Integer;
begin
Result := -1;
end;
class function TCasScriptFunction.ArgumentMustBeAssignable(const Index: Integer): boolean;
begin
Result := false;
end;
function TCasScriptFunction.CoreExecute: TCasScriptValue;
function ArgumentClassesToStr(const A: TCasScriptValueClassArray): string;
var
I: Integer;
begin
Result := '';
for I := 0 to Length(A) - 1 do
begin
if I > 0 then Result += ', ';
{ A[I] = nil may happen in case of GreedyArgumentsCalculation >= 0,
and it means then that any type of arg will be accepted. }
if A[I] = nil then
Result += 'anything' else
Result += A[I].ClassName;
end;
Result := '(' + Result + ')';
end;
var
Handler: TCasScriptRegisteredHandler;
I, GreedyArguments: Integer;
begin
GreedyArguments := Args.Count;
if GreedyArgumentsCalculation <> -1 then
MinTo1st(GreedyArguments, GreedyArgumentsCalculation);
{ We have to calculate arguments first, to know their type,
to decide which handler is suitable.
Actually, we calculate only first GreedyArguments, rest is left as nil. }
for I := 0 to GreedyArguments - 1 do
begin
ExecuteArguments[I] := Args[I].CoreExecute;
ExecuteArgumentClasses[I] := TCasScriptValueClass(ExecuteArguments[I].ClassType);
end;
for I := GreedyArguments to Args.Count - 1 do
begin
ExecuteArguments[I] := nil;
ExecuteArgumentClasses[I] := nil;
end;
{ calculate Handler }
if not FunctionHandlers.SearchArgumentClasses(
HandlersByArgument, ExecuteArgumentClasses, Handler, Cache1) then
begin
{ try promoting integer arguments to float, see if it will work then }
for I := 0 to Length(ExecuteArgumentClasses) - 1 do
if (ExecuteArgumentClasses[I] <> nil) and
(ExecuteArgumentClasses[I].InheritsFrom(TCasScriptInteger)) then
ExecuteArgumentClasses[I] := TCasScriptFloat;
if FunctionHandlers.SearchArgumentClasses(
HandlersByArgument, ExecuteArgumentClasses, Handler, Cache2) then
begin
{ So I found a handler, that will be valid if all integer args will
get promoted to float. Cool, let's do it.
I use PromoteToFloat method, that will keep it's result valid
for some time, since (depending on function handler) we may
return PromoteToFloat result to the user. }
for I := 0 to Length(ExecuteArguments) - 1 do
if (ExecuteArguments[I] <> nil) and
(ExecuteArguments[I] is TCasScriptInteger) then
ExecuteArguments[I] := TCasScriptInteger(ExecuteArguments[I]).PromoteToFloat;
end else
raise ECasScriptFunctionNoHandler.CreateFmt('Function "%s" is not defined for this combination of arguments: %s',
[Name, ArgumentClassesToStr(ExecuteArgumentClasses)]);
end;
Handler.Handler(Self, ExecuteArguments, LastExecuteResult, ParentOfLastExecuteResult);
Result := LastExecuteResult;
end;
{ TCasScriptRegisteredHandler ------------------------------------------------ }
constructor TCasScriptRegisteredHandler.Create(
AHandler: TCasScriptFunctionHandler;
AFunctionClass: TCasScriptFunctionClass;
const AArgumentClasses: TCasScriptValueClassArray;
const AVariableArgumentsCount: boolean);
begin
FHandler := AHandler;
FFunctionClass := AFunctionClass;
FArgumentClasses := AArgumentClasses;
FVariableArgumentsCount := AVariableArgumentsCount;
end;
{ TCasScriptSequence --------------------------------------------------------- }
class function TCasScriptSequence.Name: string;
begin
Result := 'sequence (;)';
end;
class function TCasScriptSequence.ShortName: string;
begin
Result := '';
end;
class function TCasScriptSequence.InfixOperatorName: string;
begin
Result := ';';
end;
class procedure TCasScriptSequence.HandleSequence(
AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
if ParentOfResult then
AResult.FreeByParentExpression else
AResult := nil;
AResult := Arguments[High(Arguments)];
ParentOfResult := false;
end;
{ TCasScriptAssignment --------------------------------------------------------- }
class function TCasScriptAssignment.Name: string;
begin
Result := 'assignment (:=)';
end;
class function TCasScriptAssignment.ShortName: string;
begin
Result := '';
end;
class function TCasScriptAssignment.InfixOperatorName: string;
begin
Result := ':=';
end;
class procedure TCasScriptAssignment.HandleAssignment(
AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
if ParentOfResult then
AResult.FreeByParentExpression else
AResult := nil;
(Arguments[0] as TCasScriptValue).AssignValue(Arguments[1]);
AResult := Arguments[0] as TCasScriptValue;
ParentOfResult := false;
end;
class function TCasScriptAssignment.ArgumentMustBeAssignable(const Index: Integer): boolean;
begin
Result := Index = 0;
end;
{ TCasScriptIf --------------------------------------------------------- }
class function TCasScriptIf.ShortName: string;
begin
Result := 'if';
end;
class procedure TCasScriptIf.HandleIf(
AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
if ParentOfResult then
AResult.FreeByParentExpression;
AResult := nil;
ParentOfResult := false;
if TCasScriptBoolean(Arguments[0]).Value then
AResult := AFunction.Args[1].CoreExecute else
AResult := AFunction.Args[2].CoreExecute;
end;
class function TCasScriptIf.GreedyArgumentsCalculation: Integer;
begin
Result := 1;
end;
{ TCasScriptWhen --------------------------------------------------------- }
class function TCasScriptWhen.ShortName: string;
begin
Result := 'when';
end;
class procedure TCasScriptWhen.HandleWhen(
AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
begin
if ParentOfResult then
AResult.FreeByParentExpression;
AResult := nil;
ParentOfResult := false;
if TCasScriptBoolean(Arguments[0]).Value then
AResult := AFunction.Args[1].CoreExecute else
begin
{ "when" returns simple const false on "else" condition }
AResult := TCasScriptBoolean.Create(false);
ParentOfResult := true;
end;
end;
class function TCasScriptWhen.GreedyArgumentsCalculation: Integer;
begin
Result := 1;
end;
{ TCasScriptWhile --------------------------------------------------------- }
class function TCasScriptWhile.ShortName: string;
begin
Result := 'while';
end;
class procedure TCasScriptWhile.HandleWhile(
AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
function ExecuteCondition: boolean;
var
Condition: TCasScriptValue;
begin
Condition := AFunction.Args[0].CoreExecute;
if Condition is TCasScriptBoolean then
Result := TCasScriptBoolean(Condition).Value else
raise ECasScriptError.Create('"if" function "condition" must return a boolean value');
end;
begin
if ParentOfResult then
AResult.FreeByParentExpression;
AResult := nil;
ParentOfResult := false;
while ExecuteCondition do
AResult := AFunction.Args[1].CoreExecute;
if AResult = nil then
begin
{ not executed even once? return const false }
AResult := TCasScriptBoolean.Create(false);
ParentOfResult := true;
end;
end;
class function TCasScriptWhile.GreedyArgumentsCalculation: Integer;
begin
Result := 0;
end;
{ TCasScriptFor --------------------------------------------------------- }
class function TCasScriptFor.ShortName: string;
begin
Result := 'for';
end;
class procedure TCasScriptFor.HandleFor(
AFunction: TCasScriptFunction; const Arguments: array of TCasScriptValue; var AResult: TCasScriptValue; var ParentOfResult: boolean);
var
BeginVal, EndVal: Int64;
I: Integer;
begin
if ParentOfResult then
AResult.FreeByParentExpression;
AResult := nil;
ParentOfResult := false;
BeginVal := TCasScriptInteger(Arguments[1]).Value;
EndVal := TCasScriptInteger(Arguments[2]).Value;
for I := BeginVal to EndVal do
begin
{ We use Arguments[0] here, not AFunction.Args[0], this way
we know we really have TCasScriptInteger here. While CheckArguments
makes sure Args[0] is TCasScriptValue, it may be TCasScriptParameterValue.
We know that TCasScriptParameterValue.Execute returns actual value,
so Arguments[0] is Ok here. }
(Arguments[0] as TCasScriptInteger).Value := I;
AResult := AFunction.Args[3].CoreExecute;
end;
if AResult = nil then
begin
{ not executed even once? return const false }
AResult := TCasScriptBoolean.Create(false);
ParentOfResult := true;
end;
end;
class function TCasScriptFor.GreedyArgumentsCalculation: Integer;
begin
Result := 3;
end;
class function TCasScriptFor.ArgumentMustBeAssignable(const Index: Integer): boolean;
begin
{ This will cause checking whether Args[0] is assignable TCasScriptValue.
Note that I cannot check in CheckArguments whether is
Args[0] is TCasScriptInteger,
as it may be TCasScriptParameterValue, and so the actual runtime type
(TCasScriptParameterValue.SourceValue) may be not set yet.
That's Ok, in HandleFor this will be automatically checked by AssignValue. }
Result := Index = 0;
end;
{ TCasScriptFunctionHandlers ------------------------------------------------- }
constructor TCasScriptFunctionHandlers.Create;
begin
inherited;
FHandlersByFunction := TObjectList.Create(true);
end;
destructor TCasScriptFunctionHandlers.Destroy;
begin
FreeAndNil(FHandlersByFunction);
inherited;
end;
function TCasScriptFunctionHandlers.SearchFunctionClass(
FunctionClass: TCasScriptFunctionClass;
out FunctionIndex: Integer;
out HandlersByArgument: TObjectList): boolean;
var
I: Integer;
begin
for I := 0 to FHandlersByFunction.Count - 1 do
begin
HandlersByArgument := FHandlersByFunction[I] as TObjectList;
if FunctionClass = (HandlersByArgument[0] as
TCasScriptRegisteredHandler).FunctionClass then
begin
FunctionIndex := I;
Result := true;
Exit;
end;
end;
Result := false;
end;
function TCasScriptFunctionHandlers.SearchFunctionClass(
FunctionClass: TCasScriptFunctionClass;
out HandlersByArgument: TObjectList): boolean;
var
FunctionIndex: Integer;
begin
Result := SearchFunctionClass(
FunctionClass, FunctionIndex, HandlersByArgument);
end;
function TCasScriptFunctionHandlers.SearchArgumentClasses(
HandlersByArgument: TObjectList;
const ArgumentClasses: TCasScriptValueClassArray;
out ArgumentIndex: Integer;
out Handler: TCasScriptRegisteredHandler): boolean;
var
I, J: Integer;
begin
for I := 0 to HandlersByArgument.Count - 1 do
begin
Handler := HandlersByArgument[I] as TCasScriptRegisteredHandler;
{ First, check do we have enough arguments: at least
Length(Handler.ArgumentClasses) are always required. }
Result := Length(ArgumentClasses) >= Length(Handler.ArgumentClasses);
if Result then
begin
for J := 0 to Length(ArgumentClasses) - 1 do
begin
Assert(Result);
{ Always accept ArgumentClasses[J] = nil, this means that they
are "lazy" arguments (not calculated before handler actually executed),
so they are simply always assumed Ok. }
if ArgumentClasses[J] <> nil then
begin
if J < Length(Handler.ArgumentClasses) then
Result := ArgumentClasses[J].InheritsFrom(Handler.ArgumentClasses[J]) else
{ This is more than required number of arguments.
Still it's Ok if it matches last argument and function allows variable
number of arguments. }
Result := Handler.VariableArgumentsCount and
(Length(Handler.ArgumentClasses) > 0) and
ArgumentClasses[J].InheritsFrom(
Handler.ArgumentClasses[High(Handler.ArgumentClasses)]);
end;
if not Result then Break;
end;
if Result then
begin
ArgumentIndex := I;
Exit;
end;
end;
end;
Result := false;
end;
function TCasScriptFunctionHandlers.SearchArgumentClasses(
HandlersByArgument: TObjectList;
const ArgumentClasses: TCasScriptValueClassArray;
out Handler: TCasScriptRegisteredHandler): boolean;
var
ArgumentIndex: Integer;
begin
Result := SearchArgumentClasses(
HandlersByArgument, ArgumentClasses, ArgumentIndex, Handler);
end;
function TCasScriptFunctionHandlers.SearchArgumentClasses(
HandlersByArgument: TObjectList;
const ArgumentClasses: TCasScriptValueClassArray;
out Handler: TCasScriptRegisteredHandler;
var Cache: TCasScriptSearchArgumentClassesCache): boolean;
function ArgumentClassesEqual(const A1, A2: TCasScriptValueClassArray): boolean;
begin
Result := (Length(A1) = Length(A2)) and
CompareMem(Pointer(A1), Pointer(A2), SizeOf(TCasScriptValueClass) * Length(A1));
end;
begin
if Cache.IsCache and
(Cache.QueryHandlersByArgument = HandlersByArgument) and
ArgumentClassesEqual(Cache.QueryArgumentClasses, ArgumentClasses) then
begin
{ Use the cached result }
Handler := Cache.AnswerHandler;
{ ArgumentIndex := Cache.ArgumentIndex; not returned here }
Result := Cache.Answer;
end else
begin
{ Result not in the cache. So calculate it, and record in the cache. }
Cache.IsCache := true;
Cache.QueryHandlersByArgument := HandlersByArgument;
{ Copying the reference here, by
Cache.QueryArgumentClasses := ArgumentClasses;
would be incorrect: our argument may be a long-lived instance in
TCasScriptFunction.ExecuteArgumentClasses, that is changed in
TCasScriptFunction.CoreExecute. With a reference copy here,
the CoreExecute would accidentaly change also our cache state,
which will cause trouble later.
Testcase: demo_models/castle_script/edit_texture.x3dv,
key "e", would cause errors, because suddenly CoreExecute may decide
that TCasScriptFloat.HandleAdd may be called without promoting int to float. }
Cache.QueryArgumentClasses := Copy(ArgumentClasses, 0, Length(ArgumentClasses));
Cache.Answer := SearchArgumentClasses(
HandlersByArgument, ArgumentClasses,
Cache.AnswerArgumentIndex, Cache.AnswerHandler);
{ Use the cached result }
Handler := Cache.AnswerHandler;
{ ArgumentIndex := Cache.ArgumentIndex; not returned here }
Result := Cache.Answer;
end;
end;
procedure TCasScriptFunctionHandlers.RegisterHandler(
AHandler: TCasScriptFunctionHandler;
AFunctionClass: TCasScriptFunctionClass;
const AArgumentClasses: array of TCasScriptValueClass;
const AVariableArgumentsCount: boolean);
var
HandlersByArgument: TObjectList;
Handler: TCasScriptRegisteredHandler;
ArgumentClassesDyn: TCasScriptValueClassArray;
begin
SetLength(ArgumentClassesDyn, High(AArgumentClasses) + 1);
if Length(ArgumentClassesDyn) > 0 then
Move(AArgumentClasses[0], ArgumentClassesDyn[0],
SizeOf(TCasScriptValueClass) * Length(ArgumentClassesDyn));
if SearchFunctionClass(AFunctionClass, HandlersByArgument) then
begin
if not SearchArgumentClasses(HandlersByArgument, ArgumentClassesDyn, Handler) then
begin
Handler := TCasScriptRegisteredHandler.Create(
AHandler, AFunctionClass, ArgumentClassesDyn, AVariableArgumentsCount);
HandlersByArgument.Add(Handler);
end;
end else
begin
HandlersByArgument := TObjectList.Create(true);
FHandlersByFunction.Add(HandlersByArgument);
Handler := TCasScriptRegisteredHandler.Create(
AHandler, AFunctionClass, ArgumentClassesDyn, AVariableArgumentsCount);
HandlersByArgument.Add(Handler);
end;
end;
function TCasScriptFunctionHandlers.SearchFunctionShortName(
const AShortName: string): TCasScriptFunctionClass;
var
I: Integer;
HandlersByArgument: TObjectList;
begin
for I := 0 to FHandlersByFunction.Count - 1 do
begin
HandlersByArgument := FHandlersByFunction[I] as TObjectList;
Result := (HandlersByArgument[0] as
TCasScriptRegisteredHandler).FunctionClass;
if SameText(AShortName, Result.ShortName) then
Exit;
end;
Result := nil;
end;
{ TCasScriptUserFunction ----------------------------------------------- }
constructor TCasScriptUserFunction.Create;
begin
inherited;
FParameters := TCasScriptValueList.Create(true);
end;
destructor TCasScriptUserFunction.Destroy;
begin
if Body <> nil then
Body.FreeByParentExpression;
FreeAndNil(FParameters);
inherited;
end;
{ TCasScriptUserFunctionList ------------------------------------------ }
function TCasScriptUserFunctionList.IndexOf(
const FunctionName: string): Integer;
begin
for Result := 0 to Count - 1 do
if SameText(FunctionName, Items[Result].Name) then
Exit;
Result := -1;
end;
{ TCasScriptProgram ---------------------------------------------------------- }
constructor TCasScriptProgram.Create;
begin
inherited;
FFunctions := TCasScriptUserFunctionList.Create(true);
FEnvironment := TCasScriptEnvironment.Create;
end;
destructor TCasScriptProgram.Destroy;
begin
FreeAndNil(FFunctions);
FreeAndNil(FEnvironment);
inherited;
end;
procedure TCasScriptProgram.ExecuteFunction(const FunctionName: string;
const Parameters: array of TCasScriptValue;
const IgnoreMissingFunction: boolean);
var
Func: TCasScriptUserFunction;
FuncIndex, I: Integer;
begin
FuncIndex := Functions.IndexOf(FunctionName);
if FuncIndex = -1 then
begin
if IgnoreMissingFunction then
Exit else
raise ECasScriptMissingFunction.CreateFmt('CastleScript function "%s" is not defined', [FunctionName]);
end;
Func := Functions[FuncIndex];
if High(Parameters) <> Func.Parameters.Count - 1 then
raise ECasScriptError.CreateFmt('CastleScript function "%s" requires %d parameters, but passed %d parameters',
[FunctionName, Func.Parameters.Count, High(Parameters) + 1]);
for I := 0 to High(Parameters) do
(Func.Parameters[I] as TCasScriptParameterValue).SourceValue := Parameters[I];
Func.Body.Execute;
{ Just for safety, clear SourceValue references.
This is safe, since no code can access Parameters instances
(trying to get their value directly or by AssignValue) after
ExecuteFunction. Code from the outside can only access it's own
global variables after execution, which have values directly stored.
This *could* become a problem if we want to return function's value
in the future, then this will possibly have to be removed,
as Func.Body.Execute may directly return one of our Parameters. }
for I := 0 to High(Parameters) do
(Func.Parameters[I] as TCasScriptParameterValue).SourceValue := nil;
end;
{ procedural utils ----------------------------------------------------------- }
procedure CreateValueIfNeeded(var Value: TCasScriptValue;
var ParentOfValue: boolean;
NeededClass: TCasScriptValueClass);
begin
if Value = nil then
begin
Value := NeededClass.Create(false);
ParentOfValue := true;
end else
if Value.ClassType <> NeededClass then
begin
if ParentOfValue then
Value.FreeByParentExpression else
Value := nil;
Value := NeededClass.Create(false);
ParentOfValue := true;
end;
end;
{ unit init/fini ------------------------------------------------------------- }
initialization
{$ifdef WORKAROUND_EXCEPTIONS_FOR_SCRIPT_EXPRESSIONS}
{$if defined(cpui386) or defined(cpux86_64)}
SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide,exOverflow, exUnderflow, exPrecision]);
{$endif}
{$endif}
FunctionHandlers := TCasScriptFunctionHandlers.Create;
FunctionHandlers.RegisterHandler(@TCasScriptSequence(nil).HandleSequence, TCasScriptSequence, [TCasScriptValue], true);
FunctionHandlers.RegisterHandler(@TCasScriptAssignment(nil).HandleAssignment, TCasScriptAssignment, [TCasScriptValue, TCasScriptValue], false);
FunctionHandlers.RegisterHandler(@TCasScriptIf(nil).HandleIf, TCasScriptIf, [TCasScriptBoolean, TCasScriptValue, TCasScriptValue], false);
FunctionHandlers.RegisterHandler(@TCasScriptWhen(nil).HandleWhen, TCasScriptWhen, [TCasScriptBoolean, TCasScriptValue], false);
FunctionHandlers.RegisterHandler(@TCasScriptWhile(nil).HandleWhile, TCasScriptWhile, [TCasScriptBoolean, TCasScriptValue], false);
FunctionHandlers.RegisterHandler(@TCasScriptFor(nil).HandleFor, TCasScriptFor, [TCasScriptInteger, TCasScriptInteger, TCasScriptInteger, TCasScriptValue], false);
{ Register handlers for TCasScriptInteger for functions in
CastleScriptCoreFunctions. }
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleAdd, TCasScriptAdd, [TCasScriptInteger], true);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleSubtract, TCasScriptSubtract, [TCasScriptInteger], true);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleNegate, TCasScriptNegate, [TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleMultiply, TCasScriptMultiply, [TCasScriptInteger], true);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleDivide, TCasScriptDivide, [TCasScriptInteger], true);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleModulo, TCasScriptModulo, [TCasScriptInteger, TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandlePower, TCasScriptPower, [TCasScriptInteger, TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleMax, TCasScriptMax, [TCasScriptInteger], true);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleMin, TCasScriptMin, [TCasScriptInteger], true);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleSqr, TCasScriptSqr, [TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleSgn, TCasScriptSgn, [TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleAbs, TCasScriptAbs, [TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleRandom, TCasScriptRandom, [TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleGreater, TCasScriptGreater, [TCasScriptInteger, TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleLesser, TCasScriptLesser, [TCasScriptInteger, TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleGreaterEq, TCasScriptGreaterEq, [TCasScriptInteger, TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleLesserEq, TCasScriptLesserEq, [TCasScriptInteger, TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleEqual, TCasScriptEqual, [TCasScriptInteger, TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).HandleNotEqual, TCasScriptNotEqual, [TCasScriptInteger, TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).ConvertFromInt , TCasScriptInt, [TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).ConvertFromFloat , TCasScriptInt, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).ConvertFromBool , TCasScriptInt, [TCasScriptBoolean], false);
FunctionHandlers.RegisterHandler(@TCasScriptInteger(nil).ConvertFromString, TCasScriptInt, [TCasScriptString], false);
{ Register handlers for TCasScriptFloat for functions in
CastleScriptCoreFunctions. }
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleAdd, TCasScriptAdd, [TCasScriptFloat], true);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleSubtract, TCasScriptSubtract, [TCasScriptFloat], true);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleMultiply, TCasScriptMultiply, [TCasScriptFloat], true);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleDivide, TCasScriptDivide, [TCasScriptFloat], true);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleNegate, TCasScriptNegate, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleModulo, TCasScriptModulo, [TCasScriptFloat, TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleSin, TCasScriptSin, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleCos, TCasScriptCos, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleTan, TCasScriptTan, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleCotan, TCasScriptCotan, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleArcSin, TCasScriptArcSin, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleArcCos, TCasScriptArcCos, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleArcTan, TCasScriptArcTan, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleArcCotan, TCasScriptArcCotan, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleSinh, TCasScriptSinh, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleCosh, TCasScriptCosh, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleTanh, TCasScriptTanh, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleCotanh, TCasScriptCotanh, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleLog2, TCasScriptLog2, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleLn, TCasScriptLn, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleLog, TCasScriptLog, [TCasScriptFloat, TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandlePower2, TCasScriptPower2, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleExp, TCasScriptExp, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandlePower, TCasScriptPower, [TCasScriptFloat, TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleSqr, TCasScriptSqr, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleSqrt, TCasScriptSqrt, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleMax, TCasScriptMax, [TCasScriptFloat], true);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleMin, TCasScriptMin, [TCasScriptFloat], true);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleSgn, TCasScriptSgn, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleAbs, TCasScriptAbs, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleRandom, TCasScriptRandom, [], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleCeil, TCasScriptCeil, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleFloor, TCasScriptFloor, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleRound, TCasScriptRound, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleGreater, TCasScriptGreater, [TCasScriptFloat, TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleLesser, TCasScriptLesser, [TCasScriptFloat, TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleGreaterEq, TCasScriptGreaterEq, [TCasScriptFloat, TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleLesserEq, TCasScriptLesserEq, [TCasScriptFloat, TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleEqual, TCasScriptEqual, [TCasScriptFloat, TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).HandleNotEqual, TCasScriptNotEqual, [TCasScriptFloat, TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).ConvertFromInt , TCasScriptFloatFun, [TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).ConvertFromFloat , TCasScriptFloatFun, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).ConvertFromBool , TCasScriptFloatFun, [TCasScriptBoolean], false);
FunctionHandlers.RegisterHandler(@TCasScriptFloat(nil).ConvertFromString, TCasScriptFloatFun, [TCasScriptString], false);
{ Register handlers for TCasScriptBoolean for functions in
CastleScriptCoreFunctions. }
FunctionHandlers.RegisterHandler(@TCasScriptBoolean(nil).HandleOr, TCasScriptOr, [TCasScriptBoolean], true);
FunctionHandlers.RegisterHandler(@TCasScriptBoolean(nil).HandleAnd, TCasScriptAnd, [TCasScriptBoolean], true);
FunctionHandlers.RegisterHandler(@TCasScriptBoolean(nil).HandleNot, TCasScriptNot, [TCasScriptBoolean], false);
FunctionHandlers.RegisterHandler(@TCasScriptBoolean(nil).HandleGreater, TCasScriptGreater, [TCasScriptBoolean, TCasScriptBoolean], false);
FunctionHandlers.RegisterHandler(@TCasScriptBoolean(nil).HandleLesser, TCasScriptLesser, [TCasScriptBoolean, TCasScriptBoolean], false);
FunctionHandlers.RegisterHandler(@TCasScriptBoolean(nil).HandleGreaterEq, TCasScriptGreaterEq, [TCasScriptBoolean, TCasScriptBoolean], false);
FunctionHandlers.RegisterHandler(@TCasScriptBoolean(nil).HandleLesserEq, TCasScriptLesserEq, [TCasScriptBoolean, TCasScriptBoolean], false);
FunctionHandlers.RegisterHandler(@TCasScriptBoolean(nil).HandleEqual, TCasScriptEqual, [TCasScriptBoolean, TCasScriptBoolean], false);
FunctionHandlers.RegisterHandler(@TCasScriptBoolean(nil).HandleNotEqual, TCasScriptNotEqual, [TCasScriptBoolean, TCasScriptBoolean], false);
FunctionHandlers.RegisterHandler(@TCasScriptBoolean(nil).ConvertFromInt , TCasScriptBool, [TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptBoolean(nil).ConvertFromFloat , TCasScriptBool, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptBoolean(nil).ConvertFromBool , TCasScriptBool, [TCasScriptBoolean], false);
FunctionHandlers.RegisterHandler(@TCasScriptBoolean(nil).ConvertFromString, TCasScriptBool, [TCasScriptString], false);
{ Register handlers for TCasScriptString for functions in
CastleScriptCoreFunctions. }
FunctionHandlers.RegisterHandler(@TCasScriptString(nil).HandleAdd, TCasScriptAdd, [TCasScriptString, TCasScriptString], false);
FunctionHandlers.RegisterHandler(@TCasScriptString(nil).HandleGreater, TCasScriptGreater, [TCasScriptString, TCasScriptString], false);
FunctionHandlers.RegisterHandler(@TCasScriptString(nil).HandleLesser, TCasScriptLesser, [TCasScriptString, TCasScriptString], false);
FunctionHandlers.RegisterHandler(@TCasScriptString(nil).HandleGreaterEq, TCasScriptGreaterEq, [TCasScriptString, TCasScriptString], false);
FunctionHandlers.RegisterHandler(@TCasScriptString(nil).HandleLesserEq, TCasScriptLesserEq, [TCasScriptString, TCasScriptString], false);
FunctionHandlers.RegisterHandler(@TCasScriptString(nil).HandleEqual, TCasScriptEqual, [TCasScriptString, TCasScriptString], false);
FunctionHandlers.RegisterHandler(@TCasScriptString(nil).HandleNotEqual, TCasScriptNotEqual, [TCasScriptString, TCasScriptString], false);
FunctionHandlers.RegisterHandler(@TCasScriptString(nil).ConvertFromInt , TCasScriptStringFun, [TCasScriptInteger], false);
FunctionHandlers.RegisterHandler(@TCasScriptString(nil).ConvertFromFloat , TCasScriptStringFun, [TCasScriptFloat], false);
FunctionHandlers.RegisterHandler(@TCasScriptString(nil).ConvertFromBool , TCasScriptStringFun, [TCasScriptBoolean], false);
FunctionHandlers.RegisterHandler(@TCasScriptString(nil).ConvertFromString, TCasScriptStringFun, [TCasScriptString], false);
FunctionHandlers.RegisterHandler(@TCasScriptString(nil).HandleWriteln, TCasScriptWriteln, [TCasScriptString], false);
FunctionHandlers.RegisterHandler(@TCasScriptString(nil).HandleCharacterFromCode, TCasScriptCharacterFromCode, [TCasScriptInteger], false);
finalization
FreeAndNil(FunctionHandlers);
end.
|