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 (!defvar
*print-readably
* nil
17 "If true, all objects will be printed readably. If readable printing
18 is impossible, an error will be signalled. This overrides the value of
20 (!defvar
*print-escape
* t
21 "Should we print in a reasonably machine-readable way? (possibly
22 overridden by *PRINT-READABLY*)")
23 (!defvar
*print-pretty
* nil
; (set later when pretty-printer is initialized)
24 "Should pretty printing be used?")
25 (!defvar
*print-base
* 10.
26 "The output base for RATIONALs (including integers).")
27 (!defvar
*print-radix
* nil
28 "Should base be verified when printing RATIONALs?")
29 (!defvar
*print-level
* nil
30 "How many levels should be printed before abbreviating with \"#\"?")
31 (!defvar
*print-length
* nil
32 "How many elements at any level should be printed before abbreviating
34 (!defvar
*print-circle
* nil
35 "Should we use #n= and #n# notation to preserve uniqueness in general (and
36 circularity in particular) when printing?")
37 (!defvar
*print-case
* :upcase
38 "What case should the printer should use default?")
39 (!defvar
*print-array
* t
40 "Should the contents of arrays be printed?")
41 (!defvar
*print-gensym
* t
42 "Should #: prefixes be used when printing symbols with null SYMBOL-PACKAGE?")
43 (!defvar
*print-lines
* nil
44 "The maximum number of lines to print per object.")
45 (!defvar
*print-right-margin
* nil
46 "The position of the right margin in ems (for pretty-printing).")
47 (!defvar
*print-miser-width
* nil
48 "If the remaining space between the current column and the right margin
49 is less than this, then print using ``miser-style'' output. Miser
50 style conditional newlines are turned on, and all indentations are
51 turned off. If NIL, never use miser mode.")
52 (defvar *print-pprint-dispatch
*
53 (sb!pretty
::make-pprint-dispatch-table
) ; for type-correctness
54 "The pprint-dispatch-table that controls how to pretty-print objects.")
55 (!defvar
*suppress-print-errors
* nil
56 "Suppress printer errors when the condition is of the type designated by this
57 variable: an unreadable object representing the error is printed instead.")
59 ;; duplicate defglobal because this file is compiled before "reader"
60 (defglobal *standard-readtable
* nil
)
62 (defun %with-standard-io-syntax
(function)
63 (declare (type function function
))
64 (let ((*package
* (find-package "COMMON-LISP-USER"))
67 (*print-case
* :upcase
)
74 (*print-miser-width
* nil
)
75 (*print-pprint-dispatch
* sb
!pretty
::*standard-pprint-dispatch-table
*)
79 (*print-right-margin
* nil
)
81 (*read-default-float-format
* 'single-float
)
84 (*readtable
* *standard-readtable
*)
85 (*suppress-print-errors
* nil
))
88 ;;;; routines to print objects
90 (macrolet ((def (fn doc
&rest forms
)
94 ,@(if (eq fn
'write
) '(stream))
95 ((:escape
*print-escape
*) *print-escape
*)
96 ((:radix
*print-radix
*) *print-radix
*)
97 ((:base
*print-base
*) *print-base
*)
98 ((:circle
*print-circle
*) *print-circle
*)
99 ((:pretty
*print-pretty
*) *print-pretty
*)
100 ((:level
*print-level
*) *print-level
*)
101 ((:length
*print-length
*) *print-length
*)
102 ((:case
*print-case
*) *print-case
*)
103 ((:array
*print-array
*) *print-array
*)
104 ((:gensym
*print-gensym
*) *print-gensym
*)
105 ((:readably
*print-readably
*) *print-readably
*)
106 ((:right-margin
*print-right-margin
*)
107 *print-right-margin
*)
108 ((:miser-width
*print-miser-width
*)
110 ((:lines
*print-lines
*) *print-lines
*)
111 ((:pprint-dispatch
*print-pprint-dispatch
*)
112 *print-pprint-dispatch
*)
113 ((:suppress-errors
*suppress-print-errors
*)
114 *suppress-print-errors
*))
116 (declare (explicit-check))
119 "Output OBJECT to the specified stream, defaulting to *STANDARD-OUTPUT*."
120 (output-object object
(out-stream-from-designator stream
))
123 "Return the printed representation of OBJECT as a string."
124 (stringify-object object
)))
126 ;;; Same as a call to (WRITE OBJECT :STREAM STREAM), but returning OBJECT.
127 (defun %write
(object stream
)
128 (declare (explicit-check))
129 (output-object object
(out-stream-from-designator stream
))
132 (defun prin1 (object &optional stream
)
133 "Output a mostly READable printed representation of OBJECT on the specified
135 (declare (explicit-check))
136 (let ((*print-escape
* t
))
137 (output-object object
(out-stream-from-designator stream
)))
140 (defun princ (object &optional stream
)
141 "Output an aesthetic but not necessarily READable printed representation
142 of OBJECT on the specified STREAM."
143 (declare (explicit-check))
144 (let ((*print-escape
* nil
)
145 (*print-readably
* nil
))
146 (output-object object
(out-stream-from-designator stream
)))
149 (defun print (object &optional stream
)
150 "Output a newline, the mostly READable printed representation of OBJECT, and
151 space to the specified STREAM."
152 (declare (explicit-check))
153 (let ((stream (out-stream-from-designator stream
)))
155 (prin1 object stream
)
156 (write-char #\space stream
)
159 (defun pprint (object &optional stream
)
160 "Prettily output OBJECT preceded by a newline."
161 (declare (explicit-check))
162 (let ((*print-pretty
* t
)
164 (stream (out-stream-from-designator stream
)))
166 (output-object object stream
))
169 (defun prin1-to-string (object)
170 "Return the printed representation of OBJECT as a string with
172 (let ((*print-escape
* t
))
173 (stringify-object object
)))
175 (defun princ-to-string (object)
176 "Return the printed representation of OBJECT as a string with
178 (let ((*print-escape
* nil
)
179 (*print-readably
* nil
))
180 (stringify-object object
)))
182 ;;; This produces the printed representation of an object as a string.
183 ;;; The few ...-TO-STRING functions above call this.
184 (defun stringify-object (object)
187 (let ((buffer-size (approx-chars-in-repr object
)))
188 (let* ((string (make-string buffer-size
:element-type
'base-char
))
189 (stream (%make-finite-base-string-output-stream string
)))
190 (declare (inline %make-finite-base-string-output-stream
))
191 (declare (truly-dynamic-extent stream
))
192 (output-integer object stream
)
193 (%shrink-vector string
194 (finite-base-string-output-stream-pointer stream
)))))
195 ;; Could do something for other numeric types, symbols, ...
197 (with-simple-output-to-string (stream)
198 (output-object object stream
)))))
200 ;;; Estimate the number of chars in the printed representation of OBJECT.
201 ;;; The answer must be an overestimate or exact; never an underestimate.
202 (defun approx-chars-in-repr (object)
203 (declare (integer object
))
204 ;; Round *PRINT-BASE* down to the nearest lower power-of-2, call that N,
205 ;; and "guess" that the one character can represent N bits.
206 ;; This is exact for bases which are exactly a power-of-2, or an overestimate
207 ;; otherwise, as mandated by the finite output stream.
209 (aref #.
(!coerce-to-specialized
210 ;; base 2 or base 3 = 1 bit per character
211 ;; base 4 .. base 7 = 2 bits per character
212 ;; base 8 .. base 15 = 3 bits per character, etc
213 #(1 1 2 2 2 2 3 3 3 3 3 3 3 3
214 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5)
216 (- *print-base
* 2))))
217 (+ (if (minusp object
) 1 0) ; leading sign
218 (if *print-radix
* 4 0) ; #rNN or trailing decimal
219 (ceiling (if (fixnump object
)
220 sb
!vm
:n-positive-fixnum-bits
221 (* (%bignum-length object
) sb
!bignum
::digit-size
))
224 ;;;; support for the PRINT-UNREADABLE-OBJECT macro
226 (defun print-not-readable-error (object stream
)
228 (error 'print-not-readable
:object object
)
230 :report
"Print unreadably."
231 (let ((*print-readably
* nil
))
232 (output-object object stream
)
235 :report
"Supply an object to be printed instead."
238 (read-evaluated-form "~@<Enter an object (evaluated): ~@:>"))
239 (output-object o stream
)
242 ;;; guts of PRINT-UNREADABLE-OBJECT
243 (defun %print-unreadable-object
(object stream type identity
&optional body
)
244 (declare (type (or null function
) body
))
246 (print-not-readable-error object stream
)
247 (flet ((print-description ()
249 (write (type-of object
) :stream stream
:circle nil
250 :level nil
:length nil
)
251 ;; Do NOT insert a pprint-newline here.
252 ;; See ba34717602d80e5fd74d10e61f4729fb0d019a0c
253 (write-char #\space stream
))
257 (when (or body
(not type
))
258 (write-char #\space stream
))
260 (write-char #\
{ stream
)
261 (write (get-lisp-obj-address object
) :stream stream
263 (write-char #\
} stream
))))
264 (cond ((print-pretty-on-stream-p stream
)
265 ;; Since we're printing prettily on STREAM, format the
266 ;; object within a logical block. PPRINT-LOGICAL-BLOCK does
267 ;; not rebind the stream when it is already a pretty stream,
268 ;; so output from the body will go to the same stream.
269 (pprint-logical-block (stream nil
:prefix
"#<" :suffix
">")
270 (print-description)))
272 (write-string "#<" stream
)
274 (write-char #\
> stream
)))))
277 ;;;; OUTPUT-OBJECT -- the main entry point
279 ;;; Objects whose print representation identifies them EQLly don't
280 ;;; need to be checked for circularity.
281 (defun uniquely-identified-by-print-p (x)
285 (symbol-package x
))))
287 (defvar *in-print-error
* nil
)
289 ;;; Output OBJECT to STREAM observing all printer control variables.
290 (defun output-object (object stream
)
291 ;; FIXME: this function is declared EXPLICIT-CHECK, so it allows STREAM
292 ;; to be T or NIL (a stream-designator), which is not really right
293 ;; if eventually the call will be to a PRINT-OBJECT method,
294 ;; since the generic function should always receive a stream.
295 (declare (explicit-check))
296 (labels ((print-it (stream)
297 (multiple-value-bind (fun pretty
)
298 (and *print-pretty
* (pprint-dispatch object
))
300 (sb!pretty
::with-pretty-stream
(stream)
301 (funcall fun stream object
))
302 (output-ugly-object stream object
))))
304 (if *suppress-print-errors
*
305 (handler-bind ((condition
306 (lambda (condition) nil
307 (when (typep condition
*suppress-print-errors
*)
308 (cond (*in-print-error
*
309 (write-string "(error printing " stream
)
310 (write-string *in-print-error
* stream
)
311 (write-string ")" stream
))
313 ;; Give outer handlers a chance.
315 (continue "Suppress the error.")
317 (let ((*print-readably
* nil
)
320 "#<error printing a " stream
)
321 (let ((*in-print-error
* "type"))
322 (output-object (type-of object
) stream
))
323 (write-string ": " stream
)
324 (let ((*in-print-error
* "condition"))
325 (output-object condition stream
))
326 (write-string ">" stream
))))
327 (return-from handle-it object
)))))
331 (multiple-value-bind (marker initiate
)
332 (check-for-circularity object t
)
333 (if (eq initiate
:initiate
)
334 (let ((*circularity-hash-table
*
335 (make-hash-table :test
'eq
)))
336 (check-it (make-broadcast-stream))
337 (let ((*circularity-counter
* 0))
341 (when (handle-circularity marker stream
)
343 (handle-it stream
))))))
344 (cond (;; Maybe we don't need to bother with circularity detection.
345 (or (not *print-circle
*)
346 (uniquely-identified-by-print-p object
))
348 (;; If we have already started circularity detection, this
349 ;; object might be a shared reference. If we have not, then
350 ;; if it is a compound object it might contain a circular
351 ;; reference to itself or multiple shared references.
352 (or *circularity-hash-table
*
353 (compound-object-p object
))
356 (handle-it stream
)))))
358 ;;; Output OBJECT to STREAM observing all printer control variables
359 ;;; except for *PRINT-PRETTY*. Note: if *PRINT-PRETTY* is non-NIL,
360 ;;; then the pretty printer will be used for any components of OBJECT,
361 ;;; just not for OBJECT itself.
362 (defun output-ugly-object (stream object
)
363 (when (%instancep object
)
364 (let* ((layout (layout-of object
))
365 (classoid (layout-classoid layout
)))
366 ;; If an instance has no layout, it has no PRINT-OBJECT method.
367 ;; Additionally, if the object is an obsolete CONDITION, don't crash.
368 ;; (There is no update-instance protocol for conditions)
369 (when (or (sb!kernel
::undefined-classoid-p classoid
)
370 (and (layout-invalid layout
) (condition-classoid-p classoid
)))
371 ;; not only is this unreadable, it's unprintable too.
372 (return-from output-ugly-object
373 (print-unreadable-object (object stream
:identity t
)
374 (format stream
"UNPRINTABLE instance of ~W" classoid
))))))
375 (print-object object stream
))
379 (defmethod print-object ((object symbol
) stream
)
380 (if (or *print-escape
* *print-readably
*)
381 ;; Write so that reading back works
382 (output-symbol object
(symbol-package object
) stream
)
383 ;; Write only the characters of the name, never the package
384 (let ((rt *readtable
*))
385 (funcall (truly-the function
386 (choose-symbol-out-fun *print-case
* (%readtable-case rt
)))
387 (symbol-name object
) stream rt
))))
389 (defun output-symbol (symbol package stream
)
390 (let* ((readably *print-readably
*)
391 (readtable (if readably
*standard-readtable
* *readtable
*))
392 (out-fun (choose-symbol-out-fun *print-case
* (%readtable-case readtable
))))
393 (flet ((output-token (name)
394 (declare (type simple-string name
))
395 (cond ((or (and (readtable-normalization readtable
)
396 (not (sb!unicode
:normalized-p name
:nfkc
)))
397 (symbol-quotep name readtable
))
398 ;; Output NAME surrounded with |'s,
399 ;; and with any embedded |'s or \'s escaped.
400 (write-char #\| stream
)
401 (dotimes (index (length name
))
402 (let ((char (char name index
)))
403 ;; Hmm. Should these depend on what characters
404 ;; are actually escapes in the readtable ?
405 ;; (See similar remark at DEFUN QUOTE-STRING)
406 (when (or (char= char
#\\) (char= char
#\|
))
407 (write-char #\\ stream
))
408 (write-char char stream
)))
409 (write-char #\| stream
))
411 (funcall (truly-the function out-fun
) name stream readtable
)))))
412 (let ((name (symbol-name symbol
))
413 (current (sane-package)))
415 ;; The ANSI spec "22.1.3.3.1 Package Prefixes for Symbols"
416 ;; requires that keywords be printed with preceding colons
417 ;; always, regardless of the value of *PACKAGE*.
418 ((eq package
*keyword-package
*)
419 (write-char #\
: stream
))
420 ;; Otherwise, if the symbol's home package is the current
421 ;; one, then a prefix is never necessary.
422 ((eq package current
))
423 ;; Uninterned symbols print with a leading #:.
425 (when (or *print-gensym
* readably
)
426 (write-string "#:" stream
)))
428 (multiple-value-bind (found accessible
) (find-symbol name current
)
429 ;; If we can find the symbol by looking it up, it need not
430 ;; be qualified. This can happen if the symbol has been
431 ;; inherited from a package other than its home package.
433 ;; To preserve print-read consistency, use the local nickname if
435 (unless (and accessible
(eq found symbol
))
436 (let ((prefix (or (car (rassoc package
(package-%local-nicknames current
)))
437 (package-name package
))))
438 (output-token prefix
))
439 (if (nth-value 1 (find-external-symbol name package
))
440 (write-char #\
: stream
)
441 (write-string "::" stream
))))))
442 (output-token name
)))))
444 ;;;; escaping symbols
446 ;;; When we print symbols we have to figure out if they need to be
447 ;;; printed with escape characters. This isn't a whole lot easier than
448 ;;; reading symbols in the first place.
450 ;;; For each character, the value of the corresponding element is a
451 ;;; fixnum with bits set corresponding to attributes that the
452 ;;; character has. At characters have at least one bit set, so we can
453 ;;; search for any character with a positive test.
454 (defvar *character-attributes
*
455 (make-array 160 ; FIXME
456 :element-type
'(unsigned-byte 16)
458 (declaim (type (simple-array (unsigned-byte 16) (#.160)) ; FIXME
459 *character-attributes
*))
461 ;;; constants which are a bit-mask for each interesting character attribute
462 (defconstant other-attribute
(ash 1 0)) ; Anything else legal.
463 (defconstant number-attribute
(ash 1 1)) ; A numeric digit.
464 (defconstant uppercase-attribute
(ash 1 2)) ; An uppercase letter.
465 (defconstant lowercase-attribute
(ash 1 3)) ; A lowercase letter.
466 (defconstant sign-attribute
(ash 1 4)) ; +-
467 (defconstant extension-attribute
(ash 1 5)) ; ^_
468 (defconstant dot-attribute
(ash 1 6)) ; .
469 (defconstant slash-attribute
(ash 1 7)) ; /
470 (defconstant funny-attribute
(ash 1 8)) ; Anything illegal.
472 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
474 ;;; LETTER-ATTRIBUTE is a local of SYMBOL-QUOTEP. It matches letters
475 ;;; that don't need to be escaped (according to READTABLE-CASE.)
476 (defparameter *attribute-names
*
477 `((number . number-attribute
) (lowercase . lowercase-attribute
)
478 (uppercase . uppercase-attribute
) (letter . letter-attribute
)
479 (sign . sign-attribute
) (extension . extension-attribute
)
480 (dot . dot-attribute
) (slash . slash-attribute
)
481 (other . other-attribute
) (funny . funny-attribute
)))
485 ;;; For each character, the value of the corresponding element is the
486 ;;; lowest base in which that character is a digit.
487 (declaim (type (simple-array (unsigned-byte 8) (128)) ; FIXME: range?
489 (defvar *digit-bases
*
490 (make-array 128 ; FIXME
491 :element-type
'(unsigned-byte 8)))
493 (defun !printer-cold-init
()
494 ;; The dispatch table will be changed later, so this doesn't really matter
495 ;; except if a full call to WRITE wants to read the current binding.
496 (setq *print-pprint-dispatch
* (sb!pretty
::make-pprint-dispatch-table
))
497 (setq *digit-bases
* (make-array 128 ; FIXME
498 :element-type
'(unsigned-byte 8)
500 *character-attributes
* (make-array 160 ; FIXME
501 :element-type
'(unsigned-byte 16)
504 (let ((char (digit-char i
36)))
505 (setf (aref *digit-bases
* (char-code char
)) i
)))
507 (flet ((set-bit (char bit
)
508 (let ((code (char-code char
)))
509 (setf (aref *character-attributes
* code
)
510 (logior bit
(aref *character-attributes
* code
))))))
512 (dolist (char '(#\
! #\
@ #\$
#\%
#\
& #\
* #\
= #\~
#\
[ #\
] #\
{ #\
}
514 (set-bit char other-attribute
))
517 (set-bit (digit-char i
) number-attribute
))
519 (do ((code (char-code #\A
) (1+ code
))
520 (end (char-code #\Z
)))
522 (declare (fixnum code end
))
523 (set-bit (code-char code
) uppercase-attribute
)
524 (set-bit (char-downcase (code-char code
)) lowercase-attribute
))
526 (set-bit #\- sign-attribute
)
527 (set-bit #\
+ sign-attribute
)
528 (set-bit #\^ extension-attribute
)
529 (set-bit #\_ extension-attribute
)
530 (set-bit #\. dot-attribute
)
531 (set-bit #\
/ slash-attribute
)
533 ;; Mark anything not explicitly allowed as funny.
534 (dotimes (i 160) ; FIXME
535 (when (zerop (aref *character-attributes
* i
))
536 (setf (aref *character-attributes
* i
) funny-attribute
))))
537 ) ; end !COLD-PRINT-INIT
539 ;;; A FSM-like thingie that determines whether a symbol is a potential
540 ;;; number or has evil characters in it.
541 (defun symbol-quotep (name readtable
)
542 (declare (simple-string name
))
543 (macrolet ((advance (tag &optional
(at-end t
))
546 ,(if at-end
'(go TEST-SIGN
) '(return nil
)))
547 (setq current
(schar name index
)
548 code
(char-code current
)
550 ((< code
160) (aref attributes code
))
551 ((upper-case-p current
) uppercase-attribute
)
552 ((lower-case-p current
) lowercase-attribute
)
553 (t other-attribute
)))
556 (test (&rest attributes
)
568 `(and (< code
128) ; FIXME
569 (< (the fixnum
(aref bases code
)) base
))))
571 (prog ((len (length name
))
572 (attributes *character-attributes
*)
573 (bases *digit-bases
*)
576 (case (%readtable-case readtable
)
577 (#.
+readtable-upcase
+ uppercase-attribute
)
578 (#.
+readtable-downcase
+ lowercase-attribute
)
579 (t (logior lowercase-attribute uppercase-attribute
))))
584 (declare (fixnum len base index bits code
))
587 TEST-SIGN
; At end, see whether it is a sign...
588 (return (not (test sign
)))
590 OTHER
; not potential number, see whether funny chars...
591 (let ((mask (logxor (logior lowercase-attribute uppercase-attribute
594 (do ((i (1- index
) (1+ i
)))
595 ((= i len
) (return-from symbol-quotep nil
))
596 (unless (zerop (logand (let* ((char (schar name i
))
597 (code (char-code char
)))
599 ((< code
160) (aref attributes code
))
600 ((upper-case-p char
) uppercase-attribute
)
601 ((lower-case-p char
) lowercase-attribute
)
602 (t other-attribute
)))
604 (return-from symbol-quotep t
))))
609 (advance LAST-DIGIT-ALPHA
)
611 (when (test letter number other slash
) (advance OTHER nil
))
612 (when (char= current
#\.
) (advance DOT-FOUND
))
613 (when (test sign extension
) (advance START-STUFF nil
))
616 DOT-FOUND
; leading dots...
617 (when (test letter
) (advance START-DOT-MARKER nil
))
618 (when (digitp) (advance DOT-DIGIT
))
619 (when (test number other
) (advance OTHER nil
))
620 (when (test extension slash sign
) (advance START-DOT-STUFF nil
))
621 (when (char= current
#\.
) (advance DOT-FOUND
))
624 START-STUFF
; leading stuff before any dot or digit
627 (advance LAST-DIGIT-ALPHA
)
629 (when (test number other
) (advance OTHER nil
))
630 (when (test letter
) (advance START-MARKER nil
))
631 (when (char= current
#\.
) (advance START-DOT-STUFF nil
))
632 (when (test sign extension slash
) (advance START-STUFF nil
))
635 START-MARKER
; number marker in leading stuff...
636 (when (test letter
) (advance OTHER nil
))
639 START-DOT-STUFF
; leading stuff containing dot without digit...
640 (when (test letter
) (advance START-DOT-STUFF nil
))
641 (when (digitp) (advance DOT-DIGIT
))
642 (when (test sign extension dot slash
) (advance START-DOT-STUFF nil
))
643 (when (test number other
) (advance OTHER nil
))
646 START-DOT-MARKER
; number marker in leading stuff with dot..
647 ;; leading stuff containing dot without digit followed by letter...
648 (when (test letter
) (advance OTHER nil
))
651 DOT-DIGIT
; in a thing with dots...
652 (when (test letter
) (advance DOT-MARKER
))
653 (when (digitp) (advance DOT-DIGIT
))
654 (when (test number other
) (advance OTHER nil
))
655 (when (test sign extension dot slash
) (advance DOT-DIGIT
))
658 DOT-MARKER
; number marker in number with dot...
659 (when (test letter
) (advance OTHER nil
))
662 LAST-DIGIT-ALPHA
; previous char is a letter digit...
663 (when (or (digitp) (test sign slash
))
664 (advance ALPHA-DIGIT
))
665 (when (test letter number other dot
) (advance OTHER nil
))
668 ALPHA-DIGIT
; seen a digit which is a letter...
669 (when (or (digitp) (test sign slash
))
671 (advance LAST-DIGIT-ALPHA
)
672 (advance ALPHA-DIGIT
)))
673 (when (test letter
) (advance ALPHA-MARKER
))
674 (when (test number other dot
) (advance OTHER nil
))
677 ALPHA-MARKER
; number marker in number with alpha digit...
678 (when (test letter
) (advance OTHER nil
))
681 DIGIT
; seen only ordinary (non-alphabetic) numeric digits...
684 (advance ALPHA-DIGIT
)
686 (when (test number other
) (advance OTHER nil
))
687 (when (test letter
) (advance MARKER
))
688 (when (test extension slash sign
) (advance DIGIT
))
689 (when (char= current
#\.
) (advance DOT-DIGIT
))
692 MARKER
; number marker in a numeric number...
693 ;; ("What," you may ask, "is a 'number marker'?" It's something
694 ;; that a conforming implementation might use in number syntax.
695 ;; See ANSI 2.3.1.1 "Potential Numbers as Tokens".)
696 (when (test letter
) (advance OTHER nil
))
699 ;;;; case hackery: One of these functions is chosen to output symbol
700 ;;;; names according to the values of *PRINT-CASE* and READTABLE-CASE.
703 ;;; READTABLE-CASE *PRINT-CASE*
705 ;;; :DOWNCASE :DOWNCASE
707 (defun output-preserve-symbol (pname stream readtable
)
708 (declare (ignore readtable
))
709 (write-string pname stream
))
712 ;;; READTABLE-CASE *PRINT-CASE*
713 ;;; :UPCASE :DOWNCASE
714 (defun output-lowercase-symbol (pname stream readtable
)
715 (declare (simple-string pname
) (ignore readtable
))
716 (dotimes (index (length pname
))
717 (let ((char (schar pname index
)))
718 (write-char (char-downcase char
) stream
))))
721 ;;; READTABLE-CASE *PRINT-CASE*
722 ;;; :DOWNCASE :UPCASE
723 (defun output-uppercase-symbol (pname stream readtable
)
724 (declare (simple-string pname
) (ignore readtable
))
725 (dotimes (index (length pname
))
726 (let ((char (schar pname index
)))
727 (write-char (char-upcase char
) stream
))))
730 ;;; READTABLE-CASE *PRINT-CASE*
731 ;;; :UPCASE :CAPITALIZE
732 ;;; :DOWNCASE :CAPITALIZE
733 (defun output-capitalize-symbol (pname stream readtable
)
734 (declare (simple-string pname
))
735 (let ((prev-not-alphanum t
)
736 (up (eql (%readtable-case readtable
) +readtable-upcase
+)))
737 (dotimes (i (length pname
))
738 (let ((char (char pname i
)))
740 (if (or prev-not-alphanum
(lower-case-p char
))
742 (char-downcase char
))
743 (if prev-not-alphanum
747 (setq prev-not-alphanum
(not (alphanumericp char
)))))))
750 ;;; READTABLE-CASE *PRINT-CASE*
752 (defun output-invert-symbol (pname stream readtable
)
753 (declare (simple-string pname
) (ignore readtable
))
756 (dotimes (i (length pname
))
757 (let ((ch (schar pname i
)))
758 (when (both-case-p ch
)
759 (if (upper-case-p ch
)
761 (setq all-upper nil
)))))
762 (cond (all-upper (output-lowercase-symbol pname stream nil
))
763 (all-lower (output-uppercase-symbol pname stream nil
))
765 (write-string pname stream
)))))
767 (defun choose-symbol-out-fun (print-case readtable-case
)
769 ((compute-fun-vector (&aux
(vector (make-array 12)))
770 ;; Pack a 2D array of functions into a simple-vector.
771 ;; Major axis is *PRINT-CASE*, minor axis is %READTABLE-CASE.
772 (dotimes (readtable-case-index 4)
773 (dotimes (print-case-index 3)
774 (let ((readtable-case
775 (elt '(:upcase
:downcase
:preserve
:invert
) readtable-case-index
))
777 (elt '(:upcase
:downcase
:capitalize
) print-case-index
)))
778 (setf (aref vector
(logior (ash print-case-index
2)
779 readtable-case-index
))
783 (:upcase
'output-preserve-symbol
)
784 (:downcase
'output-lowercase-symbol
)
785 (:capitalize
'output-capitalize-symbol
)))
788 (:upcase
'output-uppercase-symbol
)
789 (:downcase
'output-preserve-symbol
)
790 (:capitalize
'output-capitalize-symbol
)))
791 (:preserve
'output-preserve-symbol
)
792 (:invert
'output-invert-symbol
))))))
793 `(load-time-value (vector ,@(map 'list
(lambda (x) `(function ,x
)) vector
))
795 (aref (compute-fun-vector)
796 (logior (case print-case
(:upcase
0) (:downcase
4) (t 8))
797 (truly-the (mod 4) readtable-case
)))))
799 ;;;; recursive objects
801 (defmethod print-object ((list cons
) stream
)
802 (descend-into (stream)
803 (write-char #\
( stream
)
807 (punt-print-if-too-long length stream
)
808 (output-object (pop list
) stream
)
811 (when (or (atom list
)
812 (check-for-circularity list
))
813 (write-string " . " stream
)
814 (output-object list stream
)
816 (write-char #\space stream
)
818 (write-char #\
) stream
)))
820 (defun output-unreadable-vector-readably (vector stream
)
821 (declare (vector vector
))
822 (write-string "#." stream
)
823 (write `(coerce ,(coerce vector
'(vector t
))
824 '(simple-array ,(array-element-type vector
) (*)))
827 (defmethod print-object ((vector vector
) stream
)
828 (let ((readably *print-readably
*))
829 (cond ((stringp vector
)
831 (and readably
(not (typep vector
'(vector character
))))))
832 (cond ((and coerce-p
(not *read-eval
*))
833 (print-not-readable-error vector stream
))
834 ((or *print-escape
* readably
)
836 ;; OUTPUT-UNREADABLE-VECTOR-READABLY would output each char
837 ;; in #\c syntax. In addition to wasting time coercing to a
838 ;; general vector, it's not nice looking.
839 (write-string "#.(" stream
)
840 (write 'coerce
:stream stream
) ; package-qualify / casify as needed
841 (write-char #\Space stream
))
842 (write-char #\" stream
)
843 (quote-string vector stream
)
844 (write-char #\" stream
)
846 (write-char #\Space stream
)
847 (write (cond #!+sb-unicode
848 ((base-string-p vector
)
851 `'(vector ,(array-element-type vector
)))) :stream stream
)
852 (write-char #\
) stream
)))
854 (write-string vector stream
)))))
855 ((not (or *print-array
* readably
))
856 (output-terse-array vector stream
))
857 ((bit-vector-p vector
)
858 (write-string "#*" stream
)
859 (dovector (bit vector
)
860 ;; (Don't use OUTPUT-OBJECT here, since this code
861 ;; has to work for all possible *PRINT-BASE* values.)
862 (write-char (if (zerop bit
) #\
0 #\
1) stream
)))
863 ((or (not readably
) (array-readably-printable-p vector
))
864 (descend-into (stream)
865 (write-string "#(" stream
)
866 (dotimes (i (length vector
))
868 (write-char #\space stream
))
869 (punt-print-if-too-long i stream
)
870 (output-object (aref vector i
) stream
))
871 (write-string ")" stream
)))
873 (output-unreadable-vector-readably vector stream
))
875 (print-not-readable-error vector stream
)))))
877 ;;; This function outputs a string quoting characters sufficiently
878 ;;; so that someone can read it in again. Basically, put a slash in
879 ;;; front of an character satisfying NEEDS-SLASH-P.
880 (defun quote-string (string stream
)
881 (macrolet ((needs-slash-p (char)
882 ;; KLUDGE: We probably should look at the readtable, but just do
883 ;; this for now. [noted by anonymous long ago] -- WHN 19991130
884 `(or (char= ,char
#\\)
886 (with-array-data ((data string
) (start) (end)
887 :check-fill-pointer t
)
888 (do ((index start
(1+ index
)))
890 (let ((char (schar data index
)))
891 (when (needs-slash-p char
) (write-char #\\ stream
))
892 (write-char char stream
))))))
894 (defun array-readably-printable-p (array)
895 (and (eq (array-element-type array
) t
)
896 (let ((zero (position 0 (array-dimensions array
)))
897 (number (position 0 (array-dimensions array
)
898 :test
(complement #'eql
)
900 (or (null zero
) (null number
) (> zero number
)))))
902 ;;; Output the printed representation of any array in either the #< or #A
904 (defmethod print-object ((array array
) stream
)
905 (if (or *print-array
* *print-readably
*)
906 (output-array-guts array stream
)
907 (output-terse-array array stream
)))
909 ;;; Output the abbreviated #< form of an array.
910 (defun output-terse-array (array stream
)
911 (let ((*print-level
* nil
)
912 (*print-length
* nil
))
913 (print-unreadable-object (array stream
:type t
:identity t
))))
915 ;;; Convert an array into a list that can be used with MAKE-ARRAY's
916 ;;; :INITIAL-CONTENTS keyword argument.
917 (defun listify-array (array)
918 (with-array-data ((data array
) (start) (end))
919 (declare (ignore end
))
920 (labels ((listify (dimensions index
)
921 (if (null dimensions
)
923 (let* ((dimension (car dimensions
))
924 (dimensions (cdr dimensions
))
925 (count (reduce #'* dimensions
)))
926 (loop for i below dimension
927 collect
(listify dimensions index
)
928 do
(incf index count
))))))
929 (listify (array-dimensions array
) start
))))
931 (defun output-unreadable-array-readably (array stream
)
932 (write-string "#." stream
)
933 (write `(make-array ',(array-dimensions array
)
934 :element-type
',(array-element-type array
)
935 :initial-contents
',(listify-array array
))
938 ;;; Output the readable #A form of an array.
939 (defun output-array-guts (array stream
)
940 (cond ((or (not *print-readably
*)
941 (array-readably-printable-p array
))
942 (write-char #\
# stream
)
943 (let ((*print-base
* 10)
945 (output-integer (array-rank array
) stream
))
946 (write-char #\A stream
)
947 (with-array-data ((data array
) (start) (end))
948 (declare (ignore end
))
949 (sub-output-array-guts data
(array-dimensions array
) stream start
)))
951 (output-unreadable-array-readably array stream
))
953 (print-not-readable-error array stream
))))
955 (defun sub-output-array-guts (array dimensions stream index
)
956 (declare (type (simple-array * (*)) array
) (fixnum index
))
957 (cond ((null dimensions
)
958 (output-object (aref array index
) stream
))
960 (descend-into (stream)
961 (write-char #\
( stream
)
962 (let* ((dimension (car dimensions
))
963 (dimensions (cdr dimensions
))
964 (count (reduce #'* dimensions
)))
965 (dotimes (i dimension
)
967 (write-char #\space stream
))
968 (punt-print-if-too-long i stream
)
969 (sub-output-array-guts array dimensions stream index
)
971 (write-char #\
) stream
)))))
974 ;;;; integer, ratio, and complex printing (i.e. everything but floats)
976 (defun %output-radix
(base stream
)
977 (write-char #\
# stream
)
978 (write-char (case base
982 (t (%output-reasonable-integer-in-base base
10 stream
)
986 (defun %output-reasonable-integer-in-base
(n base stream
)
987 (multiple-value-bind (q r
)
989 ;; Recurse until you have all the digits pushed on
992 (%output-reasonable-integer-in-base q base stream
))
993 ;; Then as each recursive call unwinds, turn the
994 ;; digit (in remainder) into a character and output
997 (schar "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" r
)
1000 ;;; *POWER-CACHE* is an alist mapping bases to power-vectors. It is
1001 ;;; filled and probed by POWERS-FOR-BASE. SCRUB-POWER-CACHE is called
1002 ;;; always prior a GC to drop overly large bignums from the cache.
1004 ;;; It doesn't need a lock, but if you work on SCRUB-POWER-CACHE or
1005 ;;; POWERS-FOR-BASE, see that you don't break the assumptions!
1006 (defglobal *power-cache
* (make-array 37 :initial-element nil
))
1007 (declaim (type (simple-vector 37) *power-cache
*))
1009 (defconstant +power-cache-integer-length-limit
+ 2048)
1011 (defun scrub-power-cache (&aux
(cache *power-cache
*))
1012 (dotimes (i (length cache
))
1013 (let ((powers (aref cache i
)))
1015 (let ((too-big (position-if
1017 (>= (integer-length x
)
1018 +power-cache-integer-length-limit
+))
1019 (the simple-vector powers
))))
1021 (setf (aref cache i
) (subseq powers
0 too-big
))))))))
1023 ;;; Compute (and cache) a power vector for a BASE and LIMIT:
1024 ;;; the vector holds integers for which
1025 ;;; (aref powers k) == (expt base (expt 2 k))
1027 (defun powers-for-base (base limit
)
1028 (flet ((compute-powers (from)
1030 (do ((p from
(* p p
)))
1032 ;; We don't actually need this, but we also
1033 ;; prefer not to cons it up a second time...
1036 (nreverse powers
))))
1037 (let* ((cache *power-cache
*)
1038 (powers (aref cache base
)))
1039 (setf (aref cache base
)
1040 (concatenate 'vector powers
1043 (let* ((len (length powers
))
1044 (max (svref powers
(1- len
))))
1046 (return-from powers-for-base powers
)
1050 ;; Algorithm by Harald Hanche-Olsen, sbcl-devel 2005-02-05
1051 (defun %output-huge-integer-in-base
(n base stream
)
1052 (declare (type bignum n
) (type fixnum base
))
1053 ;; POWER is a vector for which the following holds:
1054 ;; (aref power k) == (expt base (expt 2 k))
1055 (let* ((power (powers-for-base base n
))
1056 (k-start (or (position-if (lambda (x) (> x n
)) power
)
1057 (bug "power-vector too short"))))
1058 (labels ((bisect (n k exactp
)
1059 (declare (fixnum k
))
1060 ;; N is the number to bisect
1061 ;; K on initial entry BASE^(2^K) > N
1062 ;; EXACTP is true if 2^K is the exact number of digits
1065 (loop repeat
(ash 1 k
) do
(write-char #\
0 stream
))))
1068 (schar "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" n
)
1072 (multiple-value-bind (q r
) (truncate n
(aref power k
))
1073 ;; EXACTP is NIL only at the head of the
1074 ;; initial number, as we don't know the number
1075 ;; of digits there, but we do know that it
1076 ;; doesn't get any leading zeros.
1078 (bisect r k
(or exactp
(plusp q
))))))))
1079 (bisect n k-start nil
))))
1081 (defun %output-integer-in-base
(integer base stream
)
1082 (when (minusp integer
)
1083 (write-char #\- stream
)
1084 (setf integer
(- integer
)))
1085 ;; The ideal cutoff point between these two algorithms is almost
1086 ;; certainly quite platform dependent: this gives 87 for 32 bit
1087 ;; SBCL, which is about right at least for x86/Darwin.
1088 (if (or (fixnump integer
)
1089 (< (integer-length integer
) (* 3 sb
!vm
:n-positive-fixnum-bits
)))
1090 (%output-reasonable-integer-in-base integer base stream
)
1091 (%output-huge-integer-in-base integer base stream
)))
1093 ;;; This gets both a method and a specifically named function
1094 ;;; since the latter is called from a few places.
1095 (defmethod print-object ((object integer
) stream
) (output-integer object stream
))
1096 (defun output-integer (integer stream
)
1097 (let ((base *print-base
*))
1098 (cond (*print-radix
*
1099 (unless (= base
10) (%output-radix base stream
))
1100 (%output-integer-in-base integer base stream
)
1101 (when (= base
10) (write-char #\. stream
)))
1103 (%output-integer-in-base integer base stream
)))))
1105 (defmethod print-object ((ratio ratio
) stream
)
1106 (let ((base *print-base
*))
1108 (%output-radix base stream
))
1109 (%output-integer-in-base
(numerator ratio
) base stream
)
1110 (write-char #\
/ stream
)
1111 (%output-integer-in-base
(denominator ratio
) base stream
)))
1113 (defmethod print-object ((complex complex
) stream
)
1114 (write-string "#C(" stream
)
1115 (output-object (realpart complex
) stream
)
1116 (write-char #\space stream
)
1117 (output-object (imagpart complex
) stream
)
1118 (write-char #\
) stream
))
1122 ;;; FLONUM-TO-STRING (and its subsidiary function FLOAT-STRING) does
1123 ;;; most of the work for all printing of floating point numbers in
1124 ;;; FORMAT. It converts a floating point number to a string in a free
1125 ;;; or fixed format with no exponent. The interpretation of the
1126 ;;; arguments is as follows:
1128 ;;; X - The floating point number to convert, which must not be
1130 ;;; WIDTH - The preferred field width, used to determine the number
1131 ;;; of fraction digits to produce if the FDIGITS parameter
1132 ;;; is unspecified or NIL. If the non-fraction digits and the
1133 ;;; decimal point alone exceed this width, no fraction digits
1134 ;;; will be produced unless a non-NIL value of FDIGITS has been
1135 ;;; specified. Field overflow is not considerd an error at this
1137 ;;; FDIGITS - The number of fractional digits to produce. Insignificant
1138 ;;; trailing zeroes may be introduced as needed. May be
1139 ;;; unspecified or NIL, in which case as many digits as possible
1140 ;;; are generated, subject to the constraint that there are no
1141 ;;; trailing zeroes.
1142 ;;; SCALE - If this parameter is specified or non-NIL, then the number
1143 ;;; printed is (* x (expt 10 scale)). This scaling is exact,
1144 ;;; and cannot lose precision.
1145 ;;; FMIN - This parameter, if specified or non-NIL, is the minimum
1146 ;;; number of fraction digits which will be produced, regardless
1147 ;;; of the value of WIDTH or FDIGITS. This feature is used by
1148 ;;; the ~E format directive to prevent complete loss of
1149 ;;; significance in the printed value due to a bogus choice of
1153 ;;; (VALUES DIGIT-STRING DIGIT-LENGTH LEADING-POINT TRAILING-POINT DECPNT)
1154 ;;; where the results have the following interpretation:
1156 ;;; DIGIT-STRING - The decimal representation of X, with decimal point.
1157 ;;; DIGIT-LENGTH - The length of the string DIGIT-STRING.
1158 ;;; LEADING-POINT - True if the first character of DIGIT-STRING is the
1160 ;;; TRAILING-POINT - True if the last character of DIGIT-STRING is the
1162 ;;; POINT-POS - The position of the digit preceding the decimal
1163 ;;; point. Zero indicates point before first digit.
1165 ;;; NOTE: FLONUM-TO-STRING goes to a lot of trouble to guarantee
1166 ;;; accuracy. Specifically, the decimal number printed is the closest
1167 ;;; possible approximation to the true value of the binary number to
1168 ;;; be printed from among all decimal representations with the same
1169 ;;; number of digits. In free-format output, i.e. with the number of
1170 ;;; digits unconstrained, it is guaranteed that all the information is
1171 ;;; preserved, so that a properly- rounding reader can reconstruct the
1172 ;;; original binary number, bit-for-bit, from its printed decimal
1173 ;;; representation. Furthermore, only as many digits as necessary to
1174 ;;; satisfy this condition will be printed.
1176 ;;; FLOAT-DIGITS actually generates the digits for positive numbers;
1177 ;;; see below for comments.
1179 (defun flonum-to-string (x &optional width fdigits scale fmin
)
1180 (declare (type float x
))
1181 ;; FIXME: I think only FORMAT-DOLLARS calls FLONUM-TO-STRING with
1182 ;; possibly-negative X.
1184 (multiple-value-bind (e string
)
1186 (flonum-to-digits x
(min (- (+ fdigits
(or scale
0)))
1188 (if (and width
(> width
1))
1189 (let ((w (multiple-value-list
1193 (if (and scale
(minusp scale
))
1196 (f (multiple-value-list
1197 (flonum-to-digits x
(- (+ (or fmin
0)
1198 (if scale scale
0)))))))
1200 ((>= (length (cadr w
)) (length (cadr f
)))
1202 (t (values-list f
))))
1203 (flonum-to-digits x
)))
1204 (let ((e (if (zerop x
)
1206 (+ e
(or scale
0))))
1207 (stream (make-string-output-stream)))
1210 (write-string string stream
:end
(min (length string
) e
))
1211 (dotimes (i (- e
(length string
)))
1212 (write-char #\
0 stream
))
1213 (write-char #\. stream
)
1214 (write-string string stream
:start
(min (length string
) e
))
1216 (dotimes (i (- fdigits
1218 (min (length string
) e
))))
1219 (write-char #\
0 stream
))))
1221 (write-string "." stream
)
1223 (write-char #\
0 stream
))
1224 (write-string string stream
:end
(when fdigits
1225 (min (length string
)
1229 (dotimes (i (+ fdigits e
(- (length string
))))
1230 (write-char #\
0 stream
)))))
1231 (let ((string (get-output-stream-string stream
)))
1232 (values string
(length string
)
1233 (char= (char string
0) #\.
)
1234 (char= (char string
(1- (length string
))) #\.
)
1235 (position #\. string
))))))
1237 ;;; implementation of figure 1 from Burger and Dybvig, 1996. It is
1238 ;;; extended in order to handle rounding.
1240 ;;; As the implementation of the Dragon from Classic CMUCL (and
1241 ;;; previously in SBCL above FLONUM-TO-STRING) says: "DO NOT EVEN
1242 ;;; THINK OF ATTEMPTING TO UNDERSTAND THIS CODE WITHOUT READING THE
1243 ;;; PAPER!", and in this case we have to add that even reading the
1244 ;;; paper might not bring immediate illumination as CSR has attempted
1245 ;;; to turn idiomatic Scheme into idiomatic Lisp.
1247 ;;; FIXME: figure 1 from Burger and Dybvig is the unoptimized
1248 ;;; algorithm, noticeably slow at finding the exponent. Figure 2 has
1249 ;;; an improved algorithm, but CSR ran out of energy.
1251 ;;; possible extension for the enthusiastic: printing floats in bases
1252 ;;; other than base 10.
1253 (defconstant single-float-min-e
1254 (- 2 sb
!vm
:single-float-bias sb
!vm
:single-float-digits
))
1255 (defconstant double-float-min-e
1256 (- 2 sb
!vm
:double-float-bias sb
!vm
:double-float-digits
))
1258 (defconstant long-float-min-e
1259 (nth-value 1 (decode-float least-positive-long-float
)))
1261 (defun flonum-to-digits (v &optional position relativep
)
1262 (let ((print-base 10) ; B
1264 (float-digits (float-digits v
)) ; p
1265 (digit-characters "0123456789")
1268 (single-float single-float-min-e
)
1269 (double-float double-float-min-e
)
1271 (long-float long-float-min-e
))))
1272 (multiple-value-bind (f e
)
1273 (integer-decode-float v
)
1274 (let ( ;; FIXME: these even tests assume normal IEEE rounding
1275 ;; mode. I wonder if we should cater for non-normal?
1278 (with-push-char (:element-type base-char
)
1279 (labels ((scale (r s m
+ m-
)
1280 (do ((r+m
+ (+ r m
+))
1282 (s s
(* s print-base
)))
1283 ((not (or (> r
+m
+ s
)
1284 (and high-ok
(= r
+m
+ s
))))
1286 (r r
(* r print-base
))
1287 (m+ m
+ (* m
+ print-base
))
1288 (m- m-
(* m- print-base
)))
1289 ((not (and (> r m-
) ; Extension to handle zero
1290 (let ((x (* (+ r m
+) print-base
)))
1294 (values k
(generate r s m
+ m-
)))))))
1295 (generate (r s m
+ m-
)
1299 (setf (values d r
) (truncate (* r print-base
) s
))
1300 (setf m
+ (* m
+ print-base
))
1301 (setf m-
(* m- print-base
))
1302 (setf tc1
(or (< r m-
) (and low-ok
(= r m-
))))
1303 (setf tc2
(let ((r+m
+ (+ r m
+)))
1305 (and high-ok
(= r
+m
+ s
)))))
1308 (push-char (char digit-characters d
))
1312 ((and (not tc1
) tc2
) (1+ d
))
1313 ((and tc1
(not tc2
)) d
)
1318 (push-char (char digit-characters d
))
1319 (return-from generate
(get-pushed-string))))))
1323 (let ((be (expt float-radix e
)))
1324 (if (/= f
(expt float-radix
(1- float-digits
)))
1330 m
+ (* be float-radix
)
1332 s
(* float-radix
2)))))
1334 (/= f
(expt float-radix
(1- float-digits
))))
1336 s
(expt float-radix
(- 1 e
))
1340 (setf r
(* f float-radix
2)
1341 s
(expt float-radix
(- 2 e
))
1346 (aver (> position
0))
1348 ;; running out of letters here
1349 (l 1 (* l print-base
)))
1350 ((>= (* s l
) (+ r m
+))
1352 (if (< (+ r
(* s
(/ (expt print-base
(- k position
)) 2)))
1354 (setf position
(- k position
))
1355 (setf position
(- k position
1))))))
1356 (let* ((x (/ (* s
(expt print-base position
)) 2))
1365 (values r s m
+ m-
))))
1366 (multiple-value-bind (r s m
+ m-
) (initialize)
1367 (scale r s m
+ m-
))))))))
1369 ;;; Given a non-negative floating point number, SCALE-EXPONENT returns
1370 ;;; a new floating point number Z in the range (0.1, 1.0] and an
1371 ;;; exponent E such that Z * 10^E is (approximately) equal to the
1372 ;;; original number. There may be some loss of precision due the
1373 ;;; floating point representation. The scaling is always done with
1374 ;;; long float arithmetic, which helps printing of lesser precisions
1375 ;;; as well as avoiding generic arithmetic.
1377 ;;; When computing our initial scale factor using EXPT, we pull out
1378 ;;; part of the computation to avoid over/under flow. When
1379 ;;; denormalized, we must pull out a large factor, since there is more
1380 ;;; negative exponent range than positive range.
1382 (eval-when (:compile-toplevel
:execute
)
1383 (setf *read-default-float-format
*
1384 #!+long-float
'long-float
#!-long-float
'double-float
))
1385 (defun scale-exponent (original-x)
1386 (let* ((x (coerce original-x
'long-float
)))
1387 (multiple-value-bind (sig exponent
) (decode-float x
)
1388 (declare (ignore sig
))
1390 (values (float 0.0e0 original-x
) 1)
1391 (let* ((ex (locally (declare (optimize (safety 0)))
1394 ;; this is the closest double float
1395 ;; to (log 2 10), but expressed so
1396 ;; that we're not vulnerable to the
1397 ;; host lisp's interpretation of
1398 ;; arithmetic. (FIXME: it turns
1399 ;; out that sbcl itself is off by 1
1400 ;; ulp in this value, which is a
1401 ;; little unfortunate.)
1404 (make-double-float 1070810131 1352628735)
1406 (error "(log 2 10) not computed")))))))
1408 (if (float-denormalized-p x
)
1410 (* x
1.0e16
(expt 10.0e0
(- (- ex
) 16)))
1412 (* x
1.0e18
(expt 10.0e0
(- (- ex
) 18)))
1413 (* x
10.0e0
(expt 10.0e0
(- (- ex
) 1))))
1414 (/ x
10.0e0
(expt 10.0e0
(1- ex
))))))
1415 (do ((d 10.0e0
(* d
10.0e0
))
1419 (do ((m 10.0e0
(* m
10.0e0
))
1423 (values (float z original-x
) ex
))
1424 (declare (long-float m
) (integer ex
))))
1425 (declare (long-float d
))))))))
1426 (eval-when (:compile-toplevel
:execute
)
1427 (setf *read-default-float-format
* 'single-float
))
1429 ;;;; entry point for the float printer
1431 ;;; the float printer as called by PRINT, PRIN1, PRINC, etc. The
1432 ;;; argument is printed free-format, in either exponential or
1433 ;;; non-exponential notation, depending on its magnitude.
1435 ;;; NOTE: When a number is to be printed in exponential format, it is
1436 ;;; scaled in floating point. Since precision may be lost in this
1437 ;;; process, the guaranteed accuracy properties of FLONUM-TO-STRING
1438 ;;; are lost. The difficulty is that FLONUM-TO-STRING performs
1439 ;;; extensive computations with integers of similar magnitude to that
1440 ;;; of the number being printed. For large exponents, the bignums
1441 ;;; really get out of hand. If bignum arithmetic becomes reasonably
1442 ;;; fast and the exponent range is not too large, then it might become
1443 ;;; attractive to handle exponential notation with the same accuracy
1444 ;;; as non-exponential notation, using the method described in the
1445 ;;; Steele and White paper.
1447 ;;; NOTE II: this has been bypassed slightly by implementing Burger
1448 ;;; and Dybvig, 1996. When someone has time (KLUDGE) they can
1449 ;;; probably (a) implement the optimizations suggested by Burger and
1450 ;;; Dyvbig, and (b) remove all vestiges of Dragon4, including from
1451 ;;; fixed-format printing.
1453 ;;; Print the appropriate exponent marker for X and the specified exponent.
1454 (defun print-float-exponent (x exp stream
)
1455 (declare (type float x
) (type integer exp
) (type stream stream
))
1456 (cond ((case *read-default-float-format
*
1457 ((short-float single-float
)
1458 (typep x
'single-float
))
1459 ((double-float #!-long-float long-float
)
1460 (typep x
'double-float
))
1463 (typep x
'long-float
)))
1465 (write-char #\e stream
)
1466 (%output-integer-in-base exp
10 stream
)))
1475 (%output-integer-in-base exp
10 stream
))))
1477 (defun output-float-infinity (x stream
)
1478 (declare (float x
) (stream stream
))
1480 (write-string "#." stream
))
1482 (return-from output-float-infinity
1483 (print-not-readable-error x stream
)))
1485 (write-string "#<" stream
)))
1486 (write-string "SB-EXT:" stream
)
1487 (write-string (symbol-name (float-format-name x
)) stream
)
1488 (write-string (if (plusp x
) "-POSITIVE-" "-NEGATIVE-")
1490 (write-string "INFINITY" stream
)
1492 (write-string ">" stream
)))
1494 (defun output-float-nan (x stream
)
1495 (print-unreadable-object (x stream
)
1496 (princ (float-format-name x
) stream
)
1497 (write-string (if (float-trapping-nan-p x
) " trapping" " quiet") stream
)
1498 (write-string " NaN" stream
)))
1500 ;;; the function called by OUTPUT-OBJECT to handle floats
1501 (defmethod print-object ((x float
) stream
)
1503 ((float-infinity-p x
)
1504 (output-float-infinity x stream
))
1506 (output-float-nan x stream
))
1508 (let ((x (cond ((minusp (float-sign x
))
1509 (write-char #\- stream
)
1515 (write-string "0.0" stream
)
1516 (print-float-exponent x
0 stream
))
1518 (output-float-aux x stream -
3 8)))))))
1520 (defun output-float-aux (x stream e-min e-max
)
1521 (multiple-value-bind (e string
)
1522 (flonum-to-digits x
)
1527 (write-string string stream
:end
(min (length string
) e
))
1528 (dotimes (i (- e
(length string
)))
1529 (write-char #\
0 stream
))
1530 (write-char #\. stream
)
1531 (write-string string stream
:start
(min (length string
) e
))
1532 (when (<= (length string
) e
)
1533 (write-char #\
0 stream
))
1534 (print-float-exponent x
0 stream
))
1536 (write-string "0." stream
)
1538 (write-char #\
0 stream
))
1539 (write-string string stream
)
1540 (print-float-exponent x
0 stream
))))
1541 (t (write-string string stream
:end
1)
1542 (write-char #\. stream
)
1543 (write-string string stream
:start
1)
1544 (print-float-exponent x
(1- e
) stream
)))))
1546 ;;;; other leaf objects
1548 ;;; If *PRINT-ESCAPE* is false, just do a WRITE-CHAR, otherwise output
1549 ;;; the character name or the character in the #\char format.
1550 (defmethod print-object ((char character
) stream
)
1551 (if (or *print-escape
* *print-readably
*)
1552 (let ((graphicp (and (graphic-char-p char
)
1553 (standard-char-p char
)))
1554 (name (char-name char
)))
1555 (write-string "#\\" stream
)
1556 (if (and name
(or (not graphicp
) *print-readably
*))
1557 (quote-string name stream
)
1558 (write-char char stream
)))
1559 (write-char char stream
)))
1561 (defmethod print-object ((sap system-area-pointer
) stream
)
1563 (format stream
"#.(~S #X~8,'0X)" 'int-sap
(sap-int sap
)))
1565 (print-unreadable-object (sap stream
)
1566 (format stream
"system area pointer: #X~8,'0X" (sap-int sap
))))))
1568 (defmethod print-object ((weak-pointer weak-pointer
) stream
)
1569 (print-unreadable-object (weak-pointer stream
)
1570 (multiple-value-bind (value validp
) (weak-pointer-value weak-pointer
)
1572 (write-string "weak pointer: " stream
)
1573 (write value
:stream stream
))
1575 (write-string "broken weak pointer" stream
))))))
1577 (defmethod print-object ((component code-component
) stream
)
1578 (print-unreadable-object (component stream
:identity t
)
1579 (let ((dinfo (%code-debug-info component
)))
1580 (cond ((eq dinfo
:bogus-lra
)
1581 (write-string "bogus code object" stream
))
1583 (format stream
"code object [~D]" (code-n-entries component
))
1584 (let ((fun-name (awhen (%code-entry-point component
0)
1585 (%simple-fun-name it
))))
1587 (write-char #\Space stream
)
1588 (write fun-name
:stream stream
))
1589 (cond ((not (typep dinfo
'sb
!c
::debug-info
)))
1590 ((neq (sb!c
::debug-info-name dinfo
) fun-name
)
1591 (write-string ", " stream
)
1592 (output-object (sb!c
::debug-info-name dinfo
) stream
)))))))))
1595 (defmethod print-object ((lra lra
) stream
)
1596 (print-unreadable-object (lra stream
:identity t
)
1597 (write-string "return PC object" stream
)))
1599 (defmethod print-object ((fdefn fdefn
) stream
)
1600 (print-unreadable-object (fdefn stream
:type t
)
1601 ;; As fdefn names are particularly relevant to those hacking on the compiler
1602 ;; and disassembler, be maximally helpful by neither abbreviating (SETF ...)
1603 ;; due to length cutoff, nor failing to print a package if needed.
1604 ;; Some folks seem to love same-named symbols way too much.
1605 (let ((*print-length
* 20)) ; arbitrary
1606 (prin1 (fdefn-name fdefn
) stream
))))
1609 (defmethod print-object ((pack simd-pack
) stream
)
1610 (cond ((and *print-readably
* *read-eval
*)
1611 (multiple-value-bind (format maker extractor
)
1613 ((simd-pack double-float
)
1614 (values "#.(~S ~S ~S)"
1615 '%make-simd-pack-double
#'%simd-pack-doubles
))
1616 ((simd-pack single-float
)
1617 (values "#.(~S ~S ~S ~S ~S)"
1618 '%make-simd-pack-single
#'%simd-pack-singles
))
1620 (values "#.(~S #X~16,'0X #X~16,'0X)"
1621 '%make-simd-pack-ub64
#'%simd-pack-ub64s
)))
1622 (multiple-value-call
1623 #'format stream format maker
(funcall extractor pack
))))
1625 (print-unreadable-object (pack stream
)
1626 (flet ((all-ones-p (value start end
&aux
(mask (- (ash 1 end
) (ash 1 start
))))
1627 (= (logand value mask
) mask
))
1628 (split-num (value start
)
1631 and v
= (ash value
(- start
)) then
(ash v -
8)
1632 collect
(logand v
#xFF
))))
1633 (multiple-value-bind (low high
)
1634 (%simd-pack-ub64s pack
)
1636 ((simd-pack double-float
)
1637 (multiple-value-bind (v0 v1
) (%simd-pack-doubles pack
)
1638 (format stream
"~S~@{ ~:[~,13E~;~*TRUE~]~}"
1640 (all-ones-p low
0 64) v0
1641 (all-ones-p high
0 64) v1
)))
1642 ((simd-pack single-float
)
1643 (multiple-value-bind (v0 v1 v2 v3
) (%simd-pack-singles pack
)
1644 (format stream
"~S~@{ ~:[~,7E~;~*TRUE~]~}"
1646 (all-ones-p low
0 32) v0
1647 (all-ones-p low
32 64) v1
1648 (all-ones-p high
0 32) v2
1649 (all-ones-p high
32 64) v3
)))
1651 (format stream
"~S~@{ ~{ ~2,'0X~}~}"
1653 (split-num low
0) (split-num low
32)
1654 (split-num high
0) (split-num high
32))))))))))
1658 (defmethod print-object ((object function
) stream
)
1659 (let* ((name (%fun-name object
))
1660 (proper-name-p (and (legal-fun-name-p name
) (fboundp name
)
1661 (eq (fdefinition name
) object
))))
1662 ;; ":TYPE T" is no good, since CLOSURE doesn't have full-fledged status.
1663 (print-unreadable-object (object stream
:identity
(not proper-name-p
))
1664 (format stream
"~A~@[ ~S~]"
1665 ;; TYPE-OF is so that GFs print as #<STANDARD-GENERIC-FUNCTION>
1666 ;; and not #<FUNCTION> before SRC;PCL;PRINT-OBJECT is loaded.
1667 (if (closurep object
) 'closure
(type-of object
))
1670 ;;;; catch-all for unknown things
1672 (defmethod print-object ((object t
) stream
)
1673 (flet ((output-it (stream)
1674 (print-unreadable-object (object stream
:identity t
)
1675 (let ((lowtag (lowtag-of object
)))
1677 (#.sb
!vm
:other-pointer-lowtag
1678 (let ((widetag (widetag-of object
)))
1680 (#.sb
!vm
:value-cell-widetag
1681 (write-string "value cell " stream
)
1682 (output-object (value-cell-ref object
) stream
))
1684 (write-string "unknown pointer object, widetag=" stream
)
1685 (let ((*print-base
* 16) (*print-radix
* t
))
1686 (output-integer widetag stream
))))))
1687 ((#.sb
!vm
:fun-pointer-lowtag
1688 #.sb
!vm
:instance-pointer-lowtag
1689 #.sb
!vm
:list-pointer-lowtag
)
1690 (write-string "unknown pointer object, lowtag=" stream
)
1691 (let ((*print-base
* 16) (*print-radix
* t
))
1692 (output-integer lowtag stream
)))
1694 (case (widetag-of object
)
1695 (#.sb
!vm
:unbound-marker-widetag
1696 (write-string "unbound marker" stream
))
1698 (write-string "unknown immediate object, lowtag=" stream
)
1699 (let ((*print-base
* 2) (*print-radix
* t
))
1700 (output-integer lowtag stream
))
1701 (write-string ", widetag=" stream
)
1702 (let ((*print-base
* 16) (*print-radix
* t
))
1703 (output-integer (widetag-of object
) stream
))))))))))
1705 ;; This block might not be necessary. Not sure, probably can't hurt.
1706 (pprint-logical-block (stream nil
) (output-it stream
))
1707 (output-it stream
))))