1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233
|
/*
===========================================================================
Return to Castle Wolfenstein multiplayer GPL Source Code
Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company.
This file is part of the Return to Castle Wolfenstein multiplayer GPL Source Code (“RTCW MP Source Code”).
RTCW MP Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RTCW MP Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RTCW MP Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the RTCW MP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW MP Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
/*****************************************************************************
* name: l_precomp.c
*
* desc: pre compiler
*
*
*****************************************************************************/
//Notes: fix: PC_StringizeTokens
//#define SCREWUP
//#define BOTLIB
//#define QUAKE
//#define QUAKEC
//#define MEQCC
#ifdef SCREWUP
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include "l_memory.h"
#include "l_script.h"
#include "l_precomp.h"
typedef enum {qfalse, qtrue} qboolean;
#endif //SCREWUP
#ifdef BOTLIB
#include "../qcommon/q_shared.h"
#include "botlib.h"
#include "be_interface.h"
#include "l_memory.h"
#include "l_script.h"
#include "l_precomp.h"
#include "l_log.h"
#endif //BOTLIB
#ifdef MEQCC
#include "qcc.h"
#include "time.h" //time & ctime
#include "math.h" //fabs
#include "l_memory.h"
#include "l_script.h"
#include "l_precomp.h"
#include "l_log.h"
#define qtrue true
#define qfalse false
#endif //MEQCC
#ifdef BSPC
//include files for usage in the BSP Converter
#include "../bspc/qbsp.h"
#include "../bspc/l_log.h"
#include "../bspc/l_mem.h"
#include "l_precomp.h"
#define qtrue true
#define qfalse false
#define Q_stricmp stricmp
#define MAX_TOKENLENGTH 1024
typedef struct pc_token_s
{
int type;
int subtype;
int intvalue;
float floatvalue;
char string[MAX_TOKENLENGTH];
} pc_token_t;
#endif //BSPC
#if defined( QUAKE ) && !defined( BSPC )
#include "l_utils.h"
#endif //QUAKE
//#define DEBUG_EVAL
#define MAX_DEFINEPARMS 128
#define DEFINEHASHING 1
//directive name with parse function
typedef struct directive_s
{
char *name;
int ( *func )( source_t *source );
} directive_t;
#define DEFINEHASHSIZE 1024
#define TOKEN_HEAP_SIZE 4096
int numtokens;
/*
int tokenheapinitialized; //true when the token heap is initialized
token_t token_heap[TOKEN_HEAP_SIZE]; //heap with tokens
token_t *freetokens; //free tokens from the heap
*/
//list with global defines added to every source loaded
define_t *globaldefines;
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void QDECL SourceError( source_t *source, char *str, ... ) {
char text[1024];
va_list ap;
va_start( ap, str );
Q_vsnprintf(text, sizeof(text), str, ap);
va_end( ap );
#ifdef BOTLIB
botimport.Print( PRT_ERROR, "file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text );
#endif //BOTLIB
#ifdef MEQCC
printf( "error: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text );
#endif //MEQCC
#ifdef BSPC
Log_Print( "error: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text );
#endif //BSPC
} //end of the function SourceError
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void QDECL SourceWarning( source_t *source, char *str, ... ) {
char text[1024];
va_list ap;
va_start( ap, str );
Q_vsnprintf(text, sizeof(text), str, ap);
va_end( ap );
#ifdef BOTLIB
botimport.Print( PRT_WARNING, "file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text );
#endif //BOTLIB
#ifdef MEQCC
printf( "warning: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text );
#endif //MEQCC
#ifdef BSPC
Log_Print( "warning: file %s, line %d: %s\n", source->scriptstack->filename, source->scriptstack->line, text );
#endif //BSPC
} //end of the function ScriptWarning
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_PushIndent( source_t *source, int type, int skip ) {
indent_t *indent;
indent = (indent_t *) GetMemory( sizeof( indent_t ) );
indent->type = type;
indent->script = source->scriptstack;
indent->skip = ( skip != 0 );
source->skip += indent->skip;
indent->next = source->indentstack;
source->indentstack = indent;
} //end of the function PC_PushIndent
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_PopIndent( source_t *source, int *type, int *skip ) {
indent_t *indent;
*type = 0;
*skip = 0;
indent = source->indentstack;
if ( !indent ) {
return;
}
//must be an indent from the current script
if ( source->indentstack->script != source->scriptstack ) {
return;
}
*type = indent->type;
*skip = indent->skip;
source->indentstack = source->indentstack->next;
source->skip -= indent->skip;
FreeMemory( indent );
} //end of the function PC_PopIndent
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_PushScript( source_t *source, script_t *script ) {
script_t *s;
for ( s = source->scriptstack; s; s = s->next )
{
if ( !Q_stricmp( s->filename, script->filename ) ) {
SourceError( source, "%s recursively included", script->filename );
return;
} //end if
} //end for
//push the script on the script stack
script->next = source->scriptstack;
source->scriptstack = script;
} //end of the function PC_PushScript
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_InitTokenHeap( void ) {
/*
int i;
if (tokenheapinitialized) return;
freetokens = NULL;
for (i = 0; i < TOKEN_HEAP_SIZE; i++)
{
token_heap[i].next = freetokens;
freetokens = &token_heap[i];
} //end for
tokenheapinitialized = qtrue;
*/
} //end of the function PC_InitTokenHeap
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
token_t *PC_CopyToken( token_t *token ) {
token_t *t;
// t = (token_t *) malloc(sizeof(token_t));
t = (token_t *) GetMemory( sizeof( token_t ) );
// t = freetokens;
if ( !t ) {
#ifdef BSPC
Error( "out of token space" );
#else
Com_Error( ERR_FATAL, "out of token space" );
#endif
return NULL;
} //end if
// freetokens = freetokens->next;
memcpy( t, token, sizeof( token_t ) );
t->next = NULL;
numtokens++;
return t;
} //end of the function PC_CopyToken
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_FreeToken( token_t *token ) {
//free(token);
FreeMemory( token );
// token->next = freetokens;
// freetokens = token;
numtokens--;
} //end of the function PC_FreeToken
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_ReadSourceToken( source_t *source, token_t *token ) {
token_t *t;
script_t *script;
int type, skip;
//if there's no token already available
while ( !source->tokens )
{
//if there's a token to read from the script
if ( PS_ReadToken( source->scriptstack, token ) ) {
return qtrue;
}
//if at the end of the script
if ( EndOfScript( source->scriptstack ) ) {
//remove all indents of the script
while ( source->indentstack &&
source->indentstack->script == source->scriptstack )
{
SourceWarning( source, "missing #endif" );
PC_PopIndent( source, &type, &skip );
} //end if
} //end if
//if this was the initial script
if ( !source->scriptstack->next ) {
return qfalse;
}
//remove the script and return to the last one
script = source->scriptstack;
source->scriptstack = source->scriptstack->next;
FreeScript( script );
} //end while
//copy the already available token
memcpy( token, source->tokens, sizeof( token_t ) );
//free the read token
t = source->tokens;
source->tokens = source->tokens->next;
PC_FreeToken( t );
return qtrue;
} //end of the function PC_ReadSourceToken
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_UnreadSourceToken( source_t *source, token_t *token ) {
token_t *t;
t = PC_CopyToken( token );
t->next = source->tokens;
source->tokens = t;
return qtrue;
} //end of the function PC_UnreadSourceToken
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_ReadDefineParms( source_t *source, define_t *define, token_t **parms, int maxparms ) {
token_t token, *t, *last;
int i, done, lastcomma, numparms, indent;
if ( !PC_ReadSourceToken( source, &token ) ) {
SourceError( source, "define %s missing parms", define->name );
return qfalse;
} //end if
//
if ( define->numparms > maxparms ) {
SourceError( source, "define with more than %d parameters", maxparms );
return qfalse;
} //end if
//
for ( i = 0; i < define->numparms; i++ ) parms[i] = NULL;
//if no leading "("
if ( strcmp( token.string, "(" ) ) {
PC_UnreadSourceToken( source, &token );
SourceError( source, "define %s missing parms", define->name );
return qfalse;
} //end if
//read the define parameters
for ( done = 0, numparms = 0, indent = 0; !done; )
{
if ( numparms >= maxparms ) {
SourceError( source, "define %s with too many parms", define->name );
return qfalse;
} //end if
if ( numparms >= define->numparms ) {
SourceWarning( source, "define %s has too many parms", define->name );
return qfalse;
} //end if
parms[numparms] = NULL;
lastcomma = 1;
last = NULL;
while ( !done )
{
//
if ( !PC_ReadSourceToken( source, &token ) ) {
SourceError( source, "define %s incomplete", define->name );
return qfalse;
} //end if
//
if ( !strcmp( token.string, "," ) ) {
if ( indent <= 0 ) {
if ( lastcomma ) {
SourceWarning( source, "too many comma's" );
}
break;
} //end if
} //end if
lastcomma = 0;
//
if ( !strcmp( token.string, "(" ) ) {
indent++;
continue;
} //end if
else if ( !strcmp( token.string, ")" ) ) {
if ( --indent <= 0 ) {
if ( !parms[define->numparms - 1] ) {
SourceWarning( source, "too few define parms" );
} //end if
done = 1;
break;
} //end if
} //end if
//
if ( numparms < define->numparms ) {
//
t = PC_CopyToken( &token );
t->next = NULL;
if ( last ) {
last->next = t;
} else { parms[numparms] = t;}
last = t;
} //end if
} //end while
numparms++;
} //end for
return qtrue;
} //end of the function PC_ReadDefineParms
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_StringizeTokens( token_t *tokens, token_t *token ) {
token_t *t;
token->type = TT_STRING;
token->whitespace_p = NULL;
token->endwhitespace_p = NULL;
token->string[0] = '\0';
strcat( token->string, "\"" );
for ( t = tokens; t; t = t->next )
{
strncat( token->string, t->string, MAX_TOKEN - strlen( token->string ) - 1 );
} //end for
strncat( token->string, "\"", MAX_TOKEN - strlen( token->string ) - 1 );
return qtrue;
} //end of the function PC_StringizeTokens
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_MergeTokens( token_t *t1, token_t *t2 ) {
//merging of a name with a name or number
if ( t1->type == TT_NAME && ( t2->type == TT_NAME || t2->type == TT_NUMBER ) ) {
strcat( t1->string, t2->string );
return qtrue;
} //end if
//merging of two strings
if ( t1->type == TT_STRING && t2->type == TT_STRING ) {
//remove trailing double quote
t1->string[strlen( t1->string ) - 1] = '\0';
//concat without leading double quote
strcat( t1->string, &t2->string[1] );
return qtrue;
} //end if
//FIXME: merging of two number of the same sub type
return qfalse;
} //end of the function PC_MergeTokens
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
/*
void PC_PrintDefine(define_t *define)
{
printf("define->name = %s\n", define->name);
printf("define->flags = %d\n", define->flags);
printf("define->builtin = %d\n", define->builtin);
printf("define->numparms = %d\n", define->numparms);
// token_t *parms; //define parameters
// token_t *tokens; //macro tokens (possibly containing parm tokens)
// struct define_s *next; //next defined macro in a list
} //end of the function PC_PrintDefine*/
#if DEFINEHASHING
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_PrintDefineHashTable( define_t **definehash ) {
int i;
define_t *d;
for ( i = 0; i < DEFINEHASHSIZE; i++ )
{
Log_Write( "%4d:", i );
for ( d = definehash[i]; d; d = d->hashnext )
{
Log_Write( " %s", d->name );
} //end for
Log_Write( "\n" );
} //end for
} //end of the function PC_PrintDefineHashTable
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
//char primes[16] = {1, 3, 5, 7, 11, 13, 17, 19, 23, 27, 29, 31, 37, 41, 43, 47};
int PC_NameHash( char *name ) {
int hash, i;
hash = 0;
for ( i = 0; name[i] != '\0'; i++ )
{
hash += name[i] * ( 119 + i );
//hash += (name[i] << 7) + i;
//hash += (name[i] << (i&15));
} //end while
hash = ( hash ^ ( hash >> 10 ) ^ ( hash >> 20 ) ) & ( DEFINEHASHSIZE - 1 );
return hash;
} //end of the function PC_NameHash
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_AddDefineToHash( define_t *define, define_t **definehash ) {
int hash;
hash = PC_NameHash( define->name );
define->hashnext = definehash[hash];
definehash[hash] = define;
} //end of the function PC_AddDefineToHash
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
define_t *PC_FindHashedDefine( define_t **definehash, char *name ) {
define_t *d;
int hash;
hash = PC_NameHash( name );
for ( d = definehash[hash]; d; d = d->hashnext )
{
if ( !strcmp( d->name, name ) ) {
return d;
}
} //end for
return NULL;
} //end of the function PC_FindHashedDefine
#endif //DEFINEHASHING
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
define_t *PC_FindDefine( define_t *defines, char *name ) {
define_t *d;
for ( d = defines; d; d = d->next )
{
if ( !strcmp( d->name, name ) ) {
return d;
}
} //end for
return NULL;
} //end of the function PC_FindDefine
//============================================================================
//
// Parameter: -
// Returns: number of the parm
// if no parm found with the given name -1 is returned
// Changes Globals: -
//============================================================================
int PC_FindDefineParm( define_t *define, char *name ) {
token_t *p;
int i;
i = 0;
for ( p = define->parms; p; p = p->next )
{
if ( !strcmp( p->string, name ) ) {
return i;
}
i++;
} //end for
return -1;
} //end of the function PC_FindDefineParm
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_FreeDefine( define_t *define ) {
token_t *t, *next;
//free the define parameters
for ( t = define->parms; t; t = next )
{
next = t->next;
PC_FreeToken( t );
} //end for
//free the define tokens
for ( t = define->tokens; t; t = next )
{
next = t->next;
PC_FreeToken( t );
} //end for
//free the define
FreeMemory(define->name);
FreeMemory( define );
} //end of the function PC_FreeDefine
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_AddBuiltinDefines( source_t *source ) {
int i;
define_t *define;
struct builtin
{
char *string;
int builtin;
} builtin[] = {
{ "__LINE__", BUILTIN_LINE },
{ "__FILE__", BUILTIN_FILE },
{ "__DATE__", BUILTIN_DATE },
{ "__TIME__", BUILTIN_TIME },
// { "__STDC__", BUILTIN_STDC },
{ NULL, 0 }
};
for ( i = 0; builtin[i].string; i++ )
{
define = (define_t *) GetMemory(sizeof(define_t));
memset( define, 0, sizeof( define_t ) );
define->name = (char *) GetMemory(strlen(builtin[i].string) + 1);
strcpy( define->name, builtin[i].string );
define->flags |= DEFINE_FIXED;
define->builtin = builtin[i].builtin;
//add the define to the source
#if DEFINEHASHING
PC_AddDefineToHash( define, source->definehash );
#else
define->next = source->defines;
source->defines = define;
#endif //DEFINEHASHING
} //end for
} //end of the function PC_AddBuiltinDefines
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_ExpandBuiltinDefine( source_t *source, token_t *deftoken, define_t *define,
token_t **firsttoken, token_t **lasttoken ) {
token_t *token;
time_t t;
char *curtime;
token = PC_CopyToken( deftoken );
switch ( define->builtin )
{
case BUILTIN_LINE:
{
sprintf( token->string, "%d", deftoken->line );
#ifdef NUMBERVALUE
token->intvalue = deftoken->line;
token->floatvalue = deftoken->line;
#endif //NUMBERVALUE
token->type = TT_NUMBER;
token->subtype = TT_DECIMAL | TT_INTEGER;
*firsttoken = token;
*lasttoken = token;
break;
} //end case
case BUILTIN_FILE:
{
strcpy( token->string, source->scriptstack->filename );
token->type = TT_NAME;
token->subtype = strlen( token->string );
*firsttoken = token;
*lasttoken = token;
break;
} //end case
case BUILTIN_DATE:
{
t = time( NULL );
curtime = ctime( &t );
strcpy( token->string, "\"" );
strncat( token->string, curtime + 4, 7 );
strncat( token->string + 7, curtime + 20, 4 );
strcat( token->string, "\"" );
free( curtime );
token->type = TT_NAME;
token->subtype = strlen( token->string );
*firsttoken = token;
*lasttoken = token;
break;
} //end case
case BUILTIN_TIME:
{
t = time( NULL );
curtime = ctime( &t );
strcpy( token->string, "\"" );
strncat( token->string, curtime + 11, 8 );
strcat( token->string, "\"" );
free( curtime );
token->type = TT_NAME;
token->subtype = strlen( token->string );
*firsttoken = token;
*lasttoken = token;
break;
} //end case
case BUILTIN_STDC:
default:
{
*firsttoken = NULL;
*lasttoken = NULL;
break;
} //end case
} //end switch
return qtrue;
} //end of the function PC_ExpandBuiltinDefine
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_ExpandDefine( source_t *source, token_t *deftoken, define_t *define,
token_t **firsttoken, token_t **lasttoken ) {
token_t *parms[MAX_DEFINEPARMS] = { NULL }, *dt, *pt, *t;
token_t *t1, *t2, *first, *last, *nextpt, token;
int parmnum, i;
//if it is a builtin define
if ( define->builtin ) {
return PC_ExpandBuiltinDefine( source, deftoken, define, firsttoken, lasttoken );
} //end if
//if the define has parameters
if ( define->numparms ) {
if ( !PC_ReadDefineParms( source, define, parms, MAX_DEFINEPARMS ) ) {
return qfalse;
}
#ifdef DEBUG_EVAL
for ( i = 0; i < define->numparms; i++ )
{
Log_Write( "define parms %d:", i );
for ( pt = parms[i]; pt; pt = pt->next )
{
Log_Write( "%s", pt->string );
} //end for
} //end for
#endif //DEBUG_EVAL
} //end if
//empty list at first
first = NULL;
last = NULL;
//create a list with tokens of the expanded define
for ( dt = define->tokens; dt; dt = dt->next )
{
parmnum = -1;
//if the token is a name, it could be a define parameter
if ( dt->type == TT_NAME ) {
parmnum = PC_FindDefineParm( define, dt->string );
} //end if
//if it is a define parameter
if ( parmnum >= 0 ) {
for ( pt = parms[parmnum]; pt; pt = pt->next )
{
t = PC_CopyToken( pt );
//add the token to the list
t->next = NULL;
if ( last ) {
last->next = t;
} else { first = t;}
last = t;
} //end for
} //end if
else
{
//if stringizing operator
if ( dt->string[0] == '#' && dt->string[1] == '\0' ) {
//the stringizing operator must be followed by a define parameter
if ( dt->next ) {
parmnum = PC_FindDefineParm( define, dt->next->string );
} else { parmnum = -1;}
//
if ( parmnum >= 0 ) {
//step over the stringizing operator
dt = dt->next;
//stringize the define parameter tokens
if ( !PC_StringizeTokens( parms[parmnum], &token ) ) {
SourceError( source, "can't stringize tokens" );
return qfalse;
} //end if
t = PC_CopyToken( &token );
} //end if
else
{
SourceWarning( source, "stringizing operator without define parameter" );
continue;
} //end if
} //end if
else
{
t = PC_CopyToken( dt );
} //end else
//add the token to the list
t->next = NULL;
if ( last ) {
last->next = t;
} else { first = t;}
last = t;
} //end else
} //end for
//check for the merging operator
for ( t = first; t; )
{
if ( t->next ) {
//if the merging operator
if ( t->next->string[0] == '#' && t->next->string[1] == '#' ) {
t1 = t;
t2 = t->next->next;
if ( t2 ) {
if ( !PC_MergeTokens( t1, t2 ) ) {
SourceError( source, "can't merge %s with %s", t1->string, t2->string );
return qfalse;
} //end if
PC_FreeToken( t1->next );
t1->next = t2->next;
if ( t2 == last ) {
last = t1;
}
PC_FreeToken( t2 );
continue;
} //end if
} //end if
} //end if
t = t->next;
} //end for
//store the first and last token of the list
*firsttoken = first;
*lasttoken = last;
//free all the parameter tokens
for ( i = 0; i < define->numparms; i++ )
{
for ( pt = parms[i]; pt; pt = nextpt )
{
nextpt = pt->next;
PC_FreeToken( pt );
} //end for
} //end for
//
return qtrue;
} //end of the function PC_ExpandDefine
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_ExpandDefineIntoSource( source_t *source, token_t *deftoken, define_t *define ) {
token_t *firsttoken, *lasttoken;
if ( !PC_ExpandDefine( source, deftoken, define, &firsttoken, &lasttoken ) ) {
return qfalse;
}
if ( firsttoken && lasttoken ) {
lasttoken->next = source->tokens;
source->tokens = firsttoken;
return qtrue;
} //end if
return qfalse;
} //end of the function PC_ExpandDefineIntoSource
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_ConvertPath( char *path ) {
char *ptr;
//remove double path seperators
for ( ptr = path; *ptr; )
{
if ( ( *ptr == '\\' || *ptr == '/' ) &&
( *( ptr + 1 ) == '\\' || *( ptr + 1 ) == '/' ) ) {
memmove(ptr, ptr+1, strlen(ptr));
} //end if
else
{
ptr++;
} //end else
} //end while
//set OS dependent path seperators
for ( ptr = path; *ptr; )
{
if ( *ptr == '/' || *ptr == '\\' ) {
*ptr = PATHSEPERATOR_CHAR;
}
ptr++;
} //end while
} //end of the function PC_ConvertPath
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_Directive_include( source_t *source ) {
script_t *script;
token_t token;
char path[MAX_QPATH];
#ifdef QUAKE
foundfile_t file;
#endif //QUAKE
if ( source->skip > 0 ) {
return qtrue;
}
//
if ( !PC_ReadSourceToken( source, &token ) ) {
SourceError( source, "#include without file name" );
return qfalse;
} //end if
if ( token.linescrossed > 0 ) {
SourceError( source, "#include without file name" );
return qfalse;
} //end if
if ( token.type == TT_STRING ) {
StripDoubleQuotes( token.string );
PC_ConvertPath( token.string );
script = LoadScriptFile( token.string );
if ( !script ) {
Q_strncpyz(path, source->includepath, sizeof(path));
Q_strcat(path, sizeof(path), token.string);
script = LoadScriptFile( path );
} //end if
} //end if
else if ( token.type == TT_PUNCTUATION && *token.string == '<' ) {
Q_strncpyz(path, source->includepath, sizeof(path));
while ( PC_ReadSourceToken( source, &token ) )
{
if ( token.linescrossed > 0 ) {
PC_UnreadSourceToken( source, &token );
break;
} //end if
if ( token.type == TT_PUNCTUATION && *token.string == '>' ) {
break;
}
Q_strcat(path, sizeof(path), token.string);
} //end while
if ( *token.string != '>' ) {
SourceWarning( source, "#include missing trailing >" );
} //end if
if ( !strlen( path ) ) {
SourceError( source, "#include without file name between < >" );
return qfalse;
} //end if
PC_ConvertPath( path );
script = LoadScriptFile( path );
} //end if
else
{
SourceError( source, "#include without file name" );
return qfalse;
} //end else
#ifdef QUAKE
if ( !script ) {
memset( &file, 0, sizeof( foundfile_t ) );
script = LoadScriptFile( path );
if ( script ) {
Q_strncpyz( script->filename, path, sizeof( script->filename ) );
}
} //end if
#endif //QUAKE
if ( !script ) {
#ifdef SCREWUP
SourceWarning( source, "file %s not found", path );
return qtrue;
#else
SourceError( source, "file %s not found", path );
return qfalse;
#endif //SCREWUP
} //end if
PC_PushScript( source, script );
return qtrue;
} //end of the function PC_Directive_include
//============================================================================
// reads a token from the current line, continues reading on the next
// line only if a backslash '\' is encountered.
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_ReadLine( source_t *source, token_t *token ) {
int crossline;
crossline = 0;
do
{
if ( !PC_ReadSourceToken( source, token ) ) {
return qfalse;
}
if ( token->linescrossed > crossline ) {
PC_UnreadSourceToken( source, token );
return qfalse;
} //end if
crossline = 1;
} while ( !strcmp( token->string, "\\" ) );
return qtrue;
} //end of the function PC_ReadLine
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_WhiteSpaceBeforeToken( token_t *token ) {
return token->endwhitespace_p - token->whitespace_p > 0;
} //end of the function PC_WhiteSpaceBeforeToken
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_ClearTokenWhiteSpace( token_t *token ) {
token->whitespace_p = NULL;
token->endwhitespace_p = NULL;
token->linescrossed = 0;
} //end of the function PC_ClearTokenWhiteSpace
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_Directive_undef( source_t *source ) {
token_t token;
define_t *define, *lastdefine;
int hash;
if ( source->skip > 0 ) {
return qtrue;
}
//
if ( !PC_ReadLine( source, &token ) ) {
SourceError( source, "undef without name" );
return qfalse;
} //end if
if ( token.type != TT_NAME ) {
PC_UnreadSourceToken( source, &token );
SourceError( source, "expected name, found %s", token.string );
return qfalse;
} //end if
#if DEFINEHASHING
hash = PC_NameHash( token.string );
for ( lastdefine = NULL, define = source->definehash[hash]; define; define = define->hashnext )
{
if ( !strcmp( define->name, token.string ) ) {
if ( define->flags & DEFINE_FIXED ) {
SourceWarning( source, "can't undef %s", token.string );
} //end if
else
{
if ( lastdefine ) {
lastdefine->hashnext = define->hashnext;
} else { source->definehash[hash] = define->hashnext;}
PC_FreeDefine( define );
} //end else
break;
} //end if
lastdefine = define;
} //end for
#else //DEFINEHASHING
for ( lastdefine = NULL, define = source->defines; define; define = define->next )
{
if ( !strcmp( define->name, token.string ) ) {
if ( define->flags & DEFINE_FIXED ) {
SourceWarning( source, "can't undef %s", token.string );
} //end if
else
{
if ( lastdefine ) {
lastdefine->next = define->next;
} else { source->defines = define->next;}
PC_FreeDefine( define );
} //end else
break;
} //end if
lastdefine = define;
} //end for
#endif //DEFINEHASHING
return qtrue;
} //end of the function PC_Directive_undef
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_Directive_define( source_t *source ) {
token_t token, *t, *last;
define_t *define;
if ( source->skip > 0 ) {
return qtrue;
}
//
if ( !PC_ReadLine( source, &token ) ) {
SourceError( source, "#define without name" );
return qfalse;
} //end if
if ( token.type != TT_NAME ) {
PC_UnreadSourceToken( source, &token );
SourceError( source, "expected name after #define, found %s", token.string );
return qfalse;
} //end if
//check if the define already exists
#if DEFINEHASHING
define = PC_FindHashedDefine( source->definehash, token.string );
#else
define = PC_FindDefine( source->defines, token.string );
#endif //DEFINEHASHING
if ( define ) {
if ( define->flags & DEFINE_FIXED ) {
SourceError( source, "can't redefine %s", token.string );
return qfalse;
} //end if
SourceWarning( source, "redefinition of %s", token.string );
//unread the define name before executing the #undef directive
PC_UnreadSourceToken( source, &token );
if ( !PC_Directive_undef( source ) ) {
return qfalse;
}
} //end if
//allocate define
define = (define_t *) GetMemory(sizeof(define_t));
memset( define, 0, sizeof( define_t ) );
define->name = (char *) GetMemory(strlen(token.string) + 1);
strcpy( define->name, token.string );
//add the define to the source
#if DEFINEHASHING
PC_AddDefineToHash( define, source->definehash );
#else //DEFINEHASHING
define->next = source->defines;
source->defines = define;
#endif //DEFINEHASHING
//if nothing is defined, just return
if ( !PC_ReadLine( source, &token ) ) {
return qtrue;
}
//if it is a define with parameters
if ( !PC_WhiteSpaceBeforeToken( &token ) && !strcmp( token.string, "(" ) ) {
//read the define parameters
last = NULL;
if ( !PC_CheckTokenString( source, ")" ) ) {
while ( 1 )
{
if ( !PC_ReadLine( source, &token ) ) {
SourceError( source, "expected define parameter" );
return qfalse;
} //end if
//if it isn't a name
if ( token.type != TT_NAME ) {
SourceError( source, "invalid define parameter" );
return qfalse;
} //end if
//
if ( PC_FindDefineParm( define, token.string ) >= 0 ) {
SourceError( source, "two the same define parameters" );
return qfalse;
} //end if
//add the define parm
t = PC_CopyToken( &token );
PC_ClearTokenWhiteSpace( t );
t->next = NULL;
if ( last ) {
last->next = t;
} else { define->parms = t;}
last = t;
define->numparms++;
//read next token
if ( !PC_ReadLine( source, &token ) ) {
SourceError( source, "define parameters not terminated" );
return qfalse;
} //end if
//
if ( !strcmp( token.string, ")" ) ) {
break;
}
//then it must be a comma
if ( strcmp( token.string, "," ) ) {
SourceError( source, "define not terminated" );
return qfalse;
} //end if
} //end while
} //end if
if ( !PC_ReadLine( source, &token ) ) {
return qtrue;
}
} //end if
//read the defined stuff
last = NULL;
do
{
t = PC_CopyToken( &token );
if ( t->type == TT_NAME && !strcmp( t->string, define->name ) ) {
SourceError( source, "recursive define (removed recursion)" );
continue;
} //end if
PC_ClearTokenWhiteSpace( t );
t->next = NULL;
if ( last ) {
last->next = t;
} else { define->tokens = t;}
last = t;
} while ( PC_ReadLine( source, &token ) );
//
if ( last ) {
//check for merge operators at the beginning or end
if ( !strcmp( define->tokens->string, "##" ) ||
!strcmp( last->string, "##" ) ) {
SourceError( source, "define with misplaced ##" );
return qfalse;
} //end if
} //end if
return qtrue;
} //end of the function PC_Directive_define
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
define_t *PC_DefineFromString( char *string ) {
script_t *script;
source_t src;
token_t *t;
int res, i;
define_t *def;
PC_InitTokenHeap();
script = LoadScriptMemory( string, strlen( string ), "*extern" );
//create a new source
memset( &src, 0, sizeof( source_t ) );
Q_strncpyz( src.filename, "*extern", sizeof( src.filename ) );
src.scriptstack = script;
#if DEFINEHASHING
src.definehash = GetClearedMemory( DEFINEHASHSIZE * sizeof( define_t * ) );
#endif //DEFINEHASHING
//create a define from the source
res = PC_Directive_define( &src );
//free any tokens if left
for ( t = src.tokens; t; t = src.tokens )
{
src.tokens = src.tokens->next;
PC_FreeToken( t );
} //end for
#ifdef DEFINEHASHING
def = NULL;
for ( i = 0; i < DEFINEHASHSIZE; i++ )
{
if ( src.definehash[i] ) {
def = src.definehash[i];
break;
} //end if
} //end for
#else
def = src.defines;
#endif //DEFINEHASHING
//
#if DEFINEHASHING
FreeMemory( src.definehash );
#endif //DEFINEHASHING
//
FreeScript( script );
//if the define was created successfully
if ( res > 0 ) {
return def;
}
//free the define if created
if ( src.defines ) {
PC_FreeDefine( def );
}
//
return NULL;
} //end of the function PC_DefineFromString
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_AddDefine( source_t *source, char *string ) {
define_t *define;
define = PC_DefineFromString( string );
if ( !define ) {
return qfalse;
}
#if DEFINEHASHING
PC_AddDefineToHash( define, source->definehash );
#else //DEFINEHASHING
define->next = source->defines;
source->defines = define;
#endif //DEFINEHASHING
return qtrue;
} //end of the function PC_AddDefine
//============================================================================
// add a globals define that will be added to all opened sources
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_AddGlobalDefine( char *string ) {
define_t *define;
define = PC_DefineFromString( string );
if ( !define ) {
return qfalse;
}
define->next = globaldefines;
globaldefines = define;
return qtrue;
} //end of the function PC_AddGlobalDefine
//============================================================================
// remove the given global define
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_RemoveGlobalDefine( char *name ) {
define_t *define;
define = PC_FindDefine( globaldefines, name );
if ( define ) {
PC_FreeDefine( define );
return qtrue;
} //end if
return qfalse;
} //end of the function PC_RemoveGlobalDefine
//============================================================================
// remove all globals defines
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_RemoveAllGlobalDefines( void ) {
define_t *define;
for ( define = globaldefines; define; define = globaldefines )
{
globaldefines = globaldefines->next;
PC_FreeDefine( define );
} //end for
} //end of the function PC_RemoveAllGlobalDefines
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
define_t *PC_CopyDefine( source_t *source, define_t *define ) {
define_t *newdefine;
token_t *token, *newtoken, *lasttoken;
newdefine = (define_t *) GetMemory(sizeof(define_t));
//copy the define name
newdefine->name = (char *) GetMemory(strlen(define->name) + 1);
strcpy( newdefine->name, define->name );
newdefine->flags = define->flags;
newdefine->builtin = define->builtin;
newdefine->numparms = define->numparms;
//the define is not linked
newdefine->next = NULL;
newdefine->hashnext = NULL;
//copy the define tokens
newdefine->tokens = NULL;
for ( lasttoken = NULL, token = define->tokens; token; token = token->next )
{
newtoken = PC_CopyToken( token );
newtoken->next = NULL;
if ( lasttoken ) {
lasttoken->next = newtoken;
} else { newdefine->tokens = newtoken;}
lasttoken = newtoken;
} //end for
//copy the define parameters
newdefine->parms = NULL;
for ( lasttoken = NULL, token = define->parms; token; token = token->next )
{
newtoken = PC_CopyToken( token );
newtoken->next = NULL;
if ( lasttoken ) {
lasttoken->next = newtoken;
} else { newdefine->parms = newtoken;}
lasttoken = newtoken;
} //end for
return newdefine;
} //end of the function PC_CopyDefine
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_AddGlobalDefinesToSource( source_t *source ) {
define_t *define, *newdefine;
for ( define = globaldefines; define; define = define->next )
{
newdefine = PC_CopyDefine( source, define );
#if DEFINEHASHING
PC_AddDefineToHash( newdefine, source->definehash );
#else //DEFINEHASHING
newdefine->next = source->defines;
source->defines = newdefine;
#endif //DEFINEHASHING
} //end for
} //end of the function PC_AddGlobalDefinesToSource
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_Directive_if_def( source_t *source, int type ) {
token_t token;
define_t *d;
int skip;
if ( !PC_ReadLine( source, &token ) ) {
SourceError( source, "#ifdef without name" );
return qfalse;
} //end if
if ( token.type != TT_NAME ) {
PC_UnreadSourceToken( source, &token );
SourceError( source, "expected name after #ifdef, found %s", token.string );
return qfalse;
} //end if
#if DEFINEHASHING
d = PC_FindHashedDefine( source->definehash, token.string );
#else
d = PC_FindDefine( source->defines, token.string );
#endif //DEFINEHASHING
skip = ( type == INDENT_IFDEF ) == ( d == NULL );
PC_PushIndent( source, type, skip );
return qtrue;
} //end of the function PC_Directiveif_def
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_Directive_ifdef( source_t *source ) {
return PC_Directive_if_def( source, INDENT_IFDEF );
} //end of the function PC_Directive_ifdef
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_Directive_ifndef( source_t *source ) {
return PC_Directive_if_def( source, INDENT_IFNDEF );
} //end of the function PC_Directive_ifndef
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_Directive_else( source_t *source ) {
int type, skip;
PC_PopIndent( source, &type, &skip );
if ( !type ) {
SourceError( source, "misplaced #else" );
return qfalse;
} //end if
if ( type == INDENT_ELSE ) {
SourceError( source, "#else after #else" );
return qfalse;
} //end if
PC_PushIndent( source, INDENT_ELSE, !skip );
return qtrue;
} //end of the function PC_Directive_else
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_Directive_endif( source_t *source ) {
int type, skip;
PC_PopIndent( source, &type, &skip );
if ( !type ) {
SourceError( source, "misplaced #endif" );
return qfalse;
} //end if
return qtrue;
} //end of the function PC_Directive_endif
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
typedef struct operator_s
{
int operator;
int priority;
int parentheses;
struct operator_s *prev, *next;
} operator_t;
typedef struct value_s
{
signed long int intvalue;
float floatvalue;
int parentheses;
struct value_s *prev, *next;
} value_t;
int PC_OperatorPriority( int op ) {
switch ( op )
{
case P_MUL: return 15;
case P_DIV: return 15;
case P_MOD: return 15;
case P_ADD: return 14;
case P_SUB: return 14;
case P_LOGIC_AND: return 7;
case P_LOGIC_OR: return 6;
case P_LOGIC_GEQ: return 12;
case P_LOGIC_LEQ: return 12;
case P_LOGIC_EQ: return 11;
case P_LOGIC_UNEQ: return 11;
case P_LOGIC_NOT: return 16;
case P_LOGIC_GREATER: return 12;
case P_LOGIC_LESS: return 12;
case P_RSHIFT: return 13;
case P_LSHIFT: return 13;
case P_BIN_AND: return 10;
case P_BIN_OR: return 8;
case P_BIN_XOR: return 9;
case P_BIN_NOT: return 16;
case P_COLON: return 5;
case P_QUESTIONMARK: return 5;
} //end switch
return qfalse;
} //end of the function PC_OperatorPriority
//#define AllocValue() GetClearedMemory(sizeof(value_t));
//#define FreeValue(val) FreeMemory(val)
//#define AllocOperator(op) op = (operator_t *) GetClearedMemory(sizeof(operator_t));
//#define FreeOperator(op) FreeMemory(op);
#define MAX_VALUES 64
#define MAX_OPERATORS 64
#define AllocValue( val ) \
if ( numvalues >= MAX_VALUES ) { \
SourceError( source, "out of value space" ); \
error = 1; \
break; \
} \
else { \
val = &value_heap[numvalues++];}
#define FreeValue( val )
//
#define AllocOperator( op ) \
if ( numoperators >= MAX_OPERATORS ) { \
SourceError( source, "out of operator space" ); \
error = 1; \
break; \
} \
else { \
op = &operator_heap[numoperators++];}
#define FreeOperator( op )
int PC_EvaluateTokens( source_t *source, token_t *tokens, signed long int *intvalue,
float *floatvalue, int integer ) {
operator_t *o, *firstoperator, *lastoperator;
value_t *v, *firstvalue, *lastvalue, *v1, *v2;
token_t *t;
int brace = 0;
int parentheses = 0;
int error = 0;
int lastwasvalue = 0;
int negativevalue = 0;
int questmarkintvalue = 0;
float questmarkfloatvalue = 0;
int gotquestmarkvalue = qfalse;
//
operator_t operator_heap[MAX_OPERATORS];
int numoperators = 0;
value_t value_heap[MAX_VALUES];
int numvalues = 0;
firstoperator = lastoperator = NULL;
firstvalue = lastvalue = NULL;
if ( intvalue ) {
*intvalue = 0;
}
if ( floatvalue ) {
*floatvalue = 0;
}
for ( t = tokens; t; t = t->next )
{
switch ( t->type )
{
case TT_NAME:
{
if ( lastwasvalue || negativevalue ) {
SourceError( source, "syntax error in #if/#elif" );
error = 1;
break;
} //end if
if ( strcmp( t->string, "defined" ) ) {
SourceError( source, "undefined name %s in #if/#elif", t->string );
error = 1;
break;
} //end if
t = t->next;
if ( !strcmp( t->string, "(" ) ) {
brace = qtrue;
t = t->next;
} //end if
if ( !t || t->type != TT_NAME ) {
SourceError( source, "defined without name in #if/#elif" );
error = 1;
break;
} //end if
//v = (value_t *) GetClearedMemory(sizeof(value_t));
AllocValue( v );
#if DEFINEHASHING
if ( PC_FindHashedDefine( source->definehash, t->string ) )
#else
if ( PC_FindDefine( source->defines, t->string ) )
#endif //DEFINEHASHING
{
v->intvalue = 1;
v->floatvalue = 1;
} //end if
else
{
v->intvalue = 0;
v->floatvalue = 0;
} //end else
v->parentheses = parentheses;
v->next = NULL;
v->prev = lastvalue;
if ( lastvalue ) {
lastvalue->next = v;
} else { firstvalue = v;}
lastvalue = v;
if ( brace ) {
t = t->next;
if ( !t || strcmp( t->string, ")" ) ) {
SourceError( source, "defined without ) in #if/#elif" );
error = 1;
break;
} //end if
} //end if
brace = qfalse;
// defined() creates a value
lastwasvalue = 1;
break;
} //end case
case TT_NUMBER:
{
if ( lastwasvalue ) {
SourceError( source, "syntax error in #if/#elif" );
error = 1;
break;
} //end if
//v = (value_t *) GetClearedMemory(sizeof(value_t));
AllocValue( v );
if ( negativevalue ) {
v->intvalue = -(signed int) t->intvalue;
v->floatvalue = -t->floatvalue;
} //end if
else
{
v->intvalue = t->intvalue;
v->floatvalue = t->floatvalue;
} //end else
v->parentheses = parentheses;
v->next = NULL;
v->prev = lastvalue;
if ( lastvalue ) {
lastvalue->next = v;
} else { firstvalue = v;}
lastvalue = v;
//last token was a value
lastwasvalue = 1;
//
negativevalue = 0;
break;
} //end case
case TT_PUNCTUATION:
{
if ( negativevalue ) {
SourceError( source, "misplaced minus sign in #if/#elif" );
error = 1;
break;
} //end if
if ( t->subtype == P_PARENTHESESOPEN ) {
parentheses++;
break;
} //end if
else if ( t->subtype == P_PARENTHESESCLOSE ) {
parentheses--;
if ( parentheses < 0 ) {
SourceError( source, "too many ) in #if/#elsif" );
error = 1;
} //end if
break;
} //end else if
//check for invalid operators on floating point values
if ( !integer ) {
if ( t->subtype == P_BIN_NOT || t->subtype == P_MOD ||
t->subtype == P_RSHIFT || t->subtype == P_LSHIFT ||
t->subtype == P_BIN_AND || t->subtype == P_BIN_OR ||
t->subtype == P_BIN_XOR ) {
SourceError( source, "illigal operator %s on floating point operands", t->string );
error = 1;
break;
} //end if
} //end if
switch ( t->subtype )
{
case P_LOGIC_NOT:
case P_BIN_NOT:
{
if ( lastwasvalue ) {
SourceError( source, "! or ~ after value in #if/#elif" );
error = 1;
break;
} //end if
break;
} //end case
case P_INC:
case P_DEC:
{
SourceError( source, "++ or -- used in #if/#elif" );
break;
} //end case
case P_SUB:
{
if ( !lastwasvalue ) {
negativevalue = 1;
break;
} //end if
} //end case
case P_MUL:
case P_DIV:
case P_MOD:
case P_ADD:
case P_LOGIC_AND:
case P_LOGIC_OR:
case P_LOGIC_GEQ:
case P_LOGIC_LEQ:
case P_LOGIC_EQ:
case P_LOGIC_UNEQ:
case P_LOGIC_GREATER:
case P_LOGIC_LESS:
case P_RSHIFT:
case P_LSHIFT:
case P_BIN_AND:
case P_BIN_OR:
case P_BIN_XOR:
case P_COLON:
case P_QUESTIONMARK:
{
if ( !lastwasvalue ) {
SourceError( source, "operator %s after operator in #if/#elif", t->string );
error = 1;
break;
} //end if
break;
} //end case
default:
{
SourceError( source, "invalid operator %s in #if/#elif", t->string );
error = 1;
break;
} //end default
} //end switch
if ( !error && !negativevalue ) {
//o = (operator_t *) GetClearedMemory(sizeof(operator_t));
AllocOperator( o );
o->operator = t->subtype;
o->priority = PC_OperatorPriority( t->subtype );
o->parentheses = parentheses;
o->next = NULL;
o->prev = lastoperator;
if ( lastoperator ) {
lastoperator->next = o;
} else { firstoperator = o;}
lastoperator = o;
lastwasvalue = 0;
} //end if
break;
} //end case
default:
{
SourceError( source, "unknown %s in #if/#elif", t->string );
error = 1;
break;
} //end default
} //end switch
if ( error ) {
break;
}
} //end for
if ( !error ) {
if ( !lastwasvalue ) {
SourceError( source, "trailing operator in #if/#elif" );
error = 1;
} //end if
else if ( parentheses ) {
SourceError( source, "too many ( in #if/#elif" );
error = 1;
} //end else if
} //end if
//
gotquestmarkvalue = qfalse;
questmarkintvalue = 0;
questmarkfloatvalue = 0;
//while there are operators
while ( !error && firstoperator )
{
v = firstvalue;
for ( o = firstoperator; o->next; o = o->next )
{
//if the current operator is nested deeper in parentheses
//than the next operator
if ( o->parentheses > o->next->parentheses ) {
break;
}
//if the current and next operator are nested equally deep in parentheses
if ( o->parentheses == o->next->parentheses ) {
//if the priority of the current operator is equal or higher
//than the priority of the next operator
if ( o->priority >= o->next->priority ) {
break;
}
} //end if
//if the arity of the operator isn't equal to 1
if ( o->operator != P_LOGIC_NOT
&& o->operator != P_BIN_NOT ) {
v = v->next;
}
//if there's no value or no next value
if ( !v ) {
SourceError( source, "mising values in #if/#elif" );
error = 1;
break;
} //end if
} //end for
if ( error ) {
break;
}
v1 = v;
v2 = v->next;
#ifdef DEBUG_EVAL
if ( integer ) {
Log_Write( "operator %s, value1 = %d", PunctuationFromNum( source->scriptstack, o->operator ), v1->intvalue );
if ( v2 ) {
Log_Write( "value2 = %d", v2->intvalue );
}
} //end if
else
{
Log_Write( "operator %s, value1 = %f", PunctuationFromNum( source->scriptstack, o->operator ), v1->floatvalue );
if ( v2 ) {
Log_Write( "value2 = %f", v2->floatvalue );
}
} //end else
#endif //DEBUG_EVAL
switch ( o->operator )
{
case P_LOGIC_NOT: v1->intvalue = !v1->intvalue;
v1->floatvalue = !v1->floatvalue; break;
case P_BIN_NOT: v1->intvalue = ~v1->intvalue;
break;
case P_MUL: v1->intvalue *= v2->intvalue;
v1->floatvalue *= v2->floatvalue; break;
case P_DIV: if ( !v2->intvalue || !v2->floatvalue ) {
SourceError( source, "divide by zero in #if/#elif" );
error = 1;
break;
}
v1->intvalue /= v2->intvalue;
v1->floatvalue /= v2->floatvalue; break;
case P_MOD: if ( !v2->intvalue ) {
SourceError( source, "divide by zero in #if/#elif" );
error = 1;
break;
}
v1->intvalue %= v2->intvalue; break;
case P_ADD: v1->intvalue += v2->intvalue;
v1->floatvalue += v2->floatvalue; break;
case P_SUB: v1->intvalue -= v2->intvalue;
v1->floatvalue -= v2->floatvalue; break;
case P_LOGIC_AND: v1->intvalue = v1->intvalue && v2->intvalue;
v1->floatvalue = v1->floatvalue && v2->floatvalue; break;
case P_LOGIC_OR: v1->intvalue = v1->intvalue || v2->intvalue;
v1->floatvalue = v1->floatvalue || v2->floatvalue; break;
case P_LOGIC_GEQ: v1->intvalue = v1->intvalue >= v2->intvalue;
v1->floatvalue = v1->floatvalue >= v2->floatvalue; break;
case P_LOGIC_LEQ: v1->intvalue = v1->intvalue <= v2->intvalue;
v1->floatvalue = v1->floatvalue <= v2->floatvalue; break;
case P_LOGIC_EQ: v1->intvalue = v1->intvalue == v2->intvalue;
v1->floatvalue = v1->floatvalue == v2->floatvalue; break;
case P_LOGIC_UNEQ: v1->intvalue = v1->intvalue != v2->intvalue;
v1->floatvalue = v1->floatvalue != v2->floatvalue; break;
case P_LOGIC_GREATER: v1->intvalue = v1->intvalue > v2->intvalue;
v1->floatvalue = v1->floatvalue > v2->floatvalue; break;
case P_LOGIC_LESS: v1->intvalue = v1->intvalue < v2->intvalue;
v1->floatvalue = v1->floatvalue < v2->floatvalue; break;
case P_RSHIFT: v1->intvalue >>= v2->intvalue;
break;
case P_LSHIFT: v1->intvalue <<= v2->intvalue;
break;
case P_BIN_AND: v1->intvalue &= v2->intvalue;
break;
case P_BIN_OR: v1->intvalue |= v2->intvalue;
break;
case P_BIN_XOR: v1->intvalue ^= v2->intvalue;
break;
case P_COLON:
{
if ( !gotquestmarkvalue ) {
SourceError( source, ": without ? in #if/#elif" );
error = 1;
break;
} //end if
if ( integer ) {
if ( !questmarkintvalue ) {
v1->intvalue = v2->intvalue;
}
} //end if
else
{
if ( !questmarkfloatvalue ) {
v1->floatvalue = v2->floatvalue;
}
} //end else
gotquestmarkvalue = qfalse;
break;
} //end case
case P_QUESTIONMARK:
{
if ( gotquestmarkvalue ) {
SourceError( source, "? after ? in #if/#elif" );
error = 1;
break;
} //end if
questmarkintvalue = v1->intvalue;
questmarkfloatvalue = v1->floatvalue;
gotquestmarkvalue = qtrue;
break;
} //end if
} //end switch
#ifdef DEBUG_EVAL
if ( integer ) {
Log_Write( "result value = %d", v1->intvalue );
} else { Log_Write( "result value = %f", v1->floatvalue );}
#endif //DEBUG_EVAL
if ( error ) {
break;
}
//if not an operator with arity 1
if ( o->operator != P_LOGIC_NOT
&& o->operator != P_BIN_NOT ) {
//remove the second value if not question mark operator
if ( o->operator != P_QUESTIONMARK ) {
v = v->next;
}
//
if (v)
{
if (v->prev) v->prev->next = v->next;
else firstvalue = v->next;
if (v->next) v->next->prev = v->prev;
}
//FreeMemory(v);
FreeValue( v );
} //end if
//remove the operator
if ( o->prev ) {
o->prev->next = o->next;
} else { firstoperator = o->next;}
if ( o->next ) {
o->next->prev = o->prev;
}
//FreeMemory(o);
FreeOperator( o );
} //end while
if ( firstvalue ) {
if ( intvalue ) {
*intvalue = firstvalue->intvalue;
}
if ( floatvalue ) {
*floatvalue = firstvalue->floatvalue;
}
} //end if
for ( o = firstoperator; o; o = lastoperator )
{
lastoperator = o->next;
//FreeMemory(o);
FreeOperator( o );
} //end for
for ( v = firstvalue; v; v = lastvalue )
{
lastvalue = v->next;
//FreeMemory(v);
FreeValue( v );
} //end for
if ( !error ) {
return qtrue;
}
if ( intvalue ) {
*intvalue = 0;
}
if ( floatvalue ) {
*floatvalue = 0;
}
return qfalse;
} //end of the function PC_EvaluateTokens
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_Evaluate( source_t *source, signed long int *intvalue,
float *floatvalue, int integer ) {
token_t token, *firsttoken, *lasttoken;
token_t *t, *nexttoken;
define_t *define;
int defined = qfalse;
if ( intvalue ) {
*intvalue = 0;
}
if ( floatvalue ) {
*floatvalue = 0;
}
//
if ( !PC_ReadLine( source, &token ) ) {
SourceError( source, "no value after #if/#elif" );
return qfalse;
} //end if
firsttoken = NULL;
lasttoken = NULL;
do
{
//if the token is a name
if ( token.type == TT_NAME ) {
if ( defined ) {
defined = qfalse;
t = PC_CopyToken( &token );
t->next = NULL;
if ( lasttoken ) {
lasttoken->next = t;
} else { firsttoken = t;}
lasttoken = t;
} //end if
else if ( !strcmp( token.string, "defined" ) ) {
defined = qtrue;
t = PC_CopyToken( &token );
t->next = NULL;
if ( lasttoken ) {
lasttoken->next = t;
} else { firsttoken = t;}
lasttoken = t;
} //end if
else
{
//then it must be a define
#if DEFINEHASHING
define = PC_FindHashedDefine( source->definehash, token.string );
#else
define = PC_FindDefine( source->defines, token.string );
#endif //DEFINEHASHING
if ( !define ) {
SourceError( source, "can't evaluate %s, not defined", token.string );
return qfalse;
} //end if
if ( !PC_ExpandDefineIntoSource( source, &token, define ) ) {
return qfalse;
}
} //end else
} //end if
//if the token is a number or a punctuation
else if ( token.type == TT_NUMBER || token.type == TT_PUNCTUATION ) {
t = PC_CopyToken( &token );
t->next = NULL;
if ( lasttoken ) {
lasttoken->next = t;
} else { firsttoken = t;}
lasttoken = t;
} //end else
else //can't evaluate the token
{
SourceError( source, "can't evaluate %s", token.string );
return qfalse;
} //end else
} while ( PC_ReadLine( source, &token ) );
//
if ( !PC_EvaluateTokens( source, firsttoken, intvalue, floatvalue, integer ) ) {
return qfalse;
}
//
#ifdef DEBUG_EVAL
Log_Write( "eval:" );
#endif //DEBUG_EVAL
for ( t = firsttoken; t; t = nexttoken )
{
#ifdef DEBUG_EVAL
Log_Write( " %s", t->string );
#endif //DEBUG_EVAL
nexttoken = t->next;
PC_FreeToken( t );
} //end for
#ifdef DEBUG_EVAL
if ( integer ) {
Log_Write( "eval result: %d", *intvalue );
} else { Log_Write( "eval result: %f", *floatvalue );}
#endif //DEBUG_EVAL
//
return qtrue;
} //end of the function PC_Evaluate
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_DollarEvaluate( source_t *source, signed long int *intvalue,
float *floatvalue, int integer ) {
int indent, defined = qfalse;
token_t token, *firsttoken, *lasttoken;
token_t *t, *nexttoken;
define_t *define;
if ( intvalue ) {
*intvalue = 0;
}
if ( floatvalue ) {
*floatvalue = 0;
}
//
if ( !PC_ReadSourceToken( source, &token ) ) {
SourceError( source, "no leading ( after $evalint/$evalfloat" );
return qfalse;
} //end if
if ( !PC_ReadSourceToken( source, &token ) ) {
SourceError( source, "nothing to evaluate" );
return qfalse;
} //end if
indent = 1;
firsttoken = NULL;
lasttoken = NULL;
do
{
//if the token is a name
if ( token.type == TT_NAME ) {
if ( defined ) {
defined = qfalse;
t = PC_CopyToken( &token );
t->next = NULL;
if ( lasttoken ) {
lasttoken->next = t;
} else { firsttoken = t;}
lasttoken = t;
} //end if
else if ( !strcmp( token.string, "defined" ) ) {
defined = qtrue;
t = PC_CopyToken( &token );
t->next = NULL;
if ( lasttoken ) {
lasttoken->next = t;
} else { firsttoken = t;}
lasttoken = t;
} //end if
else
{
//then it must be a define
#if DEFINEHASHING
define = PC_FindHashedDefine( source->definehash, token.string );
#else
define = PC_FindDefine( source->defines, token.string );
#endif //DEFINEHASHING
if ( !define ) {
SourceError( source, "can't evaluate %s, not defined", token.string );
return qfalse;
} //end if
if ( !PC_ExpandDefineIntoSource( source, &token, define ) ) {
return qfalse;
}
} //end else
} //end if
//if the token is a number or a punctuation
else if ( token.type == TT_NUMBER || token.type == TT_PUNCTUATION ) {
if ( *token.string == '(' ) {
indent++;
} else if ( *token.string == ')' ) {
indent--;
}
if ( indent <= 0 ) {
break;
}
t = PC_CopyToken( &token );
t->next = NULL;
if ( lasttoken ) {
lasttoken->next = t;
} else { firsttoken = t;}
lasttoken = t;
} //end else
else //can't evaluate the token
{
SourceError( source, "can't evaluate %s", token.string );
return qfalse;
} //end else
} while ( PC_ReadSourceToken( source, &token ) );
//
if ( !PC_EvaluateTokens( source, firsttoken, intvalue, floatvalue, integer ) ) {
return qfalse;
}
//
#ifdef DEBUG_EVAL
Log_Write( "$eval:" );
#endif //DEBUG_EVAL
for ( t = firsttoken; t; t = nexttoken )
{
#ifdef DEBUG_EVAL
Log_Write( " %s", t->string );
#endif //DEBUG_EVAL
nexttoken = t->next;
PC_FreeToken( t );
} //end for
#ifdef DEBUG_EVAL
if ( integer ) {
Log_Write( "$eval result: %d", *intvalue );
} else { Log_Write( "$eval result: %f", *floatvalue );}
#endif //DEBUG_EVAL
//
return qtrue;
} //end of the function PC_DollarEvaluate
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_Directive_elif( source_t *source ) {
signed long int value;
int type, skip;
PC_PopIndent( source, &type, &skip );
if ( !type || type == INDENT_ELSE ) {
SourceError( source, "misplaced #elif" );
return qfalse;
} //end if
if ( !PC_Evaluate( source, &value, NULL, qtrue ) ) {
return qfalse;
}
skip = ( value == 0 );
PC_PushIndent( source, INDENT_ELIF, skip );
return qtrue;
} //end of the function PC_Directive_elif
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_Directive_if( source_t *source ) {
signed long int value;
int skip;
if ( !PC_Evaluate( source, &value, NULL, qtrue ) ) {
return qfalse;
}
skip = ( value == 0 );
PC_PushIndent( source, INDENT_IF, skip );
return qtrue;
} //end of the function PC_Directive
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_Directive_line( source_t *source ) {
SourceError( source, "#line directive not supported" );
return qfalse;
} //end of the function PC_Directive_line
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_Directive_error( source_t *source ) {
token_t token;
strcpy( token.string, "" );
PC_ReadSourceToken( source, &token );
SourceError( source, "#error directive: %s", token.string );
return qfalse;
} //end of the function PC_Directive_error
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_Directive_pragma( source_t *source ) {
token_t token;
SourceWarning( source, "#pragma directive not supported" );
while ( PC_ReadLine( source, &token ) ) ;
return qtrue;
} //end of the function PC_Directive_pragma
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void UnreadSignToken( source_t *source ) {
token_t token;
token.line = source->scriptstack->line;
token.whitespace_p = source->scriptstack->script_p;
token.endwhitespace_p = source->scriptstack->script_p;
token.linescrossed = 0;
strcpy( token.string, "-" );
token.type = TT_PUNCTUATION;
token.subtype = P_SUB;
PC_UnreadSourceToken( source, &token );
} //end of the function UnreadSignToken
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_Directive_eval( source_t *source ) {
signed long int value;
token_t token;
if ( !PC_Evaluate( source, &value, NULL, qtrue ) ) {
return qfalse;
}
//
token.line = source->scriptstack->line;
token.whitespace_p = source->scriptstack->script_p;
token.endwhitespace_p = source->scriptstack->script_p;
token.linescrossed = 0;
sprintf( token.string, "%ld", labs( value ) );
token.type = TT_NUMBER;
token.subtype = TT_INTEGER | TT_LONG | TT_DECIMAL;
PC_UnreadSourceToken( source, &token );
if ( value < 0 ) {
UnreadSignToken( source );
}
return qtrue;
} //end of the function PC_Directive_eval
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_Directive_evalfloat( source_t *source ) {
float value;
token_t token;
if ( !PC_Evaluate( source, NULL, &value, qfalse ) ) {
return qfalse;
}
token.line = source->scriptstack->line;
token.whitespace_p = source->scriptstack->script_p;
token.endwhitespace_p = source->scriptstack->script_p;
token.linescrossed = 0;
sprintf( token.string, "%1.2f", fabs( value ) );
token.type = TT_NUMBER;
token.subtype = TT_FLOAT | TT_LONG | TT_DECIMAL;
PC_UnreadSourceToken( source, &token );
if ( value < 0 ) {
UnreadSignToken( source );
}
return qtrue;
} //end of the function PC_Directive_evalfloat
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
directive_t directives[20] =
{
{"if", PC_Directive_if},
{"ifdef", PC_Directive_ifdef},
{"ifndef", PC_Directive_ifndef},
{"elif", PC_Directive_elif},
{"else", PC_Directive_else},
{"endif", PC_Directive_endif},
{"include", PC_Directive_include},
{"define", PC_Directive_define},
{"undef", PC_Directive_undef},
{"line", PC_Directive_line},
{"error", PC_Directive_error},
{"pragma", PC_Directive_pragma},
{"eval", PC_Directive_eval},
{"evalfloat", PC_Directive_evalfloat},
{NULL, NULL}
};
int PC_ReadDirective( source_t *source ) {
token_t token;
int i;
//read the directive name
if ( !PC_ReadSourceToken( source, &token ) ) {
SourceError( source, "found # without name" );
return qfalse;
} //end if
//directive name must be on the same line
if ( token.linescrossed > 0 ) {
PC_UnreadSourceToken( source, &token );
SourceError( source, "found # at end of line" );
return qfalse;
} //end if
//if if is a name
if ( token.type == TT_NAME ) {
//find the precompiler directive
for ( i = 0; directives[i].name; i++ )
{
if ( !strcmp( directives[i].name, token.string ) ) {
return directives[i].func( source );
} //end if
} //end for
} //end if
SourceError( source, "unknown precompiler directive %s", token.string );
return qfalse;
} //end of the function PC_ReadDirective
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_DollarDirective_evalint( source_t *source ) {
signed long int value;
token_t token;
if ( !PC_DollarEvaluate( source, &value, NULL, qtrue ) ) {
return qfalse;
}
//
token.line = source->scriptstack->line;
token.whitespace_p = source->scriptstack->script_p;
token.endwhitespace_p = source->scriptstack->script_p;
token.linescrossed = 0;
sprintf( token.string, "%ld", labs( value ) );
token.type = TT_NUMBER;
token.subtype = TT_INTEGER | TT_LONG | TT_DECIMAL;
#ifdef NUMBERVALUE
token.intvalue = labs(value);
token.floatvalue = token.intvalue;
#endif //NUMBERVALUE
PC_UnreadSourceToken( source, &token );
if ( value < 0 ) {
UnreadSignToken( source );
}
return qtrue;
} //end of the function PC_DollarDirective_evalint
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_DollarDirective_evalfloat( source_t *source ) {
float value;
token_t token;
if ( !PC_DollarEvaluate( source, NULL, &value, qfalse ) ) {
return qfalse;
}
token.line = source->scriptstack->line;
token.whitespace_p = source->scriptstack->script_p;
token.endwhitespace_p = source->scriptstack->script_p;
token.linescrossed = 0;
sprintf( token.string, "%1.2f", fabs( value ) );
token.type = TT_NUMBER;
token.subtype = TT_FLOAT | TT_LONG | TT_DECIMAL;
#ifdef NUMBERVALUE
token.floatvalue = fabs(value);
token.intvalue = (unsigned long) token.floatvalue;
#endif //NUMBERVALUE
PC_UnreadSourceToken( source, &token );
if ( value < 0 ) {
UnreadSignToken( source );
}
return qtrue;
} //end of the function PC_DollarDirective_evalfloat
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
directive_t dollardirectives[20] =
{
{"evalint", PC_DollarDirective_evalint},
{"evalfloat", PC_DollarDirective_evalfloat},
{NULL, NULL}
};
int PC_ReadDollarDirective( source_t *source ) {
token_t token;
int i;
//read the directive name
if ( !PC_ReadSourceToken( source, &token ) ) {
SourceError( source, "found $ without name" );
return qfalse;
} //end if
//directive name must be on the same line
if ( token.linescrossed > 0 ) {
PC_UnreadSourceToken( source, &token );
SourceError( source, "found $ at end of line" );
return qfalse;
} //end if
//if if is a name
if ( token.type == TT_NAME ) {
//find the precompiler directive
for ( i = 0; dollardirectives[i].name; i++ )
{
if ( !strcmp( dollardirectives[i].name, token.string ) ) {
return dollardirectives[i].func( source );
} //end if
} //end for
} //end if
PC_UnreadSourceToken( source, &token );
SourceError( source, "unknown precompiler directive %s", token.string );
return qfalse;
} //end of the function PC_ReadDirective
#ifdef QUAKEC
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int BuiltinFunction( source_t *source ) {
token_t token;
if ( !PC_ReadSourceToken( source, &token ) ) {
return qfalse;
}
if ( token.type == TT_NUMBER ) {
PC_UnreadSourceToken( source, &token );
return qtrue;
} //end if
else
{
PC_UnreadSourceToken( source, &token );
return qfalse;
} //end else
} //end of the function BuiltinFunction
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int QuakeCMacro( source_t *source ) {
int i;
token_t token;
if ( !PC_ReadSourceToken( source, &token ) ) {
return qtrue;
}
if ( token.type != TT_NAME ) {
PC_UnreadSourceToken( source, &token );
return qtrue;
} //end if
//find the precompiler directive
for ( i = 0; dollardirectives[i].name; i++ )
{
if ( !strcmp( dollardirectives[i].name, token.string ) ) {
PC_UnreadSourceToken( source, &token );
return qfalse;
} //end if
} //end for
PC_UnreadSourceToken( source, &token );
return qtrue;
} //end of the function QuakeCMacro
#endif //QUAKEC
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_ReadToken( source_t *source, token_t *token ) {
define_t *define;
while ( 1 )
{
if ( !PC_ReadSourceToken( source, token ) ) {
return qfalse;
}
//check for precompiler directives
if ( token->type == TT_PUNCTUATION && *token->string == '#' ) {
#ifdef QUAKEC
if ( !BuiltinFunction( source ) )
#endif //QUAKC
{
//read the precompiler directive
if ( !PC_ReadDirective( source ) ) {
return qfalse;
}
continue;
} //end if
} //end if
if ( token->type == TT_PUNCTUATION && *token->string == '$' ) {
#ifdef QUAKEC
if ( !QuakeCMacro( source ) )
#endif //QUAKEC
{
//read the precompiler directive
if ( !PC_ReadDollarDirective( source ) ) {
return qfalse;
}
continue;
} //end if
} //end if
// recursively concatenate strings that are behind each other still resolving defines
if ( token->type == TT_STRING ) {
token_t newtoken;
if ( PC_ReadToken( source, &newtoken ) ) {
if ( newtoken.type == TT_STRING ) {
token->string[strlen( token->string ) - 1] = '\0';
if ( strlen( token->string ) + strlen( newtoken.string + 1 ) + 1 >= MAX_TOKEN ) {
SourceError( source, "string longer than MAX_TOKEN %d", MAX_TOKEN );
return qfalse;
}
strcat( token->string, newtoken.string + 1 );
} else
{
PC_UnreadToken( source, &newtoken );
}
}
} //end if
//if skipping source because of conditional compilation
if ( source->skip ) {
continue;
}
//if the token is a name
if ( token->type == TT_NAME ) {
//check if the name is a define macro
#if DEFINEHASHING
define = PC_FindHashedDefine( source->definehash, token->string );
#else
define = PC_FindDefine( source->defines, token->string );
#endif //DEFINEHASHING
//if it is a define macro
if ( define ) {
//expand the defined macro
if ( !PC_ExpandDefineIntoSource( source, token, define ) ) {
return qfalse;
}
continue;
} //end if
} //end if
//copy token for unreading
memcpy( &source->token, token, sizeof( token_t ) );
//found a token
return qtrue;
} //end while
} //end of the function PC_ReadToken
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_ExpectTokenString( source_t *source, char *string ) {
token_t token;
if ( !PC_ReadToken( source, &token ) ) {
SourceError( source, "couldn't find expected %s", string );
return qfalse;
} //end if
if ( strcmp( token.string, string ) ) {
SourceError( source, "expected %s, found %s", string, token.string );
return qfalse;
} //end if
return qtrue;
} //end of the function PC_ExpectTokenString
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_ExpectTokenType( source_t *source, int type, int subtype, token_t *token ) {
char str[MAX_TOKEN];
if ( !PC_ReadToken( source, token ) ) {
SourceError( source, "couldn't read expected token" );
return qfalse;
} //end if
if ( token->type != type ) {
strcpy( str, "" );
if ( type == TT_STRING ) {
strcpy( str, "string" );
}
if ( type == TT_LITERAL ) {
strcpy( str, "literal" );
}
if ( type == TT_NUMBER ) {
strcpy( str, "number" );
}
if ( type == TT_NAME ) {
strcpy( str, "name" );
}
if ( type == TT_PUNCTUATION ) {
strcpy( str, "punctuation" );
}
SourceError( source, "expected a %s, found %s", str, token->string );
return qfalse;
} //end if
if ( token->type == TT_NUMBER ) {
if ( ( token->subtype & subtype ) != subtype ) {
strcpy(str, "");
if ( subtype & TT_DECIMAL ) {
strcpy( str, "decimal" );
}
if ( subtype & TT_HEX ) {
strcpy( str, "hex" );
}
if ( subtype & TT_OCTAL ) {
strcpy( str, "octal" );
}
if ( subtype & TT_BINARY ) {
strcpy( str, "binary" );
}
if ( subtype & TT_LONG ) {
strcat( str, " long" );
}
if ( subtype & TT_UNSIGNED ) {
strcat( str, " unsigned" );
}
if ( subtype & TT_FLOAT ) {
strcat( str, " float" );
}
if ( subtype & TT_INTEGER ) {
strcat( str, " integer" );
}
SourceError( source, "expected %s, found %s", str, token->string );
return qfalse;
} //end if
} //end if
else if ( token->type == TT_PUNCTUATION ) {
if ( token->subtype != subtype ) {
SourceError( source, "found %s", token->string );
return qfalse;
} //end if
} //end else if
return qtrue;
} //end of the function PC_ExpectTokenType
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_ExpectAnyToken( source_t *source, token_t *token ) {
if ( !PC_ReadToken( source, token ) ) {
SourceError( source, "couldn't read expected token" );
return qfalse;
} //end if
else
{
return qtrue;
} //end else
} //end of the function PC_ExpectAnyToken
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_CheckTokenString( source_t *source, char *string ) {
token_t tok;
if ( !PC_ReadToken( source, &tok ) ) {
return qfalse;
}
//if the token is available
if ( !strcmp( tok.string, string ) ) {
return qtrue;
}
//
PC_UnreadSourceToken( source, &tok );
return qfalse;
} //end of the function PC_CheckTokenString
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_CheckTokenType( source_t *source, int type, int subtype, token_t *token ) {
token_t tok;
if ( !PC_ReadToken( source, &tok ) ) {
return qfalse;
}
//if the type matches
if ( tok.type == type &&
( tok.subtype & subtype ) == subtype ) {
memcpy( token, &tok, sizeof( token_t ) );
return qtrue;
} //end if
//
PC_UnreadSourceToken( source, &tok );
return qfalse;
} //end of the function PC_CheckTokenType
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_SkipUntilString( source_t *source, char *string ) {
token_t token;
while ( PC_ReadToken( source, &token ) )
{
if ( !strcmp( token.string, string ) ) {
return qtrue;
}
} //end while
return qfalse;
} //end of the function PC_SkipUntilString
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_UnreadLastToken( source_t *source ) {
PC_UnreadSourceToken( source, &source->token );
} //end of the function PC_UnreadLastToken
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_UnreadToken( source_t *source, token_t *token ) {
PC_UnreadSourceToken( source, token );
} //end of the function PC_UnreadToken
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_SetIncludePath( source_t *source, char *path ) {
size_t len;
Q_strncpyz( source->includepath, path, sizeof( source->includepath ) - 1 );
len = strlen(source->includepath);
//add trailing path seperator
if (len > 0 && source->includepath[len-1] != '\\' &&
source->includepath[len-1] != '/')
{
strcat( source->includepath, PATHSEPERATOR_STR );
} //end if
} //end of the function PC_SetIncludePath
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_SetPunctuations( source_t *source, punctuation_t *p ) {
source->punctuations = p;
} //end of the function PC_SetPunctuations
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
source_t *LoadSourceFile( const char *filename ) {
source_t *source;
script_t *script;
PC_InitTokenHeap();
script = LoadScriptFile( filename );
if ( !script ) {
return NULL;
}
script->next = NULL;
source = (source_t *) GetMemory( sizeof( source_t ) );
memset( source, 0, sizeof( source_t ) );
Q_strncpyz( source->filename, filename, sizeof( source->filename ) );
source->scriptstack = script;
source->tokens = NULL;
source->defines = NULL;
source->indentstack = NULL;
source->skip = 0;
#if DEFINEHASHING
source->definehash = GetClearedMemory( DEFINEHASHSIZE * sizeof( define_t * ) );
#endif //DEFINEHASHING
PC_AddGlobalDefinesToSource( source );
return source;
} //end of the function LoadSourceFile
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
source_t *LoadSourceMemory( char *ptr, int length, char *name ) {
source_t *source;
script_t *script;
PC_InitTokenHeap();
script = LoadScriptMemory( ptr, length, name );
if ( !script ) {
return NULL;
}
script->next = NULL;
source = (source_t *) GetMemory( sizeof( source_t ) );
memset( source, 0, sizeof( source_t ) );
Q_strncpyz( source->filename, name, sizeof( source->filename ) );
source->scriptstack = script;
source->tokens = NULL;
source->defines = NULL;
source->indentstack = NULL;
source->skip = 0;
#if DEFINEHASHING
source->definehash = GetClearedMemory( DEFINEHASHSIZE * sizeof( define_t * ) );
#endif //DEFINEHASHING
PC_AddGlobalDefinesToSource( source );
return source;
} //end of the function LoadSourceMemory
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void FreeSource( source_t *source ) {
script_t *script;
token_t *token;
define_t *define;
indent_t *indent;
int i;
//PC_PrintDefineHashTable(source->definehash);
//free all the scripts
while ( source->scriptstack )
{
script = source->scriptstack;
source->scriptstack = source->scriptstack->next;
FreeScript( script );
} //end for
//free all the tokens
while ( source->tokens )
{
token = source->tokens;
source->tokens = source->tokens->next;
PC_FreeToken( token );
} //end for
#if DEFINEHASHING
for ( i = 0; i < DEFINEHASHSIZE; i++ )
{
while ( source->definehash[i] )
{
define = source->definehash[i];
source->definehash[i] = source->definehash[i]->hashnext;
PC_FreeDefine( define );
} //end while
} //end for
#else //DEFINEHASHING
//free all defines
while ( source->defines )
{
define = source->defines;
source->defines = source->defines->next;
PC_FreeDefine( define );
} //end for
#endif //DEFINEHASHING
//free all indents
while ( source->indentstack )
{
indent = source->indentstack;
source->indentstack = source->indentstack->next;
FreeMemory( indent );
} //end for
#if DEFINEHASHING
//
if ( source->definehash ) {
FreeMemory( source->definehash );
}
#endif //DEFINEHASHING
//free the source itself
FreeMemory( source );
} //end of the function FreeSource
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
#define MAX_SOURCEFILES 64
source_t *sourceFiles[MAX_SOURCEFILES];
int PC_LoadSourceHandle( const char *filename ) {
source_t *source;
int i;
for ( i = 1; i < MAX_SOURCEFILES; i++ )
{
if ( !sourceFiles[i] ) {
break;
}
} //end for
if ( i >= MAX_SOURCEFILES ) {
return 0;
}
PS_SetBaseFolder( "" );
source = LoadSourceFile( filename );
if ( !source ) {
return 0;
}
sourceFiles[i] = source;
return i;
} //end of the function PC_LoadSourceHandle
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_FreeSourceHandle( int handle ) {
if ( handle < 1 || handle >= MAX_SOURCEFILES ) {
return qfalse;
}
if ( !sourceFiles[handle] ) {
return qfalse;
}
FreeSource( sourceFiles[handle] );
sourceFiles[handle] = NULL;
return qtrue;
} //end of the function PC_FreeSourceHandle
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_ReadTokenHandle( int handle, pc_token_t *pc_token ) {
token_t token;
int ret;
if ( handle < 1 || handle >= MAX_SOURCEFILES ) {
return 0;
}
if ( !sourceFiles[handle] ) {
return 0;
}
ret = PC_ReadToken( sourceFiles[handle], &token );
strcpy( pc_token->string, token.string );
pc_token->type = token.type;
pc_token->subtype = token.subtype;
pc_token->intvalue = token.intvalue;
pc_token->floatvalue = token.floatvalue;
if ( pc_token->type == TT_STRING ) {
StripDoubleQuotes( pc_token->string );
}
return ret;
} //end of the function PC_ReadTokenHandle
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
int PC_SourceFileAndLine( int handle, char *filename, int *line ) {
if ( handle < 1 || handle >= MAX_SOURCEFILES ) {
return qfalse;
}
if ( !sourceFiles[handle] ) {
return qfalse;
}
strcpy( filename, sourceFiles[handle]->filename );
if ( sourceFiles[handle]->scriptstack ) {
*line = sourceFiles[handle]->scriptstack->line;
} else {
*line = 0;
}
return qtrue;
} //end of the function PC_SourceFileAndLine
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_SetBaseFolder( char *path ) {
PS_SetBaseFolder( path );
} //end of the function PC_SetBaseFolder
//============================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//============================================================================
void PC_CheckOpenSourceHandles( void ) {
int i;
for ( i = 1; i < MAX_SOURCEFILES; i++ )
{
if ( sourceFiles[i] ) {
#ifdef BOTLIB
botimport.Print( PRT_ERROR, "file %s still open in precompiler\n", sourceFiles[i]->scriptstack->filename );
#endif //BOTLIB
} //end if
} //end for
} //end of the function PC_CheckOpenSourceHandles
|