Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[e90b2f]: src / lsp / format.lsp Maximize Restore History

Download this file

format.lsp    2919 lines (2693 with data), 97.7 kB

   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
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: SYSTEM -*-
;;;;
;;; -*- Package: FORMAT -*-
;;;
;;; **********************************************************************
;;; This code was written as part of the CMU Common Lisp project at
;;; Carnegie Mellon University, and has been placed in the public domain.
;;;
;;;
;;; **********************************************************************
;;;
;;; Functions to implement FORMAT and FORMATTER for CMU Common Lisp.
;;;
;;; Written by William Lott, with lots of stuff stolen from the previous
;;; version by David Adam and later rewritten by Bill Maddox.
;;;
(in-package "SYS")
(push :formatter *features*)
;;;; Float printing.
;;;
;;; Written by Bill Maddox
;;;
;;;
;;;
;;; FLONUM-TO-STRING (and its subsidiary function FLOAT-STRING) does most of
;;; the work for all printing of floating point numbers in the printer and in
;;; FORMAT. It converts a floating point number to a string in a free or
;;; fixed format with no exponent. The interpretation of the arguments is as
;;; follows:
;;;
;;; X - The floating point number to convert, which must not be
;;; negative.
;;; WIDTH - The preferred field width, used to determine the number
;;; of fraction digits to produce if the FDIGITS parameter
;;; is unspecified or NIL. If the non-fraction digits and the
;;; decimal point alone exceed this width, no fraction digits
;;; will be produced unless a non-NIL value of FDIGITS has been
;;; specified. Field overflow is not considerd an error at this
;;; level.
;;; FDIGITS - The number of fractional digits to produce. Insignificant
;;; trailing zeroes may be introduced as needed. May be
;;; unspecified or NIL, in which case as many digits as possible
;;; are generated, subject to the constraint that there are no
;;; trailing zeroes.
;;; SCALE - If this parameter is specified or non-NIL, then the number
;;; printed is (* x (expt 10 scale)). This scaling is exact,
;;; and cannot lose precision.
;;; FMIN - This parameter, if specified or non-NIL, is the minimum
;;; number of fraction digits which will be produced, regardless
;;; of the value of WIDTH or FDIGITS. This feature is used by
;;; the ~E format directive to prevent complete loss of
;;; significance in the printed value due to a bogus choice of
;;; scale factor.
;;;
;;; Most of the optional arguments are for the benefit for FORMAT and are not
;;; used by the printer.
;;;
;;; Returns:
;;; (VALUES DIGIT-STRING DIGIT-LENGTH LEADING-POINT TRAILING-POINT DECPNT)
;;; where the results have the following interpretation:
;;;
;;; DIGIT-STRING - The decimal representation of X, with decimal point.
;;; DIGIT-LENGTH - The length of the string DIGIT-STRING.
;;; LEADING-POINT - True if the first character of DIGIT-STRING is the
;;; decimal point.
;;; TRAILING-POINT - True if the last character of DIGIT-STRING is the
;;; decimal point.
;;; POINT-POS - The position of the digit preceding the decimal
;;; point. Zero indicates point before first digit.
;;;
;;; NOTE: FLONUM-TO-STRING goes to a lot of trouble to guarantee accuracy.
;;; Specifically, the decimal number printed is the closest possible
;;; approximation to the true value of the binary number to be printed from
;;; among all decimal representations with the same number of digits. In
;;; free-format output, i.e. with the number of digits unconstrained, it is
;;; guaranteed that all the information is preserved, so that a properly-
;;; rounding reader can reconstruct the original binary number, bit-for-bit,
;;; from its printed decimal representation. Furthermore, only as many digits
;;; as necessary to satisfy this condition will be printed.
;;;
;;;
;;; FLOAT-STRING actually generates the digits for positive numbers. The
;;; algorithm is essentially that of algorithm Dragon4 in "How to Print
;;; Floating-Point Numbers Accurately" by Steele and White. The current
;;; (draft) version of this paper may be found in [CMUC]<steele>tradix.press.
;;; DO NOT EVEN THINK OF ATTEMPTING TO UNDERSTAND THIS CODE WITHOUT READING
;;; THE PAPER!
(defparameter *digits* "0123456789")
(defun flonum-to-string (x &optional width fdigits scale fmin)
(declare (type float x))
;; FIXME: I think only FORMAT-DOLLARS calls FLONUM-TO-STRING with
;; possibly-negative X.
(setf x (abs x))
(cond ((zerop x)
;; Zero is a special case which FLOAT-STRING cannot handle.
(if fdigits
(let ((s (make-string (1+ fdigits) :initial-element #\0)))
(setf (schar s 0) #\.)
(values s (length s) t (zerop fdigits) 0))
(values "." 1 t t 0)))
(t
(multiple-value-bind (e string)
(if fdigits
(float-to-digits nil x
(min (- (+ fdigits (or scale 0)))
(- (or fmin 0)))
nil)
(if (and width (> width 1))
(let ((w (multiple-value-list
(float-to-digits nil x
(max 1
(+ (1- width)
(if (and scale (minusp scale))
scale 0)))
t)))
(f (multiple-value-list
(float-to-digits nil x
(- (+ (or fmin 0)
(if scale scale 0)))
nil))))
(cond
((>= (length (cadr w)) (length (cadr f)))
(values-list w))
(t (values-list f))))
(float-to-digits nil x nil nil)))
(let ((e (+ e (or scale 0)))
(stream (make-string-output-stream)))
(if (plusp e)
(progn
(write-string string stream :end (min (length string)
e))
(dotimes (i (- e (length string)))
(write-char #\0 stream))
(write-char #\. stream)
(write-string string stream :start (min (length
string) e))
(when fdigits
(dotimes (i (- fdigits
(- (length string)
(min (length string) e))))
(write-char #\0 stream))))
(progn
(write-string "." stream)
(dotimes (i (- e))
(write-char #\0 stream))
(write-string string stream)
(when fdigits
(dotimes (i (+ fdigits e (- (length string))))
(write-char #\0 stream)))))
(let ((string (get-output-stream-string stream)))
(values string (length string)
(char= (char string 0) #\.)
(char= (char string (1- (length string))) #\.)
(position #\. string))))))))
;;; SCALE-EXPONENT -- Internal
;;;
;;; Given a non-negative floating point number, SCALE-EXPONENT returns a new
;;; floating point number Z in the range (0.1, 1.0] and an exponent E such
;;; that Z * 10^E is (approximately) equal to the original number. There may
;;; be some loss of precision due the floating point representation. The
;;; scaling is always done with long float arithmetic, which helps printing of
;;; lesser precisions as well as avoiding generic arithmetic.
;;;
(defun scale-exponent (original-x)
(declare (optimize (debug 0) (safety 0)))
(let* ((x (coerce original-x 'long-float))
(delta 0))
(declare (long-float x)
(fixnum delta))
(multiple-value-bind (sig exponent)
(decode-float x)
(declare (ignore sig)
(fixnum exponent)
(long-float sig))
(when (zerop x)
(return-from scale-exponent (values (float 0.0l0 original-x) 1)))
;; When computing our initial scale factor using EXPT, we pull out part of
;; the computation to avoid over/under flow. When denormalized, we must pull
;; out a large factor, since there is more negative exponent range than
;; positive range.
(when (and (minusp exponent)
(< least-negative-normalized-long-float x
least-positive-normalized-long-float))
#+long-float
(setf x (* x 1.0l18) delta -18)
#-long-float
(setf x (* x 1.0l16) delta -16))
;; We find the appropriate factor that keeps the output within (0.1,1]
;; Note that we have to compute the exponential _every_ _time_ in the loop
;; because multiplying just by 10.0l0 every time would lead to a greater
;; loss of precission.
(loop with ex of-type fixnum
= (round (* exponent #.(log 2l0 10)))
for y of-type long-float
= (if (minusp ex)
(* x (the long-float (expt 10.0l0 (- ex))))
(/ x (the long-float (expt 10.0l0 ex))))
do (cond ((<= y 0.1l0)
(decf ex))
((> y 1.0l0)
(incf ex))
(t
(return (values y (the fixnum (+ delta ex))))))))))
#+(or)
(defun scale-exponent (original-x)
(let* ((x (coerce original-x 'long-float)))
(multiple-value-bind (sig exponent)
(decode-float x)
(declare (ignore sig))
(if (= x 0.0l0)
(values (float 0.0l0 original-x) 1)
(let* ((ex (round (* exponent (log 2l0 10))))
(x (if (minusp ex)
(if #-ecl(float-denormalized-p x)
#+ecl(< least-negative-normalized-long-float
x
least-positive-normalized-long-float)
#-long-float
(* x 1.0l16 (expt 10.0l0 (- (- ex) 16)))
#+long-float
(* x 1.0l18 (expt 10.0l0 (- (- ex) 18)))
(* x 10.0l0 (expt 10.0l0 (- (- ex) 1))))
(/ x 10.0l0 (expt 10.0l0 (1- ex))))))
(do ((d 10.0l0 (* d 10.0l0))
(y x (/ x d))
(ex ex (1+ ex)))
((< y 1.0l0)
(do ((m 10.0l0 (* m 10.0l0))
(z y (* y m))
(ex ex (1- ex)))
((>= z 0.1l0)
(values (float z original-x) ex))))))))))
(defstruct (format-directive
#-ecl(:print-function %print-format-directive)
#+ecl :named
#+ecl(:type vector))
(string t :type simple-string)
(start 0 :type (and unsigned-byte fixnum))
(end 0 :type (and unsigned-byte fixnum))
(character #\Space :type base-char)
(colonp nil :type (member t nil))
(atsignp nil :type (member t nil))
(params nil :type list))
(deftype format-directive () 'vector)
#-ecl
(defun %print-format-directive (struct stream depth)
(declare (ignore depth))
(print-unreadable-object (struct stream)
(write-string (format-directive-string struct) stream
:start (format-directive-start struct)
:end (format-directive-end struct))))
#+formatter
(defparameter *format-directive-expanders*
(make-array char-code-limit :initial-element nil))
(defparameter *format-directive-interpreters*
(make-array char-code-limit :initial-element nil))
(defparameter *default-format-error-control-string* nil)
(defparameter *default-format-error-offset* nil)
;; If this flag is 1, directives ~W, ~_, ~<...~:>, ~I or ~T were found.
;; If the flag is 2, directive ~<...~:;...~> was found.
;; NIL otherwise.
(defparameter *output-layout-mode* nil)
;; The condition FORMAT-ERROR is found later in conditions.lsp
;;;; TOKENIZE-CONTROL-STRING
(defun tokenize-control-string (string)
(declare (simple-string string)
(si::c-local))
(let ((index 0)
(end (length string))
(result nil))
(loop
(let ((next-directive (or (position #\~ string :start index) end)))
(when (> next-directive index)
(push (subseq string index next-directive) result))
(when (= next-directive end)
(return))
(let ((directive (parse-directive string next-directive)))
(push directive result)
(setf index (format-directive-end directive)))))
(nreverse result)))
(defun parse-directive (string start)
(declare (simple-string string)
(si::c-local))
(let ((posn (1+ start)) (params nil) (colonp nil) (atsignp nil)
(end (length string)))
(flet ((get-char ()
(if (= posn end)
(error 'format-error
:complaint "String ended before directive was found."
:control-string string
:offset start)
(schar string posn))))
(loop
(let ((char (get-char)))
(cond ((or (char<= #\0 char #\9) (char= char #\+) (char= char #\-))
(multiple-value-bind
(param new-posn)
(parse-integer string :start posn :junk-allowed t)
(push (cons posn param) params)
(setf posn new-posn)
(case (get-char)
(#\,)
((#\: #\@)
(decf posn))
(t
(return)))))
((or (char= char #\v) (char= char #\V))
(push (cons posn :arg) params)
(incf posn)
(case (get-char)
(#\,)
((#\: #\@)
(decf posn))
(t
(return))))
((char= char #\#)
(push (cons posn :remaining) params)
(incf posn)
(case (get-char)
(#\,)
((#\: #\@)
(decf posn))
(t
(return))))
((char= char #\')
(incf posn)
(push (cons posn (get-char)) params)
(incf posn)
(case (get-char)
(#\,)
((#\: #\@)
(decf posn))
(t
(return))))
((char= char #\,)
(push (cons posn nil) params))
((char= char #\:)
(if colonp
(error 'format-error
:complaint "Too many colons supplied."
:control-string string
:offset posn)
(setf colonp t)))
((char= char #\@)
(if atsignp
(error 'format-error
:complaint "Too many at-signs supplied."
:control-string string
:offset posn)
(setf atsignp t)))
(t
(when (char= (schar string (1- posn)) #\,)
(push (cons (1- posn) nil) params))
(return))))
(incf posn))
(let ((char (get-char)))
(when (char= char #\/)
(let ((closing-slash (position #\/ string :start (1+ posn))))
(if closing-slash
(setf posn closing-slash)
(error 'format-error
:complaint "No matching closing slash."
:control-string string
:offset posn))))
(make-format-directive
:string string :start start :end (1+ posn)
:character (char-upcase char)
:colonp colonp :atsignp atsignp
:params (nreverse params))))))
;;;; Specials used to communicate information.
;;; *UP-UP-AND-OUT-ALLOWED* -- internal.
;;;
;;; Used both by the expansion stuff and the interpreter stuff. When it is
;;; non-NIL, up-up-and-out (~:^) is allowed. Otherwise, ~:^ isn't allowed.
;;;
(defparameter *up-up-and-out-allowed* nil)
;;; *LOGICAL-BLOCK-POPPER* -- internal.
;;;
;;; Used by the interpreter stuff. When it non-NIL, its a function that will
;;; invoke PPRINT-POP in the right lexical environemnt.
;;;
(defparameter *logical-block-popper* nil)
;;; *EXPANDER-NEXT-ARG-MACRO* -- internal.
;;;
;;; Used by the expander stuff. This is bindable so that ~<...~:>
;;; can change it.
;;;
#+formatter
(defparameter *expander-next-arg-macro* 'expander-next-arg)
;;; *ONLY-SIMPLE-ARGS* -- internal.
;;;
;;; Used by the expander stuff. Initially starts as T, and gets set to NIL
;;; if someone needs to do something strange with the arg list (like use
;;; the rest, or something).
;;;
(defvar *only-simple-args*)
;;; *ORIG-ARGS-AVAILABLE* -- internal.
;;;
;;; Used by the expander stuff. We do an initial pass with this as NIL.
;;; If someone doesn't like this, they (throw 'need-orig-args nil) and we try
;;; again with it bound to T. If this is T, we don't try to do anything
;;; fancy with args.
;;;
(defparameter *orig-args-available* nil)
;;; *SIMPLE-ARGS* -- internal.
;;;
;;; Used by the expander stuff. List of (symbol . offset) for simple args.
;;;
(defvar *simple-args*)
;;;; FORMAT
#-ecl
(defun format (destination control-string &rest format-arguments)
"Provides various facilities for formatting output.
CONTROL-STRING contains a string to be output, possibly with embedded
directives, which are flagged with the escape character \"~\". Directives
generally expand into additional text to be output, usually consuming one
or more of the FORMAT-ARGUMENTS in the process. A few useful directives
are:
~A or ~nA Prints one argument as if by PRINC
~S or ~nS Prints one argument as if by PRIN1
~D or ~nD Prints one argument as a decimal integer
~% Does a TERPRI
~& Does a FRESH-LINE
where n is the width of the field in which the object is printed.
DESTINATION controls where the result will go. If DESTINATION is T, then
the output is sent to the standard output stream. If it is NIL, then the
output is returned in a string as the value of the call. Otherwise,
DESTINATION must be a stream to which the output will be sent.
Example: (FORMAT NIL \"The answer is ~D.\" 10) => \"The answer is 10.\"
FORMAT has many additional capabilities not described here. Consult
Section 22.3 (Formatted Output) of the ANSI Common Lisp standard for
details."
(etypecase destination
(null
(with-output-to-string (stream)
(formatter-aux stream control-string format-arguments)))
(string
(with-output-to-string (stream destination)
(formatter-aux stream control-string format-arguments)))
((member t)
(formatter-aux *standard-output* control-string format-arguments)
nil)
(stream
(formatter-aux destination control-string format-arguments)
nil)))
(defun formatter-aux (stream string-or-fun orig-args &optional (args orig-args))
(if (functionp string-or-fun)
(apply string-or-fun stream args)
(catch 'up-and-out
(let* ((string (etypecase string-or-fun
(simple-string
string-or-fun)
(string
(coerce string-or-fun 'simple-string))))
(*output-layout-mode* nil)
(*default-format-error-control-string* string)
(*logical-block-popper* nil))
(interpret-directive-list stream (tokenize-control-string string)
orig-args args)))))
(defun interpret-directive-list (stream directives orig-args args)
(declare (si::c-local))
(if directives
(let ((directive (car directives)))
(etypecase directive
(simple-string
(write-string directive stream)
(interpret-directive-list stream (cdr directives) orig-args args))
(#-ecl format-directive #+ecl vector
(multiple-value-bind
(new-directives new-args)
(let ((function
(svref *format-directive-interpreters*
(char-code (format-directive-character
directive))))
(*default-format-error-offset*
(1- (format-directive-end directive))))
(unless function
(error 'format-error
:complaint "Unknown format directive."))
(multiple-value-bind
(new-directives new-args)
(funcall function stream directive
(cdr directives) orig-args args)
(values new-directives new-args)))
(interpret-directive-list stream new-directives
orig-args new-args)))))
args))
;;;; FORMATTER
#+formatter
(progn
(defmacro formatter (control-string)
`#',(%formatter control-string))
(defun %formatter (control-string)
(declare (si::c-local))
(block nil
(catch 'need-orig-args
(let* ((*simple-args* nil)
(*only-simple-args* t)
(guts (expand-control-string control-string))
(args nil))
(dolist (arg *simple-args*)
(push `(,(car arg)
(error
'format-error
:complaint "Required argument missing"
:control-string ,control-string
:offset ,(cdr arg)))
args))
(return `(lambda (stream &optional ,@args &rest args)
,guts
args))))
(let ((*orig-args-available* t)
(*only-simple-args* nil))
`(lambda (stream &rest orig-args)
(let ((args orig-args))
,(expand-control-string control-string)
args)))))
(defun expand-control-string (string)
(declare (si::c-local))
(let* ((string (etypecase string
(simple-string
string)
(string
(coerce string 'simple-string))))
(*output-layout-mode* nil)
(*default-format-error-control-string* string)
(directives (tokenize-control-string string)))
`(block nil
,@(expand-directive-list directives))))
(defun expand-directive-list (directives)
(declare (si::c-local))
(let ((results nil)
(remaining-directives directives))
(loop
(unless remaining-directives
(return))
(multiple-value-bind
(form new-directives)
(expand-directive (car remaining-directives)
(cdr remaining-directives))
(push form results)
(setf remaining-directives new-directives)))
(reverse results)))
(defun expand-directive (directive more-directives)
(declare (si::c-local))
(etypecase directive
(simple-string
(values `(write-string ,directive stream)
more-directives))
(format-directive
(let ((expander
(aref *format-directive-expanders*
(char-code (format-directive-character directive))))
(*default-format-error-offset*
(1- (format-directive-end directive))))
(if expander
(funcall expander directive more-directives)
(error 'format-error
:complaint "Unknown directive."))))))
(defun expand-next-arg (&optional offset)
(declare (si::c-local))
(if (or *orig-args-available* (not *only-simple-args*))
`(,*expander-next-arg-macro*
,*default-format-error-control-string*
,(or offset *default-format-error-offset*))
(let ((symbol (gensym "FORMAT-ARG-")))
(push (cons symbol (or offset *default-format-error-offset*))
*simple-args*)
symbol)))
(defun need-hairy-args ()
(declare (si::c-local))
(when *only-simple-args*
))
;;;; Format directive definition macros and runtime support.
(defmacro expander-next-arg (string offset)
`(if args
(pop args)
(error 'format-error
:complaint "No more arguments."
:control-string ,string
:offset ,offset)))
(defmacro expander-pprint-next-arg (string offset)
`(progn
(when (null args)
(error 'format-error
:complaint "No more arguments."
:control-string ,string
:offset ,offset))
(pprint-pop)
(pop args)))
);#+formatter
(eval-when (:compile-toplevel :execute)
;;; NEXT-ARG -- internal.
;;;
;;; This macro is used to extract the next argument from the current arg list.
;;; This is the version used by format directive interpreters.
;;;
(defmacro next-arg (&optional offset)
`(progn
(when (null args)
(error 'format-error
:complaint "No more arguments."
,@(when offset
`(:offset ,offset))))
(when *logical-block-popper*
(funcall *logical-block-popper*))
(pop args)))
(defmacro def-complex-format-directive (char lambda-list &body body)
#+formatter
(let* ((name (or (char-name char) (string char)))
(defun-name (intern (concatenate 'string name "-FORMAT-DIRECTIVE-EXPANDER")))
(directive (gensym))
(directives (if lambda-list (car (last lambda-list)) (gensym))))
`(%set-format-directive-expander ,char
(ext::lambda-block ,defun-name (,directive ,directives)
,@(if lambda-list
`((let ,(mapcar #'(lambda (var)
`(,var
(,(intern (concatenate
'string
"FORMAT-DIRECTIVE-"
(symbol-name var))
(symbol-package 'foo))
,directive)))
(butlast lambda-list))
,@body))
`((declare (ignore ,directive ,directives))
,@body))))))
(defmacro def-format-directive (char lambda-list &body body)
#+formatter
(let ((directives (gensym))
(declarations nil)
(body-without-decls body))
(loop
(let ((form (car body-without-decls)))
(unless (and (consp form) (eq (car form) 'declare))
(return))
(push (pop body-without-decls) declarations)))
(setf declarations (reverse declarations))
`(def-complex-format-directive ,char (,@lambda-list ,directives)
,@declarations
(values (progn ,@body-without-decls)
,directives))))
(defmacro expand-bind-defaults (specs params &body body)
(once-only ((params params))
(if specs
(collect ((expander-bindings) (runtime-bindings))
(dolist (spec specs)
(destructuring-bind (var default) spec
(let ((symbol (gensym)))
(expander-bindings
`(,var ',symbol))
(runtime-bindings
`(list ',symbol
(let* ((param-and-offset (pop ,params))
(offset (car param-and-offset))
(param (cdr param-and-offset)))
(case param
(:arg `(or ,(expand-next-arg offset)
,,default))
(:remaining
(setf *only-simple-args* nil)
'(length args))
((nil) ,default)
(t param))))))))
`(let ,(expander-bindings)
`(let ,(list ,@(runtime-bindings))
,@(if ,params
(error 'format-error
:complaint
"Too many parameters, expected no more than ~D"
:arguments (list ,(length specs))
:offset (caar ,params)))
,,@body)))
`(progn
(when ,params
(error 'format-error
:complaint "Too many parameters, expected no more than 0"
:offset (caar ,params)))
,@body))))
(defmacro def-complex-format-interpreter (char lambda-list &body body)
(let ((directive (gensym))
(directives (if lambda-list (car (last lambda-list)) (gensym))))
`(%set-format-directive-interpreter ,char
(lambda (stream ,directive ,directives orig-args args)
(declare (ignorable stream orig-args args))
,@(if lambda-list
`((let ,(mapcar #'(lambda (var)
`(,var
(,(intern (concatenate
'string
"FORMAT-DIRECTIVE-"
(symbol-name var))
(symbol-package 'foo))
,directive)))
(butlast lambda-list))
(values (progn ,@body) args)))
`((declare (ignore ,directive ,directives))
,@body))))))
(defmacro def-format-interpreter (char lambda-list &body body)
(let ((directives (gensym)))
`(def-complex-format-interpreter ,char (,@lambda-list ,directives)
,@body
,directives)))
(defmacro interpret-bind-defaults (specs params &body body)
(once-only ((params params))
(collect ((bindings))
(dolist (spec specs)
(destructuring-bind (var default) spec
(bindings `(,var (let* ((param-and-offset (pop ,params))
(offset (car param-and-offset))
(param (cdr param-and-offset)))
(case param
(:arg (or (next-arg offset) ,default))
(:remaining (length args))
((nil) ,default)
(t param)))))))
`(let* ,(bindings)
(when ,params
(error 'format-error
:complaint
"Too many parameters, expected no more than ~D"
:arguments (list ,(length specs))
:offset (caar ,params)))
,@body))))
); eval-when
#+formatter
(defun %set-format-directive-expander (char fn)
(setf (aref *format-directive-expanders* (char-code (char-upcase char))) fn)
char)
(defun %set-format-directive-interpreter (char fn)
(declare (si::c-local))
(setf (aref *format-directive-interpreters*
(char-code (char-upcase char)))
fn)
char)
(defun find-directive (directives kind stop-at-semi)
(declare (si::c-local))
(if directives
(let ((next (car directives)))
(if (format-directive-p next)
(let ((char (format-directive-character next)))
(if (or (char= kind char)
(and stop-at-semi (char= char #\;)))
(car directives)
(find-directive
(cdr (flet ((after (char)
(member (find-directive (cdr directives)
char
nil)
directives)))
(case char
(#\( (after #\)))
(#\< (after #\>))
(#\[ (after #\]))
(#\{ (after #\}))
(t directives))))
kind stop-at-semi)))
(find-directive (cdr directives) kind stop-at-semi)))))
;;;; Simple outputting noise.
(defun format-write-field (stream string mincol colinc minpad padchar padleft)
(declare (si::c-local))
(unless padleft
(write-string string stream))
(setf minpad (max minpad 0))
(dotimes (i minpad)
(write-char padchar stream))
(and mincol minpad colinc
(do ((chars (+ (length string) minpad) (+ chars colinc)))
((>= chars mincol))
(dotimes (i colinc)
(write-char padchar stream))))
(when padleft
(write-string string stream)))
(defun format-princ (stream arg colonp atsignp mincol colinc minpad padchar)
#-formatter
(declare (si::c-local))
(format-write-field stream
(if (or arg (not colonp))
(princ-to-string arg)
"()")
mincol colinc minpad padchar atsignp))
(def-format-directive #\A (colonp atsignp params)
(if params
(expand-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
(padchar #\space))
params
`(format-princ stream ,(expand-next-arg) ',colonp ',atsignp
,mincol ,colinc ,minpad ,padchar))
`(princ ,(if colonp
`(or ,(expand-next-arg) "()")
(expand-next-arg))
stream)))
(def-format-interpreter #\A (colonp atsignp params)
(if params
(interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
(padchar #\space))
params
(format-princ stream (next-arg) colonp atsignp
mincol colinc minpad padchar))
(princ (if colonp (or (next-arg) "()") (next-arg)) stream)))
(defun format-prin1 (stream arg colonp atsignp mincol colinc minpad padchar)
#-formatter
(declare (si::c-local))
(format-write-field stream
(if (or arg (not colonp))
(prin1-to-string arg)
"()")
mincol colinc minpad padchar atsignp))
(def-format-directive #\S (colonp atsignp params)
(cond (params
(expand-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
(padchar #\space))
params
`(format-prin1 stream ,(expand-next-arg) ,colonp ,atsignp
,mincol ,colinc ,minpad ,padchar)))
(colonp
`(let ((arg ,(expand-next-arg)))
(if arg
(prin1 arg stream)
(princ "()" stream))))
(t
`(prin1 ,(expand-next-arg) stream))))
(def-format-interpreter #\S (colonp atsignp params)
(cond (params
(interpret-bind-defaults ((mincol 0) (colinc 1) (minpad 0)
(padchar #\space))
params
(format-prin1 stream (next-arg) colonp atsignp
mincol colinc minpad padchar)))
(colonp
(let ((arg (next-arg)))
(if arg
(prin1 arg stream)
(princ "()" stream))))
(t
(prin1 (next-arg) stream))))
(def-format-directive #\C (colonp atsignp params)
(expand-bind-defaults () params
(if colonp
`(format-print-named-character ,(expand-next-arg) stream)
(if atsignp
`(prin1 ,(expand-next-arg) stream)
`(write-char ,(expand-next-arg) stream)))))
(def-format-interpreter #\C (colonp atsignp params)
(interpret-bind-defaults () params
(if colonp
(format-print-named-character (next-arg) stream)
(if atsignp
(prin1 (next-arg) stream)
(write-char (next-arg) stream)))))
;;; "printing" as defined in the ANSI CL glossary, which is normative.
(defun char-printing-p (char)
(declare (si::c-local))
(and (not (eql char #\Space))
(graphic-char-p char)))
(defun format-print-named-character (char stream)
(cond ((not (char-printing-p char))
(write-string (char-name char) stream))
(t
(write-char char stream))))
(def-format-directive #\W (colonp atsignp params)
(check-output-layout-mode 1)
(expand-bind-defaults () params
(if (or colonp atsignp)
`(let (,@(when colonp
'((*print-pretty* t)))
,@(when atsignp
'((*print-level* nil)
(*print-length* nil))))
(write-object ,(expand-next-arg) stream))
`(write-object ,(expand-next-arg) stream))))
(def-format-interpreter #\W (colonp atsignp params)
(check-output-layout-mode 1)
(interpret-bind-defaults () params
(let ((*print-pretty* (or colonp *print-pretty*))
(*print-level* (and atsignp *print-level*))
(*print-length* (and atsignp *print-length*)))
(write-object (next-arg) stream))))
;;;; Integer outputting.
;;; FORMAT-PRINT-NUMBER does most of the work for the numeric printing
;;; directives. The parameters are interpreted as defined for ~D.
;;;
(defun format-print-integer (stream number print-commas-p print-sign-p
radix mincol padchar commachar commainterval)
#-formatter
(declare (si::c-local))
(let ((*print-base* radix)
(*print-radix* nil))
(if (integerp number)
(let* ((text (princ-to-string (abs number)))
(commaed (if print-commas-p
(format-add-commas text commachar commainterval)
text))
(signed (cond ((minusp number)
(concatenate 'string "-" commaed))
(print-sign-p
(concatenate 'string "+" commaed))
(t commaed))))
;; colinc = 1, minpad = 0, padleft = t
(format-write-field stream signed mincol 1 0 padchar t))
(princ number stream))))
(defun format-add-commas (string commachar commainterval)
(declare (si::c-local))
(let ((length (length string)))
(multiple-value-bind (commas extra)
(truncate (1- length) commainterval)
(let ((new-string (make-string (+ length commas)))
(first-comma (1+ extra)))
(replace new-string string :end1 first-comma :end2 first-comma)
(do ((src first-comma (+ src commainterval))
(dst first-comma (+ dst commainterval 1)))
((= src length))
(setf (schar new-string dst) commachar)
(replace new-string string :start1 (1+ dst)
:start2 src :end2 (+ src commainterval)))
new-string))))
#+formatter
(defun expand-format-integer (base colonp atsignp params)
(if (or colonp atsignp params)
(expand-bind-defaults
((mincol 0) (padchar #\space) (commachar #\,) (commainterval 3))
params
`(format-print-integer stream ,(expand-next-arg) ,colonp ,atsignp
,base ,mincol ,padchar ,commachar
,commainterval))
`(write ,(expand-next-arg) :stream stream :base ,base :radix nil
:escape nil)))
(eval-when (:compile-toplevel :execute)
(defmacro interpret-format-integer (base)
`(if (or colonp atsignp params)
(interpret-bind-defaults
((mincol 0) (padchar #\space) (commachar #\,) (commainterval 3))
params
(format-print-integer stream (next-arg) colonp atsignp ,base mincol
padchar commachar commainterval))
(write (next-arg) :stream stream :base ,base :radix nil :escape nil)))
)
(def-format-directive #\D (colonp atsignp params)
(expand-format-integer 10 colonp atsignp params))
(def-format-interpreter #\D (colonp atsignp params)
(interpret-format-integer 10))
(def-format-directive #\B (colonp atsignp params)
(expand-format-integer 2 colonp atsignp params))
(def-format-interpreter #\B (colonp atsignp params)
(interpret-format-integer 2))
(def-format-directive #\O (colonp atsignp params)
(expand-format-integer 8 colonp atsignp params))
(def-format-interpreter #\O (colonp atsignp params)
(interpret-format-integer 8))
(def-format-directive #\X (colonp atsignp params)
(expand-format-integer 16 colonp atsignp params))
(def-format-interpreter #\X (colonp atsignp params)
(interpret-format-integer 16))
(def-format-directive #\R (colonp atsignp params)
(expand-bind-defaults
((base nil) (mincol 0) (padchar #\space) (commachar #\,)
(commainterval 3))
params
(let ((n-arg (gensym)))
`(let ((,n-arg ,(expand-next-arg)))
(if ,base
(format-print-integer stream ,n-arg ,colonp ,atsignp
,base ,mincol
,padchar ,commachar ,commainterval)
,(if atsignp
(if colonp
`(format-print-old-roman stream ,n-arg)
`(format-print-roman stream ,n-arg))
(if colonp
`(format-print-ordinal stream ,n-arg)
`(format-print-cardinal stream ,n-arg))))))))
(def-format-interpreter #\R (colonp atsignp params)
(interpret-bind-defaults
((base nil) (mincol 0) (padchar #\space) (commachar #\,)
(commainterval 3))
params
(if base
(format-print-integer stream (next-arg) colonp atsignp base mincol
padchar commachar commainterval)
(if atsignp
(if colonp
(format-print-old-roman stream (next-arg))
(format-print-roman stream (next-arg)))
(if colonp
(format-print-ordinal stream (next-arg))
(format-print-cardinal stream (next-arg)))))))
(defconstant cardinal-ones
#(nil "one" "two" "three" "four" "five" "six" "seven" "eight" "nine"))
(defconstant cardinal-tens
#(nil nil "twenty" "thirty" "forty"
"fifty" "sixty" "seventy" "eighty" "ninety"))
(defconstant cardinal-teens
#("ten" "eleven" "twelve" "thirteen" "fourteen" ;;; RAD
"fifteen" "sixteen" "seventeen" "eighteen" "nineteen"))
(defconstant cardinal-periods
#("" " thousand" " million" " billion" " trillion" " quadrillion"
" quintillion" " sextillion" " septillion" " octillion" " nonillion"
" decillion" " undecillion" " duodecillion" " tredecillion"
" quattuordecillion" " quindecillion" " sexdecillion" " septendecillion"
" octodecillion" " novemdecillion" " vigintillion"))
(defconstant ordinal-ones
#(nil "first" "second" "third" "fourth"
"fifth" "sixth" "seventh" "eighth" "ninth")
"Table of ordinal ones-place digits in English")
(defconstant ordinal-tens
#(nil "tenth" "twentieth" "thirtieth" "fortieth"
"fiftieth" "sixtieth" "seventieth" "eightieth" "ninetieth")
"Table of ordinal tens-place digits in English")
(defun format-print-small-cardinal (stream n)
(declare (si::c-local))
(multiple-value-bind
(hundreds rem) (truncate n 100)
(when (plusp hundreds)
(write-string (svref cardinal-ones hundreds) stream)
(write-string " hundred" stream)
(when (plusp rem)
(write-char #\space stream)))
(when (plusp rem)
(multiple-value-bind (tens ones)
(truncate rem 10)
(cond ((< 1 tens)
(write-string (svref cardinal-tens tens) stream)
(when (plusp ones)
(write-char #\- stream)
(write-string (svref cardinal-ones ones) stream)))
((= tens 1)
(write-string (svref cardinal-teens ones) stream))
((plusp ones)
(write-string (svref cardinal-ones ones) stream)))))))
(defun format-print-cardinal (stream n)
#-formatter
(declare (si::c-local))
(cond ((minusp n)
(write-string "negative " stream)
(format-print-cardinal-aux stream (- n) 0 n))
((zerop n)
(write-string "zero" stream))
(t
(format-print-cardinal-aux stream n 0 n))))
(defun format-print-cardinal-aux (stream n period err)
(declare (si::c-local))
(multiple-value-bind (beyond here) (truncate n 1000)
(unless (<= period 20)
(error "Number too large to print in English: ~:D" err))
(unless (zerop beyond)
(format-print-cardinal-aux stream beyond (1+ period) err))
(unless (zerop here)
(unless (zerop beyond)
(write-char #\space stream))
(format-print-small-cardinal stream here)
(write-string (svref cardinal-periods period) stream))))
(defun format-print-ordinal (stream n)
#-formatter
(declare (si::c-local))
(when (minusp n)
(write-string "negative " stream))
(let ((number (abs n)))
(multiple-value-bind
(top bot) (truncate number 100)
(unless (zerop top)
(format-print-cardinal stream (- number bot)))
(when (and (plusp top) (plusp bot))
(write-char #\space stream))
(multiple-value-bind
(tens ones) (truncate bot 10)
(cond ((= bot 12) (write-string "twelfth" stream))
((= tens 1)
(write-string (svref cardinal-teens ones) stream);;;RAD
(write-string "th" stream))
((and (zerop tens) (plusp ones))
(write-string (svref ordinal-ones ones) stream))
((and (zerop ones)(plusp tens))
(write-string (svref ordinal-tens tens) stream))
((plusp bot)
(write-string (svref cardinal-tens tens) stream)
(write-char #\- stream)
(write-string (svref ordinal-ones ones) stream))
((plusp number)
(write-string "th" stream))
(t
(write-string "zeroth" stream)))))))
;;; Print Roman numerals
(defun format-print-old-roman (stream n)
#-formatter
(declare (si::c-local))
(unless (< 0 n 5000)
(error "Number too large to print in old Roman numerals: ~:D" n))
(do ((char-list '(#\D #\C #\L #\X #\V #\I) (cdr char-list))
(val-list '(500 100 50 10 5 1) (cdr val-list))
(cur-char #\M (car char-list))
(cur-val 1000 (car val-list))
(start n (do ((i start (progn
(write-char cur-char stream)
(- i cur-val))))
((< i cur-val) i))))
((zerop start))))
(defun format-print-roman (stream n)
#-formatter
(declare (si::c-local))
(unless (< 0 n 4000)
(error "Number too large to print in Roman numerals: ~:D" n))
(do ((char-list '(#\D #\C #\L #\X #\V #\I) (cdr char-list))
(val-list '(500 100 50 10 5 1) (cdr val-list))
(sub-chars '(#\C #\X #\X #\I #\I) (cdr sub-chars))
(sub-val '(100 10 10 1 1 0) (cdr sub-val))
(cur-char #\M (car char-list))
(cur-val 1000 (car val-list))
(cur-sub-char #\C (car sub-chars))
(cur-sub-val 100 (car sub-val))
(start n (do ((i start (progn
(write-char cur-char stream)
(- i cur-val))))
((< i cur-val)
(cond ((<= (- cur-val cur-sub-val) i)
(write-char cur-sub-char stream)
(write-char cur-char stream)
(- i (- cur-val cur-sub-val)))
(t i))))))
((zerop start))))
;;;; Plural.
(def-format-directive #\P (colonp atsignp params end)
(expand-bind-defaults () params
(let ((arg (cond
((not colonp)
(expand-next-arg))
(*orig-args-available*
`(if (eq orig-args args)
(error 'format-error
:complaint "No previous argument."
:offset ,(1- end))
(do ((arg-ptr orig-args (cdr arg-ptr)))
((eq (cdr arg-ptr) args)
(car arg-ptr)))))
(*only-simple-args*
(unless *simple-args*
(error 'format-error
:complaint "No previous argument."))
(caar *simple-args*))
(t
(throw 'need-orig-args nil)))))
(if atsignp
`(write-string (if (eql ,arg 1) "y" "ies") stream)
`(unless (eql ,arg 1) (write-char #\s stream))))))
(def-format-interpreter #\P (colonp atsignp params)
(interpret-bind-defaults () params
(let ((arg (if colonp
(if (eq orig-args args)
(error 'format-error
:complaint "No previous argument.")
(do ((arg-ptr orig-args (cdr arg-ptr)))
((eq (cdr arg-ptr) args)
(car arg-ptr))))
(next-arg))))
(if atsignp
(write-string (if (eql arg 1) "y" "ies") stream)
(unless (eql arg 1) (write-char #\s stream))))))
;;;; Floating point noise.
(defun decimal-string (n)
(declare (si::c-local))
(write-to-string n :base 10 :radix nil :escape nil))
(def-format-directive #\F (colonp atsignp params)
(when colonp
(error 'format-error
:complaint
"Cannot specify the colon modifier with this directive."))
(expand-bind-defaults ((w nil) (d nil) (k nil) (ovf nil) (pad #\space)) params
`(format-fixed stream ,(expand-next-arg) ,w ,d ,k ,ovf ,pad ,atsignp)))
(def-format-interpreter #\F (colonp atsignp params)
(when colonp
(error 'format-error
:complaint
"Cannot specify the colon modifier with this directive."))
(interpret-bind-defaults ((w nil) (d nil) (k nil) (ovf nil) (pad #\space))
params
(format-fixed stream (next-arg) w d k ovf pad atsignp)))
(defun format-fixed (stream number w d k ovf pad atsign)
#-formatter
(declare (si::c-local))
(if (numberp number)
(if (floatp number)
(format-fixed-aux stream number w d k ovf pad atsign)
(if (rationalp number)
(format-fixed-aux stream
(coerce number 'single-float)
w d k ovf pad atsign)
(format-write-field stream
(decimal-string number)
w 1 0 #\space t)))
(format-princ stream number nil nil w 1 0 pad)))
;;; We return true if we overflowed, so that ~G can output the overflow char
;;; instead of spaces.
;;;
(defun format-fixed-aux (stream number w d k ovf pad atsign)
(declare (si::c-local))
(cond
((or (not (or w d))
#-ecl
(and (floatp number)
(or (float-infinity-p number)
(float-nan-p number))))
(prin1 number stream)
nil)
(t
(let ((spaceleft w))
(when (and w (or atsign (minusp number))) (decf spaceleft))
(multiple-value-bind
(str len lpoint tpoint)
(sys::flonum-to-string (abs number) spaceleft d k)
;;if caller specifically requested no fraction digits, suppress the
;;optional trailing zero
(when (and d (zerop d)) (setq tpoint nil))
(when w
(decf spaceleft len)
;;optional leading zero
(when lpoint
(if (or (> spaceleft 0) tpoint) ;force at least one digit
(decf spaceleft)
(setq lpoint nil)))
;;optional trailing zero
(when tpoint
(if (> spaceleft 0)
(decf spaceleft)
(setq tpoint nil))))
(cond ((and w (< spaceleft 0) ovf)
;;field width overflow
(dotimes (i w) (write-char ovf stream))
t)
(t
(when w (dotimes (i spaceleft) (write-char pad stream)))
(if (minusp number)
(write-char #\- stream)
(if atsign (write-char #\+ stream)))
(when lpoint (write-char #\0 stream))
(write-string str stream)
(when tpoint (write-char #\0 stream))
nil)))))))
(def-format-directive #\E (colonp atsignp params)
(when colonp
(error 'format-error
:complaint
"Cannot specify the colon modifier with this directive."))
(expand-bind-defaults
((w nil) (d nil) (e nil) (k 1) (ovf nil) (pad #\space) (mark nil))
params
`(format-exponential stream ,(expand-next-arg) ,w ,d ,e ,k ,ovf ,pad ,mark
,atsignp)))
(def-format-interpreter #\E (colonp atsignp params)
(when colonp
(error 'format-error
:complaint
"Cannot specify the colon modifier with this directive."))
(interpret-bind-defaults
((w nil) (d nil) (e nil) (k 1) (ovf nil) (pad #\space) (mark nil))
params
(format-exponential stream (next-arg) w d e k ovf pad mark atsignp)))
(defun format-exponential (stream number w d e k ovf pad marker atsign)
#-formatter
(declare (si::c-local))
(if (numberp number)
(if (floatp number)
(format-exp-aux stream number w d e k ovf pad marker atsign)
(if (rationalp number)
(format-exp-aux stream
(coerce number 'single-float)
w d e k ovf pad marker atsign)
(format-write-field stream
(decimal-string number)
w 1 0 #\space t)))
(format-princ stream number nil nil w 1 0 pad)))
(defun format-exponent-marker (number)
(declare (si::c-local))
(if (typep number *read-default-float-format*)
#\e
(typecase number
(single-float #\f)
(double-float #\d)
(short-float #\s)
(long-float #\l))))
;;;Here we prevent the scale factor from shifting all significance out of
;;;a number to the right. We allow insignificant zeroes to be shifted in
;;;to the left right, athough it is an error to specify k and d such that this
;;;occurs. Perhaps we should detect both these condtions and flag them as
;;;errors. As for now, we let the user get away with it, and merely guarantee
;;;that at least one significant digit will appear.
;;; toy@rtp.ericsson.se: The Hyperspec seems to say that the exponent
;;; marker is always printed. Make it so. Also, the original version
;;; causes errors when printing infinities or NaN's. The Hyperspec is
;;; silent here, so let's just print out infinities and NaN's instead
;;; of causing an error.
(defun format-exp-aux (stream number w d e k ovf pad marker atsign)
(declare (si::c-local))
(if #-ecl
(and (floatp number)
(or (float-infinity-p number)
(float-nan-p number)))
#+ecl nil
(prin1 number stream)
(multiple-value-bind (num expt)
(sys::scale-exponent (abs number))
(let* ((expt (- expt k))
(estr (decimal-string (abs expt)))
(elen (if e (max (length estr) e) (length estr)))
(fdig (if d (if (plusp k) (1+ (- d k)) d) nil))
(fmin (if (minusp k) (- 1 k) nil))
(spaceleft (if w
(- w 2 elen
(if (or atsign (minusp number))
1 0))
nil)))
(if (and w ovf e (> elen e)) ;exponent overflow
(dotimes (i w) (write-char ovf stream))
(multiple-value-bind
(fstr flen lpoint)
(sys::flonum-to-string num spaceleft fdig k fmin)
(when w
(decf spaceleft flen)
(when lpoint
(if (> spaceleft 0)
(decf spaceleft)
(setq lpoint nil))))
(cond ((and w (< spaceleft 0) ovf)
;;significand overflow
(dotimes (i w) (write-char ovf stream)))
(t (when w
(dotimes (i spaceleft) (write-char pad stream)))
(if (minusp number)
(write-char #\- stream)
(if atsign (write-char #\+ stream)))
(when lpoint (write-char #\0 stream))
(write-string fstr stream)
(write-char (if marker
marker
(format-exponent-marker number))
stream)
(write-char (if (minusp expt) #\- #\+) stream)
(when e
;;zero-fill before exponent if necessary
(dotimes (i (- e (length estr)))
(write-char #\0 stream)))
(write-string estr stream)))))))))
(def-format-directive #\G (colonp atsignp params)
(when colonp
(error 'format-error
:complaint
"Cannot specify the colon modifier with this directive."))
(expand-bind-defaults
((w nil) (d nil) (e nil) (k nil) (ovf nil) (pad #\space) (mark nil))
params
`(format-general stream ,(expand-next-arg) ,w ,d ,e ,k ,ovf ,pad ,mark ,atsignp)))
(def-format-interpreter #\G (colonp atsignp params)
(when colonp
(error 'format-error
:complaint
"Cannot specify the colon modifier with this directive."))
(interpret-bind-defaults
((w nil) (d nil) (e nil) (k nil) (ovf nil) (pad #\space) (mark nil))
params
(format-general stream (next-arg) w d e k ovf pad mark atsignp)))
(defun format-general (stream number w d e k ovf pad marker atsign)
#-formatter
(declare (si::c-local))
(if (numberp number)
(if (floatp number)
(format-general-aux stream number w d e k ovf pad marker atsign)
(if (rationalp number)
(format-general-aux stream
(coerce number 'single-float)
w d e k ovf pad marker atsign)
(format-write-field stream
(decimal-string number)
w 1 0 #\space t)))
(format-princ stream number nil nil w 1 0 pad)))
;;; toy@rtp.ericsson.se: Same change as for format-exp-aux.
(defun format-general-aux (stream number w d e k ovf pad marker atsign)
(declare (si::c-local))
(if #-ecl
(and (floatp number)
(or (float-infinity-p number)
(float-nan-p number)))
#+ecl nil
(prin1 number stream)
(multiple-value-bind (ignore n)
(sys::scale-exponent (abs number))
(declare (ignore ignore))
;;Default d if omitted. The procedure is taken directly
;;from the definition given in the manual, and is not
;;very efficient, since we generate the digits twice.
;;Future maintainers are encouraged to improve on this.
(unless d
(multiple-value-bind (str len)
(sys::flonum-to-string (abs number))
(declare (ignore str))
(let ((q (if (= len 1) 1 (1- len))))
(setq d (max q (min n 7))))))
(let* ((ee (if e (+ e 2) 4))
(ww (if w (- w ee) nil))
(dd (- d n)))
(cond ((<= 0 dd d)
(let ((char (if (format-fixed-aux stream number ww dd nil
ovf pad atsign)
ovf
#\space)))
(dotimes (i ee) (write-char char stream))))
(t
(format-exp-aux stream number w d e (or k 1)
ovf pad marker atsign)))))))
(def-format-directive #\$ (colonp atsignp params)
(expand-bind-defaults ((d 2) (n 1) (w 0) (pad #\space)) params
`(format-dollars stream ,(expand-next-arg) ,d ,n ,w ,pad ,colonp
,atsignp)))
(def-format-interpreter #\$ (colonp atsignp params)
(interpret-bind-defaults ((d 2) (n 1) (w 0) (pad #\space)) params
(format-dollars stream (next-arg) d n w pad colonp atsignp)))
(defun format-dollars (stream number d n w pad colon atsign)
#-formatter
(declare (si::c-local))
(if (rationalp number) (setq number (coerce number 'single-float)))
(if (floatp number)
(let* ((signstr (if (minusp number) "-" (if atsign "+" "")))
(signlen (length signstr)))
(multiple-value-bind (str strlen ig2 ig3 pointplace)
(sys::flonum-to-string number nil d nil)
(declare (ignore ig2 ig3))
(when colon (write-string signstr stream))
(dotimes (i (- w signlen (max 0 (- n pointplace)) strlen))
(write-char pad stream))
(unless colon (write-string signstr stream))
(dotimes (i (- n pointplace)) (write-char #\0 stream))
(write-string str stream)))
(format-write-field stream
(decimal-string number)
w 1 0 #\space t)))
;;;; line/page breaks and other stuff like that.
(def-format-directive #\% (colonp atsignp params)
(when (or colonp atsignp)
(error 'format-error
:complaint
"Cannot specify either colon or atsign for this directive."))
(if params
(expand-bind-defaults ((count 1)) params
`(dotimes (i ,count)
(terpri stream)))
'(terpri stream)))
(def-format-interpreter #\% (colonp atsignp params)
(when (or colonp atsignp)
(error 'format-error
:complaint
"Cannot specify either colon or atsign for this directive."))
(interpret-bind-defaults ((count 1)) params
(dotimes (i count)
(terpri stream))))
(def-format-directive #\& (colonp atsignp params)
(when (or colonp atsignp)
(error 'format-error
:complaint
"Cannot specify either colon or atsign for this directive."))
(if params
(expand-bind-defaults ((count 1)) params
`(progn
(fresh-line stream)
(dotimes (i (1- ,count))
(terpri stream))))
'(fresh-line stream)))
(def-format-interpreter #\& (colonp atsignp params)
(when (or colonp atsignp)
(error 'format-error
:complaint
"Cannot specify either colon or atsign for this directive."))
(interpret-bind-defaults ((count 1)) params
(fresh-line stream)
(dotimes (i (1- count))
(terpri stream))))
(def-format-directive #\| (colonp atsignp params)
(when (or colonp atsignp)
(error 'format-error
:complaint
"Cannot specify either colon or atsign for this directive."))
(if params
(expand-bind-defaults ((count 1)) params
`(dotimes (i ,count)
(write-char #\page stream)))
'(write-char #\page stream)))
(def-format-interpreter #\| (colonp atsignp params)
(when (or colonp atsignp)
(error 'format-error
:complaint
"Cannot specify either colon or atsign for this directive."))
(interpret-bind-defaults ((count 1)) params
(dotimes (i count)
(write-char #\page stream))))
(def-format-directive #\~ (colonp atsignp params)
(when (or colonp atsignp)
(error 'format-error
:complaint
"Cannot specify either colon or atsign for this directive."))
(if params
(expand-bind-defaults ((count 1)) params
`(dotimes (i ,count)
(write-char #\~ stream)))
'(write-char #\~ stream)))
(def-format-interpreter #\~ (colonp atsignp params)
(when (or colonp atsignp)
(error 'format-error
:complaint
"Cannot specify either colon or atsign for this directive."))
(interpret-bind-defaults ((count 1)) params
(dotimes (i count)
(write-char #\~ stream))))
(def-complex-format-directive #\newline (colonp atsignp params directives)
(when (and colonp atsignp)
(error 'format-error
:complaint
"Cannot specify both colon and atsign for this directive."))
(values (expand-bind-defaults () params
(if atsignp
'(write-char #\newline stream)
nil))
(if (and (not colonp)
directives
(simple-string-p (car directives)))
(cons (string-left-trim '(#\space #\newline #\tab)
(car directives))
(cdr directives))
directives)))
(def-complex-format-interpreter #\newline (colonp atsignp params directives)
(when (and colonp atsignp)
(error 'format-error
:complaint
"Cannot specify both colon and atsign for this directive."))
(interpret-bind-defaults () params
(when atsignp
(write-char #\newline stream)))
(if (and (not colonp)
directives
(simple-string-p (car directives)))
(cons (string-left-trim '(#\space #\newline #\tab)
(car directives))
(cdr directives))
directives))
(def-complex-format-directive #\return (colonp atsignp params directives)
(when (and colonp atsignp)
(error 'format-error
:complaint
"Cannot specify both colon and atsign for this directive."))
(values (expand-bind-defaults () params
(if atsignp
'(write-char #\newline stream)
nil))
(if (and (not colonp)
directives
(simple-string-p (car directives)))
(cons (string-left-trim '(#\space #\newline #\tab)
(car directives))
(cdr directives))
directives)))
(def-complex-format-interpreter #\return (colonp atsignp params directives)
(when (and colonp atsignp)
(error 'format-error
:complaint
"Cannot specify both colon and atsign for this directive."))
(interpret-bind-defaults () params
(when atsignp
(write-char #\newline stream)))
(if (and (not colonp)
directives
(simple-string-p (car directives)))
(cons (string-left-trim '(#\space #\newline #\tab)
(car directives))
(cdr directives))
directives))
;;;; Tab and simple pretty-printing noise.
(def-format-directive #\T (colonp atsignp params)
(check-output-layout-mode 1)
(if colonp
(expand-bind-defaults ((n 1) (m 1)) params
`(pprint-tab ,(if atsignp :section-relative :section)
,n ,m stream))
(if atsignp
(expand-bind-defaults ((colrel 1) (colinc 1)) params
`(format-relative-tab stream ,colrel ,colinc))
(expand-bind-defaults ((colnum 1) (colinc 1)) params
`(format-absolute-tab stream ,colnum ,colinc)))))
(def-format-interpreter #\T (colonp atsignp params)
(check-output-layout-mode 1)
(if colonp
(interpret-bind-defaults ((n 1) (m 1)) params
(pprint-tab (if atsignp :section-relative :section) n m stream))
(if atsignp
(interpret-bind-defaults ((colrel 1) (colinc 1)) params
(format-relative-tab stream colrel colinc))
(interpret-bind-defaults ((colnum 1) (colinc 1)) params
(format-absolute-tab stream colnum colinc)))))
(defun output-spaces (stream n)
(declare (si::c-local))
(let ((spaces #.(make-string 100 :initial-element #\space)))
(loop
(when (< n (length spaces))
(return))
(write-string spaces stream)
(decf n (length spaces)))
(write-string spaces stream :end n)))
(defun format-relative-tab (stream colrel colinc)
#-formatter
(declare (si::c-local))
(if (#-ecl pp:pretty-stream-p #+ecl sys::pretty-stream-p stream)
(pprint-tab :line-relative colrel colinc stream)
(let* ((cur (#-ecl sys::charpos #+ecl sys::file-column stream))
(spaces (if (and cur (plusp colinc))
(- (* (ceiling (+ cur colrel) colinc) colinc) cur)
colrel)))
(output-spaces stream spaces))))
(defun format-absolute-tab (stream colnum colinc)
#-formatter
(declare (si::c-local))
(if (#-ecl pp:pretty-stream-p #+ecl sys::pretty-stream-p stream)
(pprint-tab :line colnum colinc stream)
(let ((cur (#-ecl sys::charpos #+ecl sys:file-column stream)))
(cond ((null cur)
(write-string " " stream))
((< cur colnum)
(output-spaces stream (- colnum cur)))
(t
(unless (zerop colinc)
(output-spaces stream
(- colinc (rem (- cur colnum) colinc)))))))))
(def-format-directive #\_ (colonp atsignp params)
(check-output-layout-mode 1)
(expand-bind-defaults () params
`(pprint-newline ,(if colonp
(if atsignp
:mandatory
:fill)
(if atsignp
:miser
:linear))
stream)))
(def-format-interpreter #\_ (colonp atsignp params)
(check-output-layout-mode 1)
(interpret-bind-defaults () params
(pprint-newline (if colonp
(if atsignp
:mandatory
:fill)
(if atsignp
:miser
:linear))
stream)))
(def-format-directive #\I (colonp atsignp params)
(check-output-layout-mode 1)
(when atsignp
(error 'format-error
:complaint "Cannot specify the at-sign modifier."))
(expand-bind-defaults ((n 0)) params
`(pprint-indent ,(if colonp :current :block) ,n stream)))
(def-format-interpreter #\I (colonp atsignp params)
(check-output-layout-mode 1)
(when atsignp
(error 'format-error
:complaint "Cannot specify the at-sign modifier."))
(interpret-bind-defaults ((n 0)) params
(pprint-indent (if colonp :current :block) n stream)))
;;;; *
(def-format-directive #\* (colonp atsignp params end)
(if atsignp
(if colonp
(error 'format-error
:complaint "Cannot specify both colon and at-sign.")
(expand-bind-defaults ((posn 0)) params
(unless *orig-args-available*
(throw 'need-orig-args nil))
`(if (<= 0 ,posn (length orig-args))
(setf args (nthcdr ,posn orig-args))
(error 'format-error
:complaint "Index ~D out of bounds. Should have been ~
between 0 and ~D."
:arguments (list ,posn (length orig-args))
:offset ,(1- end)))))
(if colonp
(expand-bind-defaults ((n 1)) params
(unless *orig-args-available*
(throw 'need-orig-args nil))
`(do ((cur-posn 0 (1+ cur-posn))
(arg-ptr orig-args (cdr arg-ptr)))
((eq arg-ptr args)
(let ((new-posn (- cur-posn ,n)))
(if (<= 0 new-posn (length orig-args))
(setf args (nthcdr new-posn orig-args))
(error 'format-error
:complaint
"Index ~D out of bounds. Should have been ~
between 0 and ~D."
:arguments
(list new-posn (length orig-args))
:offset ,(1- end)))))))
(if params
(expand-bind-defaults ((n 1)) params
(setf *only-simple-args* nil)
`(dotimes (i ,n)
,(expand-next-arg)))
(expand-next-arg)))))
(def-format-interpreter #\* (colonp atsignp params)
(if atsignp
(if colonp
(error 'format-error
:complaint "Cannot specify both colon and at-sign.")
(interpret-bind-defaults ((posn 0)) params
(if (<= 0 posn (length orig-args))
(setf args (nthcdr posn orig-args))
(error 'format-error
:complaint "Index ~D out of bounds. Should have been ~
between 0 and ~D."
:arguments (list posn (length orig-args))))))
(if colonp
(interpret-bind-defaults ((n 1)) params
(do ((cur-posn 0 (1+ cur-posn))
(arg-ptr orig-args (cdr arg-ptr)))
((eq arg-ptr args)
(let ((new-posn (- cur-posn n)))
(if (<= 0 new-posn (length orig-args))
(setf args (nthcdr new-posn orig-args))
(error 'format-error
:complaint
"Index ~D out of bounds. Should have been ~
between 0 and ~D."
:arguments
(list new-posn (length orig-args))))))))
(interpret-bind-defaults ((n 1)) params
(dotimes (i n)
(next-arg))))))
;;;; Indirection.
(def-format-directive #\? (colonp atsignp params string end)
(when colonp
(error 'format-error
:complaint "Cannot specify the colon modifier."))
(expand-bind-defaults () params
`(handler-bind
((format-error
#'(lambda (condition)
(error 'format-error
:complaint
"~A~%while processing indirect format string:"
:arguments (list condition)
:print-banner nil
:control-string ,string
:offset ,(1- end)))))
,(if atsignp
(if *orig-args-available*
`(setf args (formatter-aux stream ,(expand-next-arg) orig-args args))
(throw 'need-orig-args nil))
`(formatter-aux stream ,(expand-next-arg) ,(expand-next-arg))))))
(def-format-interpreter #\? (colonp atsignp params string end)
(when colonp
(error 'format-error
:complaint "Cannot specify the colon modifier."))
(interpret-bind-defaults () params
(handler-bind
((format-error
#'(lambda (condition)
(error 'format-error
:complaint
"~A~%while processing indirect format string:"
:arguments (list condition)
:print-banner nil
:control-string string
:offset (1- end)))))
(if atsignp
(setf args (formatter-aux stream (next-arg) orig-args args))
(formatter-aux stream (next-arg) (next-arg))))))
;;;; Capitalization.
(defun nstring-capitalize-first (s)
(nstring-downcase s)
(let ((where (position-if #'alpha-char-p s)))
(when where
(nstring-capitalize s :start 0 :end (1+ where)))
s))
(def-complex-format-directive #\( (colonp atsignp params directives)
(let ((close (find-directive directives #\) nil)))
(unless close
(error 'format-error
:complaint "No corresponding close paren."))
(let* ((posn (position close directives))
(before (subseq directives 0 posn))
(after (nthcdr (1+ posn) directives)))
(values
(expand-bind-defaults () params
#-ecl
`(let ((stream (make-case-frob-stream stream
,(if colonp
(if atsignp
:upcase
:capitalize)
(if atsignp
:capitalize-first
:downcase)))))
,@(expand-directive-list before))
#+ecl
`(let ((string (make-array 10 :element-type 'character
:fill-pointer 0 :adjustable t)))
(unwind-protect
(with-output-to-string (stream string)
,@(expand-directive-list before))
(princ (,(if colonp
(if atsignp 'nstring-upcase 'nstring-capitalize)
(if atsignp 'nstring-capitalize-first 'nstring-downcase))
string)
stream))))
after))))
(def-complex-format-interpreter #\( (colonp atsignp params directives)
(let ((close (find-directive directives #\) nil)))
(unless close
(error 'format-error
:complaint "No corresponding close paren."))
(interpret-bind-defaults () params
#-ecl
(let* ((posn (position close directives))
(before (subseq directives 0 posn))
(after (nthcdr (1+ posn) directives))
(stream (make-case-frob-stream stream
(if colonp
(if atsignp
:upcase
:capitalize)
(if atsignp
:capitalize-first
:downcase)))))
(setf args (interpret-directive-list stream before orig-args args))
after)
#+ecl
(let* ((posn (position close directives))
(before (subseq directives 0 posn))
(jumped t)
(after (nthcdr (1+ posn) directives))
(string (make-array 10 :element-type 'character
:adjustable t :fill-pointer 0)))
(unwind-protect
(with-output-to-string (stream string)
(setf args (interpret-directive-list stream before orig-args args)))
(princ (funcall
(if colonp
(if atsignp 'nstring-upcase 'nstring-capitalize)
(if atsignp 'nstring-capitalize-first 'nstring-downcase))
string) stream))
after))))
(def-complex-format-directive #\) ()
(error 'format-error
:complaint "No corresponding open paren."))
(def-complex-format-interpreter #\) ()
(error 'format-error
:complaint "No corresponding open paren."))
;;;; Conditionals
(defun parse-conditional-directive (directives)
(declare (si::c-local))
(let ((sublists nil)
(last-semi-with-colon-p nil)
(remaining directives))
(loop
(let ((close-or-semi (find-directive remaining #\] t)))
(unless close-or-semi
(error 'format-error
:complaint "No corresponding close bracket."))
(let ((posn (position close-or-semi remaining)))
(push (subseq remaining 0 posn) sublists)
(setf remaining (nthcdr (1+ posn) remaining))
(when (char= (format-directive-character close-or-semi) #\])
(return))
(setf last-semi-with-colon-p
(format-directive-colonp close-or-semi)))))
(values sublists last-semi-with-colon-p remaining)))
(def-complex-format-directive #\[ (colonp atsignp params directives)
(multiple-value-bind
(sublists last-semi-with-colon-p remaining)
(parse-conditional-directive directives)
(values
(if atsignp
(if colonp
(error 'format-error
:complaint
"Cannot specify both the colon and at-sign modifiers.")
(if (cdr sublists)
(error 'format-error
:complaint
"Can only specify one section")
(expand-bind-defaults () params
(expand-maybe-conditional (car sublists)))))
(if colonp
(if (= (length sublists) 2)
(expand-bind-defaults () params
(expand-true-false-conditional (car sublists)
(cadr sublists)))
(error 'format-error
:complaint
"Must specify exactly two sections."))
(expand-bind-defaults ((index nil)) params
(setf *only-simple-args* nil)
(let* ((clauses nil)
(case `(or ,index ,(expand-next-arg))))
(when last-semi-with-colon-p
(push `(t ,@(expand-directive-list (pop sublists)))
clauses))
(let ((count (length sublists)))
(dolist (sublist sublists)
(push `(,(decf count)
,@(expand-directive-list sublist))
clauses)))
`(case ,case ,@clauses)))))
remaining)))
#+formatter
(defun expand-maybe-conditional (sublist)
(declare (si::c-local))
(flet ((hairy ()
`(let ((prev-args args)
(arg ,(expand-next-arg)))
(when arg
(setf args prev-args)
,@(expand-directive-list sublist)))))
(if *only-simple-args*
(multiple-value-bind (guts new-args)
(let ((*simple-args* *simple-args*))
(values (expand-directive-list sublist)
*simple-args*))
(cond ((and new-args (eq *simple-args* (cdr new-args)))
(setf *simple-args* new-args)
`(when ,(caar new-args)
,@guts))
(t
(setf *only-simple-args* nil)
(hairy))))
(hairy))))
#+formatter
(defun expand-true-false-conditional (true false)
(declare (si::c-local))
(let ((arg (expand-next-arg)))
(flet ((hairy ()
`(if ,arg
(progn
,@(expand-directive-list true))
(progn
,@(expand-directive-list false)))))
(if *only-simple-args*
(multiple-value-bind
(true-guts true-args true-simple)
(let ((*simple-args* *simple-args*)
(*only-simple-args* t))
(values (expand-directive-list true)
*simple-args*
*only-simple-args*))
(multiple-value-bind
(false-guts false-args false-simple)
(let ((*simple-args* *simple-args*)
(*only-simple-args* t))
(values (expand-directive-list false)
*simple-args*
*only-simple-args*))
(if (= (length true-args) (length false-args))
`(if ,arg
(progn
,@true-guts)
,(do ((false false-args (cdr false))
(true true-args (cdr true))
(bindings nil (cons `(,(caar false) ,(caar true))
bindings)))
((eq true *simple-args*)
(setf *simple-args* true-args)
(setf *only-simple-args*
(and true-simple false-simple))
(if bindings
`(let ,bindings
,@false-guts)
`(progn
,@false-guts)))))
(progn
(setf *only-simple-args* nil)
(hairy)))))
(hairy)))))
(def-complex-format-interpreter #\[ (colonp atsignp params directives)
(multiple-value-bind
(sublists last-semi-with-colon-p remaining)
(parse-conditional-directive directives)
(setf args
(if atsignp
(if colonp
(error 'format-error
:complaint
"Cannot specify both the colon and at-sign modifiers.")
(if (cdr sublists)
(error 'format-error
:complaint
"Can only specify one section")
(interpret-bind-defaults () params
(let ((prev-args args)
(arg (next-arg)))
(if arg
(interpret-directive-list stream
(car sublists)
orig-args
prev-args)
args)))))
(if colonp
(if (= (length sublists) 2)
(interpret-bind-defaults () params
(if (next-arg)
(interpret-directive-list stream (car sublists)
orig-args args)
(interpret-directive-list stream (cadr sublists)
orig-args args)))
(error 'format-error
:complaint
"Must specify exactly two sections."))
(interpret-bind-defaults ((index (next-arg))) params
(let* ((default (and last-semi-with-colon-p
(pop sublists)))
(last (1- (length sublists)))
(sublist
(if (<= 0 index last)
(nth (- last index) sublists)
default)))
(interpret-directive-list stream sublist orig-args
args))))))
remaining))
(def-complex-format-directive #\; ()
(error 'format-error
:complaint
"~~; not contained within either ~~[...~~] or ~~<...~~>."))
(def-complex-format-interpreter #\; ()
(error 'format-error
:complaint
"~~; not contained within either ~~[...~~] or ~~<...~~>."))
(def-complex-format-interpreter #\] ()
(error 'format-error
:complaint
"No corresponding open bracket."))
(def-complex-format-directive #\] ()
(error 'format-error
:complaint
"No corresponding open bracket."))
;;;; Up-and-out.
(defvar *outside-args*)
(def-format-directive #\^ (colonp atsignp params)
(when atsignp
(error 'format-error
:complaint "cannot use the at-sign modifier with this directive"))
(when (and colonp (not *up-up-and-out-allowed*))
(error 'format-error
:complaint "attempt to use ~~:^ outside a ~~:{...~~} construct"))
`(when ,(expand-bind-defaults ((arg1 nil) (arg2 nil) (arg3 nil)) params
`(cond (,arg3 (<= ,arg1 ,arg2 ,arg3))
(,arg2 (eql ,arg1 ,arg2))
(,arg1 (eql ,arg1 0))
(t ,(if colonp
'(null outside-args)
(progn
(setf *only-simple-args* nil)
'(null args))))))
,(if colonp
'(return-from outside-loop nil)
'(return))))
(def-format-interpreter #\^ (colonp atsignp params)
(when atsignp
(error 'format-error
:complaint "cannot specify the at-sign modifier"))
(when (and colonp (not *up-up-and-out-allowed*))
(error 'format-error
:complaint "attempt to use ~~:^ outside a ~~:{...~~} construct"))
(when (interpret-bind-defaults ((arg1 nil) (arg2 nil) (arg3 nil)) params
(cond (arg3 (<= arg1 arg2 arg3))
(arg2 (eql arg1 arg2))
(arg1 (eql arg1 0))
(t (if colonp
(null *outside-args*)
(null args)))))
(throw (if colonp 'up-up-and-out 'up-and-out)
args)))
;;;; Iteration.
(def-complex-format-directive #\{ (colonp atsignp params string end directives)
(let ((close (find-directive directives #\} nil)))
(unless close
(error 'format-error
:complaint "no corresponding close brace"))
(let* ((closed-with-colon (format-directive-colonp close))
(posn (position close directives)))
(labels
((compute-insides ()
(if (zerop posn)
(if *orig-args-available*
`((handler-bind
((format-error
(lambda (condition)
(error 'format-error
:complaint
"~A~%while processing indirect format string:"
:args (list condition)
:print-banner nil
:control-string ,string
:offset ,(1- end)))))
(setf args
(formatter-aux stream inside-string orig-args args))))
(throw 'need-orig-args nil))
(let ((*up-up-and-out-allowed* colonp))
(expand-directive-list (subseq directives 0 posn)))))
(compute-loop (count)
(when atsignp
(setf *only-simple-args* nil))
`(loop
,@(unless closed-with-colon
'((when (null args)
(return))))
,@(when count
`((when (and ,count (minusp (decf ,count)))
(return))))
,@(if colonp
(let ((*expander-next-arg-macro* 'expander-next-arg)
(*only-simple-args* nil)
(*orig-args-available* t))
`((let* ((orig-args ,(expand-next-arg))
(outside-args args)
(args orig-args))
(declare (ignorable orig-args outside-args args))
(block nil
,@(compute-insides)))))
(compute-insides))
,@(when closed-with-colon
'((when (null args)
(return))))))
(compute-block (count)
(if colonp
`(block outside-loop
,(compute-loop count))
(compute-loop count)))
(compute-bindings (count)
(if atsignp
(compute-block count)
`(let* ((orig-args ,(expand-next-arg))
(args orig-args))
(declare (ignorable orig-args args))
,(let ((*expander-next-arg-macro* 'expander-next-arg)
(*only-simple-args* nil)
(*orig-args-available* t))
(compute-block count))))))
(values (if params
(expand-bind-defaults ((count nil)) params
(if (zerop posn)
`(let ((inside-string ,(expand-next-arg)))
,(compute-bindings count))
(compute-bindings count)))
(if (zerop posn)
`(let ((inside-string ,(expand-next-arg)))
,(compute-bindings nil))
(compute-bindings nil)))
(nthcdr (1+ posn) directives))))))
(def-complex-format-interpreter #\{
(colonp atsignp params string end directives)
(let ((close (find-directive directives #\} nil)))
(unless close
(error 'format-error
:complaint
"No corresponding close brace."))
(interpret-bind-defaults ((max-count nil)) params
(let* ((closed-with-colon (format-directive-colonp close))
(posn (position close directives))
(insides (if (zerop posn)
(next-arg)
(subseq directives 0 posn)))
(*up-up-and-out-allowed* colonp))
(labels
((do-guts (orig-args args)
(if (zerop posn)
(handler-bind
((format-error
#'(lambda (condition)
(error 'format-error
:complaint
"~A~%while processing indirect format string:"
:arguments (list condition)
:print-banner nil
:control-string string
:offset (1- end)))))
(formatter-aux stream insides orig-args args))
(interpret-directive-list stream insides
orig-args args)))
(bind-args (orig-args args)
(if colonp
(let* ((arg (next-arg))
(*logical-block-popper* nil)
(*outside-args* args))
(catch 'up-and-out
(do-guts arg arg))
args)
(do-guts orig-args args)))
(do-loop (orig-args args)
(catch (if colonp 'up-up-and-out 'up-and-out)
(loop
(when (and (not closed-with-colon) (null args))
(return))
(when (and max-count (minusp (decf max-count)))
(return))
(setf args (bind-args orig-args args))
(when (and closed-with-colon (null args))
(return)))
args)))
(if atsignp
(setf args (do-loop orig-args args))
(let ((arg (next-arg))
(*logical-block-popper* nil))
(do-loop arg arg)))
(nthcdr (1+ posn) directives))))))
(def-complex-format-directive #\} ()
(error 'format-error
:complaint "No corresponding open brace."))
(def-complex-format-interpreter #\} ()
(error 'format-error
:complaint "No corresponding open brace."))
;;;; Justification.
(defparameter *illegal-inside-justification*
(mapcar (lambda (x) (parse-directive x 0))
'("~W" "~:W" "~@W" "~:@W"
"~_" "~:_" "~@_" "~:@_"
"~:>" "~:@>"
"~I" "~:I" "~@I" "~:@I"
"~:T" "~:@T")))
(defun check-output-layout-mode (mode)
(declare (si::c-local))
(when (and *output-layout-mode*
(not (eql *output-layout-mode* mode)))
(error 'format-error
:complaint "Cannot mix ~~W, ~~_, ~~<...~~:>, ~~I, or ~~T with ~~<...~~:;...~~>"))
(setf *output-layout-mode* mode))
(defun illegal-inside-justification-p (directive)
(member directive *illegal-inside-justification*
:test (lambda (x y)
(and (format-directive-p x)
(format-directive-p y)
(eql (format-directive-character x) (format-directive-character y))
(eql (format-directive-colonp x) (format-directive-colonp y))
(eql (format-directive-atsignp x) (format-directive-atsignp y))))))
(def-complex-format-directive #\< (colonp atsignp params string end directives)
(multiple-value-bind
(segments first-semi close remaining)
(parse-format-justification directives)
(values
(if (format-directive-colonp close)
(multiple-value-bind
(prefix per-line-p insides suffix)
(parse-format-logical-block segments colonp first-semi
close params string end)
(expand-format-logical-block prefix per-line-p insides
suffix atsignp))
(let ((count (reduce #'+ (mapcar (lambda (x)
(count-if #'illegal-inside-justification-p x))
segments))))
(when (> count 0)
;; ANSI specifies that "an error is signalled" in this
;; situation.
(error 'format-error
:complaint "~D illegal directive~:P found inside justification block"
:arguments (list count)))
(expand-format-justification segments colonp atsignp
first-semi params)))
remaining)))
(def-complex-format-interpreter #\<
(colonp atsignp params string end directives)
(multiple-value-bind
(segments first-semi close remaining)
(parse-format-justification directives)
(setf args
(if (format-directive-colonp close)
(multiple-value-bind
(prefix per-line-p insides suffix)
(parse-format-logical-block segments colonp first-semi
close params string end)
(interpret-format-logical-block stream orig-args args
prefix per-line-p insides
suffix atsignp))
(let ((count (reduce #'+ (mapcar (lambda (x)
(count-if #'illegal-inside-justification-p x))
segments))))
(when (> count 0)
;; ANSI specifies that "an error is signalled" in this
;; situation.
(error 'format-error
:complaint "~D illegal directive~:P found inside justification block"
:arguments (list count)))
(interpret-format-justification stream orig-args args
segments colonp atsignp
first-semi params))))
remaining))
(defun parse-format-justification (directives)
(declare (si::c-local))
(let ((first-semi nil)
(close nil)
(remaining directives))
(collect ((segments))
(loop
(let ((close-or-semi (find-directive remaining #\> t)))
(unless close-or-semi
(error 'format-error
:complaint "No corresponding close bracket."))
(let ((posn (position close-or-semi remaining)))
(segments (subseq remaining 0 posn))
(setf remaining (nthcdr (1+ posn) remaining)))
(when (char= (format-directive-character close-or-semi)
#\>)
(setf close close-or-semi)
(return))
(unless first-semi
(setf first-semi close-or-semi))))
(values (segments) first-semi close remaining))))
#+formatter
(defun expand-format-justification (segments colonp atsignp first-semi params)
(declare (si::c-local))
(let ((newline-segment-p
(and first-semi
(format-directive-colonp first-semi))))
(when newline-segment-p
(check-output-layout-mode 2))
(expand-bind-defaults
((mincol 0) (colinc 1) (minpad 0) (padchar #\space))
params
`(let ((segments nil)
,@(when newline-segment-p
'((newline-segment nil)
(extra-space 0)
(line-len 72))))
(block nil
,@(when newline-segment-p
`((setf newline-segment
(with-output-to-string (stream)
,@(expand-directive-list (pop segments))))
,(expand-bind-defaults
((extra 0)
(line-len '(or #-ecl (sys::line-length stream) 72)))
(format-directive-params first-semi)
`(setf extra-space ,extra line-len ,line-len))))
,@(mapcar #'(lambda (segment)
`(push (with-output-to-string (stream)
,@(expand-directive-list segment))
segments))
segments))
(format-justification stream
,@(if newline-segment-p
'(newline-segment extra-space line-len)
'(nil 0 0))
segments ,colonp ,atsignp
,mincol ,colinc ,minpad ,padchar)))))
(defun interpret-format-justification
(stream orig-args args segments colonp atsignp first-semi params)
(declare (si::c-local))
(interpret-bind-defaults
((mincol 0) (colinc 1) (minpad 0) (padchar #\space))
params
(let ((newline-string nil)
(strings nil)
(extra-space 0)
(line-len 0))
(setf args
(catch 'up-and-out
(when (and first-semi (format-directive-colonp first-semi))
(check-output-layout-mode 2)
(interpret-bind-defaults
((extra 0)
(len (or #-ecl (sys::line-length stream) 72)))
(format-directive-params first-semi)
(setf newline-string
(with-output-to-string (stream)
(setf args
(interpret-directive-list stream
(pop segments)
orig-args
args))))
(setf extra-space extra)
(setf line-len len)))
(dolist (segment segments)
(push (with-output-to-string (stream)
(setf args
(interpret-directive-list stream segment
orig-args args)))
strings))
args))
(format-justification stream newline-string extra-space line-len strings
colonp atsignp mincol colinc minpad padchar)))
args)
(defun format-justification (stream newline-prefix extra-space line-len strings
pad-left pad-right mincol colinc minpad padchar)
#-formatter
(declare (si::c-local))
(setf strings (reverse strings))
(when (and (not pad-left) (not pad-right) (null (cdr strings)))
(setf pad-left t))
(let* ((num-gaps (1- (length strings)))
(chars (+ (* num-gaps minpad)
(loop for string in strings summing (length string))))
(length (if (> chars mincol)
(+ mincol (* (ceiling (- chars mincol) colinc) colinc))
mincol))
(padding (- length chars)))
(when (and newline-prefix
(> (+ (or (#-ecl sys::charpos #+ecl sys:file-column stream) 0)
length extra-space)
line-len))
(write-string newline-prefix stream))
(when pad-left
(incf num-gaps))
(when pad-right
(incf num-gaps))
(when (zerop num-gaps)
(incf num-gaps)
(setf pad-left t))
(flet ((do-padding (border)
(let ((pad-len (truncate padding num-gaps)))
(decf padding pad-len)
(decf num-gaps)
(unless border
(incf pad-len minpad))
(dotimes (i pad-len) (write-char padchar stream)))))
(when pad-left
(do-padding t))
(when strings
(write-string (car strings) stream)
(dolist (string (cdr strings))
(do-padding nil)
(write-string string stream)))
(when pad-right
(do-padding t)))))
(defun parse-format-logical-block
(segments colonp first-semi close params string end)
(declare (si::c-local))
(check-output-layout-mode 1)
(when params
(error 'format-error
:complaint "No parameters can be supplied with ~~<...~~:>."
:offset (caar params)))
(multiple-value-bind
(prefix insides suffix)
(multiple-value-bind (prefix-default suffix-default)
(if colonp (values "(" ")") (values "" ""))
(flet ((extract-string (list prefix-p)
(let ((directive (find-if #'format-directive-p list)))
(if directive
(error 'format-error
:complaint
"Cannot include format directives inside the ~
~:[suffix~;prefix~] segment of ~~<...~~:>"
:arguments (list prefix-p)
:offset (1- (format-directive-end directive)))
(apply #'concatenate 'string list)))))
(case (length segments)
(0 (values prefix-default nil suffix-default))
(1 (values prefix-default (car segments) suffix-default))
(2 (values (extract-string (car segments) t)
(cadr segments) suffix-default))
(3 (values (extract-string (car segments) t)
(cadr segments)
(extract-string (caddr segments) nil)))
(t
(error 'format-error
:complaint "Too many segments for ~~<...~~:>.")))))
(when (format-directive-atsignp close)
(setf insides
(add-fill-style-newlines insides
string
(if first-semi
(format-directive-end first-semi)
end))))
(values prefix
(and first-semi (format-directive-atsignp first-semi))
insides
suffix)))
(defun add-fill-style-newlines (list string offset)
(declare (si::c-local))
(if list
(let ((directive (car list)))
(if (simple-string-p directive)
(nconc (add-fill-style-newlines-aux directive string offset)
(add-fill-style-newlines (cdr list)
string
(+ offset (length directive))))
(cons directive
(add-fill-style-newlines (cdr list)
string
(format-directive-end directive)))))
nil))
(defun add-fill-style-newlines-aux (literal string offset)
(declare (si::c-local))
(let ((end (length literal))
(posn 0))
(collect ((results))
(loop
(let ((blank (position #\space literal :start posn)))
(when (null blank)
(results (subseq literal posn))
(return))
(let ((non-blank (or (position #\space literal :start blank
:test #'char/=)
end)))
(results (subseq literal posn non-blank))
(results (make-format-directive
:string string :character #\_
:start (+ offset non-blank) :end (+ offset non-blank)
:colonp t :atsignp nil :params nil))
(setf posn non-blank))
(when (= posn end)
(return))))
(results))))
#+formatter
(defun expand-format-logical-block (prefix per-line-p insides suffix atsignp)
`(let ((arg ,(if atsignp 'args (expand-next-arg))))
,@(when atsignp
(setf *only-simple-args* nil)
'((setf args nil)))
(pprint-logical-block
(stream arg
,(if per-line-p :per-line-prefix :prefix) ,prefix
:suffix ,suffix)
(let ((args arg)
,@(unless atsignp
`((orig-args arg))))
(declare (ignorable args ,@(unless atsignp '(orig-args))))
(block nil
,@(let ((*expander-next-arg-macro* 'expander-pprint-next-arg)
(*only-simple-args* nil)
(*orig-args-available* t))
(expand-directive-list insides)))))))
(defun interpret-format-logical-block
(stream orig-args args prefix per-line-p insides suffix atsignp)
(declare (si::c-local))
(let ((arg (if atsignp args (next-arg))))
(if per-line-p
(pprint-logical-block
(stream arg :per-line-prefix prefix :suffix suffix)
(let ((*logical-block-popper* #'(lambda () (pprint-pop))))
(catch 'up-and-out
(interpret-directive-list stream insides
(if atsignp orig-args arg)
arg))))
(pprint-logical-block (stream arg :prefix prefix :suffix suffix)
(let ((*logical-block-popper* #'(lambda () (pprint-pop))))
(catch 'up-and-out
(interpret-directive-list stream insides
(if atsignp orig-args arg)
arg))))))
(if atsignp nil args))
(def-complex-format-directive #\> ()
(error 'format-error
:complaint "No corresponding open bracket."))
;;;; User-defined method.
(def-format-directive #\/ (string start end colonp atsignp params)
(let ((symbol (extract-user-function-name string start end)))
(collect ((param-names) (bindings))
(dolist (param-and-offset params)
(let ((param (cdr param-and-offset)))
(let ((param-name (gensym)))
(param-names param-name)
(bindings `(,param-name
,(case param
(:arg (expand-next-arg))
(:remaining '(length args))
(t param)))))))
`(let ,(bindings)
(,symbol stream ,(expand-next-arg) ,colonp ,atsignp
,@(param-names))))))
(def-format-interpreter #\/ (string start end colonp atsignp params)
(let ((symbol (extract-user-function-name string start end)))
(collect ((args))
(dolist (param-and-offset params)
(let ((param (cdr param-and-offset)))
(case param
(:arg (let ((x (next-arg))) (when x (args x))))
(:remaining (args (length args)))
(t (args param)))))
(apply (fdefinition symbol) stream (next-arg) colonp atsignp (args)))))
(defun extract-user-function-name (string start end)
(declare (si::c-local))
(let ((slash (position #\/ string :start start :end (1- end)
:from-end t)))
(unless slash
(error 'format-error
:complaint "Malformed ~~/ directive."))
(let* ((name (string-upcase (let ((foo string))
;; Hack alert: This is to keep the compiler
;; quit about deleting code inside the subseq
;; expansion.
(subseq foo (1+ slash) (1- end)))))
(first-colon (position #\: name))
(second-colon (if first-colon (position #\: name :start (1+ first-colon))))
(package-name (if first-colon
(subseq name 0 first-colon)
"COMMON-LISP-USER"))
(package (find-package package-name)))
(unless package
(error 'format-error
:complaint "No package named ~S"
:arguments (list package-name)))
(intern (cond
((and second-colon (= second-colon (1+ first-colon)))
(subseq name (1+ second-colon)))
(first-colon
(subseq name (1+ first-colon)))
(t name))
package))))
;;;; Compile-time checking of format arguments and control string
#-ecl(progn
;;;
;;; Return the min/max numbers of arguments required for a call to
;;; FORMAT with control string FORMAT-STRING, null if we can't tell,
;;; or a string with an error message if parsing the control string
;;; causes a FORMAT-ERROR.
;;;
;;; This is called from FORMAT deftransforms.
;;;
(defun min/max-format-arguments-count (string)
#-formatter
(declare (si::c-local))
(handler-case
(catch 'give-up
;; For the side effect of validating the control string.
(%formatter string)
(%min/max-format-args (tokenize-control-string string)))
(format-error (e)
(format nil "~a" e))))
(defun %min/max-format-args (directives)
#-formatter
(declare (si::c-local))
(let ((min-req 0) (max-req 0))
(flet ((incf-both (&optional (n 1))
(incf min-req n)
(incf max-req n)))
(loop
(let ((dir (pop directives)))
(when (null dir)
(return (values min-req max-req)))
(when (format-directive-p dir)
(incf-both (count :arg (format-directive-params dir) :key #'cdr))
(let ((c (format-directive-character dir)))
(cond ((find c "ABCDEFGORSWX$/")
(incf-both))
((char= c #\P)
(unless (format-directive-colonp dir)
(incf-both)))
((or (find c "IT%&|_<>();") (char= c #\newline)))
((char= c #\[)
(multiple-value-bind (min max remaining)
(%min/max-conditional-args dir directives)
(setq directives remaining)
(incf min-req min)
(incf max-req max)))
((char= c #\{)
(multiple-value-bind (min max remaining)
(%min/max-iteration-args dir directives)
(setq directives remaining)
(incf min-req min)
(incf max-req max)))
((char= c #\?)
(cond ((format-directive-atsignp dir)
(incf min-req)
(setq max-req most-positive-fixnum))
(t (incf-both 2))))
(t (throw 'give-up nil))))))))))
;;;
;;; ANSI: if arg is out of range, no clause is selected. That means
;;; the minimum number of args required for the interior of ~[~] is
;;; always zero.
;;;
(defun %min/max-conditional-args (conditional directives)
#-formatter
(declare (si::c-local))
(multiple-value-bind (sublists last-semi-with-colon-p remaining)
(parse-conditional-directive directives)
(declare (ignore last-semi-with-colon-p))
(let ((sub-max (loop for s in sublists maximize
(nth-value 1 (%min/max-format-args s))))
(min-req 1)
max-req)
(cond ((format-directive-atsignp conditional)
(setq max-req (max 1 sub-max)))
((loop for p in (format-directive-params conditional)
thereis (or (integerp (cdr p))
(memq (cdr p) '(:remaining :arg))))
(setq min-req 0)
(setq max-req sub-max))
(t
(setq max-req (1+ sub-max))))
(values min-req max-req remaining))))
(defun %min/max-iteration-args (iteration directives)
#-formatter
(declare (si::c-local))
(let* ((close (find-directive directives #\} nil))
(posn (position close directives))
(remaining (nthcdr (1+ posn) directives)))
(if (format-directive-atsignp iteration)
(values (if (zerop posn) 1 0) most-positive-fixnum remaining)
(let ((nreq (if (zerop posn) 2 1)))
(values nreq nreq remaining)))))
)