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
* (load-time-value (find-package "COMMON-LISP-USER") t
))
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 (multiple-value-bind (fun pretty
)
188 (and *print-pretty
* (pprint-dispatch object
))
190 (with-simple-output-to-string (stream)
191 (sb!pretty
::with-pretty-stream
(stream)
192 (funcall fun stream object
)))
193 (let ((buffer-size (approx-chars-in-repr object
)))
194 (let* ((string (make-string buffer-size
:element-type
'base-char
))
195 (stream (%make-finite-base-string-output-stream string
)))
196 (declare (inline %make-finite-base-string-output-stream
))
197 (declare (truly-dynamic-extent stream
))
198 (output-integer object stream
*print-base
* *print-radix
*)
199 (%shrink-vector string
200 (finite-base-string-output-stream-pointer stream
)))))))
201 ;; Could do something for other numeric types, symbols, ...
203 (with-simple-output-to-string (stream)
204 (output-object object stream
)))))
206 ;;; Estimate the number of chars in the printed representation of OBJECT.
207 ;;; The answer must be an overestimate or exact; never an underestimate.
208 (defun approx-chars-in-repr (object)
209 (declare (integer object
))
210 ;; Round *PRINT-BASE* down to the nearest lower power-of-2, call that N,
211 ;; and "guess" that the one character can represent N bits.
212 ;; This is exact for bases which are exactly a power-of-2, or an overestimate
213 ;; otherwise, as mandated by the finite output stream.
215 (aref #.
(!coerce-to-specialized
216 ;; base 2 or base 3 = 1 bit per character
217 ;; base 4 .. base 7 = 2 bits per character
218 ;; base 8 .. base 15 = 3 bits per character, etc
219 #(1 1 2 2 2 2 3 3 3 3 3 3 3 3
220 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5)
222 (- *print-base
* 2))))
223 (+ (if (minusp object
) 1 0) ; leading sign
224 (if *print-radix
* 4 0) ; #rNN or trailing decimal
225 (ceiling (if (fixnump object
)
226 sb
!vm
:n-positive-fixnum-bits
227 (* (%bignum-length object
) sb
!bignum
::digit-size
))
230 ;;;; support for the PRINT-UNREADABLE-OBJECT macro
232 (defun print-not-readable-error (object stream
)
234 (error 'print-not-readable
:object object
)
236 :report
"Print unreadably."
237 (let ((*print-readably
* nil
))
238 (output-object object stream
)
241 :report
"Supply an object to be printed instead."
244 (read-evaluated-form "~@<Enter an object (evaluated): ~@:>"))
245 (output-object o stream
)
248 ;;; guts of PRINT-UNREADABLE-OBJECT
249 (defun %print-unreadable-object
(object stream type identity
&optional body
)
250 (declare (type (or null function
) body
))
252 (print-not-readable-error object stream
)
253 (flet ((print-description ()
255 (write (type-of object
) :stream stream
:circle nil
256 :level nil
:length nil
)
257 ;; Do NOT insert a pprint-newline here.
258 ;; See ba34717602d80e5fd74d10e61f4729fb0d019a0c
259 (write-char #\space stream
))
263 (when (or body
(not type
))
264 (write-char #\space stream
))
266 (write-char #\
{ stream
)
267 (write (get-lisp-obj-address object
) :stream stream
269 (write-char #\
} stream
))))
270 (cond ((print-pretty-on-stream-p stream
)
271 ;; Since we're printing prettily on STREAM, format the
272 ;; object within a logical block. PPRINT-LOGICAL-BLOCK does
273 ;; not rebind the stream when it is already a pretty stream,
274 ;; so output from the body will go to the same stream.
275 (pprint-logical-block (stream nil
:prefix
"#<" :suffix
">")
276 (print-description)))
278 (write-string "#<" stream
)
280 (write-char #\
> stream
)))))
283 ;;;; OUTPUT-OBJECT -- the main entry point
285 ;;; Objects whose print representation identifies them EQLly don't
286 ;;; need to be checked for circularity.
287 (defun uniquely-identified-by-print-p (x)
291 (symbol-package x
))))
293 (defvar *in-print-error
* nil
)
295 ;;; Output OBJECT to STREAM observing all printer control variables.
296 (defun output-object (object stream
)
297 ;; FIXME: this function is declared EXPLICIT-CHECK, so it allows STREAM
298 ;; to be T or NIL (a stream-designator), which is not really right
299 ;; if eventually the call will be to a PRINT-OBJECT method,
300 ;; since the generic function should always receive a stream.
301 (declare (explicit-check))
302 (labels ((print-it (stream)
303 (multiple-value-bind (fun pretty
)
304 (and *print-pretty
* (pprint-dispatch object
))
306 (sb!pretty
::with-pretty-stream
(stream)
307 (funcall fun stream object
))
308 (output-ugly-object stream object
))))
310 (if *suppress-print-errors
*
311 (handler-bind ((condition
312 (lambda (condition) nil
313 (when (typep condition
*suppress-print-errors
*)
314 (cond (*in-print-error
*
315 (write-string "(error printing " stream
)
316 (write-string *in-print-error
* stream
)
317 (write-string ")" stream
))
319 ;; Give outer handlers a chance.
321 (continue "Suppress the error.")
323 (let ((*print-readably
* nil
)
326 "#<error printing a " stream
)
327 (let ((*in-print-error
* "type"))
328 (output-object (type-of object
) stream
))
329 (write-string ": " stream
)
330 (let ((*in-print-error
* "condition"))
331 (output-object condition stream
))
332 (write-string ">" stream
))))
333 (return-from handle-it object
)))))
337 (multiple-value-bind (marker initiate
)
338 (check-for-circularity object t
)
339 (if (eq initiate
:initiate
)
340 (let ((*circularity-hash-table
*
341 (make-hash-table :test
'eq
)))
342 (check-it (make-broadcast-stream))
343 (let ((*circularity-counter
* 0))
347 (when (handle-circularity marker stream
)
349 (handle-it stream
))))))
350 (cond (;; Maybe we don't need to bother with circularity detection.
351 (or (not *print-circle
*)
352 (uniquely-identified-by-print-p object
))
354 (;; If we have already started circularity detection, this
355 ;; object might be a shared reference. If we have not, then
356 ;; if it is a compound object it might contain a circular
357 ;; reference to itself or multiple shared references.
358 (or *circularity-hash-table
*
359 (compound-object-p object
))
362 (handle-it stream
)))))
364 ;;; Output OBJECT to STREAM observing all printer control variables
365 ;;; except for *PRINT-PRETTY*. Note: if *PRINT-PRETTY* is non-NIL,
366 ;;; then the pretty printer will be used for any components of OBJECT,
367 ;;; just not for OBJECT itself.
368 (defun output-ugly-object (stream object
)
369 (when (%instancep object
)
370 (let* ((layout (layout-of object
))
371 (classoid (layout-classoid layout
)))
372 ;; If an instance has no layout, it has no PRINT-OBJECT method.
373 ;; Additionally, if the object is an obsolete CONDITION, don't crash.
374 ;; (There is no update-instance protocol for conditions)
375 (when (or (sb!kernel
::undefined-classoid-p classoid
)
376 (and (layout-invalid layout
)
377 (logtest (layout-%flags layout
) +condition-layout-flag
+)))
378 ;; not only is this unreadable, it's unprintable too.
379 (return-from output-ugly-object
380 (print-unreadable-object (object stream
:identity t
)
381 (format stream
"UNPRINTABLE instance of ~W" classoid
))))))
382 (print-object object stream
))
386 (defmethod print-object ((object symbol
) stream
)
387 (if (or *print-escape
* *print-readably
*)
388 ;; Write so that reading back works
389 (output-symbol object
(symbol-package object
) stream
)
390 ;; Write only the characters of the name, never the package
391 (let ((rt *readtable
*))
392 (funcall (truly-the function
393 (choose-symbol-out-fun *print-case
* (%readtable-case rt
)))
394 (symbol-name object
) stream rt
))))
396 (defun output-symbol (symbol package stream
)
397 (let* ((readably *print-readably
*)
398 (readtable (if readably
*standard-readtable
* *readtable
*))
399 (out-fun (choose-symbol-out-fun *print-case
* (%readtable-case readtable
))))
400 (flet ((output-token (name)
401 (declare (type simple-string name
))
402 (cond ((or (and (readtable-normalization readtable
)
403 (not (sb!unicode
:normalized-p name
:nfkc
)))
404 (symbol-quotep name readtable
))
405 ;; Output NAME surrounded with |'s,
406 ;; and with any embedded |'s or \'s escaped.
407 (write-char #\| stream
)
408 (dotimes (index (length name
))
409 (let ((char (char name index
)))
410 ;; Hmm. Should these depend on what characters
411 ;; are actually escapes in the readtable ?
412 ;; (See similar remark at DEFUN QUOTE-STRING)
413 (when (or (char= char
#\\) (char= char
#\|
))
414 (write-char #\\ stream
))
415 (write-char char stream
)))
416 (write-char #\| stream
))
418 (funcall (truly-the function out-fun
) name stream readtable
)))))
419 (let ((name (symbol-name symbol
))
420 (current (sane-package)))
422 ;; The ANSI spec "22.1.3.3.1 Package Prefixes for Symbols"
423 ;; requires that keywords be printed with preceding colons
424 ;; always, regardless of the value of *PACKAGE*.
425 ((eq package
*keyword-package
*)
426 (write-char #\
: stream
))
427 ;; Otherwise, if the symbol's home package is the current
428 ;; one, then a prefix is never necessary.
429 ((eq package current
))
430 ;; Uninterned symbols print with a leading #:.
432 (when (or *print-gensym
* readably
)
433 (write-string "#:" stream
)))
435 (multiple-value-bind (found accessible
) (find-symbol name current
)
436 ;; If we can find the symbol by looking it up, it need not
437 ;; be qualified. This can happen if the symbol has been
438 ;; inherited from a package other than its home package.
440 ;; To preserve print-read consistency, use the local nickname if
442 (unless (and accessible
(eq found symbol
))
443 (let ((prefix (or (car (rassoc package
(package-%local-nicknames current
)))
444 (package-name package
))))
445 (output-token prefix
))
446 (write-char #\
: stream
)
447 (when (eql (find-external-symbol name package
) 0)
448 (write-char #\
: stream
))))))
449 (output-token name
)))))
451 ;;;; escaping symbols
453 ;;; When we print symbols we have to figure out if they need to be
454 ;;; printed with escape characters. This isn't a whole lot easier than
455 ;;; reading symbols in the first place.
457 ;;; For each character, the value of the corresponding element is a
458 ;;; fixnum with bits set corresponding to attributes that the
459 ;;; character has. At characters have at least one bit set, so we can
460 ;;; search for any character with a positive test.
461 (defvar *character-attributes
*
462 (make-array 160 ; FIXME
463 :element-type
'(unsigned-byte 16)
465 (declaim (type (simple-array (unsigned-byte 16) (#.160)) ; FIXME
466 *character-attributes
*))
468 ;;; constants which are a bit-mask for each interesting character attribute
469 (defconstant other-attribute
(ash 1 0)) ; Anything else legal.
470 (defconstant number-attribute
(ash 1 1)) ; A numeric digit.
471 (defconstant uppercase-attribute
(ash 1 2)) ; An uppercase letter.
472 (defconstant lowercase-attribute
(ash 1 3)) ; A lowercase letter.
473 (defconstant sign-attribute
(ash 1 4)) ; +-
474 (defconstant extension-attribute
(ash 1 5)) ; ^_
475 (defconstant dot-attribute
(ash 1 6)) ; .
476 (defconstant slash-attribute
(ash 1 7)) ; /
477 (defconstant funny-attribute
(ash 1 8)) ; Anything illegal.
479 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
481 ;;; LETTER-ATTRIBUTE is a local of SYMBOL-QUOTEP. It matches letters
482 ;;; that don't need to be escaped (according to READTABLE-CASE.)
483 (defparameter *attribute-names
*
484 `((number . number-attribute
) (lowercase . lowercase-attribute
)
485 (uppercase . uppercase-attribute
) (letter . letter-attribute
)
486 (sign . sign-attribute
) (extension . extension-attribute
)
487 (dot . dot-attribute
) (slash . slash-attribute
)
488 (other . other-attribute
) (funny . funny-attribute
)))
492 ;;; For each character, the value of the corresponding element is the
493 ;;; lowest base in which that character is a digit.
494 (declaim (type (simple-array (unsigned-byte 8) (128)) ; FIXME: range?
496 (defvar *digit-bases
*
497 (make-array 128 ; FIXME
498 :element-type
'(unsigned-byte 8)))
500 (defun !printer-cold-init
()
501 ;; The dispatch table will be changed later, so this doesn't really matter
502 ;; except if a full call to WRITE wants to read the current binding.
503 (setq *print-pprint-dispatch
* (sb!pretty
::make-pprint-dispatch-table
))
504 (setq *digit-bases
* (make-array 128 ; FIXME
505 :element-type
'(unsigned-byte 8)
507 *character-attributes
* (make-array 160 ; FIXME
508 :element-type
'(unsigned-byte 16)
511 (let ((char (digit-char i
36)))
512 (setf (aref *digit-bases
* (char-code char
)) i
)))
514 (flet ((set-bit (char bit
)
515 (let ((code (char-code char
)))
516 (setf (aref *character-attributes
* code
)
517 (logior bit
(aref *character-attributes
* code
))))))
519 (dolist (char '(#\
! #\
@ #\$
#\%
#\
& #\
* #\
= #\~
#\
[ #\
] #\
{ #\
}
521 (set-bit char other-attribute
))
524 (set-bit (digit-char i
) number-attribute
))
526 (do ((code (char-code #\A
) (1+ code
))
527 (end (char-code #\Z
)))
529 (declare (fixnum code end
))
530 (set-bit (code-char code
) uppercase-attribute
)
531 (set-bit (char-downcase (code-char code
)) lowercase-attribute
))
533 (set-bit #\- sign-attribute
)
534 (set-bit #\
+ sign-attribute
)
535 (set-bit #\^ extension-attribute
)
536 (set-bit #\_ extension-attribute
)
537 (set-bit #\. dot-attribute
)
538 (set-bit #\
/ slash-attribute
)
540 ;; Mark anything not explicitly allowed as funny.
541 (dotimes (i 160) ; FIXME
542 (when (zerop (aref *character-attributes
* i
))
543 (setf (aref *character-attributes
* i
) funny-attribute
))))
544 ) ; end !COLD-PRINT-INIT
546 ;;; A FSM-like thingie that determines whether a symbol is a potential
547 ;;; number or has evil characters in it.
548 (defun symbol-quotep (name readtable
)
549 (declare (simple-string name
))
550 (macrolet ((advance (tag &optional
(at-end t
))
553 ,(if at-end
'(go TEST-SIGN
) '(return nil
)))
554 (setq current
(schar name index
)
555 code
(char-code current
)
557 ((< code
160) (aref attributes code
))
558 ((upper-case-p current
) uppercase-attribute
)
559 ((lower-case-p current
) lowercase-attribute
)
560 (t other-attribute
)))
563 (test (&rest attributes
)
575 `(and (< code
128) ; FIXME
576 (< (the fixnum
(aref bases code
)) base
))))
578 (prog ((len (length name
))
579 (attributes *character-attributes
*)
580 (bases *digit-bases
*)
583 (case (%readtable-case readtable
)
584 (#.
+readtable-upcase
+ uppercase-attribute
)
585 (#.
+readtable-downcase
+ lowercase-attribute
)
586 (t (logior lowercase-attribute uppercase-attribute
))))
591 (declare (fixnum len base index bits code
))
594 TEST-SIGN
; At end, see whether it is a sign...
595 (return (not (test sign
)))
597 OTHER
; not potential number, see whether funny chars...
598 (let ((mask (logxor (logior lowercase-attribute uppercase-attribute
601 (do ((i (1- index
) (1+ i
)))
602 ((= i len
) (return-from symbol-quotep nil
))
603 (unless (zerop (logand (let* ((char (schar name i
))
604 (code (char-code char
)))
606 ((< code
160) (aref attributes code
))
607 ((upper-case-p char
) uppercase-attribute
)
608 ((lower-case-p char
) lowercase-attribute
)
609 (t other-attribute
)))
611 (return-from symbol-quotep t
))))
616 (advance LAST-DIGIT-ALPHA
)
618 (when (test letter number other slash
) (advance OTHER nil
))
619 (when (char= current
#\.
) (advance DOT-FOUND
))
620 (when (test sign extension
) (advance START-STUFF nil
))
623 DOT-FOUND
; leading dots...
624 (when (test letter
) (advance START-DOT-MARKER nil
))
625 (when (digitp) (advance DOT-DIGIT
))
626 (when (test number other
) (advance OTHER nil
))
627 (when (test extension slash sign
) (advance START-DOT-STUFF nil
))
628 (when (char= current
#\.
) (advance DOT-FOUND
))
631 START-STUFF
; leading stuff before any dot or digit
634 (advance LAST-DIGIT-ALPHA
)
636 (when (test number other
) (advance OTHER nil
))
637 (when (test letter
) (advance START-MARKER nil
))
638 (when (char= current
#\.
) (advance START-DOT-STUFF nil
))
639 (when (test sign extension slash
) (advance START-STUFF nil
))
642 START-MARKER
; number marker in leading stuff...
643 (when (test letter
) (advance OTHER nil
))
646 START-DOT-STUFF
; leading stuff containing dot without digit...
647 (when (test letter
) (advance START-DOT-STUFF nil
))
648 (when (digitp) (advance DOT-DIGIT
))
649 (when (test sign extension dot slash
) (advance START-DOT-STUFF nil
))
650 (when (test number other
) (advance OTHER nil
))
653 START-DOT-MARKER
; number marker in leading stuff with dot..
654 ;; leading stuff containing dot without digit followed by letter...
655 (when (test letter
) (advance OTHER nil
))
658 DOT-DIGIT
; in a thing with dots...
659 (when (test letter
) (advance DOT-MARKER
))
660 (when (digitp) (advance DOT-DIGIT
))
661 (when (test number other
) (advance OTHER nil
))
662 (when (test sign extension dot slash
) (advance DOT-DIGIT
))
665 DOT-MARKER
; number marker in number with dot...
666 (when (test letter
) (advance OTHER nil
))
669 LAST-DIGIT-ALPHA
; previous char is a letter digit...
670 (when (or (digitp) (test sign slash
))
671 (advance ALPHA-DIGIT
))
672 (when (test letter number other dot
) (advance OTHER nil
))
675 ALPHA-DIGIT
; seen a digit which is a letter...
676 (when (or (digitp) (test sign slash
))
678 (advance LAST-DIGIT-ALPHA
)
679 (advance ALPHA-DIGIT
)))
680 (when (test letter
) (advance ALPHA-MARKER
))
681 (when (test number other dot
) (advance OTHER nil
))
684 ALPHA-MARKER
; number marker in number with alpha digit...
685 (when (test letter
) (advance OTHER nil
))
688 DIGIT
; seen only ordinary (non-alphabetic) numeric digits...
691 (advance ALPHA-DIGIT
)
693 (when (test number other
) (advance OTHER nil
))
694 (when (test letter
) (advance MARKER
))
695 (when (test extension slash sign
) (advance DIGIT
))
696 (when (char= current
#\.
) (advance DOT-DIGIT
))
699 MARKER
; number marker in a numeric number...
700 ;; ("What," you may ask, "is a 'number marker'?" It's something
701 ;; that a conforming implementation might use in number syntax.
702 ;; See ANSI 2.3.1.1 "Potential Numbers as Tokens".)
703 (when (test letter
) (advance OTHER nil
))
706 ;;;; case hackery: One of these functions is chosen to output symbol
707 ;;;; names according to the values of *PRINT-CASE* and READTABLE-CASE.
710 ;;; READTABLE-CASE *PRINT-CASE*
712 ;;; :DOWNCASE :DOWNCASE
714 (defun output-preserve-symbol (pname stream readtable
)
715 (declare (ignore readtable
))
716 (write-string pname stream
))
719 ;;; READTABLE-CASE *PRINT-CASE*
720 ;;; :UPCASE :DOWNCASE
721 (defun output-lowercase-symbol (pname stream readtable
)
722 (declare (simple-string pname
) (ignore readtable
))
723 (dotimes (index (length pname
))
724 (let ((char (schar pname index
)))
725 (write-char (char-downcase char
) stream
))))
728 ;;; READTABLE-CASE *PRINT-CASE*
729 ;;; :DOWNCASE :UPCASE
730 (defun output-uppercase-symbol (pname stream readtable
)
731 (declare (simple-string pname
) (ignore readtable
))
732 (dotimes (index (length pname
))
733 (let ((char (schar pname index
)))
734 (write-char (char-upcase char
) stream
))))
737 ;;; READTABLE-CASE *PRINT-CASE*
738 ;;; :UPCASE :CAPITALIZE
739 ;;; :DOWNCASE :CAPITALIZE
740 (defun output-capitalize-symbol (pname stream readtable
)
741 (declare (simple-string pname
))
742 (let ((prev-not-alphanum t
)
743 (up (eql (%readtable-case readtable
) +readtable-upcase
+)))
744 (dotimes (i (length pname
))
745 (let ((char (char pname i
)))
747 (if (or prev-not-alphanum
(lower-case-p char
))
749 (char-downcase char
))
750 (if prev-not-alphanum
754 (setq prev-not-alphanum
(not (alphanumericp char
)))))))
757 ;;; READTABLE-CASE *PRINT-CASE*
759 (defun output-invert-symbol (pname stream readtable
)
760 (declare (simple-string pname
) (ignore readtable
))
763 (dotimes (i (length pname
))
764 (let ((ch (schar pname i
)))
765 (when (both-case-p ch
)
766 (if (upper-case-p ch
)
768 (setq all-upper nil
)))))
769 (cond (all-upper (output-lowercase-symbol pname stream nil
))
770 (all-lower (output-uppercase-symbol pname stream nil
))
772 (write-string pname stream
)))))
774 (defun choose-symbol-out-fun (print-case readtable-case
)
776 ((compute-fun-vector (&aux
(vector (make-array 12)))
777 ;; Pack a 2D array of functions into a simple-vector.
778 ;; Major axis is *PRINT-CASE*, minor axis is %READTABLE-CASE.
779 (dotimes (readtable-case-index 4)
780 (dotimes (print-case-index 3)
781 (let ((readtable-case
782 (elt '(:upcase
:downcase
:preserve
:invert
) readtable-case-index
))
784 (elt '(:upcase
:downcase
:capitalize
) print-case-index
)))
785 (setf (aref vector
(logior (ash print-case-index
2)
786 readtable-case-index
))
790 (:upcase
'output-preserve-symbol
)
791 (:downcase
'output-lowercase-symbol
)
792 (:capitalize
'output-capitalize-symbol
)))
795 (:upcase
'output-uppercase-symbol
)
796 (:downcase
'output-preserve-symbol
)
797 (:capitalize
'output-capitalize-symbol
)))
798 (:preserve
'output-preserve-symbol
)
799 (:invert
'output-invert-symbol
))))))
800 `(load-time-value (vector ,@(map 'list
(lambda (x) `(function ,x
)) vector
))
802 (aref (compute-fun-vector)
803 (logior (case print-case
(:upcase
0) (:downcase
4) (t 8))
804 (truly-the (mod 4) readtable-case
)))))
806 ;;;; recursive objects
808 (defmethod print-object ((list cons
) stream
)
809 (descend-into (stream)
810 (write-char #\
( stream
)
814 (punt-print-if-too-long length stream
)
815 (output-object (pop list
) stream
)
818 (when (or (atom list
)
819 (check-for-circularity list
))
820 (write-string " . " stream
)
821 (output-object list stream
)
823 (write-char #\space stream
)
825 (write-char #\
) stream
)))
827 (defmethod print-object ((vector vector
) stream
)
828 (let ((readably *print-readably
*))
829 (cond ((stringp vector
)
830 (cond ((and readably
(not (typep vector
'(vector character
))))
831 (output-unreadable-array-readably vector stream
))
832 ((or *print-escape
* readably
)
833 (write-char #\" stream
)
834 (quote-string vector stream
)
835 (write-char #\" stream
))
837 (write-string vector stream
))))
838 ((not (or *print-array
* readably
))
839 (output-terse-array vector stream
))
840 ((bit-vector-p vector
)
841 (write-string "#*" stream
)
842 (dovector (bit vector
)
843 ;; (Don't use OUTPUT-OBJECT here, since this code
844 ;; has to work for all possible *PRINT-BASE* values.)
845 (write-char (if (zerop bit
) #\
0 #\
1) stream
)))
846 ((or (not readably
) (array-readably-printable-p vector
))
847 (descend-into (stream)
848 (write-string "#(" stream
)
849 (dotimes (i (length vector
))
851 (write-char #\space stream
))
852 (punt-print-if-too-long i stream
)
853 (output-object (aref vector i
) stream
))
854 (write-string ")" stream
)))
857 (output-unreadable-array-readably vector stream
)))))
859 ;;; This function outputs a string quoting characters sufficiently
860 ;;; so that someone can read it in again. Basically, put a slash in
861 ;;; front of an character satisfying NEEDS-SLASH-P.
862 (defun quote-string (string stream
)
863 (macrolet ((needs-slash-p (char)
864 ;; KLUDGE: We probably should look at the readtable, but just do
865 ;; this for now. [noted by anonymous long ago] -- WHN 19991130
866 `(or (char= ,char
#\\)
868 (with-array-data ((data string
) (start) (end)
869 :check-fill-pointer t
)
870 (do ((index start
(1+ index
)))
872 (let ((char (schar data index
)))
873 (when (needs-slash-p char
) (write-char #\\ stream
))
874 (write-char char stream
))))))
876 (defun array-readably-printable-p (array)
877 (and (eq (array-element-type array
) t
)
878 (let ((zero (position 0 (array-dimensions array
)))
879 (number (position 0 (array-dimensions array
)
880 :test
(complement #'eql
)
882 (or (null zero
) (null number
) (> zero number
)))))
884 ;;; Output the printed representation of any array in either the #< or #A
886 (defmethod print-object ((array array
) stream
)
887 (if (or *print-array
* *print-readably
*)
888 (output-array-guts array stream
)
889 (output-terse-array array stream
)))
891 ;;; Output the abbreviated #< form of an array.
892 (defun output-terse-array (array stream
)
893 (let ((*print-level
* nil
)
894 (*print-length
* nil
))
895 (print-unreadable-object (array stream
:type t
:identity t
))))
897 ;;; Convert an array into a list that can be used with MAKE-ARRAY's
898 ;;; :INITIAL-CONTENTS keyword argument.
899 (defun listify-array (array)
900 (flet ((compact (seq)
903 (coerce seq
'(simple-array character
(*))))
905 (coerce seq
'bit-vector
))
908 (if (typep array
'(or string bit-vector
))
910 (with-array-data ((data array
) (start) (end))
911 (declare (ignore end
))
912 (labels ((listify (dimensions index
)
913 (if (null dimensions
)
915 (let* ((dimension (car dimensions
))
916 (dimensions (cdr dimensions
))
917 (count (reduce #'* dimensions
)))
918 (loop for i below dimension
919 for list
= (listify dimensions index
)
920 collect
(if (and dimensions
921 (null (cdr dimensions
)))
924 do
(incf index count
))))))
925 (listify (array-dimensions array
) start
))))))
927 ;;; Use nonstandard #A(dimensions element-type contents)
928 ;;; to avoid using #.
929 (defun output-unreadable-array-readably (array stream
)
930 (let ((array (list* (array-dimensions array
)
931 (array-element-type array
)
932 (listify-array array
))))
933 (write-string "#A" stream
)
934 (write array
:stream stream
)
937 ;;; Output the readable #A form of an array.
938 (defun output-array-guts (array stream
)
939 (cond ((or (not *print-readably
*)
940 (array-readably-printable-p array
))
941 (write-char #\
# stream
)
942 (output-integer (array-rank array
) stream
10 nil
)
943 (write-char #\A stream
)
944 (with-array-data ((data array
) (start) (end))
945 (declare (ignore end
))
946 (sub-output-array-guts data
(array-dimensions array
) stream start
)))
948 (output-unreadable-array-readably array stream
))))
950 (defun sub-output-array-guts (array dimensions stream index
)
951 (declare (type (simple-array * (*)) array
) (fixnum index
))
952 (cond ((null dimensions
)
953 (output-object (aref array index
) stream
))
955 (descend-into (stream)
956 (write-char #\
( stream
)
957 (let* ((dimension (car dimensions
))
958 (dimensions (cdr dimensions
))
959 (count (reduce #'* dimensions
)))
960 (dotimes (i dimension
)
962 (write-char #\space stream
))
963 (punt-print-if-too-long i stream
)
964 (sub-output-array-guts array dimensions stream index
)
966 (write-char #\
) stream
)))))
969 ;;;; integer, ratio, and complex printing (i.e. everything but floats)
971 (defun %output-radix
(base stream
)
972 (write-char #\
# stream
)
973 (write-char (case base
977 (t (%output-reasonable-integer-in-base base
10 stream
)
981 (defun %output-reasonable-integer-in-base
(n base stream
)
982 (multiple-value-bind (q r
)
984 ;; Recurse until you have all the digits pushed on
987 (%output-reasonable-integer-in-base q base stream
))
988 ;; Then as each recursive call unwinds, turn the
989 ;; digit (in remainder) into a character and output
992 (schar "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" r
)
995 ;;; *POWER-CACHE* is an alist mapping bases to power-vectors. It is
996 ;;; filled and probed by POWERS-FOR-BASE. SCRUB-POWER-CACHE is called
997 ;;; always prior a GC to drop overly large bignums from the cache.
999 ;;; It doesn't need a lock, but if you work on SCRUB-POWER-CACHE or
1000 ;;; POWERS-FOR-BASE, see that you don't break the assumptions!
1001 (defglobal *power-cache
* (make-array 37 :initial-element nil
))
1002 (declaim (type (simple-vector 37) *power-cache
*))
1004 (defconstant +power-cache-integer-length-limit
+ 2048)
1006 (defun scrub-power-cache (&aux
(cache *power-cache
*))
1007 (dotimes (i (length cache
))
1008 (let ((powers (aref cache i
)))
1010 (let ((too-big (position-if
1012 (>= (integer-length x
)
1013 +power-cache-integer-length-limit
+))
1014 (the simple-vector powers
))))
1016 (setf (aref cache i
) (subseq powers
0 too-big
))))))))
1018 ;;; Compute (and cache) a power vector for a BASE and LIMIT:
1019 ;;; the vector holds integers for which
1020 ;;; (aref powers k) == (expt base (expt 2 k))
1022 (defun powers-for-base (base limit
)
1023 (flet ((compute-powers (from)
1025 (do ((p from
(* p p
)))
1027 ;; We don't actually need this, but we also
1028 ;; prefer not to cons it up a second time...
1031 (nreverse powers
))))
1032 (let* ((cache *power-cache
*)
1033 (powers (aref cache base
)))
1034 (setf (aref cache base
)
1035 (concatenate 'vector powers
1038 (let* ((len (length powers
))
1039 (max (svref powers
(1- len
))))
1041 (return-from powers-for-base powers
)
1045 ;; Algorithm by Harald Hanche-Olsen, sbcl-devel 2005-02-05
1046 (defun %output-huge-integer-in-base
(n base stream
)
1047 (declare (type bignum n
) (type fixnum base
))
1048 ;; POWER is a vector for which the following holds:
1049 ;; (aref power k) == (expt base (expt 2 k))
1050 (let* ((power (powers-for-base base n
))
1051 (k-start (or (position-if (lambda (x) (> x n
)) power
)
1052 (bug "power-vector too short"))))
1053 (labels ((bisect (n k exactp
)
1054 (declare (fixnum k
))
1055 ;; N is the number to bisect
1056 ;; K on initial entry BASE^(2^K) > N
1057 ;; EXACTP is true if 2^K is the exact number of digits
1060 (loop repeat
(ash 1 k
) do
(write-char #\
0 stream
))))
1063 (schar "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" n
)
1067 (multiple-value-bind (q r
) (truncate n
(aref power k
))
1068 ;; EXACTP is NIL only at the head of the
1069 ;; initial number, as we don't know the number
1070 ;; of digits there, but we do know that it
1071 ;; doesn't get any leading zeros.
1073 (bisect r k
(or exactp
(plusp q
))))))))
1074 (bisect n k-start nil
))))
1076 (defun %output-integer-in-base
(integer base stream
)
1077 (when (minusp integer
)
1078 (write-char #\- stream
)
1079 (setf integer
(- integer
)))
1080 ;; The ideal cutoff point between these two algorithms is almost
1081 ;; certainly quite platform dependent: this gives 87 for 32 bit
1082 ;; SBCL, which is about right at least for x86/Darwin.
1083 (if (or (fixnump integer
)
1084 (< (integer-length integer
) (* 3 sb
!vm
:n-positive-fixnum-bits
)))
1085 (%output-reasonable-integer-in-base integer base stream
)
1086 (%output-huge-integer-in-base integer base stream
)))
1088 ;;; This gets both a method and a specifically named function
1089 ;;; since the latter is called from a few places.
1090 (defmethod print-object ((object integer
) stream
)
1091 (output-integer object stream
*print-base
* *print-radix
*))
1092 (defun output-integer (integer stream base radixp
)
1094 (unless (= base
10) (%output-radix base stream
))
1095 (%output-integer-in-base integer base stream
)
1096 (when (= base
10) (write-char #\. stream
)))
1098 (%output-integer-in-base integer base stream
))))
1100 (defmethod print-object ((ratio ratio
) stream
)
1101 (let ((base *print-base
*))
1103 (%output-radix base stream
))
1104 (%output-integer-in-base
(numerator ratio
) base stream
)
1105 (write-char #\
/ stream
)
1106 (%output-integer-in-base
(denominator ratio
) base stream
)))
1108 (defmethod print-object ((complex complex
) stream
)
1109 (write-string "#C(" stream
)
1110 (output-object (realpart complex
) stream
)
1111 (write-char #\space stream
)
1112 (output-object (imagpart complex
) stream
)
1113 (write-char #\
) stream
))
1117 ;;; FLONUM-TO-STRING (and its subsidiary function FLOAT-STRING) does
1118 ;;; most of the work for all printing of floating point numbers in
1119 ;;; FORMAT. It converts a floating point number to a string in a free
1120 ;;; or fixed format with no exponent. The interpretation of the
1121 ;;; arguments is as follows:
1123 ;;; X - The floating point number to convert, which must not be
1125 ;;; WIDTH - The preferred field width, used to determine the number
1126 ;;; of fraction digits to produce if the FDIGITS parameter
1127 ;;; is unspecified or NIL. If the non-fraction digits and the
1128 ;;; decimal point alone exceed this width, no fraction digits
1129 ;;; will be produced unless a non-NIL value of FDIGITS has been
1130 ;;; specified. Field overflow is not considerd an error at this
1132 ;;; FDIGITS - The number of fractional digits to produce. Insignificant
1133 ;;; trailing zeroes may be introduced as needed. May be
1134 ;;; unspecified or NIL, in which case as many digits as possible
1135 ;;; are generated, subject to the constraint that there are no
1136 ;;; trailing zeroes.
1137 ;;; SCALE - If this parameter is specified or non-NIL, then the number
1138 ;;; printed is (* x (expt 10 scale)). This scaling is exact,
1139 ;;; and cannot lose precision.
1140 ;;; FMIN - This parameter, if specified or non-NIL, is the minimum
1141 ;;; number of fraction digits which will be produced, regardless
1142 ;;; of the value of WIDTH or FDIGITS. This feature is used by
1143 ;;; the ~E format directive to prevent complete loss of
1144 ;;; significance in the printed value due to a bogus choice of
1148 ;;; (VALUES DIGIT-STRING DIGIT-LENGTH LEADING-POINT TRAILING-POINT DECPNT)
1149 ;;; where the results have the following interpretation:
1151 ;;; DIGIT-STRING - The decimal representation of X, with decimal point.
1152 ;;; DIGIT-LENGTH - The length of the string DIGIT-STRING.
1153 ;;; LEADING-POINT - True if the first character of DIGIT-STRING is the
1155 ;;; TRAILING-POINT - True if the last character of DIGIT-STRING is the
1157 ;;; POINT-POS - The position of the digit preceding the decimal
1158 ;;; point. Zero indicates point before first digit.
1160 ;;; NOTE: FLONUM-TO-STRING goes to a lot of trouble to guarantee
1161 ;;; accuracy. Specifically, the decimal number printed is the closest
1162 ;;; possible approximation to the true value of the binary number to
1163 ;;; be printed from among all decimal representations with the same
1164 ;;; number of digits. In free-format output, i.e. with the number of
1165 ;;; digits unconstrained, it is guaranteed that all the information is
1166 ;;; preserved, so that a properly- rounding reader can reconstruct the
1167 ;;; original binary number, bit-for-bit, from its printed decimal
1168 ;;; representation. Furthermore, only as many digits as necessary to
1169 ;;; satisfy this condition will be printed.
1171 ;;; FLOAT-DIGITS actually generates the digits for positive numbers;
1172 ;;; see below for comments.
1174 (defun flonum-to-string (x &optional width fdigits scale fmin
)
1175 (declare (type float x
))
1176 ;; FIXME: I think only FORMAT-DOLLARS calls FLONUM-TO-STRING with
1177 ;; possibly-negative X.
1179 (multiple-value-bind (e string
)
1181 (flonum-to-digits x
(min (- (+ fdigits
(or scale
0)))
1183 (if (and width
(> width
1))
1184 (let ((w (multiple-value-list
1188 (if (and scale
(minusp scale
))
1191 (f (multiple-value-list
1192 (flonum-to-digits x
(- (+ (or fmin
0)
1193 (if scale scale
0)))))))
1195 ((>= (length (cadr w
)) (length (cadr f
)))
1197 (t (values-list f
))))
1198 (flonum-to-digits x
)))
1199 (let ((e (if (zerop x
)
1201 (+ e
(or scale
0))))
1202 (stream (make-string-output-stream)))
1205 (write-string string stream
:end
(min (length string
) e
))
1206 (dotimes (i (- e
(length string
)))
1207 (write-char #\
0 stream
))
1208 (write-char #\. stream
)
1209 (write-string string stream
:start
(min (length string
) e
))
1211 (dotimes (i (- fdigits
1213 (min (length string
) e
))))
1214 (write-char #\
0 stream
))))
1216 (write-string "." stream
)
1218 (write-char #\
0 stream
))
1219 (write-string string stream
:end
(when fdigits
1220 (min (length string
)
1224 (dotimes (i (+ fdigits e
(- (length string
))))
1225 (write-char #\
0 stream
)))))
1226 (let ((string (get-output-stream-string stream
)))
1227 (values string
(length string
)
1228 (char= (char string
0) #\.
)
1229 (char= (char string
(1- (length string
))) #\.
)
1230 (position #\. string
))))))
1232 ;;; implementation of figure 1 from Burger and Dybvig, 1996. It is
1233 ;;; extended in order to handle rounding.
1235 ;;; As the implementation of the Dragon from Classic CMUCL (and
1236 ;;; previously in SBCL above FLONUM-TO-STRING) says: "DO NOT EVEN
1237 ;;; THINK OF ATTEMPTING TO UNDERSTAND THIS CODE WITHOUT READING THE
1238 ;;; PAPER!", and in this case we have to add that even reading the
1239 ;;; paper might not bring immediate illumination as CSR has attempted
1240 ;;; to turn idiomatic Scheme into idiomatic Lisp.
1242 ;;; FIXME: figure 1 from Burger and Dybvig is the unoptimized
1243 ;;; algorithm, noticeably slow at finding the exponent. Figure 2 has
1244 ;;; an improved algorithm, but CSR ran out of energy.
1246 ;;; possible extension for the enthusiastic: printing floats in bases
1247 ;;; other than base 10.
1248 (defconstant single-float-min-e
1249 (- 2 sb
!vm
:single-float-bias sb
!vm
:single-float-digits
))
1250 (defconstant double-float-min-e
1251 (- 2 sb
!vm
:double-float-bias sb
!vm
:double-float-digits
))
1253 (defconstant long-float-min-e
1254 (nth-value 1 (decode-float least-positive-long-float
)))
1256 ;;; Call CHAR-FUN with the digits of FLOAT
1257 ;;; PROLOGUE-FUN and EPILOGUE-FUN are called with the exponent before
1258 ;;; and after printing to set up the state.
1259 (declaim (inline %flonum-to-digits
))
1260 (defun %flonum-to-digits
(char-fun
1263 float
&optional position relativep
)
1264 (let ((print-base 10) ; B
1266 (float-digits (float-digits float
)) ; p
1269 (single-float single-float-min-e
)
1270 (double-float double-float-min-e
)
1272 (long-float long-float-min-e
))))
1273 (multiple-value-bind (f e
)
1274 (integer-decode-float float
)
1275 (let ( ;; FIXME: these even tests assume normal IEEE rounding
1276 ;; mode. I wonder if we should cater for non-normal?
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 (funcall prologue-fun k
)
1295 (generate r s m
+ m-
)
1296 (funcall epilogue-fun k
))))))
1297 (generate (r s m
+ m-
)
1301 (setf (values d r
) (truncate (* r print-base
) s
))
1302 (setf m
+ (* m
+ print-base
))
1303 (setf m-
(* m- print-base
))
1304 (setf tc1
(or (< r m-
) (and low-ok
(= r m-
))))
1305 (setf tc2
(let ((r+m
+ (+ r m
+)))
1307 (and high-ok
(= r
+m
+ s
)))))
1310 (funcall char-fun d
)
1314 ((and (not tc1
) tc2
) (1+ d
))
1315 ((and tc1
(not tc2
)) d
)
1320 (funcall char-fun d
)))))
1324 (let ((be (expt float-radix e
)))
1325 (if (/= f
(expt float-radix
(1- float-digits
)))
1326 ;; multiply F by 2 first, avoding consing two bignums
1332 m
+ (* be float-radix
)
1334 s
(* float-radix
2)))))
1336 (/= f
(expt float-radix
(1- float-digits
))))
1338 s
(expt float-radix
(- 1 e
))
1342 (setf r
(* f float-radix
2)
1343 s
(expt float-radix
(- 2 e
))
1348 (aver (> position
0))
1350 ;; running out of letters here
1351 (l 1 (* l print-base
)))
1352 ((>= (* s l
) (+ r m
+))
1354 (if (< (+ r
(* s
(/ (expt print-base
(- k position
)) 2)))
1356 (setf position
(- k position
))
1357 (setf position
(- k position
1))))))
1358 (let* ((x (/ (* s
(expt print-base position
)) 2))
1367 (values r s m
+ m-
))))
1368 (multiple-value-bind (r s m
+ m-
) (initialize)
1369 (scale r s m
+ m-
)))))))
1371 (defun flonum-to-digits (float &optional position relativep
)
1372 (let ((digit-characters "0123456789"))
1373 (with-push-char (:element-type base-char
)
1376 (push-char (char digit-characters d
)))
1378 (lambda (k) (values k
(get-pushed-string)))
1379 float position relativep
))))
1381 (defun print-float (float stream
)
1384 (digit-characters "0123456789")
1389 (when (= position dot-position
)
1390 (write-char #\. stream
))
1391 (write-char (char digit-characters d
) stream
)
1394 (cond ((not (< e-min k e-max
))
1395 (setf dot-position
1))
1397 (setf dot-position k
))
1399 (setf dot-position -
1)
1400 (write-char #\
0 stream
)
1401 (write-char #\. stream
)
1402 (loop for i below
(- k
)
1403 do
(write-char #\
0 stream
)))))
1405 (when (<= position dot-position
)
1406 (loop for i below
(- dot-position position
)
1407 do
(write-char #\
0 stream
))
1408 (write-char #\. stream
)
1409 (write-char #\
0 stream
))
1410 (if (< e-min k e-max
)
1411 (print-float-exponent float
0 stream
)
1412 (print-float-exponent float
(1- k
) stream
)))
1415 ;;; Given a non-negative floating point number, SCALE-EXPONENT returns
1416 ;;; a new floating point number Z in the range (0.1, 1.0] and an
1417 ;;; exponent E such that Z * 10^E is (approximately) equal to the
1418 ;;; original number. There may be some loss of precision due the
1419 ;;; floating point representation. The scaling is always done with
1420 ;;; long float arithmetic, which helps printing of lesser precisions
1421 ;;; as well as avoiding generic arithmetic.
1423 ;;; When computing our initial scale factor using EXPT, we pull out
1424 ;;; part of the computation to avoid over/under flow. When
1425 ;;; denormalized, we must pull out a large factor, since there is more
1426 ;;; negative exponent range than positive range.
1428 (eval-when (:compile-toplevel
:execute
)
1429 (setf *read-default-float-format
*
1430 #!+long-float
'long-float
#!-long-float
'double-float
))
1431 (defun scale-exponent (original-x)
1432 (let* ((x (coerce original-x
'long-float
)))
1433 (multiple-value-bind (sig exponent
) (decode-float x
)
1434 (declare (ignore sig
))
1436 (values (float 0.0e0 original-x
) 1)
1437 (let* ((ex (locally (declare (optimize (safety 0)))
1440 ;; this is the closest double float
1441 ;; to (log 2 10), but expressed so
1442 ;; that we're not vulnerable to the
1443 ;; host lisp's interpretation of
1444 ;; arithmetic. (FIXME: it turns
1445 ;; out that sbcl itself is off by 1
1446 ;; ulp in this value, which is a
1447 ;; little unfortunate.)
1450 (make-double-float 1070810131 1352628735)
1452 (error "(log 2 10) not computed")))))))
1454 (if (float-denormalized-p x
)
1456 (* x
1.0e16
(expt 10.0e0
(- (- ex
) 16)))
1458 (* x
1.0e18
(expt 10.0e0
(- (- ex
) 18)))
1459 (* x
10.0e0
(expt 10.0e0
(- (- ex
) 1))))
1460 (/ x
10.0e0
(expt 10.0e0
(1- ex
))))))
1461 (do ((d 10.0e0
(* d
10.0e0
))
1465 (do ((m 10.0e0
(* m
10.0e0
))
1469 (values (float z original-x
) ex
))
1470 (declare (long-float m
) (integer ex
))))
1471 (declare (long-float d
))))))))
1472 (eval-when (:compile-toplevel
:execute
)
1473 (setf *read-default-float-format
* 'single-float
))
1475 ;;;; entry point for the float printer
1477 ;;; the float printer as called by PRINT, PRIN1, PRINC, etc. The
1478 ;;; argument is printed free-format, in either exponential or
1479 ;;; non-exponential notation, depending on its magnitude.
1481 ;;; NOTE: When a number is to be printed in exponential format, it is
1482 ;;; scaled in floating point. Since precision may be lost in this
1483 ;;; process, the guaranteed accuracy properties of FLONUM-TO-STRING
1484 ;;; are lost. The difficulty is that FLONUM-TO-STRING performs
1485 ;;; extensive computations with integers of similar magnitude to that
1486 ;;; of the number being printed. For large exponents, the bignums
1487 ;;; really get out of hand. If bignum arithmetic becomes reasonably
1488 ;;; fast and the exponent range is not too large, then it might become
1489 ;;; attractive to handle exponential notation with the same accuracy
1490 ;;; as non-exponential notation, using the method described in the
1491 ;;; Steele and White paper.
1493 ;;; NOTE II: this has been bypassed slightly by implementing Burger
1494 ;;; and Dybvig, 1996. When someone has time (KLUDGE) they can
1495 ;;; probably (a) implement the optimizations suggested by Burger and
1496 ;;; Dyvbig, and (b) remove all vestiges of Dragon4, including from
1497 ;;; fixed-format printing.
1499 ;;; Print the appropriate exponent marker for X and the specified exponent.
1500 (defun print-float-exponent (x exp stream
)
1501 (declare (type float x
) (type integer exp
) (type stream stream
))
1502 (cond ((case *read-default-float-format
*
1503 ((short-float single-float
)
1504 (typep x
'single-float
))
1505 ((double-float #!-long-float long-float
)
1506 (typep x
'double-float
))
1509 (typep x
'long-float
)))
1511 (write-char #\e stream
)
1512 (%output-integer-in-base exp
10 stream
)))
1521 (%output-integer-in-base exp
10 stream
))))
1523 (defun output-float-infinity (x stream
)
1524 (declare (float x
) (stream stream
))
1526 (write-string "#." stream
))
1528 (return-from output-float-infinity
1529 (print-not-readable-error x stream
)))
1531 (write-string "#<" stream
)))
1532 (write-string "SB-EXT:" stream
)
1533 (write-string (symbol-name (float-format-name x
)) stream
)
1534 (write-string (if (plusp x
) "-POSITIVE-" "-NEGATIVE-")
1536 (write-string "INFINITY" stream
)
1538 (write-string ">" stream
)))
1540 (defun output-float-nan (x stream
)
1541 (print-unreadable-object (x stream
)
1542 (princ (float-format-name x
) stream
)
1543 (write-string (if (float-trapping-nan-p x
) " trapping" " quiet") stream
)
1544 (write-string " NaN" stream
)))
1546 (declaim (inline output-float
))
1547 (defun output-float (x stream
)
1549 ((float-infinity-p x
)
1550 (output-float-infinity x stream
))
1552 (output-float-nan x stream
))
1554 (let ((x (cond ((minusp (float-sign x
))
1555 (write-char #\- stream
)
1561 (write-string "0.0" stream
)
1562 (print-float-exponent x
0 stream
))
1564 (print-float x stream
)))))))
1566 ;;; the function called by OUTPUT-OBJECT to handle floats
1567 (defmethod print-object ((x single-float
) stream
)
1568 (output-float x stream
))
1570 (defmethod print-object ((x double-float
) stream
)
1571 (output-float x stream
))
1574 ;;;; other leaf objects
1576 ;;; If *PRINT-ESCAPE* is false, just do a WRITE-CHAR, otherwise output
1577 ;;; the character name or the character in the #\char format.
1578 (defmethod print-object ((char character
) stream
)
1579 (if (or *print-escape
* *print-readably
*)
1580 (let ((graphicp (and (graphic-char-p char
)
1581 (standard-char-p char
)))
1582 (name (char-name char
)))
1583 (write-string "#\\" stream
)
1584 (if (and name
(or (not graphicp
) *print-readably
*))
1585 (quote-string name stream
)
1586 (write-char char stream
)))
1587 (write-char char stream
)))
1589 (defmethod print-object ((sap system-area-pointer
) stream
)
1591 (format stream
"#.(~S #X~8,'0X)" 'int-sap
(sap-int sap
)))
1593 (print-unreadable-object (sap stream
)
1594 (format stream
"system area pointer: #X~8,'0X" (sap-int sap
))))))
1596 (defmethod print-object ((weak-pointer weak-pointer
) stream
)
1597 (print-unreadable-object (weak-pointer stream
)
1598 (multiple-value-bind (value validp
) (weak-pointer-value weak-pointer
)
1600 (write-string "weak pointer: " stream
)
1601 (write value
:stream stream
))
1603 (write-string "broken weak pointer" stream
))))))
1605 (defmethod print-object ((component code-component
) stream
)
1606 (print-unreadable-object (component stream
:identity t
)
1607 (let ((dinfo (%code-debug-info component
)))
1608 (cond ((eq dinfo
:bogus-lra
)
1609 (write-string "bogus code object" stream
))
1611 (format stream
"code object [~D]" (code-n-entries component
))
1612 (let ((fun-name (awhen (%code-entry-point component
0)
1613 (%simple-fun-name it
))))
1615 (write-char #\Space stream
)
1616 (write fun-name
:stream stream
))
1617 (cond ((not (typep dinfo
'sb
!c
::debug-info
)))
1618 ((neq (sb!c
::debug-info-name dinfo
) fun-name
)
1619 (write-string ", " stream
)
1620 (output-object (sb!c
::debug-info-name dinfo
) stream
)))))))))
1623 (defmethod print-object ((lra lra
) stream
)
1624 (print-unreadable-object (lra stream
:identity t
)
1625 (write-string "return PC object" stream
)))
1627 (defmethod print-object ((fdefn fdefn
) stream
)
1628 (print-unreadable-object (fdefn stream
:type t
)
1629 ;; As fdefn names are particularly relevant to those hacking on the compiler
1630 ;; and disassembler, be maximally helpful by neither abbreviating (SETF ...)
1631 ;; due to length cutoff, nor failing to print a package if needed.
1632 ;; Some folks seem to love same-named symbols way too much.
1633 (let ((*print-length
* 20)) ; arbitrary
1634 (prin1 (fdefn-name fdefn
) stream
))))
1637 (defmethod print-object ((pack simd-pack
) stream
)
1638 (cond ((and *print-readably
* *read-eval
*)
1639 (multiple-value-bind (format maker extractor
)
1641 ((simd-pack double-float
)
1642 (values "#.(~S ~S ~S)"
1643 '%make-simd-pack-double
#'%simd-pack-doubles
))
1644 ((simd-pack single-float
)
1645 (values "#.(~S ~S ~S ~S ~S)"
1646 '%make-simd-pack-single
#'%simd-pack-singles
))
1648 (values "#.(~S #X~16,'0X #X~16,'0X)"
1649 '%make-simd-pack-ub64
#'%simd-pack-ub64s
)))
1650 (multiple-value-call
1651 #'format stream format maker
(funcall extractor pack
))))
1653 (print-unreadable-object (pack stream
)
1654 (flet ((all-ones-p (value start end
&aux
(mask (- (ash 1 end
) (ash 1 start
))))
1655 (= (logand value mask
) mask
))
1656 (split-num (value start
)
1659 and v
= (ash value
(- start
)) then
(ash v -
8)
1660 collect
(logand v
#xFF
))))
1661 (multiple-value-bind (low high
)
1662 (%simd-pack-ub64s pack
)
1664 ((simd-pack double-float
)
1665 (multiple-value-bind (v0 v1
) (%simd-pack-doubles pack
)
1666 (format stream
"~S~@{ ~:[~,13E~;~*TRUE~]~}"
1668 (all-ones-p low
0 64) v0
1669 (all-ones-p high
0 64) v1
)))
1670 ((simd-pack single-float
)
1671 (multiple-value-bind (v0 v1 v2 v3
) (%simd-pack-singles pack
)
1672 (format stream
"~S~@{ ~:[~,7E~;~*TRUE~]~}"
1674 (all-ones-p low
0 32) v0
1675 (all-ones-p low
32 64) v1
1676 (all-ones-p high
0 32) v2
1677 (all-ones-p high
32 64) v3
)))
1679 (format stream
"~S~@{ ~{ ~2,'0X~}~}"
1681 (split-num low
0) (split-num low
32)
1682 (split-num high
0) (split-num high
32))))))))))
1686 (defmethod print-object ((object function
) stream
)
1687 (let* ((name (%fun-name object
))
1688 (proper-name-p (and (legal-fun-name-p name
) (fboundp name
)
1689 (eq (fdefinition name
) object
))))
1690 ;; ":TYPE T" is no good, since CLOSURE doesn't have full-fledged status.
1691 (print-unreadable-object (object stream
:identity
(not proper-name-p
))
1692 (format stream
"~A~@[ ~S~]"
1693 ;; TYPE-OF is so that GFs print as #<STANDARD-GENERIC-FUNCTION>
1694 ;; and not #<FUNCTION> before SRC;PCL;PRINT-OBJECT is loaded.
1695 (if (closurep object
) 'closure
(type-of object
))
1698 ;;;; catch-all for unknown things
1700 (defmethod print-object ((object t
) stream
)
1701 (flet ((output-it (stream)
1702 (when (eq object sb
!pcl
:+slot-unbound
+)
1703 (print-unreadable-object (object stream
)
1704 ;; If specifically the unbound marker with 0 data,
1705 ;; as opposed to any other unbound marker.
1706 (write-string "unbound" stream
))
1707 (return-from output-it
))
1708 (print-unreadable-object (object stream
:identity t
)
1709 (let ((lowtag (lowtag-of object
)))
1711 (#.sb
!vm
:other-pointer-lowtag
1712 (let ((widetag (widetag-of object
)))
1714 (#.sb
!vm
:value-cell-widetag
1715 (write-string "value cell " stream
)
1716 (output-object (value-cell-ref object
) stream
))
1718 (write-string "unknown pointer object, widetag=" stream
)
1719 (output-integer widetag stream
16 t
)))))
1720 ((#.sb
!vm
:fun-pointer-lowtag
1721 #.sb
!vm
:instance-pointer-lowtag
1722 #.sb
!vm
:list-pointer-lowtag
)
1723 (write-string "unknown pointer object, lowtag=" stream
)
1724 (output-integer lowtag stream
16 t
))
1726 (case (widetag-of object
)
1727 (#.sb
!vm
:unbound-marker-widetag
1728 (write-string "unbound marker" stream
))
1730 (write-string "unknown immediate object, lowtag=" stream
)
1731 (output-integer lowtag stream
2 t
)
1732 (write-string ", widetag=" stream
)
1733 (output-integer (widetag-of object
) stream
16 t
)))))))))
1735 ;; This block might not be necessary. Not sure, probably can't hurt.
1736 (pprint-logical-block (stream nil
) (output-it stream
))
1737 (output-it stream
))))