-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathformat.lisp
1811 lines (1624 loc) · 69.9 KB
/
format.lisp
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
;;; -*- Mode: Common-Lisp; Syntax: Common-Lisp; Package: LINJ; Base: 10 -*-
;;; **********************************************************************
;;; 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 "LINJ")
;;This file requires the Linj readtable
(eval-when (:compile-toplevel :load-toplevel)
(setq *readtable* *linj-readtable*))
;;This is the stream "variable" to be used in code generation
(defvar *stream*)
(defvar *result-type*)
(defun emit-format-code (type fmt stream full-args)
(let ((*stream* stream)
(*result-type* type))
(%formatter fmt full-args)))
(defun gen-write-char (form)
(ecase *result-type*
(:expression (if (characterp form) form `(real-the char ,form)))
(:statement `(write-char ,form ,*stream*))
(:string-buffer `(send ,*stream* append ,form))))
(defun gen-write-string (form)
(ecase *result-type*
(:expression (if (stringp form) form `(real-the java.lang.string ,form)))
(:statement `(write-string ,form ,*stream*))
(:string-buffer `(send ,*stream* append ,form))))
(defun gen-prin1 (form)
(ecase *result-type*
(:expression form)
(:statement `(prin1 ,form ,*stream*))
(:string-buffer `(send ,*stream* append ,form))))
(defun gen-princ (form)
(ecase *result-type*
(:expression form)
(:statement `(princ ,form ,*stream*))
(:string-buffer `(send ,*stream* append ,form))))
(defun gen-terpri ()
(ecase *result-type*
(:expression '#\newline)
(:statement `(terpri ,*stream*))
(:string-buffer `(send ,*stream* append #\newline))))
(defun gen-fresh-line ()
(ecase *result-type*
(:expression '#\newline)
(:statement `(fresh-line ,*stream*))
(:string-buffer `(send ,*stream* append #\newline))))
(defun gen-progn (forms)
(ecase *result-type*
(:expression
`(concat ,@forms))
((:statement :string-buffer)
(if (and (not (endp forms)) (endp (rest forms)))
(first forms)
`(progn ,@forms)))))
(defun gen-when (test forms)
(ecase *result-type*
(:expression
`(if ,test ,(gen-progn forms) ""))
((:statement :string-buffer)
`(when ,test
,@forms))))
(defstruct (format-directive
(:print-function %print-format-directive))
(string (required-argument) :type simple-string)
(start (required-argument) :type (and unsigned-byte fixnum))
(end (required-argument) :type (and unsigned-byte fixnum))
(character (required-argument) :type base-char)
(colonp nil :type (member t nil))
(atsignp nil :type (member t nil))
(params nil :type list))
(defun required-argument ()
(error "Missing argument"))
(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))))
(defvar *format-directive-expanders*
(make-array char-code-limit :initial-element nil))
(defvar *format-directive-interpreters*
(make-array char-code-limit :initial-element nil))
(defun %print-format-error (condition stream)
(cl:format stream
"~:[~;Error in format: ~]~
~?~@[~% ~A~% ~V@T^~]"
(format-error-print-banner condition)
(format-error-complaint condition)
(format-error-arguments condition)
(format-error-control-string condition)
(format-error-offset condition)))
(defvar *default-format-error-control-string* nil)
(defvar *default-format-error-offset* nil)
(define-condition format-error (error)
((complaint :reader format-error-complaint :initarg :complaint)
(arguments :reader format-error-arguments :initarg :arguments :initform nil)
(control-string :reader format-error-control-string
:initarg :control-string
:initform *default-format-error-control-string*)
(offset :reader format-error-offset :initarg :offset
:initform *default-format-error-offset*)
(print-banner :reader format-error-print-banner :initarg :print-banner
:initform t))
(:report %print-format-error))
;;;; TOKENIZE-CONTROL-STRING
(defun tokenize-control-string (string)
(declare (simple-string string))
(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)
(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)
(unless (char= (get-char) #\,)
(decf posn)))
((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.
;;;
(defvar *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.
;;;
(defvar *logical-block-popper* nil)
;;; *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.
;;;
(defvar *orig-args-available* nil)
;;; *SIMPLE-ARGS* -- internal.
;;;
;;; Used by the expander stuff. List of (symbol . offset) for simple args.
;;;
(defvar *simple-args*)
(defvar *orig-args*)
(defvar *args*)
(defvar *no-value* (list 'progn))
;;;; FORMATTER
(defun %formatter (control-string args)
(let* ((*simple-args* nil)
(*only-simple-args* t)
(*orig-args* args)
(*args* args))
(expand-control-string control-string)))
(defun expand-control-string (string)
(let* ((string (etypecase string
(simple-string
string)
(string
(coerce string 'simple-string))))
(*default-format-error-control-string* string)
(directives (tokenize-control-string string)))
(ecase *result-type*
((:statement :string-buffer)
`(block nil
,@(expand-directive-list directives)))
(:expression
`(concat
,@(remove '(concat) (expand-directive-list directives) :test #'equal)))))) ;;small optimization
(defun expand-directive-list (directives)
(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))
(unless (equal form '(progn))
(setq results (combine-format-form form results)))
(setf remaining-directives new-directives)))
(reverse results)))
(defun combine-format-form (form forms)
(if (let-pattern (((write-string (?is ?obj (lambda (obj) (and (stringp obj) (string= obj "")))) ?ignore) form)) t)
forms
(or
(let-pattern (((write-string (?is ?string1 stringp) ?stream) form)
(((write-string (?is ?string2 stringp) ?ignore) . ?ignore) forms))
`((write-string ,(concatenate 'string ?string2 ?string1) ,?stream) ,@(rest forms)))
(let-pattern (((terpri ?stream) form)
(((write-string ?string2 ?ignore) . ?ignore) forms))
`((write-line ,?string2 ,?stream) ,@(rest forms)))
(let-pattern (((terpri ?stream) form)
(((princ ?obj ?ignore) . ?ignore) forms))
`((println ,?obj ,?stream) ,@(rest forms)))
(cons form forms))))
(defun expand-directive (directive more-directives)
(etypecase directive
(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."))))
(simple-string
(values (gen-write-string directive)
more-directives))))
(defun protect (form)
(if (or *orig-args-available* (not *only-simple-args*))
`(if (endp args)
(error "No more arguments.")
(let ((arg (first args)))
(setq args (rest args))
,form))
form))
(defun expand-next-arg (&optional offset)
(if (or *orig-args-available* (not *only-simple-args*))
'arg
(if (endp *args*)
(error 'format-error
:complaint "No more arguments."
:control-string *default-format-error-control-string*
:offset (or offset *default-format-error-offset*))
(pop *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)))
(eval-when (:compile-toplevel :execute)
(defmacro def-complex-format-directive (char lambda-list &body body)
(let ((defun-name (intern (cl:format nil
"~:@(~:C~)-FORMAT-DIRECTIVE-EXPANDER"
char)))
(directive (gensym))
(directives (if lambda-list (car (last lambda-list)) (gensym))))
`(progn
(defun ,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)))
(%set-format-directive-expander ,char #',defun-name))))
(defmacro def-format-directive (char lambda-list &body body)
(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 once-only (specs &body body)
(labels ((frob (specs body)
(if (null specs)
`(progn ,@body)
(let ((spec (first specs)))
(when (/= (length spec) 2)
(error "Malformed Once-Only binding spec: ~S." spec))
(let ((name (first spec))
(exp-temp (gensym)))
`(let ((,exp-temp ,(second spec))
(,name (gensym "OO-")))
`(let ((,,name ,,exp-temp))
,,(frob (rest specs) body))))))))
(frob specs body)))
;; (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
;; (progn (setf *only-simple-args* nil) (error "BUM"))
;; '(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 expand-bind-defaults (specs params &body body)
(once-only ((params params))
(if specs
`(let ,(mapcar #'(lambda (spec)
(destructuring-bind (var default) spec
`(,var
(let* ((param-and-offset (pop ,params))
(offset (car param-and-offset))
(param (cdr param-and-offset)))
(case param
(:arg (let ((arg (expand-next-arg offset)))
(if (false-literal-p arg) ;;nil arg
,default
arg)))
(:remaining
(if *only-simple-args*
(length *args*)
'(length args)))
;; (:remaining
;; (length *args*))
((nil) ,default)
(t param))))))
specs)
(unless (endp ,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))))
); eval-when
(defun %set-format-directive-expander (char fn)
(setf (aref *format-directive-expanders* (char-code (char-upcase char))) fn)
char)
(defun find-directive (directives kind stop-at-semi)
(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)))))
(defun format-princ (stream arg colonp atsignp mincol colinc minpad padchar)
(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)
(protect
(if colonp
(error "Linj separates NIL and (). A consequence is that it doesn't make sense to use ~~:A as ~~A will never print NIL for an empty list")
(if params
(expand-bind-defaults
((mincol 0) (colinc 1) (minpad 0) (padchar #\space)) params
(gen-princ `(format-write-field-to-string
,(expand-next-arg)
,mincol ,colinc ,minpad ,padchar ,atsignp)))
(gen-princ (expand-next-arg))))))
(defun format-prin1 (stream arg colonp atsignp mincol colinc minpad padchar)
(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)
(protect
(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)
(princ "()"))))
(t
(gen-prin1 (expand-next-arg))))))
(def-format-directive #\C (colonp atsignp params)
(protect
(expand-bind-defaults
() params
(if colonp
`(format-print-named-character ,(expand-next-arg) ,*stream*)
(if atsignp
(gen-prin1 (expand-next-arg))
(gen-write-char (expand-next-arg)))))))
(def-format-directive #\W (colonp atsignp params)
(protect
(expand-bind-defaults
() params
(if (or colonp atsignp)
`(let (,@(when colonp
'((*print-pretty* t)))
,@(when atsignp
'((*print-level* nil)
(*print-length* nil))))
(output-object ,(expand-next-arg) ,*stream*))
`(output-object ,(expand-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)
;; (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)
;; (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))))
(defun expand-format-integer (base colonp atsignp params)
(protect
(if (or colonp atsignp params)
(expand-bind-defaults
((mincol 0) (padchar #\space) (commachar #\,) (commainterval 3))
params
(gen-princ `(format-print-integer-to-string
,(expand-next-arg)
,colonp ,atsignp ,base ,mincol ,padchar ,commachar ,commainterval)))
(gen-princ `(to-base ,(expand-next-arg) ,base)))))
(def-format-directive #\D (colonp atsignp params)
(expand-format-integer 10 colonp atsignp params))
(def-format-directive #\B (colonp atsignp params)
(expand-format-integer 2 colonp atsignp params))
(def-format-directive #\O (colonp atsignp params)
(expand-format-integer 8 colonp atsignp params))
(def-format-directive #\X (colonp atsignp params)
(expand-format-integer 16 colonp atsignp params))
(def-format-directive #\R (colonp atsignp params)
(protect
(if params
(expand-bind-defaults
((base 10) (mincol 0) (padchar #\space) (commachar #\,)
(commainterval 3))
params
(gen-princ `(format-print-integer-to-string
,(expand-next-arg)
,colonp ,atsignp ,base ,mincol ,padchar ,commachar ,commainterval)))
(if atsignp
(if colonp
(gen-princ `(format-print-old-roman-to-string ,(expand-next-arg)))
(gen-princ `(format-print-roman-to-string ,(expand-next-arg))))
(if colonp
(gen-princ `(format-print-ordinal-to-string ,(expand-next-arg)))
(gen-princ `(format-print-cardinal-to-string ,(expand-next-arg))))))))
;;;; 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)))))
(t
(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))))))))
(let ((form (if atsignp
(gen-write-string `(if (eql ,arg 1) "y" "ies"))
(ecase *result-type*
(:expression `(if (eql ,arg 1) "s" ""))
(:statement `(unless (eql ,arg 1) (write-char #\s ,*stream*)))
(:string-buffer `(unless (eql ,arg 1) (send ,*stream* append #\s)))))))
(if (not colonp)
(protect form)
form)))))
;;;; Floating point noise.
(defun decimal-string (n)
(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."))
;; (protect
;; (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))))
;; (defun format-fixed (stream number w d k ovf pad atsign)
;; (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)
;; (cond
;; ((or (not (or w d))
;; (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)
;; (lisp::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."))
;; (protect
;; (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))))
;; (defun format-exponential (stream number w d e k ovf pad marker atsign)
;; (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)
;; (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.
;;; [email protected]: 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)
;; (if (and (floatp number)
;; (or (float-infinity-p number)
;; (float-nan-p number)))
;; (prin1 number stream)
;; (multiple-value-bind (num expt)
;; (lisp::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)
;; (lisp::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."))
;; (protect
;; (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))))
;; (defun format-general (stream number w d e k ovf pad marker atsign)
;; (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)))
;;; [email protected]: Same change as for format-exp-aux.
;; (defun format-general-aux (stream number w d e k ovf pad marker atsign)
;; (if (and (floatp number)
;; (or (float-infinity-p number)
;; (float-nan-p number)))
;; (prin1 number stream)
;; (multiple-value-bind (ignore n)
;; (lisp::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)
;; (lisp::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)
(protect
(expand-bind-defaults
((d 2) (n 1) (w 0) (pad #\space)) params
`(format-dollars ,*stream* ,(expand-next-arg) ,d ,n ,w ,pad ,colonp
,atsignp))))
;; (defun format-dollars (stream number d n w pad colon atsign)
;; (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)
;; (lisp::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
(ecase *result-type*