-
Notifications
You must be signed in to change notification settings - Fork 104
/
l1-io.lisp
2018 lines (1800 loc) · 78.5 KB
/
l1-io.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: LISP; Package: CCL -*-
;;;
;;; Copyright 1994-2009 Clozure Associates
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
;; L1-io.lisp
(in-package "CCL")
(setf (fdefinition '%new-ptr) (fdefinition '%new-gcable-ptr))
;;;; ======================================================================
;;;; Standard CL IO frobs
(declaim (inline %real-print-stream))
(defun %real-print-stream (&optional (stream nil))
(cond ((null stream)
*standard-output*)
((eq stream t)
*terminal-io*)
(t stream)))
;;; OK, EOFP isn't CL ...
(defun eofp (&optional (stream *standard-input*))
(stream-eofp stream))
(defun force-output (&optional stream)
(stream-force-output (%real-print-stream stream))
nil)
(defun listen (&optional (stream *standard-input*))
(let* ((stream (designated-input-stream stream)))
(stream-listen stream)))
(defun fresh-line (&optional (output-stream *standard-output*))
"Output #\Newline only if the OUTPUT-STREAM is not already at the
start of a line. Return T if #\Newline needed."
(stream-fresh-line (%real-print-stream output-stream)))
(defun column (&optional stream)
(let* ((stream (%real-print-stream stream)))
(stream-line-column stream)))
(defun clear-input (&optional input-stream)
"Clear any available input from INPUT-STREAM."
(stream-clear-input (designated-input-stream input-stream))
nil)
(defun write-char (char &optional (output-stream nil))
"Output CHAR to OUTPUT-STREAM."
(let* ((stream (%real-print-stream output-stream)))
(if (typep stream 'basic-stream)
(let* ((ioblock (basic-stream-ioblock stream)))
(funcall (ioblock-write-char-function ioblock) ioblock char))
(stream-write-char stream char))
char))
(defun write-string (string &optional output-stream &key (start 0 start-p)
(end nil end-p))
"Write the characters of the subsequence of STRING bounded by START
and END to OUTPUT-STREAM."
(let* ((stream (%real-print-stream output-stream)))
(if (typep stream 'basic-stream)
(let* ((ioblock (basic-stream-ioblock stream)))
(with-ioblock-output-locked (ioblock)
(if (and (typep string 'simple-string)
(not start-p) (not end-p))
(funcall (ioblock-write-simple-string-function ioblock)
ioblock string 0 (length string))
(progn
(setq end (check-sequence-bounds string start end))
(locally (declare (fixnum start end))
(multiple-value-bind (arr offset)
(if (typep string 'simple-string)
(values string 0)
(array-data-and-offset (require-type string 'string)))
(unless (eql 0 offset)
(incf start offset)
(incf end offset))
(funcall (ioblock-write-simple-string-function ioblock)
ioblock arr start (the fixnum (- end start)))))))))
(if (and (not start-p) (not end-p))
(stream-write-string stream string)
(stream-write-string stream string start end)))
string))
(defun write-simple-string (string output-stream start end)
"Write the characters of the subsequence of simple-string STRING bounded by START
and END to OUTPUT-STREAM."
(let* ((stream (%real-print-stream output-stream))
(string (the simple-string string))) ;; typecheck at high safety.
(if (typep stream 'basic-stream)
(let* ((ioblock (basic-stream-ioblock stream))
(start (or start 0)))
(with-ioblock-output-locked (ioblock)
(if (and (eq start 0) (null end))
(funcall (ioblock-write-simple-string-function ioblock)
ioblock string 0 (length string))
(let* ((end (check-sequence-bounds string start end)))
(funcall (ioblock-write-simple-string-function ioblock)
ioblock string start (%i- end start))))))
(if (and (not start) (not end))
(stream-write-string stream string)
(stream-write-string stream string start (or end (length string)))))
string))
(defun write-line (string &optional output-stream
&key (start 0) (end (length string)))
"Write the characters of the subsequence of STRING bounded by START
and END to OUTPUT-STREAM then output a #\Newline at end."
(write-string string output-stream :start start :end end)
(terpri output-stream)
string)
(defun terpri (&optional (stream *standard-output*))
(let* ((stream (%real-print-stream stream)))
(if (typep stream 'basic-stream)
(let* ((ioblock (basic-stream-ioblock stream)))
(funcall (ioblock-write-char-function ioblock) ioblock #\newline))
(stream-write-char stream #\newline))
nil))
;;;; ----------------------------------------------------------------------
;;;; ======================================================================
;;;; The Lisp Printer
;; coral extensions
(def-standard-initial-binding *print-abbreviate-quote* t
"Non-NIL means that the normal lisp printer --
not just the pretty-printer -- should print
lists whose first element is QUOTE or FUNCTION specially.
This variable is not part of standard Common Lisp.")
(def-standard-initial-binding *print-structure* t
"Non-NIL means that lisp structures should be printed using
\"#S(...)\" syntax. if nil, structures are printed using \"#<...>\".
This variable is not part of standard Common Lisp.")
;; things Richard Mlynarik likes.
(def-standard-initial-binding *print-simple-vector* nil
"Non-NIL means that simple-vectors whose length is less than
the value of this variable are printed even if *PRINT-ARRAY* is false.
this variable is not part of standard Common Lisp.")
(def-standard-initial-binding *print-simple-bit-vector* nil
"Non-NIL means that simple-bit-vectors whose length is less than
the value of this variable are printed even if *PRINT-ARRAY* is false.
This variable is not part of standard Common Lisp.")
(def-standard-initial-binding *print-string-length* nil
"Non-NIL means that strings longer than this are printed
using abbreviated #<string ...> syntax.
This variable is not part of standard Common Lisp.")
(def-standard-initial-binding *print-escape* t
"Non-NIL means that the lisp printer should -attempt- to output
expressions `readably.' When NIL the attempts to produce output
which is a little more human-readable (for example, pathnames
are represented by the characters of their namestring.)")
(def-standard-initial-binding *print-pretty* nil
"Non-NIL means that the lisp printer should insert extra
indentation and newlines to make output more readable and `prettier.'")
(def-standard-initial-binding *print-base* 10.
"The output base for integers and rationals.
Must be an integer between 2 and 36.")
(def-standard-initial-binding *print-radix* nil
"Non-NIL means that the lisp printer will explicitly indicate
the output radix (see *PRINT-BASE*) which is used to print
integers and rational numbers.")
(def-standard-initial-binding *print-level* nil
"Specifies the depth at which printing of lisp expressions
should be truncated. NIL means that no such truncation should occur.
Truncation is indicated by printing \"#\" instead of the
representation of the too-deeply-nested structure.
See also *PRINT-LENGTH*")
(def-standard-initial-binding *print-length* nil
"Specifies the length at which printing of lisp expressions
should be truncated. NIL means that no such truncation should occur.
truncation is indicated by printing \"...\" instead of the
rest of the overly-long list or vector.
See also *PRINT-LEVEL*")
(def-standard-initial-binding *print-circle* nil
"Non-NIL means that the lisp printer should attempt to detect
circular structures, indicating them by using \"#n=\" and \"#n#\" syntax.
If this variable is false then an attempt to
output circular structure may cause unbounded output.")
(def-standard-initial-binding *print-case* ':upcase
"Specifies the alphabetic case in which symbols should
be printed. Possible values include :UPCASE, :DOWNCASE and :CAPITALIZE") ; and :StuDLy
(def-standard-initial-binding *print-array* t
"Non-NIL means that arrays should be printed using \"#(...)\" or
\"=#nA(...)\" syntax to show their contents.
If NIL, arrays other than strings are printed using \"#<...>\".
See also the (non-Common Lisp) variables *PRINT-SIMPLE-VECTOR*
and *PRINT-SIMPLE-BIT-VECTOR*")
(def-standard-initial-binding *print-gensym* t
"Non-NIL means that symbols with no home package should be
printed using \"#:\" syntax. NIL means no prefix is printed.")
(def-standard-initial-binding *print-readably* nil
"Non-NIL means that attempts to print unreadable objects
signal PRINT-NOT-READABLE errors. NIL doesn't.")
(def-standard-initial-binding *PRINT-RIGHT-MARGIN* nil
"+#/NIL the right margin for pretty printing")
(def-standard-initial-binding *PRINT-MISER-WIDTH* 40.
"+#/NIL miser format starts when there is less than this width left")
(def-standard-initial-binding *PRINT-LINES* nil
"+#/NIL truncates printing after # lines")
(def-standard-initial-binding *DEFAULT-RIGHT-MARGIN* 70
"Controls default line length; Must be a non-negative integer")
(defvar *xp-current-object* nil) ; from xp
(defvar *circularity-hash-table* nil) ; ditto
(defvar *current-level* nil)
(defvar *current-length* nil) ; must be nil at top level
(defvar *print-catch-errors* nil)
(defun default-print-level (val)
(if (eq val :default) *print-level* val))
(defun default-print-length (val)
(if (eq val :default) *print-length* val))
(defun default-print-string-length (val)
(if (eq val :default) *print-string-length* val))
;;;; ======================================================================
(defclass xp-stream (output-stream)
(xp-structure))
(defun %write-string (string stream)
(if (characterp string)
(stream-write-char stream string)
(stream-write-entire-string stream string)))
;; *print-simple-vector*
;; *print-simple-bit-vector*
;; *print-string-length*
;; for things like *print-level* which must [no longer] be integers > 0
(defun get-*print-frob* (symbol
&optional (nil-means target::target-most-positive-fixnum)
(t-means nil))
(declare (type symbol symbol))
(let ((value (symbol-value symbol)))
(when *print-readably*
(case symbol
((*print-length* *print-level* *print-lines* *print-string-length*)
(setq value nil))
((*print-escape* *print-gensym* *print-array* *print-simple-vector*
*print-simple-bit-vector*)
(setq value t))
(t nil)))
(cond ((null value)
nil-means)
((and (integerp value)) ; (> value 0))
(min (max value -1) value target::target-most-positive-fixnum))
((and t-means (eq value 't))
t-means)
(t
(setf (symbol-value symbol) nil)
(error "~s had illegal value ~s. reset to ~s"
symbol value 'nil)))))
(defun pp-newline (stream kind)
(case kind
((:newline)
(fresh-line stream))
((:unconditional :mandatory)
(stream-write-char stream #\Newline))
(t nil)))
(defun pp-space (stream &optional (newline-kind ':fill))
(stream-write-char stream #\space)
(pp-newline stream newline-kind))
(defun pp-start-block (stream &optional prefix)
(cond ((null prefix))
((characterp prefix)
(stream-write-char stream prefix))
((stringp prefix)
(%write-string prefix stream))
(t (report-bad-arg prefix '(or character string (eql nil))))))
(defun pp-end-block (stream &optional suffix)
(cond ((null suffix))
((characterp suffix)
(stream-write-char stream suffix))
((stringp suffix)
(%write-string suffix stream))
(t (report-bad-arg suffix '(or character string (eql nil))))))
#|
(defmethod pp-set-indentation ((stream stream) kind n)
(declare (ignore kind n))
nil)
|#
;;;; ======================================================================
;; list-kludge is so that we can simultaneously detect shared list tails
;; and avoid printing lists as (foo . (bar . (baz . nil)))
;; if non-nil, it is the remaining *print-length* and object is
;; a list tail
(defmethod write-internal-1 ((stream t) object level list-kludge)
(declare (type fixnum level) (type (or null fixnum) list-kludge))
;;>> Anybody passing in list-kludge had better be internal to the lisp printer.
;(if list-kludge (error "Internal printer error"))
(let ((circle *print-circle*)
(pretty *print-pretty*))
(cond ((or pretty circle)
; what about this level stuff??
; most peculiar
(maybe-initiate-xp-printing
#'(lambda (s o) (write+ o s)) stream object))
((not list-kludge)
(write-a-frob object stream level list-kludge))
((null object))
(t
(stream-write-char stream #\space)
(when (not (consp object))
(stream-write-char stream #\.)
(stream-write-char stream #\space))
(write-a-frob object stream level list-kludge)))))
(defmethod write-internal-1 ((stream xp-stream) object level list-kludge)
(when level
(setq *current-level* (if (and *print-level* (not *print-readably*))
(- *print-level* level)
0)))
(write+ object (slot-value stream 'xp-structure) list-kludge))
(defvar *inside-printer-error* nil)
(defvar *signal-printing-errors* nil)
(queue-fixup (setq *signal-printing-errors* t))
(defun write-internal (stream object level list-kludge)
(if (bogus-thing-p object)
(print-unreadable-object
(object stream)
(princ (%str-cat "BOGUS object @ #x" (%integer-to-string (%address-of object) 16.))
stream))
(progn
(flet ((handler (condition)
(declare (ignore condition))
(unless *signal-printing-errors*
(return-from write-internal
(let ((*print-pretty* nil)
(*print-circle* nil))
(if *inside-printer-error*
(when (eql 1 (incf *inside-printer-error*))
(%write-string "#<Recursive printing error " stream)
(stream-write-char stream #\space)
(%write-address (%address-of object) stream)
(stream-write-char stream #\>))
(let ((*inside-printer-error* 0))
; using format here considered harmful.
(%write-string "#<error printing " stream)
(write-internal stream (type-of object) (max level 2) nil)
(stream-write-char stream #\space)
(%write-address (%address-of object) stream)
(stream-write-char stream #\>))))))))
(declare (dynamic-extent #'handler))
(handler-bind
((error #'handler))
(write-internal-1 stream object level list-kludge)))
object)))
;;;; ======================================================================
;;;; internals of write-internal
;; bd common-lisp (and lisp machine) printer depth counts
;; count from 0 upto *print-level* instead of from
;; *print-level* down to 0 (which this printer sensibly does.)
(defun backtranslate-level (level)
(let ((print-level (get-*print-frob* '*print-level*)))
(if (not (and level print-level))
target::target-most-positive-fixnum
(if (> level print-level)
;; wtf!
1
(- print-level level)))))
; so we can print-circle for print-object methods.
(defvar %current-write-level% nil)
(defvar %current-write-stream% nil)
(defun %current-write-level% (stream &optional decrement?)
(if (eq stream %current-write-stream%)
(if decrement? (1- %current-write-level%) %current-write-level%)
(get-*print-frob* '*print-level*)))
;;>> Some notes:
;;>> CL defining print-object to be a multmethod dispatching on
;;>> both the object and the stream just can't work
;;>> There are a couple of reasons:
;;>> - CL wants *print-circle* structure to be automatically detected
;;>> This means that there must be a printing pre-pass to some stream
;;>> other than the one specified by the user, which means that any
;;>> print-object method which specialises on its second argument is
;;>> going to lose big.
;;>> - CL wants *print-level* truncation to happen automatically
;;>> and doesn't pass a level argument to print-object (as it should)
;;>> This means that the current level must be associated with the
;;>> stream in some fashion. The quicky kludge Bill uses here
;;>> (binding a special variable) loses for
;;>> + Entering a break loop whilst printing to a stream
;;>> (Should start level from (get-*print-level*) again)
;;>> + Performing output to more than one stream in an interleaved fashion
;;>> (Say a print-object method which writes to *trace-output*)
;;>> The solution, again, is to actually call the print-object methods
;;>> on a write-aux-stream, where that stream is responsible for
;;>> doing *print-level* truncation.
;;>> - BTW The select-method-order should be (stream object) to even have
;;>> a chance of winning. Not that it could win in any case, for the above reasons.
;;>> It isn't that much work to change the printer to always use an
;;>> automatically-level-truncating write-aux-stream
;;>> It is a pity that CL is so BD.
;;>>
(defun write-a-frob (object stream level list-kludge)
(declare (type stream stream) (type fixnum level)
(type (or null fixnum) list-kludge))
(cond ((not list-kludge)
(let ((%current-write-stream% stream) ;>> SIGH
(%current-write-level% level))
(print-object object stream)))
((%i< list-kludge 1)
;; *print-length* truncation
(stream-write-entire-string stream "..."))
((not (consp object))
(write-a-frob object stream level nil))
(t
(write-internal stream (%car object) level nil)
;;>> must do a tail-call!!
(write-internal-1 stream (%cdr object) level (if (consp (%cdr object))
(%i- list-kludge 1)
list-kludge)))))
(defmethod print-object :around ((object t) stream)
(if *print-catch-errors*
(handler-case (call-next-method)
(error () (write-string "#<error printing object>" stream)))
(call-next-method)))
(defmethod print-object ((object t) stream)
(let ((level (%current-write-level% stream)) ; what an abortion. This should be an ARGUMENT!
(%type (%type-of object)))
(declare (type symbol %type)
(type fixnum level))
(flet ((depth (stream v)
(declare (type fixnum v) (type stream stream))
(when (%i<= v 0)
;; *print-level* truncation
(stream-write-entire-string stream "#")
t)))
(cond
((eq %type 'cons)
(unless (depth stream level)
(write-a-cons object stream level)))
;; Don't do *print-level* truncation for anything between
;; here and the (depth ...) case.
((or (eq %type 'symbol)
(null object))
(write-a-symbol object stream))
((or (stringp object)
(bit-vector-p object))
(cond ((or (not (stringp object))
(%i> (length (the string object))
(get-*print-frob* '*print-string-length*)))
(write-an-array object stream level))
((or *print-escape* *print-readably*)
(write-escaped-string object stream))
(t
(%write-string object stream))))
((and (eq %type 'structure)
(not (null (ccl::struct-def object)))
(null (cdr (sd-slots (ccl::struct-def object)))))
;; else fall through to write-a-uvector
;; ansi PRINT-LEVEL.8 & PRINT-LEVEL.9
(unless (depth stream level)
(write-a-structure object stream level)))
((depth stream level))
((eq %type 'package)
(write-a-package object stream))
((eq %type 'macptr)
(write-a-macptr object stream))
((eq %type 'dead-macptr)
(write-a-dead-macptr object stream))
((eq %type 'internal-structure)
(write-an-istruct object stream level))
((and (eq %type 'structure)
(not (null (ccl::struct-def object))))
;; else fall through to write-a-uvector
(if (and *print-pretty* *print-structure*)
(let ((*current-level* (if (and *print-level* (not *print-readably*))
(- *print-level* level)
0)))
(pretty-structure stream object))
(write-a-structure object stream level)))
((functionp object)
(write-a-function object stream level))
((arrayp object)
(cond ((or (not (stringp object))
(%i> (length (the string object))
(get-*print-frob* '*print-string-length*)))
(write-an-array object stream level))
((or *print-escape* *print-readably*)
(write-escaped-string object stream))
(t
(%write-string object stream))))
; whazzat
((uvectorp object)
(write-a-uvector object stream level))
(t
(print-unreadable-object (object stream)
(let* ((address (%address-of object)))
(cond ((eq object (%unbound-marker-8))
(%write-string "Unbound" stream))
((eq object (%slot-unbound-marker))
(%write-string "Slot-Unbound" stream))
(t
(cond
(t
(%write-string "Unprintable " stream)
(write-a-symbol %type stream)
(%write-string " : " stream)))
(%write-address address stream))))))))
nil))
(defun write-a-dead-macptr (macptr stream)
(print-unreadable-object (macptr stream)
(%write-string "A Dead Mac Pointer" stream)))
;;;; ======================================================================
;;;; Powerful, wonderful tools for printing unreadable objects.
(defun print-not-readable-error (object stream)
(error (make-condition 'print-not-readable :object object :stream stream)))
; Start writing an unreadable OBJECT on STREAM, error out if *PRINT-READABLY* is true.
(defun write-unreadable-start (object stream)
(if *print-readably*
(print-not-readable-error object stream)
(pp-start-block stream "#<")))
(defun %print-unreadable-object (object stream type id thunk)
(cond ((null stream) (setq stream *standard-output*))
((eq stream t) (setq stream *terminal-io*)))
(write-unreadable-start object stream)
(when type
(princ (type-of object) stream))
(when thunk
(when type (stream-write-char stream #\space))
(funcall thunk))
(if id
(%write-address object stream #\>)
(pp-end-block stream ">"))
nil)
;;;; ======================================================================
;;;; internals of internals of write-internal
(defmethod print-object ((char character) stream &aux name)
(cond ((or *print-escape* *print-readably*) ;print #\ for read-ability
(stream-write-char stream #\#)
(stream-write-char stream #\\)
(if (and (or (eql char #\newline)
(not (standard-char-p char)))
(setq name (char-name char)))
(%write-string name stream)
(stream-write-char stream char)))
(t
(stream-write-char stream char))))
(defun get-*print-base* ()
(let ((base *print-base*))
(unless (and (fixnump base)
(%i< 1 base) (%i< base 37.))
(setq *print-base* 10.)
(error "~S had illegal value ~S. Reset to ~S"
'*print-base* base 10))
base))
(defun write-radix (base stream)
(stream-write-char stream #\#)
(case base
(2 (stream-write-char stream #\b))
(8 (stream-write-char stream #\o))
(16 (stream-write-char stream #\x))
(t (%pr-integer base 10. stream)
(stream-write-char stream #\r))))
(defun write-an-integer (num stream
&optional (base (get-*print-base*))
(print-radix *print-radix*))
(when (and print-radix (not (eq base 10)))
(write-radix base stream))
(%pr-integer num base stream)
(when (and print-radix (eq base 10))
(stream-write-char stream #\.)))
(defmethod print-object ((num integer) stream)
(write-an-integer num stream))
(defun %write-address (object stream &optional foo)
(if foo (pp-space stream))
(write-an-integer (if (integerp object) object (%address-of object)) stream 16. t)
(if foo (pp-end-block stream foo)))
(defmethod print-object ((num ratio) stream)
(let ((base (get-*print-base*)))
(when *print-radix*
(write-radix base stream))
(%pr-integer (numerator num) base stream)
(stream-write-char stream #\/)
(%pr-integer (denominator num) base stream)))
;;>> Doesn't do *print-level* truncation
(defmethod print-object ((c complex) stream)
(pp-start-block stream "#C(")
(print-object (realpart c) stream)
(pp-space stream)
(print-object (imagpart c) stream)
(pp-end-block stream #\)))
(defmethod print-object ((float float) stream)
(print-a-float float stream))
(defun float-exponent-char (float)
(if (case *read-default-float-format*
(single-float (typep float 'single-float))
(double-float (typep float 'double-float))
(t (typep float *read-default-float-format*)))
#\E
(if (typep float 'double-float)
#\D
#\S)))
(defun default-float-p (float)
(case *read-default-float-format*
(single-float (typep float 'single-float))
(double-float (typep float 'double-float))
(t (typep float *read-default-float-format*))))
(defun print-a-nan (float stream)
(if (infinity-p float)
(output-float-infinity float stream)
(output-float-nan float stream)))
(defun output-float-infinity (x stream)
(declare (float x) (stream stream))
(format stream "~:[-~;~]1~c++0"
(plusp x)
(if (typep x *read-default-float-format*)
#\E
(typecase x
(double-float #\D)
(single-float #\S)))))
(defun output-float-nan (x stream)
(declare (float x) (stream stream))
(format stream "1~c+-0 #| not-a-number |#"
(if (typep x *read-default-float-format*)
#\E
(etypecase x
(double-float #\D)
(single-float #\S)))))
(defun print-float-free-form-1 (string before-pt stream)
(declare (fixnum before-pt))
(let ((strlen (length string)))
(declare (fixnum strlen))
(cond ((zerop strlen)
(stream-write-entire-string stream "0.0"))
((> before-pt 0)
(cond ((> strlen before-pt)
(write-string string stream :start 0 :end before-pt)
(stream-write-char stream #\.)
(write-string string stream :start before-pt :end strlen))
(t ; 0's after
(stream-write-entire-string stream string)
(dotimes (i (- before-pt strlen))
(stream-write-char stream #\0))
(stream-write-entire-string stream ".0"))))
(t
(stream-write-entire-string stream "0.")
(dotimes (i (- before-pt))
(stream-write-char stream #\0))
(stream-write-entire-string stream string)))))
(defun print-float-free-form (float stream)
(setq stream (%real-print-stream stream))
(if (nan-or-infinity-p float)
(print-a-nan float stream)
(multiple-value-bind (string before-pt)
(flonum-to-string float)
(when (minusp (float-sign float))
(stream-write-char stream #\-))
(print-float-free-form-1 string before-pt stream))))
;; nanning => recursive from print-a-nan - don't check again
(defun print-a-float (float stream &optional exp-p nanning)
(let ((strlen 0)
(exponent-char (float-exponent-char float)))
(declare (fixnum strlen))
(setq stream (%real-print-stream stream))
(if (and (not nanning)(nan-or-infinity-p float))
(print-a-nan float stream)
(multiple-value-bind (string before-pt)
(flonum-to-string float)
(declare (fixnum before-pt))
(setq strlen (length string))
(when (minusp (float-sign float))
(stream-write-char stream #\-))
(cond
((and (not exp-p)
(> before-pt -3)
(<= before-pt 7))
(print-float-free-form-1 string before-pt stream))
(t
(setq exp-p t)
(stream-write-char stream (if (> strlen 0)(char string 0) #\0))
(stream-write-char stream #\.)
(if (> strlen 1)
(write-string string stream :start 1 :end strlen)
(stream-write-char stream #\0))
(stream-write-char stream exponent-char)
(when (and exp-p (not (minusp (1- before-pt))))
(stream-write-char stream #\+))
(let ((*print-base* 10)
(*print-radix* nil))
(princ (1- before-pt) stream))))
(when (and (not exp-p)
(not (default-float-p float)))
(stream-write-char stream exponent-char)
(stream-write-char stream #\0))))))
;;>> Doesn't do *print-level* truncation
(defmethod print-object ((class class) stream)
(print-unreadable-object (class stream)
(print-object (class-name (class-of class)) stream)
(pp-space stream)
(print-object (class-name class) stream)))
(defmethod print-object ((value-cell value-cell) stream)
(print-unreadable-object (value-cell stream :type t :identity t)
(prin1 (uvref value-cell target::value-cell.value-cell) stream)))
;(defun symbol-begins-with-vowel-p (sym)
; (and (symbolp sym)
; (not (%izerop (%str-length (setq sym (symbol-name sym)))))
; (%str-member (schar sym 0) "AEIOU")))
;;;; ----------------------------------------------------------------------
;;;; CLOSsage
(defmethod print-object ((instance standard-object) stream)
(if (%i<= %current-write-level% 0) ; *print-level* truncation
(stream-write-entire-string stream "#")
(print-unreadable-object (instance stream :identity t)
(let* ((class (class-of instance))
(class-name (class-name class)))
(cond ((not (and (symbolp class-name)
(eq class (find-class class-name nil))))
(%write-string "An instance of" stream)
(pp-space stream)
(print-object class stream))
(t
(write-a-symbol class-name stream)))))))
(defmethod print-object ((method standard-method) stream)
(print-method method stream (%class.name (class-of method))))
(defmethod print-object ((method-function method-function) stream)
(let ((method (%method-function-method method-function)))
(if (typep method 'standard-method)
(print-method (%method-function-method method-function)
stream
(%class.name (class-of method-function)))
(call-next-method))))
(defun print-method (method stream type-string)
(print-unreadable-object (method stream)
(let ((name (%method-name method))
(qualifiers (%method-qualifiers method))
(specializers (mapcar #'(lambda (specializer)
(if (typep specializer 'eql-specializer)
(list 'eql
(eql-specializer-object specializer))
(or (class-name specializer)
specializer)))
(%method-specializers method)))
(level-1 (%i- %current-write-level% 1)))
(cond
((< level-1 0)
;; *print-level* truncation
(stream-write-entire-string stream "#"))
(t
(prin1 type-string stream)
(pp-space stream)
(write-internal stream name level-1 nil)
(pp-space stream)
(when qualifiers
(write-internal stream (if (cdr qualifiers) qualifiers (car qualifiers))
level-1 nil)
(pp-space stream))
(write-internal stream specializers level-1 nil))))))
;; Need this stub or we'll get the standard-object method
(defmethod print-object ((gf standard-generic-function) stream)
(write-a-function gf stream (%current-write-level% stream)))
;; This shouldn't ever happen, but if it does, don't want the standard-object method
(defmethod print-object ((mo metaobject) stream)
(print-unreadable-object (mo stream :type t :identity t)))
(defmethod print-object ((cm combined-method) stream)
(print-unreadable-object (cm stream :identity t)
(%write-string "Combined-Method" stream)
(pp-space stream)
(let ((name (function-name cm)))
(if (and (functionp name) (function-is-current-definition? name))
(setq name (function-name name)))
(write-internal stream name (%current-write-level% stream) nil))))
(defun print-specializer-names (specializers stream)
(flet ((print-specializer (spec stream)
(write-1 (if (typep spec 'class) (%class.name spec) spec) stream)))
(pp-start-block stream #\()
(if (atom specializers)
(print-specializer specializers stream)
(progn (print-specializer (car specializers) stream)
(dolist (spec (cdr specializers))
(pp-space stream)
(print-specializer spec stream))))
(pp-end-block stream #\))))
;;;; ----------------------------------------------------------------------
(defun write-a-cons (cons stream level)
(declare (type cons cons) (type stream stream) (type fixnum level))
(let ((print-length (get-*print-frob* '*print-length*))
(level-1 (%i- level 1))
(head (%car cons))
(tail (%cdr cons)))
(declare (type fixnum print-length) (type fixnum level-1))
(unless (and *print-abbreviate-quote*
(write-abbreviate-quote head tail stream level-1))
(progn
(pp-start-block stream #\()
(if (= print-length 0)
(%write-string "..." stream)
(progn
(write-internal stream head level-1 nil)
(write-internal stream tail level-1
(if (atom tail)
print-length
(%i- print-length 1)))))
(pp-end-block stream #\))))))
;;;; hack for quote and backquote
;; for debugging
;(setq *backquote-expand* nil)
(defvar *backquote-hack* (list '*backquote-hack*)) ;uid
(defun write-abbreviate-quote (head tail stream level-1)
(declare (type stream stream) (type fixnum level-1))
(when (symbolp head)
(cond ((or (eq head 'quote) (eq head 'function))
(when (and (consp tail)
(null (%cdr tail)))
(%write-string (if (eq head 'function) "#'" "'") stream)
(write-internal stream (%car tail) level-1 nil)
t))
((eq head 'backquote-expander)
(when (and (consp tail)
(consp (cdr tail))
(consp (cddr tail))
(consp (cdddr tail))
(null (cddddr tail)))
(let ((tail tail))
(set (%car tail)
*backquote-hack*) ;,
(set (%car (setq tail (%cdr tail)))
*backquote-hack*) ;,.
(set (%car (setq tail (%cdr tail)))
*backquote-hack*) ;,@
(stream-write-char stream #\`)
(write-internal stream (%cadr tail) level-1 nil)
t)))
((and (boundp head)
(eq (symbol-value head) *backquote-hack*))
;;",foo" = (#:|,| . foo)
(stream-write-char stream #\,)
(let* ((n (symbol-name head))
(l (length n)))
(declare (type simple-string n) (type fixnum l))
;; possibilities are #:|`,| #:|,.| and #:|,@|
(if (eql l 3)
(stream-write-char stream (schar n 2)))
(write-internal stream tail level-1 nil)
t))
(t nil))))
(eval-when (compile eval)
(defmacro %char-needs-escape-p (char escape &rest losers)
(setq losers (remove-duplicates (cons escape losers)))
(setq char (require-type char 'symbol))
(dolist (c losers)
(unless (or (characterp c) (symbolp c)) (report-bad-arg c '(or character symbol))))
(cond ((null (cdr losers))
`(eq ,char ,escape))
((and (every #'characterp losers)
;(every #'string-char-p losers)
(%i> (length losers) 2))
`(%str-member ,char ,(concatenate 'string losers)))
(t
`(or ,@(mapcar #'(lambda (e) `(eq ,char ,e))
losers)))))
(defmacro %write-escaped-char (stream char escape &rest losers)
`(progn
(when (%char-needs-escape-p ,char ,escape ,@losers)
(stream-write-char ,stream ,escape))
(stream-write-char ,stream ,char)))
)
(defun write-escaped-string (string stream &optional (delim #\"))
(declare (type string string) (type character delim)
(type stream stream))
(write-char delim stream)
(do* ((limit (length string))
(i 0 (1+ i)))
((= i limit))
(declare (type fixnum limit) (type fixnum i))
(let* ((char (char string i))
(needs-escape? (%char-needs-escape-p char #\\ delim)))
(if needs-escape?
(write-char #\\ stream))
(write-char char stream)))
(write-char delim stream))