3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 ;;;; exported printer control variables
16 ;;; FIXME: Many of these have nontrivial types, e.g. *PRINT-LEVEL*,
17 ;;; *PRINT-LENGTH*, and *PRINT-LINES* are (OR NULL UNSIGNED-BYTE).
19 (defvar *print-readably* nil
21 "If true, all objects will printed readably. If readable printing is
22 impossible, an error will be signalled. This overrides the value of
24 (defvar *print-escape* T
26 "Flag which indicates that slashification is on. See the manual")
27 (defvar *print-pretty* nil ; (set later when pretty-printer is initialized)
29 "Flag which indicates that pretty printing is to be used")
30 (defvar *print-base* 10.
32 "The output base for integers and rationals.")
33 (defvar *print-radix* nil
35 "This flag requests to verify base when printing rationals.")
36 (defvar *print-level* nil
38 "How many levels deep to print. Unlimited if null.")
39 (defvar *print-length* nil
41 "How many elements to print on each level. Unlimited if null.")
42 (defvar *print-circle* nil
44 "Whether to worry about circular list structures. See the manual.")
45 (defvar *print-case* :upcase
47 "What kind of case the printer should use by default")
48 (defvar *print-array* t
50 "Whether the array should print its guts out")
51 (defvar *print-gensym* t
53 "If true, symbols with no home package are printed with a #: prefix.
54 If false, no prefix is printed.")
55 (defvar *print-lines* nil
57 "The maximum number of lines to print. If NIL, unlimited.")
58 (defvar *print-right-margin* nil
60 "The position of the right margin in ems. If NIL, try to determine this
61 from the stream in use.")
62 (defvar *print-miser-width* nil
64 "If the remaining space between the current column and the right margin
65 is less than this, then print using ``miser-style'' output. Miser
66 style conditional newlines are turned on, and all indentations are
67 turned off. If NIL, never use miser mode.")
68 (defvar *print-pprint-dispatch* nil
70 "The pprint-dispatch-table that controls how to pretty print objects. See
71 COPY-PPRINT-DISPATH, PPRINT-DISPATCH, and SET-PPRINT-DISPATCH.")
73 (defmacro with-standard-io-syntax (&body body)
75 "Bind the reader and printer control variables to values that enable READ
76 to reliably read the results of PRINT. These values are:
77 *PACKAGE* the COMMON-LISP-USER package
87 *PRINT-MISER-WIDTH* NIL
91 *PRINT-RIGHT-MARGIN* NIL
93 *READ-DEFAULT-FLOAT-FORMAT* SINGLE-FLOAT
96 *READTABLE* the standard readtable."
97 `(%with-standard-io-syntax #'(lambda () ,@body)))
99 (defun %with-standard-io-syntax (function)
100 (let ((*package* (find-package "COMMON-LISP-USER"))
103 (*print-case* :upcase)
110 (*print-miser-width* nil)
114 (*print-right-margin* nil)
116 (*read-default-float-format* 'single-float)
118 (*read-suppress* nil)
119 ;; FIXME: It doesn't seem like a good idea to expose our
120 ;; disaster-recovery *STANDARD-READTABLE* here. Perhaps we
121 ;; should do a COPY-READTABLE? The consing would be unfortunate,
123 (*readtable* *standard-readtable*))
126 ;;;; routines to print objects
128 (defun write (object &key
129 ((:stream stream) *standard-output*)
130 ((:escape *print-escape*) *print-escape*)
131 ((:radix *print-radix*) *print-radix*)
132 ((:base *print-base*) *print-base*)
133 ((:circle *print-circle*) *print-circle*)
134 ((:pretty *print-pretty*) *print-pretty*)
135 ((:level *print-level*) *print-level*)
136 ((:length *print-length*) *print-length*)
137 ((:case *print-case*) *print-case*)
138 ((:array *print-array*) *print-array*)
139 ((:gensym *print-gensym*) *print-gensym*)
140 ((:readably *print-readably*) *print-readably*)
141 ((:right-margin *print-right-margin*)
142 *print-right-margin*)
143 ((:miser-width *print-miser-width*)
145 ((:lines *print-lines*) *print-lines*)
146 ((:pprint-dispatch *print-pprint-dispatch*)
147 *print-pprint-dispatch*))
149 "Outputs OBJECT to the specified stream, defaulting to *STANDARD-OUTPUT*"
150 (output-object object (out-synonym-of stream))
153 (defun prin1 (object &optional stream)
155 "Outputs a mostly READable printed representation of OBJECT on the specified
157 (let ((*print-escape* T))
158 (output-object object (out-synonym-of stream)))
161 (defun princ (object &optional stream)
163 "Outputs an aesthetic but not necessarily READable printed representation
164 of OBJECT on the specified STREAM."
165 (let ((*print-escape* NIL)
166 (*print-readably* NIL))
167 (output-object object (out-synonym-of stream)))
170 (defun print (object &optional stream)
172 "Outputs a terpri, the mostly READable printed represenation of OBJECT, and
173 space to the specified STREAM."
174 (let ((stream (out-synonym-of stream)))
176 (prin1 object stream)
177 (write-char #\space stream)
180 (defun pprint (object &optional stream)
182 "Prettily outputs OBJECT preceded by a newline."
183 (let ((*print-pretty* t)
185 (stream (out-synonym-of stream)))
187 (output-object object stream))
190 (defun write-to-string
192 ((:escape *print-escape*) *print-escape*)
193 ((:radix *print-radix*) *print-radix*)
194 ((:base *print-base*) *print-base*)
195 ((:circle *print-circle*) *print-circle*)
196 ((:pretty *print-pretty*) *print-pretty*)
197 ((:level *print-level*) *print-level*)
198 ((:length *print-length*) *print-length*)
199 ((:case *print-case*) *print-case*)
200 ((:array *print-array*) *print-array*)
201 ((:gensym *print-gensym*) *print-gensym*)
202 ((:readably *print-readably*) *print-readably*)
203 ((:right-margin *print-right-margin*) *print-right-margin*)
204 ((:miser-width *print-miser-width*) *print-miser-width*)
205 ((:lines *print-lines*) *print-lines*)
206 ((:pprint-dispatch *print-pprint-dispatch*)
207 *print-pprint-dispatch*))
209 "Returns the printed representation of OBJECT as a string."
210 (stringify-object object))
212 (defun prin1-to-string (object)
214 "Returns the printed representation of OBJECT as a string with
216 (stringify-object object t))
218 (defun princ-to-string (object)
220 "Returns the printed representation of OBJECT as a string with
222 (stringify-object object nil))
224 ;;; This produces the printed representation of an object as a string. The
225 ;;; few ...-TO-STRING functions above call this.
226 (defvar *string-output-streams* ())
227 (defun stringify-object (object &optional (*print-escape* *print-escape*))
228 (let ((stream (if *string-output-streams*
229 (pop *string-output-streams*)
230 (make-string-output-stream))))
231 (setup-printer-state)
232 (output-object object stream)
234 (get-output-stream-string stream)
235 (push stream *string-output-streams*))))
237 ;;;; support for the PRINT-UNREADABLE-OBJECT macro
239 (defun %print-unreadable-object (object stream type identity body)
240 (when *print-readably*
241 (error 'print-not-readable :object object))
242 (write-string "#<" stream)
244 (write (type-of object) :stream stream :circle nil
245 :level nil :length nil)
246 (write-char #\space stream))
250 (unless (and type (null body))
251 (write-char #\space stream))
252 (write-char #\{ stream)
253 (write (get-lisp-obj-address object) :stream stream
255 (write-char #\} stream))
256 (write-char #\> stream)
259 ;;;; WHITESPACE-CHAR-P
261 ;;; This is used in other files, but is defined in this one for some reason.
263 (defun whitespace-char-p (char)
265 "Determines whether or not the character is considered whitespace."
266 (or (char= char #\space)
267 (char= char (code-char tab-char-code))
268 (char= char (code-char return-char-code))
269 (char= char #\linefeed)))
271 ;;;; circularity detection stuff
273 ;;; When *PRINT-CIRCLE* is T, this gets bound to a hash table that (eventually)
274 ;;; ends up with entries for every object printed. When we are initially
275 ;;; looking for circularities, we enter a T when we find an object for the
276 ;;; first time, and a 0 when we encounter an object a second time around.
277 ;;; When we are actually printing, the 0 entries get changed to the actual
278 ;;; marker value when they are first printed.
279 (defvar *circularity-hash-table* nil)
281 ;;; When NIL, we are just looking for circularities. After we have found them
282 ;;; all, this gets bound to 0. Then whenever we need a new marker, it is
284 (defvar *circularity-counter* nil)
286 (defun check-for-circularity (object &optional assign)
288 "Check to see whether OBJECT is a circular reference, and return something
289 non-NIL if it is. If ASSIGN is T, then the number to use in the #n= and
290 #n# noise is assigned at this time. Note: CHECK-FOR-CIRCULARITY must
291 be called *EXACTLY* once with ASSIGN T, or the circularity detection noise
292 will get confused about when to use #n= and when to use #n#. If this
293 returns non-NIL when ASSIGN is T, then you must call HANDLE-CIRCULARITY
294 on it. If you are not using this inside a WITH-CIRCULARITY-DETECTION,
295 then you have to be prepared to handle a return value of :INITIATE which
296 means it needs to initiate the circularity detection noise. See the
297 source for info on how to do that."
298 (cond ((null *print-circle*)
299 ;; Don't bother, nobody cares.
301 ((null *circularity-hash-table*)
303 ((null *circularity-counter*)
304 (ecase (gethash object *circularity-hash-table*)
307 (setf (gethash object *circularity-hash-table*) t)
308 ;; We need to keep looking.
312 (setf (gethash object *circularity-hash-table*) 0)
313 ;; It's a circular reference.
316 ;; It's a circular reference.
319 (let ((value (gethash object *circularity-hash-table*)))
322 ;; If NIL, we found an object that wasn't there the first time
323 ;; around. If T, exactly one occurance of this object appears.
324 ;; Either way, just print the thing without any special
325 ;; processing. Note: you might argue that finding a new object
326 ;; means that something is broken, but this can happen. If
327 ;; someone uses the ~@<...~:> format directive, it conses a
328 ;; new list each time though format (i.e. the &REST list), so
329 ;; we will have different cdrs.
333 (let ((value (incf *circularity-counter*)))
334 ;; First occurance of this object. Set the counter.
335 (setf (gethash object *circularity-hash-table*) value)
339 ;; Second or later occurance.
342 (defun handle-circularity (marker stream)
344 "Handle the results of CHECK-FOR-CIRCULARITY. If this returns T then
345 you should go ahead and print the object. If it returns NIL, then
346 you should blow it off."
349 ;; Someone forgot to initiate circularity detection.
350 (let ((*print-circle* nil))
351 (error "trying to use CHECK-FOR-CIRCULARITY when ~
352 circularity checking isn't initiated")))
354 ;; It's a second (or later) reference to the object while we are
355 ;; just looking. So don't bother groveling it again.
358 (write-char #\# stream)
359 (let ((*print-base* 10) (*print-radix* nil))
360 (cond ((minusp marker)
361 (output-integer (- marker) stream)
362 (write-char #\# stream)
365 (output-integer marker stream)
366 (write-char #\= stream)
369 ;;;; OUTPUT-OBJECT -- the main entry point
371 (defvar *pretty-printer* nil
373 "The current pretty printer. Should be either a function that takes two
374 arguments (the object and the stream) or NIL to indicate that there is
375 no pretty printer installed.")
377 (defun output-object (object stream)
379 "Output OBJECT to STREAM observing all printer control variables."
380 (labels ((print-it (stream)
383 (funcall *pretty-printer* object stream)
384 (let ((*print-pretty* nil))
385 (output-ugly-object object stream)))
386 (output-ugly-object object stream)))
388 (let ((marker (check-for-circularity object t)))
391 (let ((*circularity-hash-table*
392 (make-hash-table :test 'eq)))
393 (check-it (make-broadcast-stream))
394 (let ((*circularity-counter* 0))
399 (when (handle-circularity marker stream)
400 (print-it stream)))))))
401 (cond ((or (not *print-circle*)
404 (and (symbolp object) (symbol-package object) t))
405 ;; If it a number, character, or interned symbol, we do not want
406 ;; to check for circularity/sharing.
408 ((or *circularity-hash-table*
410 (typep object 'instance)
411 (typep object '(array t *)))
412 ;; If we have already started circularity detection, this object
413 ;; might be a sharded reference. If we have not, then if it is
414 ;; a cons, a instance, or an array of element type t it might
415 ;; contain a circular reference to itself or multiple shared
419 (print-it stream)))))
421 (defun output-ugly-object (object stream)
423 "Output OBJECT to STREAM observing all printer control variables except
424 for *PRINT-PRETTY*. Note: if *PRINT-PRETTY* is non-NIL, then the pretty
425 printer will be used for any components of OBJECT, just not for OBJECT
428 ;; KLUDGE: The TYPECASE approach here is non-ANSI; the ANSI definition of
429 ;; PRINT-OBJECT says it provides printing and we're supposed to provide
430 ;; PRINT-OBJECT methods covering all classes. We deviate from this
431 ;; by using PRINT-OBJECT only when we print instance values. However,
432 ;; ANSI makes it hard to tell that we're deviating from this:
433 ;; (1) ANSI specifies that the user isn't supposed to call PRINT-OBJECT
435 ;; (2) ANSI (section 11.1.2.1.2) says it's undefined to define
436 ;; a method on an external symbol in the CL package which is
437 ;; applicable to arg lists containing only direct instances of
438 ;; standardized classes.
439 ;; Thus, in order for the user to detect our sleaziness, he has to do
440 ;; something relatively obscure like
441 ;; (1) actually use tools like FIND-METHOD to look for PRINT-OBJECT
443 ;; (2) define a PRINT-OBJECT method which is specialized on the stream
444 ;; value (e.g. a Gray stream object).
445 ;; As long as no one comes up with a non-obscure way of detecting this
446 ;; sleaziness, fixing this nonconformity will probably have a low
447 ;; priority. -- WHN 20000121
449 (output-integer object stream))
452 (output-symbol object stream)
453 (output-list object stream)))
455 (print-object object stream))
457 (unless (and (funcallable-instance-p object)
458 (printed-as-funcallable-standard-class object stream))
459 (output-function object stream)))
461 (output-symbol object stream))
465 (output-integer object stream))
467 (output-float object stream))
469 (output-ratio object stream))
471 (output-ratio object stream))
473 (output-complex object stream))))
475 (output-character object stream))
477 (output-vector object stream))
479 (output-array object stream))
481 (output-sap object stream))
483 (output-weak-pointer object stream))
485 (output-lra object stream))
487 (output-code-component object stream))
489 (output-fdefn object stream))
491 (output-random object stream))))
495 ;;; Values of *PRINT-CASE* and (READTABLE-CASE *READTABLE*) the last time the
496 ;;; printer was called.
497 (defvar *previous-case* nil)
498 (defvar *previous-readtable-case* nil)
500 ;;; This variable contains the current definition of one of three symbol
501 ;;; printers. SETUP-PRINTER-STATE sets this variable.
502 (defvar *internal-symbol-output-function* nil)
504 ;;; This function sets the internal global symbol
505 ;;; *INTERNAL-SYMBOL-OUTPUT-FUNCTION* to the right function depending
506 ;;; on the value of *PRINT-CASE*. See the manual for details. The
507 ;;; print buffer stream is also reset.
508 (defun setup-printer-state ()
509 (unless (and (eq *print-case* *previous-case*)
510 (eq (readtable-case *readtable*) *previous-readtable-case*))
511 (setq *previous-case* *print-case*)
512 (setq *previous-readtable-case* (readtable-case *readtable*))
513 (unless (member *print-case* '(:upcase :downcase :capitalize))
514 (setq *print-case* :upcase)
515 (error "invalid *PRINT-CASE* value: ~S" *previous-case*))
516 (unless (member *previous-readtable-case*
517 '(:upcase :downcase :invert :preserve))
518 (setf (readtable-case *readtable*) :upcase)
519 (error "invalid READTABLE-CASE value: ~S" *previous-readtable-case*))
521 (setq *internal-symbol-output-function*
522 (case *previous-readtable-case*
525 (:upcase #'output-preserve-symbol)
526 (:downcase #'output-lowercase-symbol)
527 (:capitalize #'output-capitalize-symbol)))
530 (:upcase #'output-uppercase-symbol)
531 (:downcase #'output-preserve-symbol)
532 (:capitalize #'output-capitalize-symbol)))
533 (:preserve #'output-preserve-symbol)
534 (:invert #'output-invert-symbol)))))
536 ;;; Output PNAME (a symbol-name or package-name) surrounded with |'s,
537 ;;; and with any embedded |'s or \'s escaped.
538 (defun output-quoted-symbol-name (pname stream)
539 (write-char #\| stream)
540 (dotimes (index (length pname))
541 (let ((char (schar pname index)))
542 (when (or (char= char #\\) (char= char #\|))
543 (write-char #\\ stream))
544 (write-char char stream)))
545 (write-char #\| stream))
547 (defun output-symbol (object stream)
548 (if (or *print-escape* *print-readably*)
549 (let ((package (symbol-package object))
550 (name (symbol-name object)))
552 ;; The ANSI spec "22.1.3.3.1 Package Prefixes for Symbols"
553 ;; requires that keywords be printed with preceding colons
554 ;; always, regardless of the value of *PACKAGE*.
555 ((eq package *keyword-package*)
556 (write-char #\: stream))
557 ;; Otherwise, if the symbol's home package is the current
558 ;; one, then a prefix is never necessary.
559 ((eq package *package*))
560 ;; Uninterned symbols print with a leading #:.
562 (when (or *print-gensym* *print-readably*)
563 (write-string "#:" stream)))
565 (multiple-value-bind (symbol accessible) (find-symbol name *package*)
566 ;; If we can find the symbol by looking it up, it need not
567 ;; be qualified. This can happen if the symbol has been
568 ;; inherited from a package other than its home package.
569 (unless (and accessible (eq symbol object))
570 (output-symbol-name (package-name package) stream)
571 (multiple-value-bind (symbol externalp)
572 (find-external-symbol name package)
573 (declare (ignore symbol))
575 (write-char #\: stream)
576 (write-string "::" stream)))))))
577 (output-symbol-name name stream))
578 (output-symbol-name (symbol-name object) stream nil)))
580 ;;; Output the string NAME as if it were a symbol name. In other words,
581 ;;; diddle its case according to *PRINT-CASE* and READTABLE-CASE.
582 (defun output-symbol-name (name stream &optional (maybe-quote t))
583 (declare (type simple-base-string name))
584 (setup-printer-state)
585 (if (and maybe-quote (symbol-quotep name))
586 (output-quoted-symbol-name name stream)
587 (funcall *internal-symbol-output-function* name stream)))
589 ;;;; escaping symbols
591 ;;; When we print symbols we have to figure out if they need to be
592 ;;; printed with escape characters. This isn't a whole lot easier than
593 ;;; reading symbols in the first place.
595 ;;; For each character, the value of the corresponding element is a
596 ;;; fixnum with bits set corresponding to attributes that the
597 ;;; character has. At characters have at least one bit set, so we can
598 ;;; search for any character with a positive test.
599 (defvar *character-attributes*
600 (make-array char-code-limit :element-type '(unsigned-byte 16)
602 (declaim (type (simple-array (unsigned-byte 16) (#.char-code-limit))
603 *character-attributes*))
605 (eval-when (:compile-toplevel :load-toplevel :execute)
607 ;;; Constants which are a bit-mask for each interesting character attribute.
608 (defconstant other-attribute (ash 1 0)) ; Anything else legal.
609 (defconstant number-attribute (ash 1 1)) ; A numeric digit.
610 (defconstant uppercase-attribute (ash 1 2)) ; An uppercase letter.
611 (defconstant lowercase-attribute (ash 1 3)) ; A lowercase letter.
612 (defconstant sign-attribute (ash 1 4)) ; +-
613 (defconstant extension-attribute (ash 1 5)) ; ^_
614 (defconstant dot-attribute (ash 1 6)) ; .
615 (defconstant slash-attribute (ash 1 7)) ; /
616 (defconstant funny-attribute (ash 1 8)) ; Anything illegal.
618 ;;; LETTER-ATTRIBUTE is a local of SYMBOL-QUOTEP. It matches letters that
619 ;;; don't need to be escaped (according to READTABLE-CASE.)
620 (defconstant attribute-names
621 `((number . number-attribute) (lowercase . lowercase-attribute)
622 (uppercase . uppercase-attribute) (letter . letter-attribute)
623 (sign . sign-attribute) (extension . extension-attribute)
624 (dot . dot-attribute) (slash . slash-attribute)
625 (other . other-attribute) (funny . funny-attribute)))
629 (flet ((set-bit (char bit)
630 (let ((code (char-code char)))
631 (setf (aref *character-attributes* code)
632 (logior bit (aref *character-attributes* code))))))
634 (dolist (char '(#\! #\@ #\$ #\% #\& #\* #\= #\~ #\[ #\] #\{ #\}
636 (set-bit char other-attribute))
639 (set-bit (digit-char i) number-attribute))
641 (do ((code (char-code #\A) (1+ code))
642 (end (char-code #\Z)))
644 (declare (fixnum code end))
645 (set-bit (code-char code) uppercase-attribute)
646 (set-bit (char-downcase (code-char code)) lowercase-attribute))
648 (set-bit #\- sign-attribute)
649 (set-bit #\+ sign-attribute)
650 (set-bit #\^ extension-attribute)
651 (set-bit #\_ extension-attribute)
652 (set-bit #\. dot-attribute)
653 (set-bit #\/ slash-attribute)
655 ;; Mark anything not explicitly allowed as funny.
656 (dotimes (i char-code-limit)
657 (when (zerop (aref *character-attributes* i))
658 (setf (aref *character-attributes* i) funny-attribute))))
660 ;;; For each character, the value of the corresponding element is the lowest
661 ;;; base in which that character is a digit.
662 (defvar *digit-bases*
663 (make-array char-code-limit
664 :element-type '(unsigned-byte 8)
665 :initial-element 36))
666 (declaim (type (simple-array (unsigned-byte 8) (#.char-code-limit))
670 (let ((char (digit-char i 36)))
671 (setf (aref *digit-bases* (char-code char)) i)))
673 ;;; A FSM-like thingie that determines whether a symbol is a potential
674 ;;; number or has evil characters in it.
675 (defun symbol-quotep (name)
676 (declare (simple-string name))
677 (macrolet ((advance (tag &optional (at-end t))
680 ,(if at-end '(go TEST-SIGN) '(return nil)))
681 (setq current (schar name index)
682 code (char-code current)
683 bits (aref attributes code))
686 (test (&rest attributes)
692 (or (cdr (assoc x attribute-names))
697 `(< (the fixnum (aref bases code)) base)))
699 (prog ((len (length name))
700 (attributes *character-attributes*)
701 (bases *digit-bases*)
704 (case (readtable-case *readtable*)
705 (:upcase uppercase-attribute)
706 (:downcase lowercase-attribute)
707 (t (logior lowercase-attribute uppercase-attribute))))
712 (declare (fixnum len base index bits code))
715 TEST-SIGN ; At end, see whether it is a sign...
716 (return (not (test sign)))
718 OTHER ; Not potential number, see whether funny chars...
719 (let ((mask (logxor (logior lowercase-attribute uppercase-attribute
722 (do ((i (1- index) (1+ i)))
723 ((= i len) (return-from symbol-quotep nil))
724 (unless (zerop (logand (aref attributes (char-code (schar name i)))
726 (return-from symbol-quotep t))))
731 (advance LAST-DIGIT-ALPHA)
733 (when (test letter number other slash) (advance OTHER nil))
734 (when (char= current #\.) (advance DOT-FOUND))
735 (when (test sign extension) (advance START-STUFF nil))
738 DOT-FOUND ; Leading dots...
739 (when (test letter) (advance START-DOT-MARKER nil))
740 (when (digitp) (advance DOT-DIGIT))
741 (when (test number other) (advance OTHER nil))
742 (when (test extension slash sign) (advance START-DOT-STUFF nil))
743 (when (char= current #\.) (advance DOT-FOUND))
746 START-STUFF ; Leading stuff before any dot or digit.
749 (advance LAST-DIGIT-ALPHA)
751 (when (test number other) (advance OTHER nil))
752 (when (test letter) (advance START-MARKER nil))
753 (when (char= current #\.) (advance START-DOT-STUFF nil))
754 (when (test sign extension slash) (advance START-STUFF nil))
757 START-MARKER ; Number marker in leading stuff...
758 (when (test letter) (advance OTHER nil))
761 START-DOT-STUFF ; Leading stuff containing dot w/o digit...
762 (when (test letter) (advance START-DOT-STUFF nil))
763 (when (digitp) (advance DOT-DIGIT))
764 (when (test sign extension dot slash) (advance START-DOT-STUFF nil))
765 (when (test number other) (advance OTHER nil))
768 START-DOT-MARKER ; Number marker in leading stuff w/ dot..
769 ;; Leading stuff containing dot w/o digit followed by letter...
770 (when (test letter) (advance OTHER nil))
773 DOT-DIGIT ; In a thing with dots...
774 (when (test letter) (advance DOT-MARKER))
775 (when (digitp) (advance DOT-DIGIT))
776 (when (test number other) (advance OTHER nil))
777 (when (test sign extension dot slash) (advance DOT-DIGIT))
780 DOT-MARKER ; Number maker in number with dot...
781 (when (test letter) (advance OTHER nil))
784 LAST-DIGIT-ALPHA ; Previous char is a letter digit...
785 (when (or (digitp) (test sign slash))
786 (advance ALPHA-DIGIT))
787 (when (test letter number other dot) (advance OTHER nil))
790 ALPHA-DIGIT ; Seen a digit which is a letter...
791 (when (or (digitp) (test sign slash))
793 (advance LAST-DIGIT-ALPHA)
794 (advance ALPHA-DIGIT)))
795 (when (test letter) (advance ALPHA-MARKER))
796 (when (test number other dot) (advance OTHER nil))
799 ALPHA-MARKER ; Number marker in number with alpha digit...
800 (when (test letter) (advance OTHER nil))
803 DIGIT ; Seen only real numeric digits...
806 (advance ALPHA-DIGIT)
808 (when (test number other) (advance OTHER nil))
809 (when (test letter) (advance MARKER))
810 (when (test extension slash sign) (advance DIGIT))
811 (when (char= current #\.) (advance DOT-DIGIT))
814 MARKER ; Number marker in a numeric number...
815 (when (test letter) (advance OTHER nil))
818 ;;;; *INTERNAL-SYMBOL-OUTPUT-FUNCTION*
820 ;;;; Case hackery. These functions are stored in
821 ;;;; *INTERNAL-SYMBOL-OUTPUT-FUNCTION* according to the values of *PRINT-CASE*
822 ;;;; and READTABLE-CASE.
825 ;; READTABLE-CASE *PRINT-CASE*
827 ;; :DOWNCASE :DOWNCASE
829 (defun output-preserve-symbol (pname stream)
830 (declare (simple-string pname))
831 (write-string pname stream))
834 ;; READTABLE-CASE *PRINT-CASE*
836 (defun output-lowercase-symbol (pname stream)
837 (declare (simple-string pname))
838 (dotimes (index (length pname))
839 (let ((char (schar pname index)))
840 (write-char (char-downcase char) stream))))
843 ;; READTABLE-CASE *PRINT-CASE*
845 (defun output-uppercase-symbol (pname stream)
846 (declare (simple-string pname))
847 (dotimes (index (length pname))
848 (let ((char (schar pname index)))
849 (write-char (char-upcase char) stream))))
852 ;; READTABLE-CASE *PRINT-CASE*
853 ;; :UPCASE :CAPITALIZE
854 ;; :DOWNCASE :CAPITALIZE
855 (defun output-capitalize-symbol (pname stream)
856 (declare (simple-string pname))
857 (let ((prev-not-alpha t)
858 (up (eq (readtable-case *readtable*) :upcase)))
859 (dotimes (i (length pname))
860 (let ((char (char pname i)))
862 (if (or prev-not-alpha (lower-case-p char))
864 (char-downcase char))
869 (setq prev-not-alpha (not (alpha-char-p char)))))))
872 ;; READTABLE-CASE *PRINT-CASE*
874 (defun output-invert-symbol (pname stream)
875 (declare (simple-string pname))
878 (dotimes (i (length pname))
879 (let ((ch (schar pname i)))
880 (when (both-case-p ch)
881 (if (upper-case-p ch)
883 (setq all-upper nil)))))
884 (cond (all-upper (output-lowercase-symbol pname stream))
885 (all-lower (output-uppercase-symbol pname stream))
887 (write-string pname stream)))))
891 (let ((*readtable* (copy-readtable nil)))
892 (format t "READTABLE-CASE Input Symbol-name~@
893 ----------------------------------~%")
894 (dolist (readtable-case '(:upcase :downcase :preserve :invert))
895 (setf (readtable-case *readtable*) readtable-case)
896 (dolist (input '("ZEBRA" "Zebra" "zebra"))
897 (format t "~&:~A~16T~A~24T~A"
898 (string-upcase readtable-case)
900 (symbol-name (read-from-string input)))))))
903 (let ((*readtable* (copy-readtable nil)))
904 (format t "READTABLE-CASE *PRINT-CASE* Symbol-name Output Princ~@
905 --------------------------------------------------------~%")
906 (dolist (readtable-case '(:upcase :downcase :preserve :invert))
907 (setf (readtable-case *readtable*) readtable-case)
908 (dolist (*print-case* '(:upcase :downcase :capitalize))
909 (dolist (symbol '(|ZEBRA| |Zebra| |zebra|))
910 (format t "~&:~A~15T:~A~29T~A~42T~A~50T~A"
911 (string-upcase readtable-case)
912 (string-upcase *print-case*)
914 (prin1-to-string symbol)
915 (princ-to-string symbol)))))))
918 ;;;; recursive objects
920 (defun output-list (list stream)
921 (descend-into (stream)
922 (write-char #\( stream)
926 (punt-if-too-long length stream)
927 (output-object (pop list) stream)
930 (when (or (atom list) (check-for-circularity list))
931 (write-string " . " stream)
932 (output-object list stream)
934 (write-char #\space stream)
936 (write-char #\) stream)))
938 (defun output-vector (vector stream)
939 (declare (vector vector))
940 (cond ((stringp vector)
941 (if (or *print-escape* *print-readably*)
942 (quote-string vector stream)
943 (write-string vector stream)))
944 ((not (or *print-array* *print-readably*))
945 (output-terse-array vector stream))
946 ((bit-vector-p vector)
947 (write-string "#*" stream)
948 (dotimes (i (length vector))
949 (output-object (aref vector i) stream)))
951 (when (and *print-readably*
952 (not (eq (array-element-type vector) 't)))
953 (error 'print-not-readable :object vector))
954 (descend-into (stream)
955 (write-string "#(" stream)
956 (dotimes (i (length vector))
958 (write-char #\space stream))
959 (punt-if-too-long i stream)
960 (output-object (aref vector i) stream))
961 (write-string ")" stream)))))
963 ;;; This function outputs a string quoting characters sufficiently that so
964 ;;; someone can read it in again. Basically, put a slash in front of an
965 ;;; character satisfying NEEDS-SLASH-P
966 (defun quote-string (string stream)
967 (macrolet ((needs-slash-p (char)
968 ;; KLUDGE: We probably should look at the readtable, but just do
969 ;; this for now. [noted by anonymous long ago] -- WHN 19991130
970 `(or (char= ,char #\\)
972 (write-char #\" stream)
973 (with-array-data ((data string) (start) (end (length string)))
974 (do ((index start (1+ index)))
976 (let ((char (schar data index)))
977 (when (needs-slash-p char) (write-char #\\ stream))
978 (write-char char stream))))
979 (write-char #\" stream)))
981 (defun output-array (array stream)
983 "Outputs the printed representation of any array in either the #< or #A
985 (if (or *print-array* *print-readably*)
986 (output-array-guts array stream)
987 (output-terse-array array stream)))
989 ;;; to output the abbreviated #< form of an array
990 (defun output-terse-array (array stream)
991 (let ((*print-level* nil)
992 (*print-length* nil))
993 (print-unreadable-object (array stream :type t :identity t))))
995 ;;; to output the readable #A form of an array
996 (defun output-array-guts (array stream)
997 (when (and *print-readably*
998 (not (eq (array-element-type array) t)))
999 (error 'print-not-readable :object array))
1000 (write-char #\# stream)
1001 (let ((*print-base* 10))
1002 (output-integer (array-rank array) stream))
1003 (write-char #\A stream)
1004 (with-array-data ((data array) (start) (end))
1005 (declare (ignore end))
1006 (sub-output-array-guts data (array-dimensions array) stream start)))
1008 (defun sub-output-array-guts (array dimensions stream index)
1009 (declare (type (simple-array * (*)) array) (fixnum index))
1010 (cond ((null dimensions)
1011 (output-object (aref array index) stream))
1013 (descend-into (stream)
1014 (write-char #\( stream)
1015 (let* ((dimension (car dimensions))
1016 (dimensions (cdr dimensions))
1017 (count (reduce #'* dimensions)))
1018 (dotimes (i dimension)
1020 (write-char #\space stream))
1021 (punt-if-too-long i stream)
1022 (sub-output-array-guts array dimensions stream index)
1023 (incf index count)))
1024 (write-char #\) stream)))))
1026 ;;; a trivial non-generic-function placeholder for PRINT-OBJECT, for use
1027 ;;; until CLOS is set up (at which time it will be replaced with
1028 ;;; the real generic function implementation)
1029 (defun print-object (instance stream)
1030 (default-structure-print instance stream *current-level*))
1032 ;;;; integer, ratio, and complex printing (i.e. everything but floats)
1034 (defun output-integer (integer stream)
1035 ;; FIXME: This UNLESS form should be pulled out into something like
1036 ;; GET-REASONABLE-PRINT-BASE, along the lines of GET-REASONABLE-PACKAGE
1037 ;; for the *PACKAGE* variable.
1038 (unless (and (fixnump *print-base*)
1039 (< 1 *print-base* 37))
1040 (let ((obase *print-base*))
1041 (setq *print-base* 10.)
1042 (error "~A is not a reasonable value for *PRINT-BASE*." obase)))
1043 (when (and (not (= *print-base* 10.))
1045 ;; First print leading base information, if any.
1046 (write-char #\# stream)
1047 (write-char (case *print-base*
1051 (T (let ((fixbase *print-base*)
1054 (sub-output-integer fixbase stream))
1057 ;; Then output a minus sign if the number is negative, then output
1058 ;; the absolute value of the number.
1059 (cond ((bignump integer) (print-bignum integer stream))
1061 (write-char #\- stream)
1062 (sub-output-integer (- integer) stream))
1064 (sub-output-integer integer stream)))
1065 ;; Print any trailing base information, if any.
1066 (if (and (= *print-base* 10.) *print-radix*)
1067 (write-char #\. stream)))
1069 (defun sub-output-integer (integer stream)
1072 ;; Recurse until you have all the digits pushed on the stack.
1073 (if (not (zerop (multiple-value-setq (quotient remainder)
1074 (truncate integer *print-base*))))
1075 (sub-output-integer quotient stream))
1076 ;; Then as each recursive call unwinds, turn the digit (in remainder)
1077 ;; into a character and output the character.
1078 (write-char (code-char (if (and (> remainder 9.)
1079 (> *print-base* 10.))
1080 (+ (char-code #\A) (- remainder 10.))
1081 (+ (char-code #\0) remainder)))
1084 ;;;; bignum printing
1086 ;;;; written by Steven Handerson (based on Skef's idea)
1088 ;;;; rewritten to remove assumptions about the length of fixnums for the
1089 ;;;; MIPS port by William Lott
1091 ;;; *BASE-POWER* holds the number that we keep dividing into the bignum for
1092 ;;; each *print-base*. We want this number as close to *most-positive-fixnum*
1093 ;;; as possible, i.e. (floor (log most-positive-fixnum *print-base*)).
1094 (defparameter *base-power* (make-array 37 :initial-element nil))
1096 ;;; *FIXNUM-POWER--1* holds the number of digits for each *print-base* that
1097 ;;; fit in the corresponding *base-power*.
1098 (defparameter *fixnum-power--1* (make-array 37 :initial-element nil))
1100 ;;; Print the bignum to the stream. We first generate the correct value for
1101 ;;; *base-power* and *fixnum-power--1* if we have not already. Then we call
1102 ;;; bignum-print-aux to do the printing.
1103 (defun print-bignum (big stream)
1104 (unless (aref *base-power* *print-base*)
1105 (do ((power-1 -1 (1+ power-1))
1106 (new-divisor *print-base* (* new-divisor *print-base*))
1107 (divisor 1 new-divisor))
1108 ((not (fixnump new-divisor))
1109 (setf (aref *base-power* *print-base*) divisor)
1110 (setf (aref *fixnum-power--1* *print-base*) power-1))))
1111 (bignum-print-aux (cond ((minusp big)
1112 (write-char #\- stream)
1115 (aref *base-power* *print-base*)
1116 (aref *fixnum-power--1* *print-base*)
1120 (defun bignum-print-aux (big divisor power-1 stream)
1121 (multiple-value-bind (newbig fix) (truncate big divisor)
1122 (if (fixnump newbig)
1123 (sub-output-integer newbig stream)
1124 (bignum-print-aux newbig divisor power-1 stream))
1125 (do ((zeros power-1 (1- zeros))
1126 (base-power *print-base* (* base-power *print-base*)))
1128 (dotimes (i zeros) (write-char #\0 stream))
1129 (sub-output-integer fix stream)))))
1131 (defun output-ratio (ratio stream)
1133 (write-char #\# stream)
1135 (2 (write-char #\b stream))
1136 (8 (write-char #\o stream))
1137 (16 (write-char #\x stream))
1138 (t (write *print-base* :stream stream :radix nil :base 10)))
1139 (write-char #\r stream))
1140 (let ((*print-radix* nil))
1141 (output-integer (numerator ratio) stream)
1142 (write-char #\/ stream)
1143 (output-integer (denominator ratio) stream)))
1145 (defun output-complex (complex stream)
1146 (write-string "#C(" stream)
1147 (output-object (realpart complex) stream)
1148 (write-char #\space stream)
1149 (output-object (imagpart complex) stream)
1150 (write-char #\) stream))
1154 ;;;; written by Bill Maddox
1156 ;;; FLONUM-TO-STRING (and its subsidiary function FLOAT-STRING) does most of
1157 ;;; the work for all printing of floating point numbers in the printer and in
1158 ;;; FORMAT. It converts a floating point number to a string in a free or
1159 ;;; fixed format with no exponent. The interpretation of the arguments is as
1162 ;;; X - The floating point number to convert, which must not be
1164 ;;; WIDTH - The preferred field width, used to determine the number
1165 ;;; of fraction digits to produce if the FDIGITS parameter
1166 ;;; is unspecified or NIL. If the non-fraction digits and the
1167 ;;; decimal point alone exceed this width, no fraction digits
1168 ;;; will be produced unless a non-NIL value of FDIGITS has been
1169 ;;; specified. Field overflow is not considerd an error at this
1171 ;;; FDIGITS - The number of fractional digits to produce. Insignificant
1172 ;;; trailing zeroes may be introduced as needed. May be
1173 ;;; unspecified or NIL, in which case as many digits as possible
1174 ;;; are generated, subject to the constraint that there are no
1175 ;;; trailing zeroes.
1176 ;;; SCALE - If this parameter is specified or non-NIL, then the number
1177 ;;; printed is (* x (expt 10 scale)). This scaling is exact,
1178 ;;; and cannot lose precision.
1179 ;;; FMIN - This parameter, if specified or non-NIL, is the minimum
1180 ;;; number of fraction digits which will be produced, regardless
1181 ;;; of the value of WIDTH or FDIGITS. This feature is used by
1182 ;;; the ~E format directive to prevent complete loss of
1183 ;;; significance in the printed value due to a bogus choice of
1186 ;;; Most of the optional arguments are for the benefit for FORMAT and are not
1187 ;;; used by the printer.
1190 ;;; (VALUES DIGIT-STRING DIGIT-LENGTH LEADING-POINT TRAILING-POINT DECPNT)
1191 ;;; where the results have the following interpretation:
1193 ;;; DIGIT-STRING - The decimal representation of X, with decimal point.
1194 ;;; DIGIT-LENGTH - The length of the string DIGIT-STRING.
1195 ;;; LEADING-POINT - True if the first character of DIGIT-STRING is the
1197 ;;; TRAILING-POINT - True if the last character of DIGIT-STRING is the
1199 ;;; POINT-POS - The position of the digit preceding the decimal
1200 ;;; point. Zero indicates point before first digit.
1202 ;;; NOTE: FLONUM-TO-STRING goes to a lot of trouble to guarantee accuracy.
1203 ;;; Specifically, the decimal number printed is the closest possible
1204 ;;; approximation to the true value of the binary number to be printed from
1205 ;;; among all decimal representations with the same number of digits. In
1206 ;;; free-format output, i.e. with the number of digits unconstrained, it is
1207 ;;; guaranteed that all the information is preserved, so that a properly-
1208 ;;; rounding reader can reconstruct the original binary number, bit-for-bit,
1209 ;;; from its printed decimal representation. Furthermore, only as many digits
1210 ;;; as necessary to satisfy this condition will be printed.
1212 ;;; FLOAT-STRING actually generates the digits for positive numbers. The
1213 ;;; algorithm is essentially that of algorithm Dragon4 in "How to Print
1214 ;;; Floating-Point Numbers Accurately" by Steele and White. The current
1215 ;;; (draft) version of this paper may be found in [CMUC]<steele>tradix.press.
1216 ;;; DO NOT EVEN THINK OF ATTEMPTING TO UNDERSTAND THIS CODE WITHOUT READING
1219 (defvar *digits* "0123456789")
1221 (defun flonum-to-string (x &optional width fdigits scale fmin)
1223 ;; Zero is a special case which FLOAT-STRING cannot handle.
1225 (let ((s (make-string (1+ fdigits) :initial-element #\0)))
1226 (setf (schar s 0) #\.)
1227 (values s (length s) t (zerop fdigits) 0))
1228 (values "." 1 t t 0)))
1230 (multiple-value-bind (sig exp) (integer-decode-float x)
1231 (let* ((precision (float-precision x))
1232 (digits (float-digits x))
1233 (fudge (- digits precision))
1234 (width (if width (max width 1) nil)))
1235 (float-string (ash sig (- fudge)) (+ exp fudge) precision width
1236 fdigits scale fmin))))))
1238 (defun float-string (fraction exponent precision width fdigits scale fmin)
1239 (let ((r fraction) (s 1) (m- 1) (m+ 1) (k 0)
1240 (digits 0) (decpnt 0) (cutoff nil) (roundup nil) u low high
1241 (digit-string (make-array 50
1242 :element-type 'base-char
1245 ;; Represent fraction as r/s, error bounds as m+/s and m-/s.
1246 ;; Rational arithmetic avoids loss of precision in subsequent calculations.
1247 (cond ((> exponent 0)
1248 (setq r (ash fraction exponent))
1249 (setq m- (ash 1 exponent))
1252 (setq s (ash 1 (- exponent)))))
1253 ;;adjust the error bounds m+ and m- for unequal gaps
1254 (when (= fraction (ash 1 precision))
1255 (setq m+ (ash m+ 1))
1258 ;;scale value by requested amount, and update error bounds
1261 (let ((scale-factor (expt 10 (- scale))))
1262 (setq s (* s scale-factor)))
1263 (let ((scale-factor (expt 10 scale)))
1264 (setq r (* r scale-factor))
1265 (setq m+ (* m+ scale-factor))
1266 (setq m- (* m- scale-factor)))))
1267 ;;scale r and s and compute initial k, the base 10 logarithm of r
1269 ((>= r (ceiling s 10)))
1273 (setq m+ (* m+ 10)))
1276 ((< (+ (ash r 1) m+) (ash s 1)))
1279 ;;determine number of fraction digits to generate
1281 ;;use specified number of fraction digits
1282 (setq cutoff (- fdigits))
1283 ;;don't allow less than fmin fraction digits
1284 (if (and fmin (> cutoff (- fmin))) (setq cutoff (- fmin))))
1286 ;;use as many fraction digits as width will permit
1287 ;;but force at least fmin digits even if width will be exceeded
1289 (setq cutoff (- 1 width))
1290 (setq cutoff (1+ (- k width))))
1291 (if (and fmin (> cutoff (- fmin))) (setq cutoff (- fmin)))))
1292 ;;If we decided to cut off digit generation before precision has
1293 ;;been exhausted, rounding the last digit may cause a carry propagation.
1294 ;;We can prevent this, preserving left-to-right digit generation, with
1295 ;;a few magical adjustments to m- and m+. Of course, correct rounding
1296 ;;is also preserved.
1297 (when (or fdigits width)
1298 (let ((a (- cutoff k))
1301 (dotimes (i a) (setq y (* y 10)))
1302 (dotimes (i (- a)) (setq y (ceiling y 10))))
1303 (setq m- (max y m-))
1304 (setq m+ (max y m+))
1305 (when (= m+ y) (setq roundup t))))
1306 (when (< (+ (ash r 1) m+) (ash s 1)) (return)))
1307 ;;zero-fill before fraction if no integer part
1309 (setq decpnt digits)
1310 (vector-push-extend #\. digit-string)
1312 (incf digits) (vector-push-extend #\0 digit-string)))
1313 ;;generate the significant digits
1317 (vector-push-extend #\. digit-string)
1318 (setq decpnt digits))
1319 (multiple-value-setq (u r) (truncate (* r 10) s))
1322 (setq low (< (ash r 1) m-))
1324 (setq high (>= (ash r 1) (- (ash s 1) m+)))
1325 (setq high (> (ash r 1) (- (ash s 1) m+))))
1326 ;;stop when either precision is exhausted or we have printed as many
1327 ;;fraction digits as permitted
1328 (when (or low high (and cutoff (<= k cutoff))) (return))
1329 (vector-push-extend (char *digits* u) digit-string)
1331 ;; If cutoff occurred before first digit, then no digits are
1332 ;; generated at all.
1333 (when (or (not cutoff) (>= k cutoff))
1334 ;;last digit may need rounding
1335 (vector-push-extend (char *digits*
1336 (cond ((and low (not high)) u)
1337 ((and high (not low)) (1+ u))
1338 (t (if (<= (ash r 1) s) u (1+ u)))))
1341 ;;zero-fill after integer part if no fraction
1343 (dotimes (i k) (incf digits) (vector-push-extend #\0 digit-string))
1344 (vector-push-extend #\. digit-string)
1345 (setq decpnt digits))
1346 ;;add trailing zeroes to pad fraction if fdigits specified
1348 (dotimes (i (- fdigits (- digits decpnt)))
1350 (vector-push-extend #\0 digit-string)))
1352 (values digit-string (1+ digits) (= decpnt 0) (= decpnt digits) decpnt)))
1354 ;;; Given a non-negative floating point number, SCALE-EXPONENT returns a new
1355 ;;; floating point number Z in the range (0.1, 1.0] and an exponent E such
1356 ;;; that Z * 10^E is (approximately) equal to the original number. There may
1357 ;;; be some loss of precision due the floating point representation. The
1358 ;;; scaling is always done with long float arithmetic, which helps printing of
1359 ;;; lesser precisions as well as avoiding generic arithmetic.
1361 ;;; When computing our initial scale factor using EXPT, we pull out part of
1362 ;;; the computation to avoid over/under flow. When denormalized, we must pull
1363 ;;; out a large factor, since there is more negative exponent range than
1365 (defun scale-exponent (original-x)
1366 (let* ((x (coerce original-x 'long-float)))
1367 (multiple-value-bind (sig exponent) (decode-float x)
1368 (declare (ignore sig))
1370 (values (float 0.0l0 original-x) 1)
1371 (let* ((ex (round (* exponent (log 2l0 10))))
1373 (if (float-denormalized-p x)
1375 (* x 1.0l16 (expt 10.0l0 (- (- ex) 16)))
1377 (* x 1.0l18 (expt 10.0l0 (- (- ex) 18)))
1378 (* x 10.0l0 (expt 10.0l0 (- (- ex) 1))))
1379 (/ x 10.0l0 (expt 10.0l0 (1- ex))))))
1380 (do ((d 10.0l0 (* d 10.0l0))
1384 (do ((m 10.0l0 (* m 10.0l0))
1388 (values (float z original-x) ex))))))))))
1390 ;;;; entry point for the float printer
1392 ;;; Entry point for the float printer as called by PRINT, PRIN1, PRINC,
1393 ;;; etc. The argument is printed free-format, in either exponential or
1394 ;;; non-exponential notation, depending on its magnitude.
1396 ;;; NOTE: When a number is to be printed in exponential format, it is scaled in
1397 ;;; floating point. Since precision may be lost in this process, the
1398 ;;; guaranteed accuracy properties of FLONUM-TO-STRING are lost. The
1399 ;;; difficulty is that FLONUM-TO-STRING performs extensive computations with
1400 ;;; integers of similar magnitude to that of the number being printed. For
1401 ;;; large exponents, the bignums really get out of hand. If bignum arithmetic
1402 ;;; becomes reasonably fast and the exponent range is not too large, then it
1403 ;;; might become attractive to handle exponential notation with the same
1404 ;;; accuracy as non-exponential notation, using the method described in the
1405 ;;; Steele and White paper.
1407 ;;; Print the appropriate exponent marker for X and the specified exponent.
1408 (defun print-float-exponent (x exp stream)
1409 (declare (type float x) (type integer exp) (type stream stream))
1410 (let ((*print-radix* nil)
1411 (plusp (plusp exp)))
1412 (if (typep x *read-default-float-format*)
1414 (format stream "e~:[~;+~]~D" plusp exp))
1415 (format stream "~C~:[~;+~]~D"
1423 ;;; Write out an infinity using #. notation, or flame out if
1424 ;;; *print-readably* is true and *read-eval* is false.
1426 (defun output-float-infinity (x stream)
1427 (declare (type float x) (type stream stream))
1429 (write-string "#." stream))
1431 (error 'print-not-readable :object x))
1433 (write-string "#<" stream)))
1434 (write-string "EXT:" stream)
1435 (princ (float-format-name x) stream)
1436 (write-string (if (plusp x) "-POSITIVE-" "-NEGATIVE-")
1438 (write-string "INFINITY" stream)
1440 (write-string ">" stream)))
1442 ;;; Output a #< NaN or die trying.
1443 (defun output-float-nan (x stream)
1444 (print-unreadable-object (x stream)
1445 (princ (float-format-name x) stream)
1446 (write-string (if (float-trapping-nan-p x) " trapping" " quiet") stream)
1447 (write-string " NaN" stream)))
1449 ;;; the function called by OUTPUT-OBJECT to handle floats
1450 (defun output-float (x stream)
1452 ((float-infinity-p x)
1453 (output-float-infinity x stream))
1455 (output-float-nan x stream))
1457 (let ((x (cond ((minusp (float-sign x))
1458 (write-char #\- stream)
1464 (write-string "0.0" stream)
1465 (print-float-exponent x 0 stream))
1467 (output-float-aux x stream (float 1/1000 x) (float 10000000 x))))))))
1468 (defun output-float-aux (x stream e-min e-max)
1469 (if (and (>= x e-min) (< x e-max))
1471 (multiple-value-bind (str len lpoint tpoint) (flonum-to-string x)
1472 (declare (ignore len))
1473 (when lpoint (write-char #\0 stream))
1474 (write-string str stream)
1475 (when tpoint (write-char #\0 stream))
1476 (print-float-exponent x 0 stream))
1477 ;; exponential format
1478 (multiple-value-bind (f ex) (scale-exponent x)
1479 (multiple-value-bind (str len lpoint tpoint)
1480 (flonum-to-string f nil nil 1)
1481 (declare (ignore len))
1482 (when lpoint (write-char #\0 stream))
1483 (write-string str stream)
1484 (when tpoint (write-char #\0 stream))
1485 ;; Subtract out scale factor of 1 passed to FLONUM-TO-STRING.
1486 (print-float-exponent x (1- ex) stream)))))
1488 ;;;; other leaf objects
1490 ;;; If *PRINT-ESCAPE* is false, just do a WRITE-CHAR, otherwise output the
1491 ;;; character name or the character in the #\char format.
1492 (defun output-character (char stream)
1493 (if (or *print-escape* *print-readably*)
1494 (let ((name (char-name char)))
1495 (write-string "#\\" stream)
1497 (write-string name stream)
1498 (write-char char stream)))
1499 (write-char char stream)))
1501 (defun output-sap (sap stream)
1502 (declare (type system-area-pointer sap))
1504 (format stream "#.(~S #X~8,'0X)" 'int-sap (sap-int sap)))
1506 (print-unreadable-object (sap stream)
1507 (format stream "system area pointer: #X~8,'0X" (sap-int sap))))))
1509 (defun output-weak-pointer (weak-pointer stream)
1510 (declare (type weak-pointer weak-pointer))
1511 (print-unreadable-object (weak-pointer stream)
1512 (multiple-value-bind (value validp) (weak-pointer-value weak-pointer)
1514 (write-string "weak pointer: " stream)
1515 (write value :stream stream))
1517 (write-string "broken weak pointer" stream))))))
1519 (defun output-code-component (component stream)
1520 (print-unreadable-object (component stream :identity t)
1521 (let ((dinfo (%code-debug-info component)))
1522 (cond ((eq dinfo :bogus-lra)
1523 (write-string "bogus code object" stream))
1525 (write-string "code object" stream)
1527 (write-char #\space stream)
1528 (output-object (sb!c::debug-info-name dinfo) stream)))))))
1530 (defun output-lra (lra stream)
1531 (print-unreadable-object (lra stream :identity t)
1532 (write-string "return PC object" stream)))
1534 (defun output-fdefn (fdefn stream)
1535 (print-unreadable-object (fdefn stream)
1536 (write-string "FDEFINITION object for " stream)
1537 (output-object (fdefn-name fdefn) stream)))
1541 ;;; Output OBJECT as using PRINT-OBJECT if it's a
1542 ;;; FUNCALLABLE-STANDARD-CLASS, or return NIL otherwise.
1544 ;;; The definition here is a simple temporary placeholder. It will be
1545 ;;; overwritten by a smarter version (capable of calling generic
1546 ;;; PRINT-OBJECT when appropriate) when CLOS is installed.
1547 (defun printed-as-clos-funcallable-standard-class (object stream)
1548 (declare (ignore object stream))
1551 (defun output-function (object stream)
1552 (let* ((*print-length* 3) ; in case we have to..
1553 (*print-level* 3) ; ..print an interpreted function definition
1554 (name (cond ((find (function-subtype object)
1555 #(#.sb!vm:closure-header-type
1556 #.sb!vm:byte-code-closure-type))
1558 ((sb!eval::interpreted-function-p object)
1559 (or (sb!eval::interpreted-function-%name object)
1560 (sb!eval:interpreted-function-lambda-expression
1562 ((find (function-subtype object)
1563 #(#.sb!vm:function-header-type
1564 #.sb!vm:closure-function-header-type))
1565 (%function-name object))
1566 (t 'no-name-available)))
1567 (identified-by-name-p (and (symbolp name)
1569 (eq (fdefinition name) object))))
1570 (print-unreadable-object (object
1572 :identity (not identified-by-name-p))
1573 (prin1 'function stream)
1574 (unless (eq name 'no-name-available)
1575 (format stream " ~S" name)))))
1577 ;;;; catch-all for unknown things
1579 (defun output-random (object stream)
1580 (print-unreadable-object (object stream :identity t)
1581 (let ((lowtag (get-lowtag object)))
1583 (#.sb!vm:other-pointer-type
1584 (let ((type (get-type object)))
1586 (#.sb!vm:value-cell-header-type
1587 (write-string "value cell " stream)
1588 (output-object (sb!c:value-cell-ref object) stream))
1590 (write-string "unknown pointer object, type=" stream)
1591 (let ((*print-base* 16) (*print-radix* t))
1592 (output-integer type stream))))))
1593 ((#.sb!vm:function-pointer-type
1594 #.sb!vm:instance-pointer-type
1595 #.sb!vm:list-pointer-type)
1596 (write-string "unknown pointer object, type=" stream))
1598 (case (get-type object)
1599 (#.sb!vm:unbound-marker-type
1600 (write-string "unbound marker" stream))
1602 (write-string "unknown immediate object, lowtag=" stream)
1603 (let ((*print-base* 2) (*print-radix* t))
1604 (output-integer lowtag stream))
1605 (write-string ", type=" stream)
1606 (let ((*print-base* 16) (*print-radix* t))
1607 (output-integer (get-type object) stream)))))))))