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 be printed readably. If readable printing
22 is impossible, an error will be signalled. This overrides the value of
24 (defvar *print-escape* t
26 "Should we print in a reasonably machine-readable way? (possibly
27 overridden by *PRINT-READABLY*)")
28 (defvar *print-pretty* nil ; (set later when pretty-printer is initialized)
30 "Should pretty printing be used?")
31 (defvar *print-base* 10.
33 "The output base for RATIONALs (including integers).")
34 (defvar *print-radix* nil
36 "Should base be verified when printing RATIONALs?")
37 (defvar *print-level* nil
39 "How many levels should be printed before abbreviating with \"#\"?")
40 (defvar *print-length* nil
42 "How many elements at any level should be printed before abbreviating
44 (defvar *print-circle* nil
46 "Should we use #n= and #n# notation to preserve uniqueness in general (and
47 circularity in particular) when printing?")
48 (defvar *print-case* :upcase
50 "What case should the printer should use default?")
51 (defvar *print-array* t
53 "Should the contents of arrays be printed?")
54 (defvar *print-gensym* t
56 "Should #: prefixes be used when printing symbols with null SYMBOL-PACKAGE?")
57 (defvar *print-lines* nil
59 "The maximum number of lines to print per object.")
60 (defvar *print-right-margin* nil
62 "The position of the right margin in ems (for pretty-printing).")
63 (defvar *print-miser-width* nil
65 "If the remaining space between the current column and the right margin
66 is less than this, then print using ``miser-style'' output. Miser
67 style conditional newlines are turned on, and all indentations are
68 turned off. If NIL, never use miser mode.")
69 (defvar *print-pprint-dispatch*)
71 (setf (fdocumentation '*print-pprint-dispatch* 'variable)
72 "The pprint-dispatch-table that controls how to pretty-print objects.")
74 (defmacro with-standard-io-syntax (&body body)
76 "Bind the reader and printer control variables to values that enable READ
77 to reliably read the results of PRINT. These values are:
78 *PACKAGE* the COMMON-LISP-USER package
88 *PRINT-MISER-WIDTH* NIL
89 *PRINT-PPRINT-DISPATCH* the standard pprint dispatch table
93 *PRINT-RIGHT-MARGIN* NIL
95 *READ-DEFAULT-FLOAT-FORMAT* SINGLE-FLOAT
98 *READTABLE* the standard readtable"
99 `(%with-standard-io-syntax (lambda () ,@body)))
101 (defun %with-standard-io-syntax (function)
102 (declare (type function function))
103 (let ((*package* (find-package "COMMON-LISP-USER"))
106 (*print-case* :upcase)
113 (*print-miser-width* nil)
114 (*print-pprint-dispatch* sb!pretty::*standard-pprint-dispatch-table*)
118 (*print-right-margin* nil)
120 (*read-default-float-format* 'single-float)
122 (*read-suppress* nil)
123 (*readtable* *standard-readtable*))
126 ;;;; routines to print objects
129 ;;; keyword variables shared by WRITE and WRITE-TO-STRING, and
130 ;;; the bindings they map to.
131 (eval-when (:compile-toplevel :load-toplevel)
132 (defvar *printer-keyword-variables*
133 '(:escape *print-escape*
136 :circle *print-circle*
137 :pretty *print-pretty*
139 :length *print-length*
142 :gensym *print-gensym*
143 :readably *print-readably*
144 :right-margin *print-right-margin*
145 :miser-width *print-miser-width*
147 :pprint-dispatch *print-pprint-dispatch*)))
149 (defun write (object &key
150 ((:stream stream) *standard-output*)
151 ((:escape *print-escape*) *print-escape*)
152 ((:radix *print-radix*) *print-radix*)
153 ((:base *print-base*) *print-base*)
154 ((:circle *print-circle*) *print-circle*)
155 ((:pretty *print-pretty*) *print-pretty*)
156 ((:level *print-level*) *print-level*)
157 ((:length *print-length*) *print-length*)
158 ((:case *print-case*) *print-case*)
159 ((:array *print-array*) *print-array*)
160 ((:gensym *print-gensym*) *print-gensym*)
161 ((:readably *print-readably*) *print-readably*)
162 ((:right-margin *print-right-margin*)
163 *print-right-margin*)
164 ((:miser-width *print-miser-width*)
166 ((:lines *print-lines*) *print-lines*)
167 ((:pprint-dispatch *print-pprint-dispatch*)
168 *print-pprint-dispatch*))
170 "Output OBJECT to the specified stream, defaulting to *STANDARD-OUTPUT*."
171 (output-object object (out-synonym-of stream))
174 ;;; Optimize common case of constant keyword arguments
175 (define-compiler-macro write (&whole form object &rest keys)
179 ;; Odd number of keys, punt
181 (return-from write form)))
182 (let* ((key (pop keys))
184 (variable (or (getf *printer-keyword-variables* key)
185 (when (eq :stream key)
187 (return-from write form))))
188 (when (assoc variable bind)
189 ;; First key has precedence, but we still need to execute the
190 ;; argument, and in the right order.
191 (setf variable (gensym "IGNORE"))
192 (push variable ignore))
193 (push (list variable value) bind)))
194 (unless (assoc 'stream bind)
195 (push (list 'stream '*standard-output*) bind))
196 (once-only ((object object))
197 `(let ,(nreverse bind)
198 ,@(when ignore `((declare (ignore ,@ignore))))
199 (output-object ,object (out-synonym-of stream))
202 (defun prin1 (object &optional stream)
204 "Output a mostly READable printed representation of OBJECT on the specified
206 (let ((*print-escape* t))
207 (output-object object (out-synonym-of stream)))
210 (defun princ (object &optional stream)
212 "Output an aesthetic but not necessarily READable printed representation
213 of OBJECT on the specified STREAM."
214 (let ((*print-escape* nil)
215 (*print-readably* nil))
216 (output-object object (out-synonym-of stream)))
219 (defun print (object &optional stream)
221 "Output a newline, the mostly READable printed representation of OBJECT, and
222 space to the specified STREAM."
223 (let ((stream (out-synonym-of stream)))
225 (prin1 object stream)
226 (write-char #\space stream)
229 (defun pprint (object &optional stream)
231 "Prettily output OBJECT preceded by a newline."
232 (let ((*print-pretty* t)
234 (stream (out-synonym-of stream)))
236 (output-object object stream))
239 (defun write-to-string
241 ((:escape *print-escape*) *print-escape*)
242 ((:radix *print-radix*) *print-radix*)
243 ((:base *print-base*) *print-base*)
244 ((:circle *print-circle*) *print-circle*)
245 ((:pretty *print-pretty*) *print-pretty*)
246 ((:level *print-level*) *print-level*)
247 ((:length *print-length*) *print-length*)
248 ((:case *print-case*) *print-case*)
249 ((:array *print-array*) *print-array*)
250 ((:gensym *print-gensym*) *print-gensym*)
251 ((:readably *print-readably*) *print-readably*)
252 ((:right-margin *print-right-margin*) *print-right-margin*)
253 ((:miser-width *print-miser-width*) *print-miser-width*)
254 ((:lines *print-lines*) *print-lines*)
255 ((:pprint-dispatch *print-pprint-dispatch*)
256 *print-pprint-dispatch*))
258 "Return the printed representation of OBJECT as a string."
259 (stringify-object object))
261 ;;; Optimize common case of constant keyword arguments
262 (define-compiler-macro write-to-string (&whole form object &rest keys)
266 ;; Odd number of keys, punt
268 (return-from write-to-string form)))
269 (let* ((key (pop keys))
271 (variable (or (getf *printer-keyword-variables* key)
272 (return-from write-to-string form))))
273 (when (assoc variable bind)
274 ;; First key has precedence, but we still need to execute the
275 ;; argument, and in the right order.
276 (setf variable (gensym "IGNORE"))
277 (push variable ignore))
278 (push (list variable value) bind)))
280 (once-only ((object object))
281 `(let ,(nreverse bind)
282 ,@(when ignore `((declare (ignore ,@ignore))))
283 (stringify-object ,object)))
284 `(stringify-object ,object))))
286 (defun prin1-to-string (object)
288 "Return the printed representation of OBJECT as a string with
290 (let ((*print-escape* t))
291 (stringify-object object)))
293 (defun princ-to-string (object)
295 "Return the printed representation of OBJECT as a string with
297 (let ((*print-escape* nil)
298 (*print-readably* nil))
299 (stringify-object object)))
301 ;;; This produces the printed representation of an object as a string.
302 ;;; The few ...-TO-STRING functions above call this.
303 (defun stringify-object (object)
304 (let ((stream (make-string-output-stream)))
305 (setup-printer-state)
306 (output-object object stream)
307 (get-output-stream-string stream)))
309 ;;;; support for the PRINT-UNREADABLE-OBJECT macro
311 (defun read-unreadable-replacement ()
312 (format *query-io* "~@<Enter an object (evaluated): ~@:>")
313 (finish-output *query-io*)
314 (list (eval (read *query-io*))))
316 ;;; guts of PRINT-UNREADABLE-OBJECT
317 (defun %print-unreadable-object (object stream type identity body)
318 (declare (type (or null function) body))
319 (when *print-readably*
321 (error 'print-not-readable :object object)
323 :report "Print unreadably.")
325 :report "Supply an object to be printed instead."
326 :interactive read-unreadable-replacement
327 (write o :stream stream)
328 (return-from %print-unreadable-object nil))))
329 (flet ((print-description ()
331 (write (type-of object) :stream stream :circle nil
332 :level nil :length nil)
333 (write-char #\space stream)
334 (pprint-newline :fill stream))
338 (when (or body (not type))
339 (write-char #\space stream))
340 (pprint-newline :fill stream)
341 (write-char #\{ stream)
342 (write (get-lisp-obj-address object) :stream stream
344 (write-char #\} stream))))
345 (cond ((print-pretty-on-stream-p stream)
346 ;; Since we're printing prettily on STREAM, format the
347 ;; object within a logical block. PPRINT-LOGICAL-BLOCK does
348 ;; not rebind the stream when it is already a pretty stream,
349 ;; so output from the body will go to the same stream.
350 (pprint-logical-block (stream nil :prefix "#<" :suffix ">")
351 (print-description)))
353 (write-string "#<" stream)
355 (write-char #\> stream))))
358 ;;;; OUTPUT-OBJECT -- the main entry point
360 ;;; Objects whose print representation identifies them EQLly don't
361 ;;; need to be checked for circularity.
362 (defun uniquely-identified-by-print-p (x)
366 (symbol-package x))))
368 ;;; Output OBJECT to STREAM observing all printer control variables.
369 (defun output-object (object stream)
370 (labels ((print-it (stream)
372 (sb!pretty:output-pretty-object object stream)
373 (output-ugly-object object stream)))
375 (multiple-value-bind (marker initiate)
376 (check-for-circularity object t)
377 (if (eq initiate :initiate)
378 (let ((*circularity-hash-table*
379 (make-hash-table :test 'eq)))
380 (check-it (make-broadcast-stream))
381 (let ((*circularity-counter* 0))
385 (when (handle-circularity marker stream)
387 (print-it stream))))))
388 (cond (;; Maybe we don't need to bother with circularity detection.
389 (or (not *print-circle*)
390 (uniquely-identified-by-print-p object))
392 (;; If we have already started circularity detection, this
393 ;; object might be a shared reference. If we have not, then
394 ;; if it is a compound object it might contain a circular
395 ;; reference to itself or multiple shared references.
396 (or *circularity-hash-table*
397 (compound-object-p object))
400 (print-it stream)))))
402 ;;; a hack to work around recurring gotchas with printing while
403 ;;; DEFGENERIC PRINT-OBJECT is being built
405 ;;; (hopefully will go away naturally when CLOS moves into cold init)
406 (defvar *print-object-is-disabled-p*)
408 ;;; Output OBJECT to STREAM observing all printer control variables
409 ;;; except for *PRINT-PRETTY*. Note: if *PRINT-PRETTY* is non-NIL,
410 ;;; then the pretty printer will be used for any components of OBJECT,
411 ;;; just not for OBJECT itself.
412 (defun output-ugly-object (object stream)
414 ;; KLUDGE: The TYPECASE approach here is non-ANSI; the ANSI definition of
415 ;; PRINT-OBJECT says it provides printing and we're supposed to provide
416 ;; PRINT-OBJECT methods covering all classes. We deviate from this
417 ;; by using PRINT-OBJECT only when we print instance values. However,
418 ;; ANSI makes it hard to tell that we're deviating from this:
419 ;; (1) ANSI specifies that the user isn't supposed to call PRINT-OBJECT
421 ;; (2) ANSI (section 11.1.2.1.2) says it's undefined to define
422 ;; a method on an external symbol in the CL package which is
423 ;; applicable to arg lists containing only direct instances of
424 ;; standardized classes.
425 ;; Thus, in order for the user to detect our sleaziness in conforming
426 ;; code, he has to do something relatively obscure like
427 ;; (1) actually use tools like FIND-METHOD to look for PRINT-OBJECT
429 ;; (2) define a PRINT-OBJECT method which is specialized on the stream
430 ;; value (e.g. a Gray stream object).
431 ;; As long as no one comes up with a non-obscure way of detecting this
432 ;; sleaziness, fixing this nonconformity will probably have a low
433 ;; priority. -- WHN 2001-11-25
436 (output-symbol object stream)
437 (output-list object stream)))
439 (cond ((not (and (boundp '*print-object-is-disabled-p*)
440 *print-object-is-disabled-p*))
441 (print-object object stream))
442 ((typep object 'structure-object)
443 (default-structure-print object stream *current-level-in-print*))
445 (write-string "#<INSTANCE but not STRUCTURE-OBJECT>" stream))))
446 (funcallable-instance
448 ((not (and (boundp '*print-object-is-disabled-p*)
449 *print-object-is-disabled-p*))
450 (print-object object stream))
451 (t (output-fun object stream))))
453 (output-fun object stream))
455 (output-symbol object stream))
459 (output-integer object stream))
461 (output-float object stream))
463 (output-ratio object stream))
465 (output-complex object stream))))
467 (output-character object stream))
469 (output-vector object stream))
471 (output-array object stream))
473 (output-sap object stream))
475 (output-weak-pointer object stream))
477 (output-lra object stream))
479 (output-code-component object stream))
481 (output-fdefn object stream))
483 (output-random object stream))))
487 ;;; values of *PRINT-CASE* and (READTABLE-CASE *READTABLE*) the last
488 ;;; time the printer was called
489 (defvar *previous-case* nil)
490 (defvar *previous-readtable-case* nil)
492 ;;; This variable contains the current definition of one of three
493 ;;; symbol printers. SETUP-PRINTER-STATE sets this variable.
494 (defvar *internal-symbol-output-fun* nil)
496 ;;; This function sets the internal global symbol
497 ;;; *INTERNAL-SYMBOL-OUTPUT-FUN* to the right function depending on
498 ;;; the value of *PRINT-CASE*. See the manual for details. The print
499 ;;; buffer stream is also reset.
500 (defun setup-printer-state ()
501 (unless (and (eq *print-case* *previous-case*)
502 (eq (readtable-case *readtable*) *previous-readtable-case*))
503 (setq *previous-case* *print-case*)
504 (setq *previous-readtable-case* (readtable-case *readtable*))
505 (unless (member *print-case* '(:upcase :downcase :capitalize))
506 (setq *print-case* :upcase)
507 (error "invalid *PRINT-CASE* value: ~S" *previous-case*))
508 (unless (member *previous-readtable-case*
509 '(:upcase :downcase :invert :preserve))
510 (setf (readtable-case *readtable*) :upcase)
511 (error "invalid READTABLE-CASE value: ~S" *previous-readtable-case*))
513 (setq *internal-symbol-output-fun*
514 (case *previous-readtable-case*
517 (:upcase #'output-preserve-symbol)
518 (:downcase #'output-lowercase-symbol)
519 (:capitalize #'output-capitalize-symbol)))
522 (:upcase #'output-uppercase-symbol)
523 (:downcase #'output-preserve-symbol)
524 (:capitalize #'output-capitalize-symbol)))
525 (:preserve #'output-preserve-symbol)
526 (:invert #'output-invert-symbol)))))
528 ;;; Output PNAME (a symbol-name or package-name) surrounded with |'s,
529 ;;; and with any embedded |'s or \'s escaped.
530 (defun output-quoted-symbol-name (pname stream)
531 (write-char #\| stream)
532 (dotimes (index (length pname))
533 (let ((char (schar pname index)))
534 (when (or (char= char #\\) (char= char #\|))
535 (write-char #\\ stream))
536 (write-char char stream)))
537 (write-char #\| stream))
539 (defun output-symbol (object stream)
540 (if (or *print-escape* *print-readably*)
541 (let ((package (symbol-package object))
542 (name (symbol-name object)))
544 ;; The ANSI spec "22.1.3.3.1 Package Prefixes for Symbols"
545 ;; requires that keywords be printed with preceding colons
546 ;; always, regardless of the value of *PACKAGE*.
547 ((eq package *keyword-package*)
548 (write-char #\: stream))
549 ;; Otherwise, if the symbol's home package is the current
550 ;; one, then a prefix is never necessary.
551 ((eq package (sane-package)))
552 ;; Uninterned symbols print with a leading #:.
554 (when (or *print-gensym* *print-readably*)
555 (write-string "#:" stream)))
557 (multiple-value-bind (symbol accessible)
558 (find-symbol name (sane-package))
559 ;; If we can find the symbol by looking it up, it need not
560 ;; be qualified. This can happen if the symbol has been
561 ;; inherited from a package other than its home package.
562 (unless (and accessible (eq symbol object))
563 (output-symbol-name (package-name package) stream)
564 (multiple-value-bind (symbol externalp)
565 (find-external-symbol name package)
566 (declare (ignore symbol))
568 (write-char #\: stream)
569 (write-string "::" stream)))))))
570 (output-symbol-name name stream))
571 (output-symbol-name (symbol-name object) stream nil)))
573 ;;; Output the string NAME as if it were a symbol name. In other
574 ;;; words, diddle its case according to *PRINT-CASE* and
576 (defun output-symbol-name (name stream &optional (maybe-quote t))
577 (declare (type simple-string name))
578 (let ((*readtable* (if *print-readably* *standard-readtable* *readtable*)))
579 (setup-printer-state)
580 (if (and maybe-quote (symbol-quotep name))
581 (output-quoted-symbol-name name stream)
582 (funcall *internal-symbol-output-fun* name stream))))
584 ;;;; escaping symbols
586 ;;; When we print symbols we have to figure out if they need to be
587 ;;; printed with escape characters. This isn't a whole lot easier than
588 ;;; reading symbols in the first place.
590 ;;; For each character, the value of the corresponding element is a
591 ;;; fixnum with bits set corresponding to attributes that the
592 ;;; character has. At characters have at least one bit set, so we can
593 ;;; search for any character with a positive test.
594 (defvar *character-attributes*
595 (make-array 160 ; FIXME
596 :element-type '(unsigned-byte 16)
598 (declaim (type (simple-array (unsigned-byte 16) (#.160)) ; FIXME
599 *character-attributes*))
601 ;;; constants which are a bit-mask for each interesting character attribute
602 (defconstant other-attribute (ash 1 0)) ; Anything else legal.
603 (defconstant number-attribute (ash 1 1)) ; A numeric digit.
604 (defconstant uppercase-attribute (ash 1 2)) ; An uppercase letter.
605 (defconstant lowercase-attribute (ash 1 3)) ; A lowercase letter.
606 (defconstant sign-attribute (ash 1 4)) ; +-
607 (defconstant extension-attribute (ash 1 5)) ; ^_
608 (defconstant dot-attribute (ash 1 6)) ; .
609 (defconstant slash-attribute (ash 1 7)) ; /
610 (defconstant funny-attribute (ash 1 8)) ; Anything illegal.
612 (eval-when (:compile-toplevel :load-toplevel :execute)
614 ;;; LETTER-ATTRIBUTE is a local of SYMBOL-QUOTEP. It matches letters
615 ;;; that don't need to be escaped (according to READTABLE-CASE.)
616 (defparameter *attribute-names*
617 `((number . number-attribute) (lowercase . lowercase-attribute)
618 (uppercase . uppercase-attribute) (letter . letter-attribute)
619 (sign . sign-attribute) (extension . extension-attribute)
620 (dot . dot-attribute) (slash . slash-attribute)
621 (other . other-attribute) (funny . funny-attribute)))
625 (flet ((set-bit (char bit)
626 (let ((code (char-code char)))
627 (setf (aref *character-attributes* code)
628 (logior bit (aref *character-attributes* code))))))
630 (dolist (char '(#\! #\@ #\$ #\% #\& #\* #\= #\~ #\[ #\] #\{ #\}
632 (set-bit char other-attribute))
635 (set-bit (digit-char i) number-attribute))
637 (do ((code (char-code #\A) (1+ code))
638 (end (char-code #\Z)))
640 (declare (fixnum code end))
641 (set-bit (code-char code) uppercase-attribute)
642 (set-bit (char-downcase (code-char code)) lowercase-attribute))
644 (set-bit #\- sign-attribute)
645 (set-bit #\+ sign-attribute)
646 (set-bit #\^ extension-attribute)
647 (set-bit #\_ extension-attribute)
648 (set-bit #\. dot-attribute)
649 (set-bit #\/ slash-attribute)
651 ;; Mark anything not explicitly allowed as funny.
652 (dotimes (i 160) ; FIXME
653 (when (zerop (aref *character-attributes* i))
654 (setf (aref *character-attributes* i) funny-attribute))))
656 ;;; For each character, the value of the corresponding element is the
657 ;;; lowest base in which that character is a digit.
658 (defvar *digit-bases*
659 (make-array 128 ; FIXME
660 :element-type '(unsigned-byte 8)
661 :initial-element 36))
662 (declaim (type (simple-array (unsigned-byte 8) (#.128)) ; FIXME
665 (let ((char (digit-char i 36)))
666 (setf (aref *digit-bases* (char-code char)) i)))
668 ;;; A FSM-like thingie that determines whether a symbol is a potential
669 ;;; number or has evil characters in it.
670 (defun symbol-quotep (name)
671 (declare (simple-string name))
672 (macrolet ((advance (tag &optional (at-end t))
675 ,(if at-end '(go TEST-SIGN) '(return nil)))
676 (setq current (schar name index)
677 code (char-code current)
679 ((< code 160) (aref attributes code))
680 ((upper-case-p current) uppercase-attribute)
681 ((lower-case-p current) lowercase-attribute)
682 (t other-attribute)))
685 (test (&rest attributes)
697 `(and (< code 128) ; FIXME
698 (< (the fixnum (aref bases code)) base))))
700 (prog ((len (length name))
701 (attributes *character-attributes*)
702 (bases *digit-bases*)
705 (case (readtable-case *readtable*)
706 (:upcase uppercase-attribute)
707 (:downcase lowercase-attribute)
708 (t (logior lowercase-attribute uppercase-attribute))))
713 (declare (fixnum len base index bits code))
716 TEST-SIGN ; At end, see whether it is a sign...
717 (return (not (test sign)))
719 OTHER ; not potential number, see whether funny chars...
720 (let ((mask (logxor (logior lowercase-attribute uppercase-attribute
723 (do ((i (1- index) (1+ i)))
724 ((= i len) (return-from symbol-quotep nil))
725 (unless (zerop (logand (let* ((char (schar name i))
726 (code (char-code char)))
728 ((< code 160) (aref attributes code))
729 ((upper-case-p char) uppercase-attribute)
730 ((lower-case-p char) lowercase-attribute)
731 (t other-attribute)))
733 (return-from symbol-quotep t))))
738 (advance LAST-DIGIT-ALPHA)
740 (when (test letter number other slash) (advance OTHER nil))
741 (when (char= current #\.) (advance DOT-FOUND))
742 (when (test sign extension) (advance START-STUFF nil))
745 DOT-FOUND ; leading dots...
746 (when (test letter) (advance START-DOT-MARKER nil))
747 (when (digitp) (advance DOT-DIGIT))
748 (when (test number other) (advance OTHER nil))
749 (when (test extension slash sign) (advance START-DOT-STUFF nil))
750 (when (char= current #\.) (advance DOT-FOUND))
753 START-STUFF ; leading stuff before any dot or digit
756 (advance LAST-DIGIT-ALPHA)
758 (when (test number other) (advance OTHER nil))
759 (when (test letter) (advance START-MARKER nil))
760 (when (char= current #\.) (advance START-DOT-STUFF nil))
761 (when (test sign extension slash) (advance START-STUFF nil))
764 START-MARKER ; number marker in leading stuff...
765 (when (test letter) (advance OTHER nil))
768 START-DOT-STUFF ; leading stuff containing dot without digit...
769 (when (test letter) (advance START-DOT-STUFF nil))
770 (when (digitp) (advance DOT-DIGIT))
771 (when (test sign extension dot slash) (advance START-DOT-STUFF nil))
772 (when (test number other) (advance OTHER nil))
775 START-DOT-MARKER ; number marker in leading stuff with dot..
776 ;; leading stuff containing dot without digit followed by letter...
777 (when (test letter) (advance OTHER nil))
780 DOT-DIGIT ; in a thing with dots...
781 (when (test letter) (advance DOT-MARKER))
782 (when (digitp) (advance DOT-DIGIT))
783 (when (test number other) (advance OTHER nil))
784 (when (test sign extension dot slash) (advance DOT-DIGIT))
787 DOT-MARKER ; number marker in number with dot...
788 (when (test letter) (advance OTHER nil))
791 LAST-DIGIT-ALPHA ; previous char is a letter digit...
792 (when (or (digitp) (test sign slash))
793 (advance ALPHA-DIGIT))
794 (when (test letter number other dot) (advance OTHER nil))
797 ALPHA-DIGIT ; seen a digit which is a letter...
798 (when (or (digitp) (test sign slash))
800 (advance LAST-DIGIT-ALPHA)
801 (advance ALPHA-DIGIT)))
802 (when (test letter) (advance ALPHA-MARKER))
803 (when (test number other dot) (advance OTHER nil))
806 ALPHA-MARKER ; number marker in number with alpha digit...
807 (when (test letter) (advance OTHER nil))
810 DIGIT ; seen only ordinary (non-alphabetic) numeric digits...
813 (advance ALPHA-DIGIT)
815 (when (test number other) (advance OTHER nil))
816 (when (test letter) (advance MARKER))
817 (when (test extension slash sign) (advance DIGIT))
818 (when (char= current #\.) (advance DOT-DIGIT))
821 MARKER ; number marker in a numeric number...
822 ;; ("What," you may ask, "is a 'number marker'?" It's something
823 ;; that a conforming implementation might use in number syntax.
824 ;; See ANSI 2.3.1.1 "Potential Numbers as Tokens".)
825 (when (test letter) (advance OTHER nil))
828 ;;;; *INTERNAL-SYMBOL-OUTPUT-FUN*
830 ;;;; case hackery: These functions are stored in
831 ;;;; *INTERNAL-SYMBOL-OUTPUT-FUN* according to the values of
832 ;;;; *PRINT-CASE* and READTABLE-CASE.
835 ;;; READTABLE-CASE *PRINT-CASE*
837 ;;; :DOWNCASE :DOWNCASE
839 (defun output-preserve-symbol (pname stream)
840 (declare (simple-string pname))
841 (write-string pname stream))
844 ;;; READTABLE-CASE *PRINT-CASE*
845 ;;; :UPCASE :DOWNCASE
846 (defun output-lowercase-symbol (pname stream)
847 (declare (simple-string pname))
848 (dotimes (index (length pname))
849 (let ((char (schar pname index)))
850 (write-char (char-downcase char) stream))))
853 ;;; READTABLE-CASE *PRINT-CASE*
854 ;;; :DOWNCASE :UPCASE
855 (defun output-uppercase-symbol (pname stream)
856 (declare (simple-string pname))
857 (dotimes (index (length pname))
858 (let ((char (schar pname index)))
859 (write-char (char-upcase char) stream))))
862 ;;; READTABLE-CASE *PRINT-CASE*
863 ;;; :UPCASE :CAPITALIZE
864 ;;; :DOWNCASE :CAPITALIZE
865 (defun output-capitalize-symbol (pname stream)
866 (declare (simple-string pname))
867 (let ((prev-not-alphanum t)
868 (up (eq (readtable-case *readtable*) :upcase)))
869 (dotimes (i (length pname))
870 (let ((char (char pname i)))
872 (if (or prev-not-alphanum (lower-case-p char))
874 (char-downcase char))
875 (if prev-not-alphanum
879 (setq prev-not-alphanum (not (alphanumericp char)))))))
882 ;;; READTABLE-CASE *PRINT-CASE*
884 (defun output-invert-symbol (pname stream)
885 (declare (simple-string pname))
888 (dotimes (i (length pname))
889 (let ((ch (schar pname i)))
890 (when (both-case-p ch)
891 (if (upper-case-p ch)
893 (setq all-upper nil)))))
894 (cond (all-upper (output-lowercase-symbol pname stream))
895 (all-lower (output-uppercase-symbol pname stream))
897 (write-string pname stream)))))
901 (let ((*readtable* (copy-readtable nil)))
902 (format t "READTABLE-CASE Input Symbol-name~@
903 ----------------------------------~%")
904 (dolist (readtable-case '(:upcase :downcase :preserve :invert))
905 (setf (readtable-case *readtable*) readtable-case)
906 (dolist (input '("ZEBRA" "Zebra" "zebra"))
907 (format t "~&:~A~16T~A~24T~A"
908 (string-upcase readtable-case)
910 (symbol-name (read-from-string input)))))))
913 (let ((*readtable* (copy-readtable nil)))
914 (format t "READTABLE-CASE *PRINT-CASE* Symbol-name Output Princ~@
915 --------------------------------------------------------~%")
916 (dolist (readtable-case '(:upcase :downcase :preserve :invert))
917 (setf (readtable-case *readtable*) readtable-case)
918 (dolist (*print-case* '(:upcase :downcase :capitalize))
919 (dolist (symbol '(|ZEBRA| |Zebra| |zebra|))
920 (format t "~&:~A~15T:~A~29T~A~42T~A~50T~A"
921 (string-upcase readtable-case)
922 (string-upcase *print-case*)
924 (prin1-to-string symbol)
925 (princ-to-string symbol)))))))
928 ;;;; recursive objects
930 (defun output-list (list stream)
931 (descend-into (stream)
932 (write-char #\( stream)
936 (punt-print-if-too-long length stream)
937 (output-object (pop list) stream)
940 (when (or (atom list)
941 (check-for-circularity list))
942 (write-string " . " stream)
943 (output-object list stream)
945 (write-char #\space stream)
947 (write-char #\) stream)))
949 (defun output-vector (vector stream)
950 (declare (vector vector))
951 (cond ((stringp vector)
952 (cond ((and *print-readably*
953 (not (eq (array-element-type vector)
956 (make-array 0 :element-type 'character))))))
958 (error 'print-not-readable :object vector)
960 :report "Print unreadably."
961 (let ((*print-readably* nil))
962 (output-vector vector stream)))
964 :report "Supply an object to be printed instead."
965 :interactive read-unreadable-replacement
966 (write o :stream stream))))
967 ((or *print-escape* *print-readably*)
968 (write-char #\" stream)
969 (quote-string vector stream)
970 (write-char #\" stream))
972 (write-string vector stream))))
973 ((not (or *print-array* *print-readably*))
974 (output-terse-array vector stream))
975 ((bit-vector-p vector)
976 (write-string "#*" stream)
977 (dovector (bit vector)
978 ;; (Don't use OUTPUT-OBJECT here, since this code
979 ;; has to work for all possible *PRINT-BASE* values.)
980 (write-char (if (zerop bit) #\0 #\1) stream)))
982 (when (and *print-readably*
983 (not (array-readably-printable-p vector)))
985 (error 'print-not-readable :object vector)
987 :report "Print unreadably.")
989 :report "Supply an object to be printed instead."
990 :interactive read-unreadable-replacement
991 (return-from output-vector (write o :stream stream)))))
992 (descend-into (stream)
993 (write-string "#(" stream)
994 (dotimes (i (length vector))
996 (write-char #\space stream))
997 (punt-print-if-too-long i stream)
998 (output-object (aref vector i) stream))
999 (write-string ")" stream)))))
1001 ;;; This function outputs a string quoting characters sufficiently
1002 ;;; so that someone can read it in again. Basically, put a slash in
1003 ;;; front of an character satisfying NEEDS-SLASH-P.
1004 (defun quote-string (string stream)
1005 (macrolet ((needs-slash-p (char)
1006 ;; KLUDGE: We probably should look at the readtable, but just do
1007 ;; this for now. [noted by anonymous long ago] -- WHN 19991130
1008 `(or (char= ,char #\\)
1009 (char= ,char #\"))))
1010 (with-array-data ((data string) (start) (end)
1011 :check-fill-pointer t)
1012 (do ((index start (1+ index)))
1014 (let ((char (schar data index)))
1015 (when (needs-slash-p char) (write-char #\\ stream))
1016 (write-char char stream))))))
1018 (defun array-readably-printable-p (array)
1019 (and (eq (array-element-type array) t)
1020 (let ((zero (position 0 (array-dimensions array)))
1021 (number (position 0 (array-dimensions array)
1022 :test (complement #'eql)
1024 (or (null zero) (null number) (> zero number)))))
1026 ;;; Output the printed representation of any array in either the #< or #A
1028 (defun output-array (array stream)
1029 (if (or *print-array* *print-readably*)
1030 (output-array-guts array stream)
1031 (output-terse-array array stream)))
1033 ;;; Output the abbreviated #< form of an array.
1034 (defun output-terse-array (array stream)
1035 (let ((*print-level* nil)
1036 (*print-length* nil))
1037 (print-unreadable-object (array stream :type t :identity t))))
1039 ;;; Output the readable #A form of an array.
1040 (defun output-array-guts (array stream)
1041 (when (and *print-readably*
1042 (not (array-readably-printable-p array)))
1044 (error 'print-not-readable :object array)
1045 (print-unreadably ()
1046 :report "Print unreadably.")
1048 :report "Supply an object to be printed instead."
1049 :interactive read-unreadable-replacement
1050 (return-from output-array-guts (write o :stream stream)))))
1051 (write-char #\# stream)
1052 (let ((*print-base* 10)
1053 (*print-radix* nil))
1054 (output-integer (array-rank array) stream))
1055 (write-char #\A stream)
1056 (with-array-data ((data array) (start) (end))
1057 (declare (ignore end))
1058 (sub-output-array-guts data (array-dimensions array) stream start)))
1060 (defun sub-output-array-guts (array dimensions stream index)
1061 (declare (type (simple-array * (*)) array) (fixnum index))
1062 (cond ((null dimensions)
1063 (output-object (aref array index) stream))
1065 (descend-into (stream)
1066 (write-char #\( stream)
1067 (let* ((dimension (car dimensions))
1068 (dimensions (cdr dimensions))
1069 (count (reduce #'* dimensions)))
1070 (dotimes (i dimension)
1072 (write-char #\space stream))
1073 (punt-print-if-too-long i stream)
1074 (sub-output-array-guts array dimensions stream index)
1075 (incf index count)))
1076 (write-char #\) stream)))))
1078 ;;; a trivial non-generic-function placeholder for PRINT-OBJECT, for
1079 ;;; use until CLOS is set up (at which time it will be replaced with
1080 ;;; the real generic function implementation)
1081 (defun print-object (instance stream)
1082 (default-structure-print instance stream *current-level-in-print*))
1084 ;;;; integer, ratio, and complex printing (i.e. everything but floats)
1086 (defun %output-radix (base stream)
1087 (write-char #\# stream)
1088 (write-char (case base
1092 (t (%output-reasonable-integer-in-base base 10 stream)
1096 (defun %output-reasonable-integer-in-base (n base stream)
1097 (multiple-value-bind (q r)
1099 ;; Recurse until you have all the digits pushed on
1102 (%output-reasonable-integer-in-base q base stream))
1103 ;; Then as each recursive call unwinds, turn the
1104 ;; digit (in remainder) into a character and output
1107 (schar "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" r)
1110 ;;; *POWER-CACHE* is an alist mapping bases to power-vectors. It is
1111 ;;; filled and probed by POWERS-FOR-BASE. SCRUB-POWER-CACHE is called
1112 ;;; always prior a GC to drop overly large bignums from the cache.
1114 ;;; It doesn't need a lock, but if you work on SCRUB-POWER-CACHE or
1115 ;;; POWERS-FOR-BASE, see that you don't break the assumptions!
1116 (defvar *power-cache* nil)
1118 (defconstant +power-cache-integer-length-limit+ 2048)
1120 (defun scrub-power-cache ()
1121 (let ((cache *power-cache*))
1122 (dolist (cell cache)
1123 (let ((powers (cdr cell)))
1124 (declare (simple-vector powers))
1125 (let ((too-big (position-if
1127 (>= (integer-length x)
1128 +power-cache-integer-length-limit+))
1131 (setf (cdr cell) (subseq powers 0 too-big))))))
1132 ;; Since base 10 is overwhelmingly common, make sure it's at head.
1133 ;; Try to keep other bases in a hopefully sensible order as well.
1134 (if (eql 10 (caar cache))
1135 (setf *power-cache* cache)
1136 ;; If we modify the list destructively we need to copy it, otherwise
1137 ;; an alist lookup in progress might be screwed.
1138 (setf *power-cache* (sort (copy-list cache)
1140 (declare (fixnum a b))
1150 ;;; Compute (and cache) a power vector for a BASE and LIMIT:
1151 ;;; the vector holds integers for which
1152 ;;; (aref powers k) == (expt base (expt 2 k))
1154 (defun powers-for-base (base limit)
1155 (flet ((compute-powers (from)
1157 (do ((p from (* p p)))
1159 ;; We don't actually need this, but we also
1160 ;; prefer not to cons it up a second time...
1163 (nreverse powers))))
1164 ;; Grab a local reference so that we won't stuff consed at the
1165 ;; head by other threads -- or sorting by SCRUB-POWER-CACHE.
1166 (let ((cache *power-cache*))
1167 (let ((cell (assoc base cache)))
1169 (let* ((powers (cdr cell))
1170 (len (length powers))
1171 (max (svref powers (1- len))))
1175 (concatenate 'vector powers
1176 (compute-powers (* max max)))))
1177 (setf (cdr cell) new)
1179 (let ((powers (coerce (compute-powers base) 'vector)))
1180 ;; Add new base to head: SCRUB-POWER-CACHE will later
1181 ;; put it to a better place.
1182 (setf *power-cache* (acons base powers cache))
1185 ;; Algorithm by Harald Hanche-Olsen, sbcl-devel 2005-02-05
1186 (defun %output-huge-integer-in-base (n base stream)
1187 (declare (type bignum n) (type fixnum base))
1188 ;; POWER is a vector for which the following holds:
1189 ;; (aref power k) == (expt base (expt 2 k))
1190 (let* ((power (powers-for-base base n))
1191 (k-start (or (position-if (lambda (x) (> x n)) power)
1192 (bug "power-vector too short"))))
1193 (labels ((bisect (n k exactp)
1194 (declare (fixnum k))
1195 ;; N is the number to bisect
1196 ;; K on initial entry BASE^(2^K) > N
1197 ;; EXACTP is true if 2^K is the exact number of digits
1200 (loop repeat (ash 1 k) do (write-char #\0 stream))))
1203 (schar "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" n)
1207 (multiple-value-bind (q r) (truncate n (aref power k))
1208 ;; EXACTP is NIL only at the head of the
1209 ;; initial number, as we don't know the number
1210 ;; of digits there, but we do know that it
1211 ;; doesn't get any leading zeros.
1213 (bisect r k (or exactp (plusp q))))))))
1214 (bisect n k-start nil))))
1216 (defun %output-integer-in-base (integer base stream)
1217 (when (minusp integer)
1218 (write-char #\- stream)
1219 (setf integer (- integer)))
1220 ;; The ideal cutoff point between these two algorithms is almost
1221 ;; certainly quite platform dependent: this gives 87 for 32 bit
1222 ;; SBCL, which is about right at least for x86/Darwin.
1223 (if (or (fixnump integer)
1224 (< (integer-length integer) (* 3 sb!vm:n-positive-fixnum-bits)))
1225 (%output-reasonable-integer-in-base integer base stream)
1226 (%output-huge-integer-in-base integer base stream)))
1228 (defun output-integer (integer stream)
1229 (let ((base *print-base*))
1230 (when (and (/= base 10) *print-radix*)
1231 (%output-radix base stream))
1232 (%output-integer-in-base integer base stream)
1233 (when (and *print-radix* (= base 10))
1234 (write-char #\. stream))))
1236 (defun output-ratio (ratio stream)
1237 (let ((base *print-base*))
1239 (%output-radix base stream))
1240 (%output-integer-in-base (numerator ratio) base stream)
1241 (write-char #\/ stream)
1242 (%output-integer-in-base (denominator ratio) base stream)))
1244 (defun output-complex (complex stream)
1245 (write-string "#C(" stream)
1246 ;; FIXME: Could this just be OUTPUT-NUMBER?
1247 (output-object (realpart complex) stream)
1248 (write-char #\space stream)
1249 (output-object (imagpart complex) stream)
1250 (write-char #\) stream))
1254 ;;; FLONUM-TO-STRING (and its subsidiary function FLOAT-STRING) does
1255 ;;; most of the work for all printing of floating point numbers in
1256 ;;; FORMAT. It converts a floating point number to a string in a free
1257 ;;; or fixed format with no exponent. The interpretation of the
1258 ;;; arguments is as follows:
1260 ;;; X - The floating point number to convert, which must not be
1262 ;;; WIDTH - The preferred field width, used to determine the number
1263 ;;; of fraction digits to produce if the FDIGITS parameter
1264 ;;; is unspecified or NIL. If the non-fraction digits and the
1265 ;;; decimal point alone exceed this width, no fraction digits
1266 ;;; will be produced unless a non-NIL value of FDIGITS has been
1267 ;;; specified. Field overflow is not considerd an error at this
1269 ;;; FDIGITS - The number of fractional digits to produce. Insignificant
1270 ;;; trailing zeroes may be introduced as needed. May be
1271 ;;; unspecified or NIL, in which case as many digits as possible
1272 ;;; are generated, subject to the constraint that there are no
1273 ;;; trailing zeroes.
1274 ;;; SCALE - If this parameter is specified or non-NIL, then the number
1275 ;;; printed is (* x (expt 10 scale)). This scaling is exact,
1276 ;;; and cannot lose precision.
1277 ;;; FMIN - This parameter, if specified or non-NIL, is the minimum
1278 ;;; number of fraction digits which will be produced, regardless
1279 ;;; of the value of WIDTH or FDIGITS. This feature is used by
1280 ;;; the ~E format directive to prevent complete loss of
1281 ;;; significance in the printed value due to a bogus choice of
1285 ;;; (VALUES DIGIT-STRING DIGIT-LENGTH LEADING-POINT TRAILING-POINT DECPNT)
1286 ;;; where the results have the following interpretation:
1288 ;;; DIGIT-STRING - The decimal representation of X, with decimal point.
1289 ;;; DIGIT-LENGTH - The length of the string DIGIT-STRING.
1290 ;;; LEADING-POINT - True if the first character of DIGIT-STRING is the
1292 ;;; TRAILING-POINT - True if the last character of DIGIT-STRING is the
1294 ;;; POINT-POS - The position of the digit preceding the decimal
1295 ;;; point. Zero indicates point before first digit.
1297 ;;; NOTE: FLONUM-TO-STRING goes to a lot of trouble to guarantee
1298 ;;; accuracy. Specifically, the decimal number printed is the closest
1299 ;;; possible approximation to the true value of the binary number to
1300 ;;; be printed from among all decimal representations with the same
1301 ;;; number of digits. In free-format output, i.e. with the number of
1302 ;;; digits unconstrained, it is guaranteed that all the information is
1303 ;;; preserved, so that a properly- rounding reader can reconstruct the
1304 ;;; original binary number, bit-for-bit, from its printed decimal
1305 ;;; representation. Furthermore, only as many digits as necessary to
1306 ;;; satisfy this condition will be printed.
1308 ;;; FLOAT-DIGITS actually generates the digits for positive numbers;
1309 ;;; see below for comments.
1311 (defun flonum-to-string (x &optional width fdigits scale fmin)
1312 (declare (type float x))
1313 ;; FIXME: I think only FORMAT-DOLLARS calls FLONUM-TO-STRING with
1314 ;; possibly-negative X.
1316 (multiple-value-bind (e string)
1318 (flonum-to-digits x (min (- (+ fdigits (or scale 0)))
1320 (if (and width (> width 1))
1321 (let ((w (multiple-value-list
1325 (if (and scale (minusp scale))
1328 (f (multiple-value-list
1329 (flonum-to-digits x (- (+ (or fmin 0)
1330 (if scale scale 0)))))))
1332 ((>= (length (cadr w)) (length (cadr f)))
1334 (t (values-list f))))
1335 (flonum-to-digits x)))
1336 (let ((e (if (zerop x)
1338 (+ e (or scale 0))))
1339 (stream (make-string-output-stream)))
1342 (write-string string stream :end (min (length string) e))
1343 (dotimes (i (- e (length string)))
1344 (write-char #\0 stream))
1345 (write-char #\. stream)
1346 (write-string string stream :start (min (length string) e))
1348 (dotimes (i (- fdigits
1350 (min (length string) e))))
1351 (write-char #\0 stream))))
1353 (write-string "." stream)
1355 (write-char #\0 stream))
1356 (write-string string stream :end (when fdigits
1357 (min (length string)
1361 (dotimes (i (+ fdigits e (- (length string))))
1362 (write-char #\0 stream)))))
1363 (let ((string (get-output-stream-string stream)))
1364 (values string (length string)
1365 (char= (char string 0) #\.)
1366 (char= (char string (1- (length string))) #\.)
1367 (position #\. string))))))
1369 ;;; implementation of figure 1 from Burger and Dybvig, 1996. It is
1370 ;;; extended in order to handle rounding.
1372 ;;; As the implementation of the Dragon from Classic CMUCL (and
1373 ;;; previously in SBCL above FLONUM-TO-STRING) says: "DO NOT EVEN
1374 ;;; THINK OF ATTEMPTING TO UNDERSTAND THIS CODE WITHOUT READING THE
1375 ;;; PAPER!", and in this case we have to add that even reading the
1376 ;;; paper might not bring immediate illumination as CSR has attempted
1377 ;;; to turn idiomatic Scheme into idiomatic Lisp.
1379 ;;; FIXME: figure 1 from Burger and Dybvig is the unoptimized
1380 ;;; algorithm, noticeably slow at finding the exponent. Figure 2 has
1381 ;;; an improved algorithm, but CSR ran out of energy.
1383 ;;; possible extension for the enthusiastic: printing floats in bases
1384 ;;; other than base 10.
1385 (defconstant single-float-min-e
1386 (- 2 sb!vm:single-float-bias sb!vm:single-float-digits))
1387 (defconstant double-float-min-e
1388 (- 2 sb!vm:double-float-bias sb!vm:double-float-digits))
1390 (defconstant long-float-min-e
1391 (nth-value 1 (decode-float least-positive-long-float)))
1393 (defun flonum-to-digits (v &optional position relativep)
1394 (let ((print-base 10) ; B
1396 (float-digits (float-digits v)) ; p
1397 (digit-characters "0123456789")
1400 (single-float single-float-min-e)
1401 (double-float double-float-min-e)
1403 (long-float long-float-min-e))))
1404 (multiple-value-bind (f e)
1405 (integer-decode-float v)
1406 (let (;; FIXME: these even tests assume normal IEEE rounding
1407 ;; mode. I wonder if we should cater for non-normal?
1410 (with-push-char (:element-type base-char)
1411 (labels ((scale (r s m+ m-)
1413 (s s (* s print-base)))
1414 ((not (or (> (+ r m+) s)
1415 (and high-ok (= (+ r m+) s))))
1417 (r r (* r print-base))
1418 (m+ m+ (* m+ print-base))
1419 (m- m- (* m- print-base)))
1420 ((not (and (plusp (- r m-)) ; Extension to handle zero
1421 (or (< (* (+ r m+) print-base) s)
1423 (= (* (+ r m+) print-base) s)))))
1424 (values k (generate r s m+ m-)))))))
1425 (generate (r s m+ m-)
1429 (setf (values d r) (truncate (* r print-base) s))
1430 (setf m+ (* m+ print-base))
1431 (setf m- (* m- print-base))
1432 (setf tc1 (or (< r m-) (and low-ok (= r m-))))
1433 (setf tc2 (or (> (+ r m+) s)
1434 (and high-ok (= (+ r m+) s))))
1437 (push-char (char digit-characters d))
1441 ((and (not tc1) tc2) (1+ d))
1442 ((and tc1 (not tc2)) d)
1444 (if (< (* r 2) s) d (1+ d))))))
1445 (push-char (char digit-characters d))
1446 (return-from generate (get-pushed-string))))))
1450 (let* ((be (expt float-radix e))
1451 (be1 (* be float-radix)))
1452 (if (/= f (expt float-radix (1- float-digits)))
1462 (/= f (expt float-radix (1- float-digits))))
1464 s (* (expt float-radix (- e)) 2)
1467 (setf r (* f float-radix 2)
1468 s (* (expt float-radix (- 1 e)) 2)
1473 (aver (> position 0))
1475 ;; running out of letters here
1476 (l 1 (* l print-base)))
1477 ((>= (* s l) (+ r m+))
1479 (if (< (+ r (* s (/ (expt print-base (- k position)) 2)))
1480 (* s (expt print-base k)))
1481 (setf position (- k position))
1482 (setf position (- k position 1))))))
1483 (let ((low (max m- (/ (* s (expt print-base position)) 2)))
1484 (high (max m+ (/ (* s (expt print-base position)) 2))))
1491 (values r s m+ m-))))
1492 (multiple-value-bind (r s m+ m-) (initialize)
1493 (scale r s m+ m-))))))))
1495 ;;; Given a non-negative floating point number, SCALE-EXPONENT returns
1496 ;;; a new floating point number Z in the range (0.1, 1.0] and an
1497 ;;; exponent E such that Z * 10^E is (approximately) equal to the
1498 ;;; original number. There may be some loss of precision due the
1499 ;;; floating point representation. The scaling is always done with
1500 ;;; long float arithmetic, which helps printing of lesser precisions
1501 ;;; as well as avoiding generic arithmetic.
1503 ;;; When computing our initial scale factor using EXPT, we pull out
1504 ;;; part of the computation to avoid over/under flow. When
1505 ;;; denormalized, we must pull out a large factor, since there is more
1506 ;;; negative exponent range than positive range.
1508 (eval-when (:compile-toplevel :execute)
1509 (setf *read-default-float-format*
1510 #!+long-float 'long-float #!-long-float 'double-float))
1511 (defun scale-exponent (original-x)
1512 (let* ((x (coerce original-x 'long-float)))
1513 (multiple-value-bind (sig exponent) (decode-float x)
1514 (declare (ignore sig))
1516 (values (float 0.0e0 original-x) 1)
1517 (let* ((ex (locally (declare (optimize (safety 0)))
1520 ;; this is the closest double float
1521 ;; to (log 2 10), but expressed so
1522 ;; that we're not vulnerable to the
1523 ;; host lisp's interpretation of
1524 ;; arithmetic. (FIXME: it turns
1525 ;; out that sbcl itself is off by 1
1526 ;; ulp in this value, which is a
1527 ;; little unfortunate.)
1530 (sb!kernel:make-double-float 1070810131 1352628735)
1532 (error "(log 2 10) not computed")))))))
1534 (if (float-denormalized-p x)
1536 (* x 1.0e16 (expt 10.0e0 (- (- ex) 16)))
1538 (* x 1.0e18 (expt 10.0e0 (- (- ex) 18)))
1539 (* x 10.0e0 (expt 10.0e0 (- (- ex) 1))))
1540 (/ x 10.0e0 (expt 10.0e0 (1- ex))))))
1541 (do ((d 10.0e0 (* d 10.0e0))
1545 (do ((m 10.0e0 (* m 10.0e0))
1549 (values (float z original-x) ex))
1550 (declare (long-float m) (integer ex))))
1551 (declare (long-float d))))))))
1552 (eval-when (:compile-toplevel :execute)
1553 (setf *read-default-float-format* 'single-float))
1555 ;;;; entry point for the float printer
1557 ;;; the float printer as called by PRINT, PRIN1, PRINC, etc. The
1558 ;;; argument is printed free-format, in either exponential or
1559 ;;; non-exponential notation, depending on its magnitude.
1561 ;;; NOTE: When a number is to be printed in exponential format, it is
1562 ;;; scaled in floating point. Since precision may be lost in this
1563 ;;; process, the guaranteed accuracy properties of FLONUM-TO-STRING
1564 ;;; are lost. The difficulty is that FLONUM-TO-STRING performs
1565 ;;; extensive computations with integers of similar magnitude to that
1566 ;;; of the number being printed. For large exponents, the bignums
1567 ;;; really get out of hand. If bignum arithmetic becomes reasonably
1568 ;;; fast and the exponent range is not too large, then it might become
1569 ;;; attractive to handle exponential notation with the same accuracy
1570 ;;; as non-exponential notation, using the method described in the
1571 ;;; Steele and White paper.
1573 ;;; NOTE II: this has been bypassed slightly by implementing Burger
1574 ;;; and Dybvig, 1996. When someone has time (KLUDGE) they can
1575 ;;; probably (a) implement the optimizations suggested by Burger and
1576 ;;; Dyvbig, and (b) remove all vestiges of Dragon4, including from
1577 ;;; fixed-format printing.
1579 ;;; Print the appropriate exponent marker for X and the specified exponent.
1580 (defun print-float-exponent (x exp stream)
1581 (declare (type float x) (type integer exp) (type stream stream))
1582 (let ((*print-radix* nil))
1583 (if (typep x *read-default-float-format*)
1585 (format stream "e~D" exp))
1586 (format stream "~C~D"
1594 (defun output-float-infinity (x stream)
1595 (declare (float x) (stream stream))
1597 (write-string "#." stream))
1600 (error 'print-not-readable :object x)
1601 (print-unreadably ()
1602 :report "Print unreadably."
1603 (let ((*print-readably* nil))
1604 (output-float-infinity x stream)))
1606 :report "Supply an object to be printed instead."
1607 :interactive read-unreadable-replacement
1608 (write o :stream stream))))
1610 (write-string "#<" stream)))
1611 (write-string "SB-EXT:" stream)
1612 (write-string (symbol-name (float-format-name x)) stream)
1613 (write-string (if (plusp x) "-POSITIVE-" "-NEGATIVE-")
1615 (write-string "INFINITY" stream)
1617 (write-string ">" stream)))
1619 (defun output-float-nan (x stream)
1620 (print-unreadable-object (x stream)
1621 (princ (float-format-name x) stream)
1622 (write-string (if (float-trapping-nan-p x) " trapping" " quiet") stream)
1623 (write-string " NaN" stream)))
1625 ;;; the function called by OUTPUT-OBJECT to handle floats
1626 (defun output-float (x stream)
1628 ((float-infinity-p x)
1629 (output-float-infinity x stream))
1631 (output-float-nan x stream))
1633 (let ((x (cond ((minusp (float-sign x))
1634 (write-char #\- stream)
1640 (write-string "0.0" stream)
1641 (print-float-exponent x 0 stream))
1643 (output-float-aux x stream -3 8)))))))
1645 (defun output-float-aux (x stream e-min e-max)
1646 (multiple-value-bind (e string)
1647 (flonum-to-digits x)
1652 (write-string string stream :end (min (length string) e))
1653 (dotimes (i (- e (length string)))
1654 (write-char #\0 stream))
1655 (write-char #\. stream)
1656 (write-string string stream :start (min (length string) e))
1657 (when (<= (length string) e)
1658 (write-char #\0 stream))
1659 (print-float-exponent x 0 stream))
1661 (write-string "0." stream)
1663 (write-char #\0 stream))
1664 (write-string string stream)
1665 (print-float-exponent x 0 stream))))
1666 (t (write-string string stream :end 1)
1667 (write-char #\. stream)
1668 (write-string string stream :start 1)
1669 (print-float-exponent x (1- e) stream)))))
1671 ;;;; other leaf objects
1673 ;;; If *PRINT-ESCAPE* is false, just do a WRITE-CHAR, otherwise output
1674 ;;; the character name or the character in the #\char format.
1675 (defun output-character (char stream)
1676 (if (or *print-escape* *print-readably*)
1677 (let ((graphicp (and (graphic-char-p char)
1678 (standard-char-p char)))
1679 (name (char-name char)))
1680 (write-string "#\\" stream)
1681 (if (and name (not graphicp))
1682 (quote-string name stream)
1683 (write-char char stream)))
1684 (write-char char stream)))
1686 (defun output-sap (sap stream)
1687 (declare (type system-area-pointer sap))
1689 (format stream "#.(~S #X~8,'0X)" 'int-sap (sap-int sap)))
1691 (print-unreadable-object (sap stream)
1692 (format stream "system area pointer: #X~8,'0X" (sap-int sap))))))
1694 (defun output-weak-pointer (weak-pointer stream)
1695 (declare (type weak-pointer weak-pointer))
1696 (print-unreadable-object (weak-pointer stream)
1697 (multiple-value-bind (value validp) (weak-pointer-value weak-pointer)
1699 (write-string "weak pointer: " stream)
1700 (write value :stream stream))
1702 (write-string "broken weak pointer" stream))))))
1704 (defun output-code-component (component stream)
1705 (print-unreadable-object (component stream :identity t)
1706 (let ((dinfo (%code-debug-info component)))
1707 (cond ((eq dinfo :bogus-lra)
1708 (write-string "bogus code object" stream))
1710 (write-string "code object" stream)
1712 (write-char #\space stream)
1713 (output-object (sb!c::debug-info-name dinfo) stream)))))))
1715 (defun output-lra (lra stream)
1716 (print-unreadable-object (lra stream :identity t)
1717 (write-string "return PC object" stream)))
1719 (defun output-fdefn (fdefn stream)
1720 (print-unreadable-object (fdefn stream)
1721 (write-string "FDEFINITION object for " stream)
1722 (output-object (fdefn-name fdefn) stream)))
1726 ;;; Output OBJECT as using PRINT-OBJECT if it's a
1727 ;;; FUNCALLABLE-STANDARD-CLASS, or return NIL otherwise.
1729 ;;; The definition here is a simple temporary placeholder. It will be
1730 ;;; overwritten by a smarter version (capable of calling generic
1731 ;;; PRINT-OBJECT when appropriate) when CLOS is installed.
1732 (defun printed-as-funcallable-standard-class (object stream)
1733 (declare (ignore object stream))
1736 (defun output-fun (object stream)
1737 (let* ((*print-length* 3) ; in case we have to..
1738 (*print-level* 3) ; ..print an interpreted function definition
1739 (name (%fun-name object))
1740 (proper-name-p (and (legal-fun-name-p name) (fboundp name)
1741 (eq (fdefinition name) object))))
1742 (print-unreadable-object (object stream :identity (not proper-name-p))
1743 (format stream "~:[FUNCTION~;CLOSURE~]~@[ ~S~]"
1747 ;;;; catch-all for unknown things
1749 (defun output-random (object stream)
1750 (print-unreadable-object (object stream :identity t)
1751 (let ((lowtag (lowtag-of object)))
1753 (#.sb!vm:other-pointer-lowtag
1754 (let ((widetag (widetag-of object)))
1756 (#.sb!vm:value-cell-header-widetag
1757 (write-string "value cell " stream)
1758 (output-object (value-cell-ref object) stream))
1760 (write-string "unknown pointer object, widetag=" stream)
1761 (let ((*print-base* 16) (*print-radix* t))
1762 (output-integer widetag stream))))))
1763 ((#.sb!vm:fun-pointer-lowtag
1764 #.sb!vm:instance-pointer-lowtag
1765 #.sb!vm:list-pointer-lowtag)
1766 (write-string "unknown pointer object, lowtag=" stream)
1767 (let ((*print-base* 16) (*print-radix* t))
1768 (output-integer lowtag stream)))
1770 (case (widetag-of object)
1771 (#.sb!vm:unbound-marker-widetag
1772 (write-string "unbound marker" stream))
1774 (write-string "unknown immediate object, lowtag=" stream)
1775 (let ((*print-base* 2) (*print-radix* t))
1776 (output-integer lowtag stream))
1777 (write-string ", widetag=" stream)
1778 (let ((*print-base* 16) (*print-radix* t))
1779 (output-integer (widetag-of object) stream)))))))))