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 ;;; guts of PRINT-UNREADABLE-OBJECT
240 (defun %print-unreadable-object (object stream type identity body)
241 (when *print-readably*
242 (error 'print-not-readable :object object))
243 (flet ((print-description ()
245 (write (type-of object) :stream stream :circle nil
246 :level nil :length nil)
247 (when (or body identity)
248 (write-char #\space stream)
249 (pprint-newline :fill stream)))
254 (write-char #\space stream)
255 (pprint-newline :fill stream))
256 (write-char #\{ stream)
257 (write (get-lisp-obj-address object) :stream stream
259 (write-char #\} stream))))
260 (cond ((print-pretty-on-stream-p stream)
261 ;; Since we're printing prettily on STREAM, format the
262 ;; object within a logical block. PPRINT-LOGICAL-BLOCK does
263 ;; not rebind the stream when it is already a pretty stream,
264 ;; so output from the body will go to the same stream.
265 (pprint-logical-block (stream nil :prefix "#<" :suffix ">")
266 (print-description)))
268 (write-string "#<" stream)
270 (write-char #\> stream))))
273 ;;;; WHITESPACE-CHAR-P
275 ;;; This is used in other files, but is defined in this one for some reason.
277 (defun whitespace-char-p (char)
279 "Determines whether or not the character is considered whitespace."
280 (or (char= char #\space)
281 (char= char (code-char tab-char-code))
282 (char= char (code-char return-char-code))
283 (char= char #\linefeed)))
285 ;;;; circularity detection stuff
287 ;;; When *PRINT-CIRCLE* is T, this gets bound to a hash table that (eventually)
288 ;;; ends up with entries for every object printed. When we are initially
289 ;;; looking for circularities, we enter a T when we find an object for the
290 ;;; first time, and a 0 when we encounter an object a second time around.
291 ;;; When we are actually printing, the 0 entries get changed to the actual
292 ;;; marker value when they are first printed.
293 (defvar *circularity-hash-table* nil)
295 ;;; When NIL, we are just looking for circularities. After we have found them
296 ;;; all, this gets bound to 0. Then whenever we need a new marker, it is
298 (defvar *circularity-counter* nil)
300 (defun check-for-circularity (object &optional assign)
302 "Check to see whether OBJECT is a circular reference, and return something
303 non-NIL if it is. If ASSIGN is T, then the number to use in the #n= and
304 #n# noise is assigned at this time. Note: CHECK-FOR-CIRCULARITY must
305 be called *EXACTLY* once with ASSIGN T, or the circularity detection noise
306 will get confused about when to use #n= and when to use #n#. If this
307 returns non-NIL when ASSIGN is T, then you must call HANDLE-CIRCULARITY
308 on it. If you are not using this inside a WITH-CIRCULARITY-DETECTION,
309 then you have to be prepared to handle a return value of :INITIATE which
310 means it needs to initiate the circularity detection noise. See the
311 source for info on how to do that."
312 (cond ((null *print-circle*)
313 ;; Don't bother, nobody cares.
315 ((null *circularity-hash-table*)
317 ((null *circularity-counter*)
318 (ecase (gethash object *circularity-hash-table*)
321 (setf (gethash object *circularity-hash-table*) t)
322 ;; We need to keep looking.
326 (setf (gethash object *circularity-hash-table*) 0)
327 ;; It's a circular reference.
330 ;; It's a circular reference.
333 (let ((value (gethash object *circularity-hash-table*)))
336 ;; If NIL, we found an object that wasn't there the first time
337 ;; around. If T, exactly one occurance of this object appears.
338 ;; Either way, just print the thing without any special
339 ;; processing. Note: you might argue that finding a new object
340 ;; means that something is broken, but this can happen. If
341 ;; someone uses the ~@<...~:> format directive, it conses a
342 ;; new list each time though format (i.e. the &REST list), so
343 ;; we will have different cdrs.
347 (let ((value (incf *circularity-counter*)))
348 ;; First occurance of this object. Set the counter.
349 (setf (gethash object *circularity-hash-table*) value)
353 ;; Second or later occurance.
356 (defun handle-circularity (marker stream)
358 "Handle the results of CHECK-FOR-CIRCULARITY. If this returns T then
359 you should go ahead and print the object. If it returns NIL, then
360 you should blow it off."
363 ;; Someone forgot to initiate circularity detection.
364 (let ((*print-circle* nil))
365 (error "trying to use CHECK-FOR-CIRCULARITY when ~
366 circularity checking isn't initiated")))
368 ;; It's a second (or later) reference to the object while we are
369 ;; just looking. So don't bother groveling it again.
372 (write-char #\# stream)
373 (let ((*print-base* 10) (*print-radix* nil))
374 (cond ((minusp marker)
375 (output-integer (- marker) stream)
376 (write-char #\# stream)
379 (output-integer marker stream)
380 (write-char #\= stream)
383 ;;;; OUTPUT-OBJECT -- the main entry point
385 (defvar *pretty-printer* nil
387 "The current pretty printer. Should be either a function that takes two
388 arguments (the object and the stream) or NIL to indicate that there is
389 no pretty printer installed.")
391 (defun output-object (object stream)
393 "Output OBJECT to STREAM observing all printer control variables."
394 (labels ((print-it (stream)
397 (funcall *pretty-printer* object stream)
398 (let ((*print-pretty* nil))
399 (output-ugly-object object stream)))
400 (output-ugly-object object stream)))
402 (let ((marker (check-for-circularity object t)))
405 (let ((*circularity-hash-table*
406 (make-hash-table :test 'eq)))
407 (check-it (make-broadcast-stream))
408 (let ((*circularity-counter* 0))
413 (when (handle-circularity marker stream)
414 (print-it stream)))))))
415 (cond ((or (not *print-circle*)
418 (and (symbolp object) (symbol-package object) t))
419 ;; If it a number, character, or interned symbol, we do not want
420 ;; to check for circularity/sharing.
422 ((or *circularity-hash-table*
424 (typep object 'instance)
425 (typep object '(array t *)))
426 ;; If we have already started circularity detection, this object
427 ;; might be a sharded reference. If we have not, then if it is
428 ;; a cons, a instance, or an array of element type t it might
429 ;; contain a circular reference to itself or multiple shared
433 (print-it stream)))))
435 (defun output-ugly-object (object stream)
437 "Output OBJECT to STREAM observing all printer control variables except
438 for *PRINT-PRETTY*. Note: if *PRINT-PRETTY* is non-NIL, then the pretty
439 printer will be used for any components of OBJECT, just not for OBJECT
442 ;; KLUDGE: The TYPECASE approach here is non-ANSI; the ANSI definition of
443 ;; PRINT-OBJECT says it provides printing and we're supposed to provide
444 ;; PRINT-OBJECT methods covering all classes. We deviate from this
445 ;; by using PRINT-OBJECT only when we print instance values. However,
446 ;; ANSI makes it hard to tell that we're deviating from this:
447 ;; (1) ANSI specifies that the user isn't supposed to call PRINT-OBJECT
449 ;; (2) ANSI (section 11.1.2.1.2) says it's undefined to define
450 ;; a method on an external symbol in the CL package which is
451 ;; applicable to arg lists containing only direct instances of
452 ;; standardized classes.
453 ;; Thus, in order for the user to detect our sleaziness, he has to do
454 ;; something relatively obscure like
455 ;; (1) actually use tools like FIND-METHOD to look for PRINT-OBJECT
457 ;; (2) define a PRINT-OBJECT method which is specialized on the stream
458 ;; value (e.g. a Gray stream object).
459 ;; As long as no one comes up with a non-obscure way of detecting this
460 ;; sleaziness, fixing this nonconformity will probably have a low
461 ;; priority. -- WHN 20000121
463 (output-integer object stream))
466 (output-symbol object stream)
467 (output-list object stream)))
469 (print-object object stream))
471 (unless (and (funcallable-instance-p object)
472 (printed-as-funcallable-standard-class object stream))
473 (output-function object stream)))
475 (output-symbol object stream))
479 (output-integer object stream))
481 (output-float object stream))
483 (output-ratio object stream))
485 (output-ratio object stream))
487 (output-complex object stream))))
489 (output-character object stream))
491 (output-vector object stream))
493 (output-array object stream))
495 (output-sap object stream))
497 (output-weak-pointer object stream))
499 (output-lra object stream))
501 (output-code-component object stream))
503 (output-fdefn object stream))
505 (output-random object stream))))
509 ;;; Values of *PRINT-CASE* and (READTABLE-CASE *READTABLE*) the last time the
510 ;;; printer was called.
511 (defvar *previous-case* nil)
512 (defvar *previous-readtable-case* nil)
514 ;;; This variable contains the current definition of one of three symbol
515 ;;; printers. SETUP-PRINTER-STATE sets this variable.
516 (defvar *internal-symbol-output-function* nil)
518 ;;; This function sets the internal global symbol
519 ;;; *INTERNAL-SYMBOL-OUTPUT-FUNCTION* to the right function depending
520 ;;; on the value of *PRINT-CASE*. See the manual for details. The
521 ;;; print buffer stream is also reset.
522 (defun setup-printer-state ()
523 (unless (and (eq *print-case* *previous-case*)
524 (eq (readtable-case *readtable*) *previous-readtable-case*))
525 (setq *previous-case* *print-case*)
526 (setq *previous-readtable-case* (readtable-case *readtable*))
527 (unless (member *print-case* '(:upcase :downcase :capitalize))
528 (setq *print-case* :upcase)
529 (error "invalid *PRINT-CASE* value: ~S" *previous-case*))
530 (unless (member *previous-readtable-case*
531 '(:upcase :downcase :invert :preserve))
532 (setf (readtable-case *readtable*) :upcase)
533 (error "invalid READTABLE-CASE value: ~S" *previous-readtable-case*))
535 (setq *internal-symbol-output-function*
536 (case *previous-readtable-case*
539 (:upcase #'output-preserve-symbol)
540 (:downcase #'output-lowercase-symbol)
541 (:capitalize #'output-capitalize-symbol)))
544 (:upcase #'output-uppercase-symbol)
545 (:downcase #'output-preserve-symbol)
546 (:capitalize #'output-capitalize-symbol)))
547 (:preserve #'output-preserve-symbol)
548 (:invert #'output-invert-symbol)))))
550 ;;; Output PNAME (a symbol-name or package-name) surrounded with |'s,
551 ;;; and with any embedded |'s or \'s escaped.
552 (defun output-quoted-symbol-name (pname stream)
553 (write-char #\| stream)
554 (dotimes (index (length pname))
555 (let ((char (schar pname index)))
556 (when (or (char= char #\\) (char= char #\|))
557 (write-char #\\ stream))
558 (write-char char stream)))
559 (write-char #\| stream))
561 (defun output-symbol (object stream)
562 (if (or *print-escape* *print-readably*)
563 (let ((package (symbol-package object))
564 (name (symbol-name object)))
566 ;; The ANSI spec "22.1.3.3.1 Package Prefixes for Symbols"
567 ;; requires that keywords be printed with preceding colons
568 ;; always, regardless of the value of *PACKAGE*.
569 ((eq package *keyword-package*)
570 (write-char #\: stream))
571 ;; Otherwise, if the symbol's home package is the current
572 ;; one, then a prefix is never necessary.
573 ((eq package (sane-package)))
574 ;; Uninterned symbols print with a leading #:.
576 (when (or *print-gensym* *print-readably*)
577 (write-string "#:" stream)))
579 (multiple-value-bind (symbol accessible)
580 (find-symbol name (sane-package))
581 ;; If we can find the symbol by looking it up, it need not
582 ;; be qualified. This can happen if the symbol has been
583 ;; inherited from a package other than its home package.
584 (unless (and accessible (eq symbol object))
585 (output-symbol-name (package-name package) stream)
586 (multiple-value-bind (symbol externalp)
587 (find-external-symbol name package)
588 (declare (ignore symbol))
590 (write-char #\: stream)
591 (write-string "::" stream)))))))
592 (output-symbol-name name stream))
593 (output-symbol-name (symbol-name object) stream nil)))
595 ;;; Output the string NAME as if it were a symbol name. In other words,
596 ;;; diddle its case according to *PRINT-CASE* and READTABLE-CASE.
597 (defun output-symbol-name (name stream &optional (maybe-quote t))
598 (declare (type simple-base-string name))
599 (setup-printer-state)
600 (if (and maybe-quote (symbol-quotep name))
601 (output-quoted-symbol-name name stream)
602 (funcall *internal-symbol-output-function* name stream)))
604 ;;;; escaping symbols
606 ;;; When we print symbols we have to figure out if they need to be
607 ;;; printed with escape characters. This isn't a whole lot easier than
608 ;;; reading symbols in the first place.
610 ;;; For each character, the value of the corresponding element is a
611 ;;; fixnum with bits set corresponding to attributes that the
612 ;;; character has. At characters have at least one bit set, so we can
613 ;;; search for any character with a positive test.
614 (defvar *character-attributes*
615 (make-array char-code-limit
616 :element-type '(unsigned-byte 16)
618 (declaim (type (simple-array (unsigned-byte 16) (#.char-code-limit))
619 *character-attributes*))
621 ;;; Constants which are a bit-mask for each interesting character attribute.
622 (defconstant other-attribute (ash 1 0)) ; Anything else legal.
623 (defconstant number-attribute (ash 1 1)) ; A numeric digit.
624 (defconstant uppercase-attribute (ash 1 2)) ; An uppercase letter.
625 (defconstant lowercase-attribute (ash 1 3)) ; A lowercase letter.
626 (defconstant sign-attribute (ash 1 4)) ; +-
627 (defconstant extension-attribute (ash 1 5)) ; ^_
628 (defconstant dot-attribute (ash 1 6)) ; .
629 (defconstant slash-attribute (ash 1 7)) ; /
630 (defconstant funny-attribute (ash 1 8)) ; Anything illegal.
632 (eval-when (:compile-toplevel :load-toplevel :execute)
634 ;;; LETTER-ATTRIBUTE is a local of SYMBOL-QUOTEP. It matches letters
635 ;;; that don't need to be escaped (according to READTABLE-CASE.)
636 (defparameter *attribute-names*
637 `((number . number-attribute) (lowercase . lowercase-attribute)
638 (uppercase . uppercase-attribute) (letter . letter-attribute)
639 (sign . sign-attribute) (extension . extension-attribute)
640 (dot . dot-attribute) (slash . slash-attribute)
641 (other . other-attribute) (funny . funny-attribute)))
645 (flet ((set-bit (char bit)
646 (let ((code (char-code char)))
647 (setf (aref *character-attributes* code)
648 (logior bit (aref *character-attributes* code))))))
650 (dolist (char '(#\! #\@ #\$ #\% #\& #\* #\= #\~ #\[ #\] #\{ #\}
652 (set-bit char other-attribute))
655 (set-bit (digit-char i) number-attribute))
657 (do ((code (char-code #\A) (1+ code))
658 (end (char-code #\Z)))
660 (declare (fixnum code end))
661 (set-bit (code-char code) uppercase-attribute)
662 (set-bit (char-downcase (code-char code)) lowercase-attribute))
664 (set-bit #\- sign-attribute)
665 (set-bit #\+ sign-attribute)
666 (set-bit #\^ extension-attribute)
667 (set-bit #\_ extension-attribute)
668 (set-bit #\. dot-attribute)
669 (set-bit #\/ slash-attribute)
671 ;; Mark anything not explicitly allowed as funny.
672 (dotimes (i char-code-limit)
673 (when (zerop (aref *character-attributes* i))
674 (setf (aref *character-attributes* i) funny-attribute))))
676 ;;; For each character, the value of the corresponding element is the lowest
677 ;;; base in which that character is a digit.
678 (defvar *digit-bases*
679 (make-array char-code-limit
680 :element-type '(unsigned-byte 8)
681 :initial-element 36))
682 (declaim (type (simple-array (unsigned-byte 8) (#.char-code-limit))
686 (let ((char (digit-char i 36)))
687 (setf (aref *digit-bases* (char-code char)) i)))
689 ;;; A FSM-like thingie that determines whether a symbol is a potential
690 ;;; number or has evil characters in it.
691 (defun symbol-quotep (name)
692 (declare (simple-string name))
693 (macrolet ((advance (tag &optional (at-end t))
696 ,(if at-end '(go TEST-SIGN) '(return nil)))
697 (setq current (schar name index)
698 code (char-code current)
699 bits (aref attributes code))
702 (test (&rest attributes)
714 `(< (the fixnum (aref bases code)) base)))
716 (prog ((len (length name))
717 (attributes *character-attributes*)
718 (bases *digit-bases*)
721 (case (readtable-case *readtable*)
722 (:upcase uppercase-attribute)
723 (:downcase lowercase-attribute)
724 (t (logior lowercase-attribute uppercase-attribute))))
729 (declare (fixnum len base index bits code))
732 TEST-SIGN ; At end, see whether it is a sign...
733 (return (not (test sign)))
735 OTHER ; Not potential number, see whether funny chars...
736 (let ((mask (logxor (logior lowercase-attribute uppercase-attribute
739 (do ((i (1- index) (1+ i)))
740 ((= i len) (return-from symbol-quotep nil))
741 (unless (zerop (logand (aref attributes (char-code (schar name i)))
743 (return-from symbol-quotep t))))
748 (advance LAST-DIGIT-ALPHA)
750 (when (test letter number other slash) (advance OTHER nil))
751 (when (char= current #\.) (advance DOT-FOUND))
752 (when (test sign extension) (advance START-STUFF nil))
755 DOT-FOUND ; Leading dots...
756 (when (test letter) (advance START-DOT-MARKER nil))
757 (when (digitp) (advance DOT-DIGIT))
758 (when (test number other) (advance OTHER nil))
759 (when (test extension slash sign) (advance START-DOT-STUFF nil))
760 (when (char= current #\.) (advance DOT-FOUND))
763 START-STUFF ; Leading stuff before any dot or digit.
766 (advance LAST-DIGIT-ALPHA)
768 (when (test number other) (advance OTHER nil))
769 (when (test letter) (advance START-MARKER nil))
770 (when (char= current #\.) (advance START-DOT-STUFF nil))
771 (when (test sign extension slash) (advance START-STUFF nil))
774 START-MARKER ; Number marker in leading stuff...
775 (when (test letter) (advance OTHER nil))
778 START-DOT-STUFF ; Leading stuff containing dot w/o digit...
779 (when (test letter) (advance START-DOT-STUFF nil))
780 (when (digitp) (advance DOT-DIGIT))
781 (when (test sign extension dot slash) (advance START-DOT-STUFF nil))
782 (when (test number other) (advance OTHER nil))
785 START-DOT-MARKER ; Number marker in leading stuff w/ dot..
786 ;; Leading stuff containing dot w/o digit followed by letter...
787 (when (test letter) (advance OTHER nil))
790 DOT-DIGIT ; In a thing with dots...
791 (when (test letter) (advance DOT-MARKER))
792 (when (digitp) (advance DOT-DIGIT))
793 (when (test number other) (advance OTHER nil))
794 (when (test sign extension dot slash) (advance DOT-DIGIT))
797 DOT-MARKER ; Number maker in number with dot...
798 (when (test letter) (advance OTHER nil))
801 LAST-DIGIT-ALPHA ; Previous char is a letter digit...
802 (when (or (digitp) (test sign slash))
803 (advance ALPHA-DIGIT))
804 (when (test letter number other dot) (advance OTHER nil))
807 ALPHA-DIGIT ; Seen a digit which is a letter...
808 (when (or (digitp) (test sign slash))
810 (advance LAST-DIGIT-ALPHA)
811 (advance ALPHA-DIGIT)))
812 (when (test letter) (advance ALPHA-MARKER))
813 (when (test number other dot) (advance OTHER nil))
816 ALPHA-MARKER ; Number marker in number with alpha digit...
817 (when (test letter) (advance OTHER nil))
820 DIGIT ; Seen only real numeric digits...
823 (advance ALPHA-DIGIT)
825 (when (test number other) (advance OTHER nil))
826 (when (test letter) (advance MARKER))
827 (when (test extension slash sign) (advance DIGIT))
828 (when (char= current #\.) (advance DOT-DIGIT))
831 MARKER ; Number marker in a numeric number...
832 (when (test letter) (advance OTHER nil))
835 ;;;; *INTERNAL-SYMBOL-OUTPUT-FUNCTION*
837 ;;;; Case hackery. These functions are stored in
838 ;;;; *INTERNAL-SYMBOL-OUTPUT-FUNCTION* according to the values of *PRINT-CASE*
839 ;;;; and READTABLE-CASE.
842 ;; READTABLE-CASE *PRINT-CASE*
844 ;; :DOWNCASE :DOWNCASE
846 (defun output-preserve-symbol (pname stream)
847 (declare (simple-string pname))
848 (write-string pname stream))
851 ;; READTABLE-CASE *PRINT-CASE*
853 (defun output-lowercase-symbol (pname stream)
854 (declare (simple-string pname))
855 (dotimes (index (length pname))
856 (let ((char (schar pname index)))
857 (write-char (char-downcase char) stream))))
860 ;; READTABLE-CASE *PRINT-CASE*
862 (defun output-uppercase-symbol (pname stream)
863 (declare (simple-string pname))
864 (dotimes (index (length pname))
865 (let ((char (schar pname index)))
866 (write-char (char-upcase char) stream))))
869 ;; READTABLE-CASE *PRINT-CASE*
870 ;; :UPCASE :CAPITALIZE
871 ;; :DOWNCASE :CAPITALIZE
872 (defun output-capitalize-symbol (pname stream)
873 (declare (simple-string pname))
874 (let ((prev-not-alpha t)
875 (up (eq (readtable-case *readtable*) :upcase)))
876 (dotimes (i (length pname))
877 (let ((char (char pname i)))
879 (if (or prev-not-alpha (lower-case-p char))
881 (char-downcase char))
886 (setq prev-not-alpha (not (alpha-char-p char)))))))
889 ;; READTABLE-CASE *PRINT-CASE*
891 (defun output-invert-symbol (pname stream)
892 (declare (simple-string pname))
895 (dotimes (i (length pname))
896 (let ((ch (schar pname i)))
897 (when (both-case-p ch)
898 (if (upper-case-p ch)
900 (setq all-upper nil)))))
901 (cond (all-upper (output-lowercase-symbol pname stream))
902 (all-lower (output-uppercase-symbol pname stream))
904 (write-string pname stream)))))
908 (let ((*readtable* (copy-readtable nil)))
909 (format t "READTABLE-CASE Input Symbol-name~@
910 ----------------------------------~%")
911 (dolist (readtable-case '(:upcase :downcase :preserve :invert))
912 (setf (readtable-case *readtable*) readtable-case)
913 (dolist (input '("ZEBRA" "Zebra" "zebra"))
914 (format t "~&:~A~16T~A~24T~A"
915 (string-upcase readtable-case)
917 (symbol-name (read-from-string input)))))))
920 (let ((*readtable* (copy-readtable nil)))
921 (format t "READTABLE-CASE *PRINT-CASE* Symbol-name Output Princ~@
922 --------------------------------------------------------~%")
923 (dolist (readtable-case '(:upcase :downcase :preserve :invert))
924 (setf (readtable-case *readtable*) readtable-case)
925 (dolist (*print-case* '(:upcase :downcase :capitalize))
926 (dolist (symbol '(|ZEBRA| |Zebra| |zebra|))
927 (format t "~&:~A~15T:~A~29T~A~42T~A~50T~A"
928 (string-upcase readtable-case)
929 (string-upcase *print-case*)
931 (prin1-to-string symbol)
932 (princ-to-string symbol)))))))
935 ;;;; recursive objects
937 (defun output-list (list stream)
938 (descend-into (stream)
939 (write-char #\( stream)
943 (punt-print-if-too-long length stream)
944 (output-object (pop list) stream)
947 (when (or (atom list) (check-for-circularity list))
948 (write-string " . " stream)
949 (output-object list stream)
951 (write-char #\space stream)
953 (write-char #\) stream)))
955 (defun output-vector (vector stream)
956 (declare (vector vector))
957 (cond ((stringp vector)
958 (cond ((or *print-escape* *print-readably*)
959 (write-char #\" stream)
960 (quote-string vector stream)
961 (write-char #\" stream))
963 (write-string vector stream))))
964 ((not (or *print-array* *print-readably*))
965 (output-terse-array vector stream))
966 ((bit-vector-p vector)
967 (write-string "#*" stream)
968 (dotimes (i (length vector))
969 (output-object (aref vector i) stream)))
971 (when (and *print-readably*
972 (not (eq (array-element-type vector) 't)))
973 (error 'print-not-readable :object vector))
974 (descend-into (stream)
975 (write-string "#(" stream)
976 (dotimes (i (length vector))
978 (write-char #\space stream))
979 (punt-print-if-too-long i stream)
980 (output-object (aref vector i) stream))
981 (write-string ")" stream)))))
983 ;;; This function outputs a string quoting characters sufficiently
984 ;;; that so someone can read it in again. Basically, put a slash in
985 ;;; front of an character satisfying NEEDS-SLASH-P.
986 (defun quote-string (string stream)
987 (macrolet ((needs-slash-p (char)
988 ;; KLUDGE: We probably should look at the readtable, but just do
989 ;; this for now. [noted by anonymous long ago] -- WHN 19991130
990 `(or (char= ,char #\\)
992 (with-array-data ((data string) (start) (end (length string)))
993 (do ((index start (1+ index)))
995 (let ((char (schar data index)))
996 (when (needs-slash-p char) (write-char #\\ stream))
997 (write-char char stream))))))
999 (defun output-array (array stream)
1001 "Outputs the printed representation of any array in either the #< or #A
1003 (if (or *print-array* *print-readably*)
1004 (output-array-guts array stream)
1005 (output-terse-array array stream)))
1007 ;;; to output the abbreviated #< form of an array
1008 (defun output-terse-array (array stream)
1009 (let ((*print-level* nil)
1010 (*print-length* nil))
1011 (print-unreadable-object (array stream :type t :identity t))))
1013 ;;; to output the readable #A form of an array
1014 (defun output-array-guts (array stream)
1015 (when (and *print-readably*
1016 (not (eq (array-element-type array) t)))
1017 (error 'print-not-readable :object array))
1018 (write-char #\# stream)
1019 (let ((*print-base* 10))
1020 (output-integer (array-rank array) stream))
1021 (write-char #\A stream)
1022 (with-array-data ((data array) (start) (end))
1023 (declare (ignore end))
1024 (sub-output-array-guts data (array-dimensions array) stream start)))
1026 (defun sub-output-array-guts (array dimensions stream index)
1027 (declare (type (simple-array * (*)) array) (fixnum index))
1028 (cond ((null dimensions)
1029 (output-object (aref array index) stream))
1031 (descend-into (stream)
1032 (write-char #\( stream)
1033 (let* ((dimension (car dimensions))
1034 (dimensions (cdr dimensions))
1035 (count (reduce #'* dimensions)))
1036 (dotimes (i dimension)
1038 (write-char #\space stream))
1039 (punt-print-if-too-long i stream)
1040 (sub-output-array-guts array dimensions stream index)
1041 (incf index count)))
1042 (write-char #\) stream)))))
1044 ;;; a trivial non-generic-function placeholder for PRINT-OBJECT, for
1045 ;;; use until CLOS is set up (at which time it will be replaced with
1046 ;;; the real generic function implementation)
1047 (defun print-object (instance stream)
1048 (default-structure-print instance stream *current-level*))
1050 ;;;; integer, ratio, and complex printing (i.e. everything but floats)
1052 (defun output-integer (integer stream)
1053 ;; FIXME: This UNLESS form should be pulled out into something like
1054 ;; (SANE-PRINT-BASE), along the lines of (SANE-PACKAGE) for the
1055 ;; *PACKAGE* variable.
1056 (unless (and (fixnump *print-base*)
1057 (< 1 *print-base* 37))
1058 (let ((obase *print-base*))
1059 (setq *print-base* 10.)
1060 (error "~A is not a reasonable value for *PRINT-BASE*." obase)))
1061 (when (and (not (= *print-base* 10.))
1063 ;; First print leading base information, if any.
1064 (write-char #\# stream)
1065 (write-char (case *print-base*
1069 (T (let ((fixbase *print-base*)
1072 (sub-output-integer fixbase stream))
1075 ;; Then output a minus sign if the number is negative, then output
1076 ;; the absolute value of the number.
1077 (cond ((bignump integer) (print-bignum integer stream))
1079 (write-char #\- stream)
1080 (sub-output-integer (- integer) stream))
1082 (sub-output-integer integer stream)))
1083 ;; Print any trailing base information, if any.
1084 (if (and (= *print-base* 10.) *print-radix*)
1085 (write-char #\. stream)))
1087 (defun sub-output-integer (integer stream)
1090 ;; Recurse until you have all the digits pushed on the stack.
1091 (if (not (zerop (multiple-value-setq (quotient remainder)
1092 (truncate integer *print-base*))))
1093 (sub-output-integer quotient stream))
1094 ;; Then as each recursive call unwinds, turn the digit (in remainder)
1095 ;; into a character and output the character.
1096 (write-char (code-char (if (and (> remainder 9.)
1097 (> *print-base* 10.))
1098 (+ (char-code #\A) (- remainder 10.))
1099 (+ (char-code #\0) remainder)))
1102 ;;;; bignum printing
1104 ;;;; written by Steven Handerson (based on Skef's idea)
1106 ;;;; rewritten to remove assumptions about the length of fixnums for the
1107 ;;;; MIPS port by William Lott
1109 ;;; *BASE-POWER* holds the number that we keep dividing into the bignum for
1110 ;;; each *print-base*. We want this number as close to *most-positive-fixnum*
1111 ;;; as possible, i.e. (floor (log most-positive-fixnum *print-base*)).
1112 (defparameter *base-power* (make-array 37 :initial-element nil))
1114 ;;; *FIXNUM-POWER--1* holds the number of digits for each *print-base* that
1115 ;;; fit in the corresponding *base-power*.
1116 (defparameter *fixnum-power--1* (make-array 37 :initial-element nil))
1118 ;;; Print the bignum to the stream. We first generate the correct value for
1119 ;;; *base-power* and *fixnum-power--1* if we have not already. Then we call
1120 ;;; bignum-print-aux to do the printing.
1121 (defun print-bignum (big stream)
1122 (unless (aref *base-power* *print-base*)
1123 (do ((power-1 -1 (1+ power-1))
1124 (new-divisor *print-base* (* new-divisor *print-base*))
1125 (divisor 1 new-divisor))
1126 ((not (fixnump new-divisor))
1127 (setf (aref *base-power* *print-base*) divisor)
1128 (setf (aref *fixnum-power--1* *print-base*) power-1))))
1129 (bignum-print-aux (cond ((minusp big)
1130 (write-char #\- stream)
1133 (aref *base-power* *print-base*)
1134 (aref *fixnum-power--1* *print-base*)
1138 (defun bignum-print-aux (big divisor power-1 stream)
1139 (multiple-value-bind (newbig fix) (truncate big divisor)
1140 (if (fixnump newbig)
1141 (sub-output-integer newbig stream)
1142 (bignum-print-aux newbig divisor power-1 stream))
1143 (do ((zeros power-1 (1- zeros))
1144 (base-power *print-base* (* base-power *print-base*)))
1146 (dotimes (i zeros) (write-char #\0 stream))
1147 (sub-output-integer fix stream)))))
1149 (defun output-ratio (ratio stream)
1151 (write-char #\# stream)
1153 (2 (write-char #\b stream))
1154 (8 (write-char #\o stream))
1155 (16 (write-char #\x stream))
1156 (t (write *print-base* :stream stream :radix nil :base 10)))
1157 (write-char #\r stream))
1158 (let ((*print-radix* nil))
1159 (output-integer (numerator ratio) stream)
1160 (write-char #\/ stream)
1161 (output-integer (denominator ratio) stream)))
1163 (defun output-complex (complex stream)
1164 (write-string "#C(" stream)
1165 (output-object (realpart complex) stream)
1166 (write-char #\space stream)
1167 (output-object (imagpart complex) stream)
1168 (write-char #\) stream))
1172 ;;;; written by Bill Maddox
1174 ;;; FLONUM-TO-STRING (and its subsidiary function FLOAT-STRING) does most of
1175 ;;; the work for all printing of floating point numbers in the printer and in
1176 ;;; FORMAT. It converts a floating point number to a string in a free or
1177 ;;; fixed format with no exponent. The interpretation of the arguments is as
1180 ;;; X - The floating point number to convert, which must not be
1182 ;;; WIDTH - The preferred field width, used to determine the number
1183 ;;; of fraction digits to produce if the FDIGITS parameter
1184 ;;; is unspecified or NIL. If the non-fraction digits and the
1185 ;;; decimal point alone exceed this width, no fraction digits
1186 ;;; will be produced unless a non-NIL value of FDIGITS has been
1187 ;;; specified. Field overflow is not considerd an error at this
1189 ;;; FDIGITS - The number of fractional digits to produce. Insignificant
1190 ;;; trailing zeroes may be introduced as needed. May be
1191 ;;; unspecified or NIL, in which case as many digits as possible
1192 ;;; are generated, subject to the constraint that there are no
1193 ;;; trailing zeroes.
1194 ;;; SCALE - If this parameter is specified or non-NIL, then the number
1195 ;;; printed is (* x (expt 10 scale)). This scaling is exact,
1196 ;;; and cannot lose precision.
1197 ;;; FMIN - This parameter, if specified or non-NIL, is the minimum
1198 ;;; number of fraction digits which will be produced, regardless
1199 ;;; of the value of WIDTH or FDIGITS. This feature is used by
1200 ;;; the ~E format directive to prevent complete loss of
1201 ;;; significance in the printed value due to a bogus choice of
1204 ;;; Most of the optional arguments are for the benefit for FORMAT and are not
1205 ;;; used by the printer.
1208 ;;; (VALUES DIGIT-STRING DIGIT-LENGTH LEADING-POINT TRAILING-POINT DECPNT)
1209 ;;; where the results have the following interpretation:
1211 ;;; DIGIT-STRING - The decimal representation of X, with decimal point.
1212 ;;; DIGIT-LENGTH - The length of the string DIGIT-STRING.
1213 ;;; LEADING-POINT - True if the first character of DIGIT-STRING is the
1215 ;;; TRAILING-POINT - True if the last character of DIGIT-STRING is the
1217 ;;; POINT-POS - The position of the digit preceding the decimal
1218 ;;; point. Zero indicates point before first digit.
1220 ;;; NOTE: FLONUM-TO-STRING goes to a lot of trouble to guarantee accuracy.
1221 ;;; Specifically, the decimal number printed is the closest possible
1222 ;;; approximation to the true value of the binary number to be printed from
1223 ;;; among all decimal representations with the same number of digits. In
1224 ;;; free-format output, i.e. with the number of digits unconstrained, it is
1225 ;;; guaranteed that all the information is preserved, so that a properly-
1226 ;;; rounding reader can reconstruct the original binary number, bit-for-bit,
1227 ;;; from its printed decimal representation. Furthermore, only as many digits
1228 ;;; as necessary to satisfy this condition will be printed.
1230 ;;; FLOAT-STRING actually generates the digits for positive numbers. The
1231 ;;; algorithm is essentially that of algorithm Dragon4 in "How to Print
1232 ;;; Floating-Point Numbers Accurately" by Steele and White. The current
1233 ;;; (draft) version of this paper may be found in [CMUC]<steele>tradix.press.
1234 ;;; DO NOT EVEN THINK OF ATTEMPTING TO UNDERSTAND THIS CODE WITHOUT READING
1237 (defvar *digits* "0123456789")
1239 (defun flonum-to-string (x &optional width fdigits scale fmin)
1241 ;; Zero is a special case which FLOAT-STRING cannot handle.
1243 (let ((s (make-string (1+ fdigits) :initial-element #\0)))
1244 (setf (schar s 0) #\.)
1245 (values s (length s) t (zerop fdigits) 0))
1246 (values "." 1 t t 0)))
1248 (multiple-value-bind (sig exp) (integer-decode-float x)
1249 (let* ((precision (float-precision x))
1250 (digits (float-digits x))
1251 (fudge (- digits precision))
1252 (width (if width (max width 1) nil)))
1253 (float-string (ash sig (- fudge)) (+ exp fudge) precision width
1254 fdigits scale fmin))))))
1256 (defun float-string (fraction exponent precision width fdigits scale fmin)
1257 (let ((r fraction) (s 1) (m- 1) (m+ 1) (k 0)
1258 (digits 0) (decpnt 0) (cutoff nil) (roundup nil) u low high
1259 (digit-string (make-array 50
1260 :element-type 'base-char
1263 ;; Represent fraction as r/s, error bounds as m+/s and m-/s.
1264 ;; Rational arithmetic avoids loss of precision in subsequent calculations.
1265 (cond ((> exponent 0)
1266 (setq r (ash fraction exponent))
1267 (setq m- (ash 1 exponent))
1270 (setq s (ash 1 (- exponent)))))
1271 ;;adjust the error bounds m+ and m- for unequal gaps
1272 (when (= fraction (ash 1 precision))
1273 (setq m+ (ash m+ 1))
1276 ;;scale value by requested amount, and update error bounds
1279 (let ((scale-factor (expt 10 (- scale))))
1280 (setq s (* s scale-factor)))
1281 (let ((scale-factor (expt 10 scale)))
1282 (setq r (* r scale-factor))
1283 (setq m+ (* m+ scale-factor))
1284 (setq m- (* m- scale-factor)))))
1285 ;;scale r and s and compute initial k, the base 10 logarithm of r
1287 ((>= r (ceiling s 10)))
1291 (setq m+ (* m+ 10)))
1294 ((< (+ (ash r 1) m+) (ash s 1)))
1297 ;;determine number of fraction digits to generate
1299 ;;use specified number of fraction digits
1300 (setq cutoff (- fdigits))
1301 ;;don't allow less than fmin fraction digits
1302 (if (and fmin (> cutoff (- fmin))) (setq cutoff (- fmin))))
1304 ;;use as many fraction digits as width will permit
1305 ;;but force at least fmin digits even if width will be exceeded
1307 (setq cutoff (- 1 width))
1308 (setq cutoff (1+ (- k width))))
1309 (if (and fmin (> cutoff (- fmin))) (setq cutoff (- fmin)))))
1310 ;;If we decided to cut off digit generation before precision has
1311 ;;been exhausted, rounding the last digit may cause a carry propagation.
1312 ;;We can prevent this, preserving left-to-right digit generation, with
1313 ;;a few magical adjustments to m- and m+. Of course, correct rounding
1314 ;;is also preserved.
1315 (when (or fdigits width)
1316 (let ((a (- cutoff k))
1319 (dotimes (i a) (setq y (* y 10)))
1320 (dotimes (i (- a)) (setq y (ceiling y 10))))
1321 (setq m- (max y m-))
1322 (setq m+ (max y m+))
1323 (when (= m+ y) (setq roundup t))))
1324 (when (< (+ (ash r 1) m+) (ash s 1)) (return)))
1325 ;;zero-fill before fraction if no integer part
1327 (setq decpnt digits)
1328 (vector-push-extend #\. digit-string)
1330 (incf digits) (vector-push-extend #\0 digit-string)))
1331 ;;generate the significant digits
1335 (vector-push-extend #\. digit-string)
1336 (setq decpnt digits))
1337 (multiple-value-setq (u r) (truncate (* r 10) s))
1340 (setq low (< (ash r 1) m-))
1342 (setq high (>= (ash r 1) (- (ash s 1) m+)))
1343 (setq high (> (ash r 1) (- (ash s 1) m+))))
1344 ;;stop when either precision is exhausted or we have printed as many
1345 ;;fraction digits as permitted
1346 (when (or low high (and cutoff (<= k cutoff))) (return))
1347 (vector-push-extend (char *digits* u) digit-string)
1349 ;; If cutoff occurred before first digit, then no digits are
1350 ;; generated at all.
1351 (when (or (not cutoff) (>= k cutoff))
1352 ;;last digit may need rounding
1353 (vector-push-extend (char *digits*
1354 (cond ((and low (not high)) u)
1355 ((and high (not low)) (1+ u))
1356 (t (if (<= (ash r 1) s) u (1+ u)))))
1359 ;;zero-fill after integer part if no fraction
1361 (dotimes (i k) (incf digits) (vector-push-extend #\0 digit-string))
1362 (vector-push-extend #\. digit-string)
1363 (setq decpnt digits))
1364 ;;add trailing zeroes to pad fraction if fdigits specified
1366 (dotimes (i (- fdigits (- digits decpnt)))
1368 (vector-push-extend #\0 digit-string)))
1370 (values digit-string (1+ digits) (= decpnt 0) (= decpnt digits) decpnt)))
1372 ;;; Given a non-negative floating point number, SCALE-EXPONENT returns a new
1373 ;;; floating point number Z in the range (0.1, 1.0] and an exponent E such
1374 ;;; that Z * 10^E is (approximately) equal to the original number. There may
1375 ;;; be some loss of precision due the floating point representation. The
1376 ;;; scaling is always done with long float arithmetic, which helps printing of
1377 ;;; lesser precisions as well as avoiding generic arithmetic.
1379 ;;; When computing our initial scale factor using EXPT, we pull out part of
1380 ;;; the computation to avoid over/under flow. When denormalized, we must pull
1381 ;;; out a large factor, since there is more negative exponent range than
1383 (defun scale-exponent (original-x)
1384 (let* ((x (coerce original-x 'long-float)))
1385 (multiple-value-bind (sig exponent) (decode-float x)
1386 (declare (ignore sig))
1388 (values (float 0.0l0 original-x) 1)
1389 (let* ((ex (round (* exponent (log 2l0 10))))
1391 (if (float-denormalized-p x)
1393 (* x 1.0l16 (expt 10.0l0 (- (- ex) 16)))
1395 (* x 1.0l18 (expt 10.0l0 (- (- ex) 18)))
1396 (* x 10.0l0 (expt 10.0l0 (- (- ex) 1))))
1397 (/ x 10.0l0 (expt 10.0l0 (1- ex))))))
1398 (do ((d 10.0l0 (* d 10.0l0))
1402 (do ((m 10.0l0 (* m 10.0l0))
1406 (values (float z original-x) ex))))))))))
1408 ;;;; entry point for the float printer
1410 ;;; Entry point for the float printer as called by PRINT, PRIN1, PRINC,
1411 ;;; etc. The argument is printed free-format, in either exponential or
1412 ;;; non-exponential notation, depending on its magnitude.
1414 ;;; NOTE: When a number is to be printed in exponential format, it is scaled in
1415 ;;; floating point. Since precision may be lost in this process, the
1416 ;;; guaranteed accuracy properties of FLONUM-TO-STRING are lost. The
1417 ;;; difficulty is that FLONUM-TO-STRING performs extensive computations with
1418 ;;; integers of similar magnitude to that of the number being printed. For
1419 ;;; large exponents, the bignums really get out of hand. If bignum arithmetic
1420 ;;; becomes reasonably fast and the exponent range is not too large, then it
1421 ;;; might become attractive to handle exponential notation with the same
1422 ;;; accuracy as non-exponential notation, using the method described in the
1423 ;;; Steele and White paper.
1425 ;;; Print the appropriate exponent marker for X and the specified exponent.
1426 (defun print-float-exponent (x exp stream)
1427 (declare (type float x) (type integer exp) (type stream stream))
1428 (let ((*print-radix* nil)
1429 (plusp (plusp exp)))
1430 (if (typep x *read-default-float-format*)
1432 (format stream "e~:[~;+~]~D" plusp exp))
1433 (format stream "~C~:[~;+~]~D"
1441 ;;; Write out an infinity using #. notation, or flame out if
1442 ;;; *PRINT-READABLY* is true and *READ-EVAL* is false.
1443 (defun output-float-infinity (x stream)
1444 (declare (type float x) (type stream stream))
1446 (write-string "#." stream))
1448 (error 'print-not-readable :object x))
1450 (write-string "#<" stream)))
1451 (write-string "EXT:" stream)
1452 (princ (float-format-name x) stream)
1453 (write-string (if (plusp x) "-POSITIVE-" "-NEGATIVE-")
1455 (write-string "INFINITY" stream)
1457 (write-string ">" stream)))
1459 ;;; Output a #< NaN or die trying.
1460 (defun output-float-nan (x stream)
1461 (print-unreadable-object (x stream)
1462 (princ (float-format-name x) stream)
1463 (write-string (if (float-trapping-nan-p x) " trapping" " quiet") stream)
1464 (write-string " NaN" stream)))
1466 ;;; the function called by OUTPUT-OBJECT to handle floats
1467 (defun output-float (x stream)
1469 ((float-infinity-p x)
1470 (output-float-infinity x stream))
1472 (output-float-nan x stream))
1474 (let ((x (cond ((minusp (float-sign x))
1475 (write-char #\- stream)
1481 (write-string "0.0" stream)
1482 (print-float-exponent x 0 stream))
1484 (output-float-aux x stream (float 1/1000 x) (float 10000000 x))))))))
1485 (defun output-float-aux (x stream e-min e-max)
1486 (if (and (>= x e-min) (< x e-max))
1488 (multiple-value-bind (str len lpoint tpoint) (flonum-to-string x)
1489 (declare (ignore len))
1490 (when lpoint (write-char #\0 stream))
1491 (write-string str stream)
1492 (when tpoint (write-char #\0 stream))
1493 (print-float-exponent x 0 stream))
1494 ;; exponential format
1495 (multiple-value-bind (f ex) (scale-exponent x)
1496 (multiple-value-bind (str len lpoint tpoint)
1497 (flonum-to-string f nil nil 1)
1498 (declare (ignore len))
1499 (when lpoint (write-char #\0 stream))
1500 (write-string str stream)
1501 (when tpoint (write-char #\0 stream))
1502 ;; Subtract out scale factor of 1 passed to FLONUM-TO-STRING.
1503 (print-float-exponent x (1- ex) stream)))))
1505 ;;;; other leaf objects
1507 ;;; If *PRINT-ESCAPE* is false, just do a WRITE-CHAR, otherwise output
1508 ;;; the character name or the character in the #\char format.
1509 (defun output-character (char stream)
1510 (if (or *print-escape* *print-readably*)
1511 (let ((name (char-name char)))
1512 (write-string "#\\" stream)
1514 (quote-string name stream)
1515 (write-char char stream)))
1516 (write-char char stream)))
1518 (defun output-sap (sap stream)
1519 (declare (type system-area-pointer sap))
1521 (format stream "#.(~S #X~8,'0X)" 'int-sap (sap-int sap)))
1523 (print-unreadable-object (sap stream)
1524 (format stream "system area pointer: #X~8,'0X" (sap-int sap))))))
1526 (defun output-weak-pointer (weak-pointer stream)
1527 (declare (type weak-pointer weak-pointer))
1528 (print-unreadable-object (weak-pointer stream)
1529 (multiple-value-bind (value validp) (weak-pointer-value weak-pointer)
1531 (write-string "weak pointer: " stream)
1532 (write value :stream stream))
1534 (write-string "broken weak pointer" stream))))))
1536 (defun output-code-component (component stream)
1537 (print-unreadable-object (component stream :identity t)
1538 (let ((dinfo (%code-debug-info component)))
1539 (cond ((eq dinfo :bogus-lra)
1540 (write-string "bogus code object" stream))
1542 (write-string "code object" stream)
1544 (write-char #\space stream)
1545 (output-object (sb!c::debug-info-name dinfo) stream)))))))
1547 (defun output-lra (lra stream)
1548 (print-unreadable-object (lra stream :identity t)
1549 (write-string "return PC object" stream)))
1551 (defun output-fdefn (fdefn stream)
1552 (print-unreadable-object (fdefn stream)
1553 (write-string "FDEFINITION object for " stream)
1554 (output-object (fdefn-name fdefn) stream)))
1558 ;;; Output OBJECT as using PRINT-OBJECT if it's a
1559 ;;; FUNCALLABLE-STANDARD-CLASS, or return NIL otherwise.
1561 ;;; The definition here is a simple temporary placeholder. It will be
1562 ;;; overwritten by a smarter version (capable of calling generic
1563 ;;; PRINT-OBJECT when appropriate) when CLOS is installed.
1564 (defun printed-as-clos-funcallable-standard-class (object stream)
1565 (declare (ignore object stream))
1568 (defun output-function (object stream)
1569 (let* ((*print-length* 3) ; in case we have to..
1570 (*print-level* 3) ; ..print an interpreted function definition
1571 (name (cond ((find (function-subtype object)
1572 #(#.sb!vm:closure-header-type
1573 #.sb!vm:byte-code-closure-type))
1575 ((sb!eval::interpreted-function-p object)
1576 (or (sb!eval::interpreted-function-%name object)
1577 (sb!eval:interpreted-function-lambda-expression
1579 ((find (function-subtype object)
1580 #(#.sb!vm:function-header-type
1581 #.sb!vm:closure-function-header-type))
1582 (%function-name object))
1583 (t 'no-name-available)))
1584 (identified-by-name-p (and (symbolp name)
1586 (eq (fdefinition name) object))))
1587 (print-unreadable-object (object
1589 :identity (not identified-by-name-p))
1590 (prin1 'function stream)
1591 (unless (eq name 'no-name-available)
1592 (format stream " ~S" name)))))
1594 ;;;; catch-all for unknown things
1596 (defun output-random (object stream)
1597 (print-unreadable-object (object stream :identity t)
1598 (let ((lowtag (get-lowtag object)))
1600 (#.sb!vm:other-pointer-type
1601 (let ((type (get-type object)))
1603 (#.sb!vm:value-cell-header-type
1604 (write-string "value cell " stream)
1605 (output-object (sb!c:value-cell-ref object) stream))
1607 (write-string "unknown pointer object, type=" stream)
1608 (let ((*print-base* 16) (*print-radix* t))
1609 (output-integer type stream))))))
1610 ((#.sb!vm:function-pointer-type
1611 #.sb!vm:instance-pointer-type
1612 #.sb!vm:list-pointer-type)
1613 (write-string "unknown pointer object, type=" stream))
1615 (case (get-type object)
1616 (#.sb!vm:unbound-marker-type
1617 (write-string "unbound marker" stream))
1619 (write-string "unknown immediate object, lowtag=" stream)
1620 (let ((*print-base* 2) (*print-radix* t))
1621 (output-integer lowtag stream))
1622 (write-string ", type=" stream)
1623 (let ((*print-base* 16) (*print-radix* t))
1624 (output-integer (get-type object) stream)))))))))