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
18 "If true, all objects will be printed readably. If readable printing
19 is impossible, an error will be signalled. This overrides the value of
21 (!defvar
*print-escape
* t
23 "Should we print in a reasonably machine-readable way? (possibly
24 overridden by *PRINT-READABLY*)")
25 (!defvar
*print-pretty
* nil
; (set later when pretty-printer is initialized)
27 "Should pretty printing be used?")
28 (!defvar
*print-base
* 10.
30 "The output base for RATIONALs (including integers).")
31 (!defvar
*print-radix
* nil
33 "Should base be verified when printing RATIONALs?")
34 (!defvar
*print-level
* nil
36 "How many levels should be printed before abbreviating with \"#\"?")
37 (!defvar
*print-length
* nil
39 "How many elements at any level should be printed before abbreviating
41 (!defvar
*print-circle
* nil
43 "Should we use #n= and #n# notation to preserve uniqueness in general (and
44 circularity in particular) when printing?")
45 (!defvar
*print-case
* :upcase
47 "What case should the printer should use default?")
48 (!defvar
*print-array
* t
50 "Should the contents of arrays be printed?")
51 (!defvar
*print-gensym
* t
53 "Should #: prefixes be used when printing symbols with null SYMBOL-PACKAGE?")
54 (!defvar
*print-lines
* nil
56 "The maximum number of lines to print per object.")
57 (!defvar
*print-right-margin
* nil
59 "The position of the right margin in ems (for pretty-printing).")
60 (!defvar
*print-miser-width
* nil
62 "If the remaining space between the current column and the right margin
63 is less than this, then print using ``miser-style'' output. Miser
64 style conditional newlines are turned on, and all indentations are
65 turned off. If NIL, never use miser mode.")
66 (defvar *print-pprint-dispatch
*
67 (sb!pretty
::make-pprint-dispatch-table
) ; for type-correctness
69 "The pprint-dispatch-table that controls how to pretty-print objects.")
70 (!defvar
*suppress-print-errors
* nil
72 "Suppress printer errors when the condition is of the type designated by this
73 variable: an unreadable object representing the error is printed instead.")
75 ;; duplicate defglobal because this file is compiled before "reader"
76 (defglobal *standard-readtable
* nil
)
78 (defun %with-standard-io-syntax
(function)
79 (declare (type function function
))
80 (let ((*package
* (find-package "COMMON-LISP-USER"))
83 (*print-case
* :upcase
)
90 (*print-miser-width
* nil
)
91 (*print-pprint-dispatch
* sb
!pretty
::*standard-pprint-dispatch-table
*)
95 (*print-right-margin
* nil
)
97 (*read-default-float-format
* 'single-float
)
100 (*readtable
* *standard-readtable
*)
101 (*suppress-print-errors
* nil
))
104 ;;;; routines to print objects
106 (macrolet ((def (fn doc
&rest forms
)
107 (declare (ignorable doc
))
111 ,@(if (eq fn
'write
) '(stream))
112 ((:escape
*print-escape
*) *print-escape
*)
113 ((:radix
*print-radix
*) *print-radix
*)
114 ((:base
*print-base
*) *print-base
*)
115 ((:circle
*print-circle
*) *print-circle
*)
116 ((:pretty
*print-pretty
*) *print-pretty
*)
117 ((:level
*print-level
*) *print-level
*)
118 ((:length
*print-length
*) *print-length
*)
119 ((:case
*print-case
*) *print-case
*)
120 ((:array
*print-array
*) *print-array
*)
121 ((:gensym
*print-gensym
*) *print-gensym
*)
122 ((:readably
*print-readably
*) *print-readably
*)
123 ((:right-margin
*print-right-margin
*)
124 *print-right-margin
*)
125 ((:miser-width
*print-miser-width
*)
127 ((:lines
*print-lines
*) *print-lines
*)
128 ((:pprint-dispatch
*print-pprint-dispatch
*)
129 *print-pprint-dispatch
*)
130 ((:suppress-errors
*suppress-print-errors
*)
131 *suppress-print-errors
*))
133 (declare (explicit-check))
136 "Output OBJECT to the specified stream, defaulting to *STANDARD-OUTPUT*."
137 (output-object object
(out-stream-from-designator stream
))
140 "Return the printed representation of OBJECT as a string."
141 (stringify-object object
)))
143 ;;; Same as a call to (WRITE OBJECT :STREAM STREAM), but returning OBJECT.
144 (defun %write
(object stream
)
145 (declare (explicit-check))
146 (output-object object
(out-stream-from-designator stream
))
149 (defun prin1 (object &optional stream
)
151 "Output a mostly READable printed representation of OBJECT on the specified
153 (declare (explicit-check))
154 (let ((*print-escape
* t
))
155 (output-object object
(out-stream-from-designator stream
)))
158 (defun princ (object &optional stream
)
160 "Output an aesthetic but not necessarily READable printed representation
161 of OBJECT on the specified STREAM."
162 (declare (explicit-check))
163 (let ((*print-escape
* nil
)
164 (*print-readably
* nil
))
165 (output-object object
(out-stream-from-designator stream
)))
168 (defun print (object &optional stream
)
170 "Output a newline, the mostly READable printed representation of OBJECT, and
171 space to the specified STREAM."
172 (declare (explicit-check))
173 (let ((stream (out-stream-from-designator stream
)))
175 (prin1 object stream
)
176 (write-char #\space stream
)
179 (defun pprint (object &optional stream
)
181 "Prettily output OBJECT preceded by a newline."
182 (declare (explicit-check))
183 (let ((*print-pretty
* t
)
185 (stream (out-stream-from-designator stream
)))
187 (output-object object stream
))
190 (defun prin1-to-string (object)
192 "Return the printed representation of OBJECT as a string with
194 (let ((*print-escape
* t
))
195 (stringify-object object
)))
197 (defun princ-to-string (object)
199 "Return the printed representation of OBJECT as a string with
201 (let ((*print-escape
* nil
)
202 (*print-readably
* nil
))
203 (stringify-object object
)))
205 ;;; This produces the printed representation of an object as a string.
206 ;;; The few ...-TO-STRING functions above call this.
207 (defun stringify-object (object)
208 (with-simple-output-to-string (stream)
209 (output-object object stream
)))
211 ;;;; support for the PRINT-UNREADABLE-OBJECT macro
213 (defun print-not-readable-error (object stream
)
215 (error 'print-not-readable
:object object
)
217 :report
"Print unreadably."
218 (let ((*print-readably
* nil
))
219 (output-object object stream
)
222 :report
"Supply an object to be printed instead."
225 (read-evaluated-form "~@<Enter an object (evaluated): ~@:>"))
226 (output-object o stream
)
229 ;;; guts of PRINT-UNREADABLE-OBJECT
230 (defun %print-unreadable-object
(object stream type identity
&optional body
)
231 (declare (type (or null function
) body
))
233 (print-not-readable-error object stream
)
234 (flet ((print-description ()
236 (write (type-of object
) :stream stream
:circle nil
237 :level nil
:length nil
)
238 ;; Do NOT insert a pprint-newline here.
239 ;; See ba34717602d80e5fd74d10e61f4729fb0d019a0c
240 (write-char #\space stream
))
244 (when (or body
(not type
))
245 (write-char #\space stream
))
247 (write-char #\
{ stream
)
248 (write (get-lisp-obj-address object
) :stream stream
250 (write-char #\
} stream
))))
251 (cond ((print-pretty-on-stream-p stream
)
252 ;; Since we're printing prettily on STREAM, format the
253 ;; object within a logical block. PPRINT-LOGICAL-BLOCK does
254 ;; not rebind the stream when it is already a pretty stream,
255 ;; so output from the body will go to the same stream.
256 (pprint-logical-block (stream nil
:prefix
"#<" :suffix
">")
257 (print-description)))
259 (write-string "#<" stream
)
261 (write-char #\
> stream
)))))
264 ;;;; OUTPUT-OBJECT -- the main entry point
266 ;;; Objects whose print representation identifies them EQLly don't
267 ;;; need to be checked for circularity.
268 (defun uniquely-identified-by-print-p (x)
272 (symbol-package x
))))
274 (defvar *in-print-error
* nil
)
276 ;;; Output OBJECT to STREAM observing all printer control variables.
277 (defun output-object (object stream
)
278 ;; FIXME: this function is declared EXPLICIT-CHECK, so it allows STREAM
279 ;; to be T or NIL (a stream-designator), which is not really right
280 ;; if eventually the call will be to a PRINT-OBJECT method,
281 ;; since the generic function should always receive a stream.
282 (declare (explicit-check))
283 (labels ((print-it (stream)
284 (multiple-value-bind (fun pretty
)
285 (and *print-pretty
* (pprint-dispatch object
))
287 (sb!pretty
::with-pretty-stream
(stream)
288 (funcall fun stream object
))
289 (output-ugly-object stream object
))))
291 (if *suppress-print-errors
*
292 (handler-bind ((condition
293 (lambda (condition) nil
294 (when (typep condition
*suppress-print-errors
*)
295 (cond (*in-print-error
*
296 (write-string "(error printing " stream
)
297 (write-string *in-print-error
* stream
)
298 (write-string ")" stream
))
300 ;; Give outer handlers a chance.
302 (continue "Suppress the error.")
304 (let ((*print-readably
* nil
)
307 "#<error printing a " stream
)
308 (let ((*in-print-error
* "type"))
309 (output-object (type-of object
) stream
))
310 (write-string ": " stream
)
311 (let ((*in-print-error
* "condition"))
312 (output-object condition stream
))
313 (write-string ">" stream
))))
314 (return-from handle-it object
)))))
318 (multiple-value-bind (marker initiate
)
319 (check-for-circularity object t
)
320 (if (eq initiate
:initiate
)
321 (let ((*circularity-hash-table
*
322 (make-hash-table :test
'eq
)))
323 (check-it (make-broadcast-stream))
324 (let ((*circularity-counter
* 0))
328 (when (handle-circularity marker stream
)
330 (handle-it stream
))))))
331 (cond (;; Maybe we don't need to bother with circularity detection.
332 (or (not *print-circle
*)
333 (uniquely-identified-by-print-p object
))
335 (;; If we have already started circularity detection, this
336 ;; object might be a shared reference. If we have not, then
337 ;; if it is a compound object it might contain a circular
338 ;; reference to itself or multiple shared references.
339 (or *circularity-hash-table
*
340 (compound-object-p object
))
343 (handle-it stream
)))))
345 ;;; Output OBJECT to STREAM observing all printer control variables
346 ;;; except for *PRINT-PRETTY*. Note: if *PRINT-PRETTY* is non-NIL,
347 ;;; then the pretty printer will be used for any components of OBJECT,
348 ;;; just not for OBJECT itself.
349 (defun output-ugly-object (stream object
)
350 (when (%instancep object
)
351 (let* ((layout (layout-of object
))
352 (classoid (layout-classoid layout
)))
353 ;; If an instance has no layout, it has no PRINT-OBJECT method.
354 ;; Additionally, if the object is an obsolete CONDITION, don't crash.
355 ;; (There is no update-instance protocol for conditions)
356 (when (or (sb!kernel
::undefined-classoid-p classoid
)
357 (and (layout-invalid layout
) (condition-classoid-p classoid
)))
358 ;; not only is this unreadable, it's unprintable too.
359 (return-from output-ugly-object
360 (print-unreadable-object (object stream
:identity t
)
361 (format stream
"UNPRINTABLE instance of ~W" classoid
))))))
362 (print-object object stream
))
364 ;;; Note: Now that PRINT-OBJECT works right away, indirections could be removed.
365 ;;; i.e. do (DEFMETHOD PRINT-OBJECT ((X CONS) STREAM) (ACTUAL-PRINTER-GUTS))
366 ;;; But in a bootstrap situation you might wish to call a named printer directly.
367 (defmethod print-object ((object function
) stream
) (output-fun object stream
))
368 (defmethod print-object ((object symbol
) stream
) (output-symbol object stream
))
369 (defmethod print-object ((object cons
) stream
) (output-list object stream
))
370 (defmethod print-object ((object integer
) stream
) (output-integer object stream
))
371 (defmethod print-object ((object float
) stream
) (output-float object stream
))
372 (defmethod print-object ((object ratio
) stream
) (output-ratio object stream
))
373 (defmethod print-object ((object complex
) stream
) (output-complex object stream
))
374 (defmethod print-object ((object character
) stream
) (output-character object stream
))
375 (defmethod print-object ((object vector
) stream
) (output-vector object stream
))
376 (defmethod print-object ((object array
) stream
) (output-array object stream
))
377 (defmethod print-object ((object system-area-pointer
) stream
) (output-sap object stream
))
378 (defmethod print-object ((object weak-pointer
) stream
) (output-weak-pointer object stream
))
379 (defmethod print-object ((object code-component
) stream
) (output-code-component object stream
))
380 (defmethod print-object ((object fdefn
) stream
) (output-fdefn object stream
))
381 #!-
(or x86 x86-64
) (defmethod print-object ((object lra
) stream
) (output-lra object stream
))
385 (defun output-symbol (object stream
)
386 (declare (symbol object
))
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 (if (nth-value 1 (find-external-symbol name package
))
447 (write-char #\
: stream
)
448 (write-string "::" 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 (defun output-list (list 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 (defun output-unreadable-vector-readably (vector stream
)
828 (declare (vector vector
))
829 (write-string "#." stream
)
830 (write `(coerce ,(coerce vector
'(vector t
))
831 '(simple-array ,(array-element-type vector
) (*)))
834 (defun output-vector (vector stream
&aux
(readably *print-readably
*))
835 (declare (vector vector
))
836 (cond ((stringp vector
)
838 (and readably
(not (typep vector
'(vector character
))))))
839 (cond ((and coerce-p
(not *read-eval
*))
840 (print-not-readable-error vector stream
))
841 ((or *print-escape
* readably
)
843 ;; OUTPUT-UNREADABLE-VECTOR-READABLY would output each char
844 ;; in #\c syntax. In addition to wasting time coercing to a
845 ;; general vector, it's not nice looking.
846 (write-string "#.(" stream
)
847 (write 'coerce
:stream stream
) ; package-qualify / casify as needed
848 (write-char #\Space stream
))
849 (write-char #\" stream
)
850 (quote-string vector stream
)
851 (write-char #\" stream
)
853 (write-char #\Space stream
)
854 (write (cond #!+sb-unicode
855 ((base-string-p vector
)
858 `'(vector ,(array-element-type vector
)))) :stream stream
)
859 (write-char #\
) stream
)))
861 (write-string vector stream
)))))
862 ((not (or *print-array
* readably
))
863 (output-terse-array vector stream
))
864 ((bit-vector-p vector
)
865 (write-string "#*" stream
)
866 (dovector (bit vector
)
867 ;; (Don't use OUTPUT-OBJECT here, since this code
868 ;; has to work for all possible *PRINT-BASE* values.)
869 (write-char (if (zerop bit
) #\
0 #\
1) stream
)))
870 ((or (not readably
) (array-readably-printable-p vector
))
871 (descend-into (stream)
872 (write-string "#(" stream
)
873 (dotimes (i (length vector
))
875 (write-char #\space stream
))
876 (punt-print-if-too-long i stream
)
877 (output-object (aref vector i
) stream
))
878 (write-string ")" stream
)))
880 (output-unreadable-vector-readably vector stream
))
882 (print-not-readable-error vector stream
))))
884 ;;; This function outputs a string quoting characters sufficiently
885 ;;; so that someone can read it in again. Basically, put a slash in
886 ;;; front of an character satisfying NEEDS-SLASH-P.
887 (defun quote-string (string stream
)
888 (macrolet ((needs-slash-p (char)
889 ;; KLUDGE: We probably should look at the readtable, but just do
890 ;; this for now. [noted by anonymous long ago] -- WHN 19991130
891 `(or (char= ,char
#\\)
893 (with-array-data ((data string
) (start) (end)
894 :check-fill-pointer t
)
895 (do ((index start
(1+ index
)))
897 (let ((char (schar data index
)))
898 (when (needs-slash-p char
) (write-char #\\ stream
))
899 (write-char char stream
))))))
901 (defun array-readably-printable-p (array)
902 (and (eq (array-element-type array
) t
)
903 (let ((zero (position 0 (array-dimensions array
)))
904 (number (position 0 (array-dimensions array
)
905 :test
(complement #'eql
)
907 (or (null zero
) (null number
) (> zero number
)))))
909 ;;; Output the printed representation of any array in either the #< or #A
911 (defun output-array (array stream
)
912 (if (or *print-array
* *print-readably
*)
913 (output-array-guts array stream
)
914 (output-terse-array array stream
)))
916 ;;; Output the abbreviated #< form of an array.
917 (defun output-terse-array (array stream
)
918 (let ((*print-level
* nil
)
919 (*print-length
* nil
))
920 (print-unreadable-object (array stream
:type t
:identity t
))))
922 ;;; Convert an array into a list that can be used with MAKE-ARRAY's
923 ;;; :INITIAL-CONTENTS keyword argument.
924 (defun listify-array (array)
925 (with-array-data ((data array
) (start) (end))
926 (declare (ignore end
))
927 (labels ((listify (dimensions index
)
928 (if (null dimensions
)
930 (let* ((dimension (car dimensions
))
931 (dimensions (cdr dimensions
))
932 (count (reduce #'* dimensions
)))
933 (loop for i below dimension
934 collect
(listify dimensions index
)
935 do
(incf index count
))))))
936 (listify (array-dimensions array
) start
))))
938 (defun output-unreadable-array-readably (array stream
)
939 (write-string "#." stream
)
940 (write `(make-array ',(array-dimensions array
)
941 :element-type
',(array-element-type array
)
942 :initial-contents
',(listify-array array
))
945 ;;; Output the readable #A form of an array.
946 (defun output-array-guts (array stream
)
947 (cond ((or (not *print-readably
*)
948 (array-readably-printable-p array
))
949 (write-char #\
# stream
)
950 (let ((*print-base
* 10)
952 (output-integer (array-rank array
) stream
))
953 (write-char #\A stream
)
954 (with-array-data ((data array
) (start) (end))
955 (declare (ignore end
))
956 (sub-output-array-guts data
(array-dimensions array
) stream start
)))
958 (output-unreadable-array-readably array stream
))
960 (print-not-readable-error array stream
))))
962 (defun sub-output-array-guts (array dimensions stream index
)
963 (declare (type (simple-array * (*)) array
) (fixnum index
))
964 (cond ((null dimensions
)
965 (output-object (aref array index
) stream
))
967 (descend-into (stream)
968 (write-char #\
( stream
)
969 (let* ((dimension (car dimensions
))
970 (dimensions (cdr dimensions
))
971 (count (reduce #'* dimensions
)))
972 (dotimes (i dimension
)
974 (write-char #\space stream
))
975 (punt-print-if-too-long i stream
)
976 (sub-output-array-guts array dimensions stream index
)
978 (write-char #\
) stream
)))))
981 ;;;; integer, ratio, and complex printing (i.e. everything but floats)
983 (defun %output-radix
(base stream
)
984 (write-char #\
# stream
)
985 (write-char (case base
989 (t (%output-reasonable-integer-in-base base
10 stream
)
993 (defun %output-reasonable-integer-in-base
(n base stream
)
994 (multiple-value-bind (q r
)
996 ;; Recurse until you have all the digits pushed on
999 (%output-reasonable-integer-in-base q base stream
))
1000 ;; Then as each recursive call unwinds, turn the
1001 ;; digit (in remainder) into a character and output
1004 (schar "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" r
)
1007 ;;; *POWER-CACHE* is an alist mapping bases to power-vectors. It is
1008 ;;; filled and probed by POWERS-FOR-BASE. SCRUB-POWER-CACHE is called
1009 ;;; always prior a GC to drop overly large bignums from the cache.
1011 ;;; It doesn't need a lock, but if you work on SCRUB-POWER-CACHE or
1012 ;;; POWERS-FOR-BASE, see that you don't break the assumptions!
1013 (defglobal *power-cache
* (make-array 37 :initial-element nil
))
1014 (declaim (type (simple-vector 37) *power-cache
*))
1016 (defconstant +power-cache-integer-length-limit
+ 2048)
1018 (defun scrub-power-cache (&aux
(cache *power-cache
*))
1019 (dotimes (i (length cache
))
1020 (let ((powers (aref cache i
)))
1022 (let ((too-big (position-if
1024 (>= (integer-length x
)
1025 +power-cache-integer-length-limit
+))
1026 (the simple-vector powers
))))
1028 (setf (aref cache i
) (subseq powers
0 too-big
))))))))
1030 ;;; Compute (and cache) a power vector for a BASE and LIMIT:
1031 ;;; the vector holds integers for which
1032 ;;; (aref powers k) == (expt base (expt 2 k))
1034 (defun powers-for-base (base limit
)
1035 (flet ((compute-powers (from)
1037 (do ((p from
(* p p
)))
1039 ;; We don't actually need this, but we also
1040 ;; prefer not to cons it up a second time...
1043 (nreverse powers
))))
1044 (let* ((cache *power-cache
*)
1045 (powers (aref cache base
)))
1046 (setf (aref cache base
)
1047 (concatenate 'vector powers
1050 (let* ((len (length powers
))
1051 (max (svref powers
(1- len
))))
1053 (return-from powers-for-base powers
)
1057 ;; Algorithm by Harald Hanche-Olsen, sbcl-devel 2005-02-05
1058 (defun %output-huge-integer-in-base
(n base stream
)
1059 (declare (type bignum n
) (type fixnum base
))
1060 ;; POWER is a vector for which the following holds:
1061 ;; (aref power k) == (expt base (expt 2 k))
1062 (let* ((power (powers-for-base base n
))
1063 (k-start (or (position-if (lambda (x) (> x n
)) power
)
1064 (bug "power-vector too short"))))
1065 (labels ((bisect (n k exactp
)
1066 (declare (fixnum k
))
1067 ;; N is the number to bisect
1068 ;; K on initial entry BASE^(2^K) > N
1069 ;; EXACTP is true if 2^K is the exact number of digits
1072 (loop repeat
(ash 1 k
) do
(write-char #\
0 stream
))))
1075 (schar "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" n
)
1079 (multiple-value-bind (q r
) (truncate n
(aref power k
))
1080 ;; EXACTP is NIL only at the head of the
1081 ;; initial number, as we don't know the number
1082 ;; of digits there, but we do know that it
1083 ;; doesn't get any leading zeros.
1085 (bisect r k
(or exactp
(plusp q
))))))))
1086 (bisect n k-start nil
))))
1088 (defun %output-integer-in-base
(integer base stream
)
1089 (when (minusp integer
)
1090 (write-char #\- stream
)
1091 (setf integer
(- integer
)))
1092 ;; The ideal cutoff point between these two algorithms is almost
1093 ;; certainly quite platform dependent: this gives 87 for 32 bit
1094 ;; SBCL, which is about right at least for x86/Darwin.
1095 (if (or (fixnump integer
)
1096 (< (integer-length integer
) (* 3 sb
!vm
:n-positive-fixnum-bits
)))
1097 (%output-reasonable-integer-in-base integer base stream
)
1098 (%output-huge-integer-in-base integer base stream
)))
1100 (defun output-integer (integer stream
)
1101 (let ((base *print-base
*))
1102 (when (and (/= base
10) *print-radix
*)
1103 (%output-radix base stream
))
1104 (%output-integer-in-base integer base stream
)
1105 (when (and *print-radix
* (= base
10))
1106 (write-char #\. stream
))))
1108 (defun output-ratio (ratio stream
)
1109 (let ((base *print-base
*))
1111 (%output-radix base stream
))
1112 (%output-integer-in-base
(numerator ratio
) base stream
)
1113 (write-char #\
/ stream
)
1114 (%output-integer-in-base
(denominator ratio
) base stream
)))
1116 (defun output-complex (complex stream
)
1117 (write-string "#C(" stream
)
1118 (output-object (realpart complex
) stream
)
1119 (write-char #\space stream
)
1120 (output-object (imagpart complex
) stream
)
1121 (write-char #\
) stream
))
1125 ;;; FLONUM-TO-STRING (and its subsidiary function FLOAT-STRING) does
1126 ;;; most of the work for all printing of floating point numbers in
1127 ;;; FORMAT. It converts a floating point number to a string in a free
1128 ;;; or fixed format with no exponent. The interpretation of the
1129 ;;; arguments is as follows:
1131 ;;; X - The floating point number to convert, which must not be
1133 ;;; WIDTH - The preferred field width, used to determine the number
1134 ;;; of fraction digits to produce if the FDIGITS parameter
1135 ;;; is unspecified or NIL. If the non-fraction digits and the
1136 ;;; decimal point alone exceed this width, no fraction digits
1137 ;;; will be produced unless a non-NIL value of FDIGITS has been
1138 ;;; specified. Field overflow is not considerd an error at this
1140 ;;; FDIGITS - The number of fractional digits to produce. Insignificant
1141 ;;; trailing zeroes may be introduced as needed. May be
1142 ;;; unspecified or NIL, in which case as many digits as possible
1143 ;;; are generated, subject to the constraint that there are no
1144 ;;; trailing zeroes.
1145 ;;; SCALE - If this parameter is specified or non-NIL, then the number
1146 ;;; printed is (* x (expt 10 scale)). This scaling is exact,
1147 ;;; and cannot lose precision.
1148 ;;; FMIN - This parameter, if specified or non-NIL, is the minimum
1149 ;;; number of fraction digits which will be produced, regardless
1150 ;;; of the value of WIDTH or FDIGITS. This feature is used by
1151 ;;; the ~E format directive to prevent complete loss of
1152 ;;; significance in the printed value due to a bogus choice of
1156 ;;; (VALUES DIGIT-STRING DIGIT-LENGTH LEADING-POINT TRAILING-POINT DECPNT)
1157 ;;; where the results have the following interpretation:
1159 ;;; DIGIT-STRING - The decimal representation of X, with decimal point.
1160 ;;; DIGIT-LENGTH - The length of the string DIGIT-STRING.
1161 ;;; LEADING-POINT - True if the first character of DIGIT-STRING is the
1163 ;;; TRAILING-POINT - True if the last character of DIGIT-STRING is the
1165 ;;; POINT-POS - The position of the digit preceding the decimal
1166 ;;; point. Zero indicates point before first digit.
1168 ;;; NOTE: FLONUM-TO-STRING goes to a lot of trouble to guarantee
1169 ;;; accuracy. Specifically, the decimal number printed is the closest
1170 ;;; possible approximation to the true value of the binary number to
1171 ;;; be printed from among all decimal representations with the same
1172 ;;; number of digits. In free-format output, i.e. with the number of
1173 ;;; digits unconstrained, it is guaranteed that all the information is
1174 ;;; preserved, so that a properly- rounding reader can reconstruct the
1175 ;;; original binary number, bit-for-bit, from its printed decimal
1176 ;;; representation. Furthermore, only as many digits as necessary to
1177 ;;; satisfy this condition will be printed.
1179 ;;; FLOAT-DIGITS actually generates the digits for positive numbers;
1180 ;;; see below for comments.
1182 (defun flonum-to-string (x &optional width fdigits scale fmin
)
1183 (declare (type float x
))
1184 ;; FIXME: I think only FORMAT-DOLLARS calls FLONUM-TO-STRING with
1185 ;; possibly-negative X.
1187 (multiple-value-bind (e string
)
1189 (flonum-to-digits x
(min (- (+ fdigits
(or scale
0)))
1191 (if (and width
(> width
1))
1192 (let ((w (multiple-value-list
1196 (if (and scale
(minusp scale
))
1199 (f (multiple-value-list
1200 (flonum-to-digits x
(- (+ (or fmin
0)
1201 (if scale scale
0)))))))
1203 ((>= (length (cadr w
)) (length (cadr f
)))
1205 (t (values-list f
))))
1206 (flonum-to-digits x
)))
1207 (let ((e (if (zerop x
)
1209 (+ e
(or scale
0))))
1210 (stream (make-string-output-stream)))
1213 (write-string string stream
:end
(min (length string
) e
))
1214 (dotimes (i (- e
(length string
)))
1215 (write-char #\
0 stream
))
1216 (write-char #\. stream
)
1217 (write-string string stream
:start
(min (length string
) e
))
1219 (dotimes (i (- fdigits
1221 (min (length string
) e
))))
1222 (write-char #\
0 stream
))))
1224 (write-string "." stream
)
1226 (write-char #\
0 stream
))
1227 (write-string string stream
:end
(when fdigits
1228 (min (length string
)
1232 (dotimes (i (+ fdigits e
(- (length string
))))
1233 (write-char #\
0 stream
)))))
1234 (let ((string (get-output-stream-string stream
)))
1235 (values string
(length string
)
1236 (char= (char string
0) #\.
)
1237 (char= (char string
(1- (length string
))) #\.
)
1238 (position #\. string
))))))
1240 ;;; implementation of figure 1 from Burger and Dybvig, 1996. It is
1241 ;;; extended in order to handle rounding.
1243 ;;; As the implementation of the Dragon from Classic CMUCL (and
1244 ;;; previously in SBCL above FLONUM-TO-STRING) says: "DO NOT EVEN
1245 ;;; THINK OF ATTEMPTING TO UNDERSTAND THIS CODE WITHOUT READING THE
1246 ;;; PAPER!", and in this case we have to add that even reading the
1247 ;;; paper might not bring immediate illumination as CSR has attempted
1248 ;;; to turn idiomatic Scheme into idiomatic Lisp.
1250 ;;; FIXME: figure 1 from Burger and Dybvig is the unoptimized
1251 ;;; algorithm, noticeably slow at finding the exponent. Figure 2 has
1252 ;;; an improved algorithm, but CSR ran out of energy.
1254 ;;; possible extension for the enthusiastic: printing floats in bases
1255 ;;; other than base 10.
1256 (defconstant single-float-min-e
1257 (- 2 sb
!vm
:single-float-bias sb
!vm
:single-float-digits
))
1258 (defconstant double-float-min-e
1259 (- 2 sb
!vm
:double-float-bias sb
!vm
:double-float-digits
))
1261 (defconstant long-float-min-e
1262 (nth-value 1 (decode-float least-positive-long-float
)))
1264 (defun flonum-to-digits (v &optional position relativep
)
1265 (let ((print-base 10) ; B
1267 (float-digits (float-digits v
)) ; p
1268 (digit-characters "0123456789")
1271 (single-float single-float-min-e
)
1272 (double-float double-float-min-e
)
1274 (long-float long-float-min-e
))))
1275 (multiple-value-bind (f e
)
1276 (integer-decode-float v
)
1277 (let ( ;; FIXME: these even tests assume normal IEEE rounding
1278 ;; mode. I wonder if we should cater for non-normal?
1281 (with-push-char (:element-type base-char
)
1282 (labels ((scale (r s m
+ m-
)
1283 (do ((r+m
+ (+ r m
+))
1285 (s s
(* s print-base
)))
1286 ((not (or (> r
+m
+ s
)
1287 (and high-ok
(= r
+m
+ s
))))
1289 (r r
(* r print-base
))
1290 (m+ m
+ (* m
+ print-base
))
1291 (m- m-
(* m- print-base
)))
1292 ((not (and (> r m-
) ; Extension to handle zero
1293 (let ((x (* (+ r m
+) print-base
)))
1297 (values k
(generate r s m
+ m-
)))))))
1298 (generate (r s m
+ m-
)
1302 (setf (values d r
) (truncate (* r print-base
) s
))
1303 (setf m
+ (* m
+ print-base
))
1304 (setf m-
(* m- print-base
))
1305 (setf tc1
(or (< r m-
) (and low-ok
(= r m-
))))
1306 (setf tc2
(let ((r+m
+ (+ r m
+)))
1308 (and high-ok
(= r
+m
+ s
)))))
1311 (push-char (char digit-characters d
))
1315 ((and (not tc1
) tc2
) (1+ d
))
1316 ((and tc1
(not tc2
)) d
)
1321 (push-char (char digit-characters d
))
1322 (return-from generate
(get-pushed-string))))))
1326 (let ((be (expt float-radix e
)))
1327 (if (/= f
(expt float-radix
(1- float-digits
)))
1333 m
+ (* be float-radix
)
1335 s
(* float-radix
2)))))
1337 (/= f
(expt float-radix
(1- float-digits
))))
1339 s
(expt float-radix
(- 1 e
))
1343 (setf r
(* f float-radix
2)
1344 s
(expt float-radix
(- 2 e
))
1349 (aver (> position
0))
1351 ;; running out of letters here
1352 (l 1 (* l print-base
)))
1353 ((>= (* s l
) (+ r m
+))
1355 (if (< (+ r
(* s
(/ (expt print-base
(- k position
)) 2)))
1357 (setf position
(- k position
))
1358 (setf position
(- k position
1))))))
1359 (let* ((x (/ (* s
(expt print-base position
)) 2))
1368 (values r s m
+ m-
))))
1369 (multiple-value-bind (r s m
+ m-
) (initialize)
1370 (scale r s m
+ m-
))))))))
1372 ;;; Given a non-negative floating point number, SCALE-EXPONENT returns
1373 ;;; a new floating point number Z in the range (0.1, 1.0] and an
1374 ;;; exponent E such that Z * 10^E is (approximately) equal to the
1375 ;;; original number. There may be some loss of precision due the
1376 ;;; floating point representation. The scaling is always done with
1377 ;;; long float arithmetic, which helps printing of lesser precisions
1378 ;;; as well as avoiding generic arithmetic.
1380 ;;; When computing our initial scale factor using EXPT, we pull out
1381 ;;; part of the computation to avoid over/under flow. When
1382 ;;; denormalized, we must pull out a large factor, since there is more
1383 ;;; negative exponent range than positive range.
1385 (eval-when (:compile-toplevel
:execute
)
1386 (setf *read-default-float-format
*
1387 #!+long-float
'long-float
#!-long-float
'double-float
))
1388 (defun scale-exponent (original-x)
1389 (let* ((x (coerce original-x
'long-float
)))
1390 (multiple-value-bind (sig exponent
) (decode-float x
)
1391 (declare (ignore sig
))
1393 (values (float 0.0e0 original-x
) 1)
1394 (let* ((ex (locally (declare (optimize (safety 0)))
1397 ;; this is the closest double float
1398 ;; to (log 2 10), but expressed so
1399 ;; that we're not vulnerable to the
1400 ;; host lisp's interpretation of
1401 ;; arithmetic. (FIXME: it turns
1402 ;; out that sbcl itself is off by 1
1403 ;; ulp in this value, which is a
1404 ;; little unfortunate.)
1407 (make-double-float 1070810131 1352628735)
1409 (error "(log 2 10) not computed")))))))
1411 (if (float-denormalized-p x
)
1413 (* x
1.0e16
(expt 10.0e0
(- (- ex
) 16)))
1415 (* x
1.0e18
(expt 10.0e0
(- (- ex
) 18)))
1416 (* x
10.0e0
(expt 10.0e0
(- (- ex
) 1))))
1417 (/ x
10.0e0
(expt 10.0e0
(1- ex
))))))
1418 (do ((d 10.0e0
(* d
10.0e0
))
1422 (do ((m 10.0e0
(* m
10.0e0
))
1426 (values (float z original-x
) ex
))
1427 (declare (long-float m
) (integer ex
))))
1428 (declare (long-float d
))))))))
1429 (eval-when (:compile-toplevel
:execute
)
1430 (setf *read-default-float-format
* 'single-float
))
1432 ;;;; entry point for the float printer
1434 ;;; the float printer as called by PRINT, PRIN1, PRINC, etc. The
1435 ;;; argument is printed free-format, in either exponential or
1436 ;;; non-exponential notation, depending on its magnitude.
1438 ;;; NOTE: When a number is to be printed in exponential format, it is
1439 ;;; scaled in floating point. Since precision may be lost in this
1440 ;;; process, the guaranteed accuracy properties of FLONUM-TO-STRING
1441 ;;; are lost. The difficulty is that FLONUM-TO-STRING performs
1442 ;;; extensive computations with integers of similar magnitude to that
1443 ;;; of the number being printed. For large exponents, the bignums
1444 ;;; really get out of hand. If bignum arithmetic becomes reasonably
1445 ;;; fast and the exponent range is not too large, then it might become
1446 ;;; attractive to handle exponential notation with the same accuracy
1447 ;;; as non-exponential notation, using the method described in the
1448 ;;; Steele and White paper.
1450 ;;; NOTE II: this has been bypassed slightly by implementing Burger
1451 ;;; and Dybvig, 1996. When someone has time (KLUDGE) they can
1452 ;;; probably (a) implement the optimizations suggested by Burger and
1453 ;;; Dyvbig, and (b) remove all vestiges of Dragon4, including from
1454 ;;; fixed-format printing.
1456 ;;; Print the appropriate exponent marker for X and the specified exponent.
1457 (defun print-float-exponent (x exp stream
)
1458 (declare (type float x
) (type integer exp
) (type stream stream
))
1459 (cond ((case *read-default-float-format
*
1460 ((short-float single-float
)
1461 (typep x
'single-float
))
1462 ((double-float #!-long-float long-float
)
1463 (typep x
'double-float
))
1466 (typep x
'long-float
)))
1468 (write-char #\e stream
)
1469 (%output-integer-in-base exp
10 stream
)))
1478 (%output-integer-in-base exp
10 stream
))))
1480 (defun output-float-infinity (x stream
)
1481 (declare (float x
) (stream stream
))
1483 (write-string "#." stream
))
1485 (return-from output-float-infinity
1486 (print-not-readable-error x stream
)))
1488 (write-string "#<" stream
)))
1489 (write-string "SB-EXT:" stream
)
1490 (write-string (symbol-name (float-format-name x
)) stream
)
1491 (write-string (if (plusp x
) "-POSITIVE-" "-NEGATIVE-")
1493 (write-string "INFINITY" stream
)
1495 (write-string ">" stream
)))
1497 (defun output-float-nan (x stream
)
1498 (print-unreadable-object (x stream
)
1499 (princ (float-format-name x
) stream
)
1500 (write-string (if (float-trapping-nan-p x
) " trapping" " quiet") stream
)
1501 (write-string " NaN" stream
)))
1503 ;;; the function called by OUTPUT-OBJECT to handle floats
1504 (defun output-float (x stream
)
1506 ((float-infinity-p x
)
1507 (output-float-infinity x stream
))
1509 (output-float-nan x stream
))
1511 (let ((x (cond ((minusp (float-sign x
))
1512 (write-char #\- stream
)
1518 (write-string "0.0" stream
)
1519 (print-float-exponent x
0 stream
))
1521 (output-float-aux x stream -
3 8)))))))
1523 (defun output-float-aux (x stream e-min e-max
)
1524 (multiple-value-bind (e string
)
1525 (flonum-to-digits x
)
1530 (write-string string stream
:end
(min (length string
) e
))
1531 (dotimes (i (- e
(length string
)))
1532 (write-char #\
0 stream
))
1533 (write-char #\. stream
)
1534 (write-string string stream
:start
(min (length string
) e
))
1535 (when (<= (length string
) e
)
1536 (write-char #\
0 stream
))
1537 (print-float-exponent x
0 stream
))
1539 (write-string "0." stream
)
1541 (write-char #\
0 stream
))
1542 (write-string string stream
)
1543 (print-float-exponent x
0 stream
))))
1544 (t (write-string string stream
:end
1)
1545 (write-char #\. stream
)
1546 (write-string string stream
:start
1)
1547 (print-float-exponent x
(1- e
) stream
)))))
1549 ;;;; other leaf objects
1551 ;;; If *PRINT-ESCAPE* is false, just do a WRITE-CHAR, otherwise output
1552 ;;; the character name or the character in the #\char format.
1553 (defun output-character (char stream
)
1554 (if (or *print-escape
* *print-readably
*)
1555 (let ((graphicp (and (graphic-char-p char
)
1556 (standard-char-p char
)))
1557 (name (char-name char
)))
1558 (write-string "#\\" stream
)
1559 (if (and name
(or (not graphicp
) *print-readably
*))
1560 (quote-string name stream
)
1561 (write-char char stream
)))
1562 (write-char char stream
)))
1564 (defun output-sap (sap stream
)
1565 (declare (type system-area-pointer sap
))
1567 (format stream
"#.(~S #X~8,'0X)" 'int-sap
(sap-int sap
)))
1569 (print-unreadable-object (sap stream
)
1570 (format stream
"system area pointer: #X~8,'0X" (sap-int sap
))))))
1572 (defun output-weak-pointer (weak-pointer stream
)
1573 (declare (type weak-pointer weak-pointer
))
1574 (print-unreadable-object (weak-pointer stream
)
1575 (multiple-value-bind (value validp
) (weak-pointer-value weak-pointer
)
1577 (write-string "weak pointer: " stream
)
1578 (write value
:stream stream
))
1580 (write-string "broken weak pointer" stream
))))))
1582 (defun output-code-component (component stream
)
1583 (print-unreadable-object (component stream
:identity t
)
1584 (let ((dinfo (%code-debug-info component
)))
1585 (cond ((eq dinfo
:bogus-lra
)
1586 (write-string "bogus code object" stream
))
1588 (format stream
"code object [~D]" (code-n-entries component
))
1589 (let ((fun-name (awhen (%code-entry-point component
0)
1590 (%simple-fun-name it
))))
1592 (write-char #\Space stream
)
1593 (write fun-name
:stream stream
))
1594 (cond ((not (typep dinfo
'sb
!c
::debug-info
)))
1595 ((neq (sb!c
::debug-info-name dinfo
) fun-name
)
1596 (write-string ", " stream
)
1597 (output-object (sb!c
::debug-info-name dinfo
) stream
)))))))))
1599 (defun output-lra (lra stream
)
1600 (print-unreadable-object (lra stream
:identity t
)
1601 (write-string "return PC object" stream
)))
1603 (defun output-fdefn (fdefn stream
)
1604 (print-unreadable-object (fdefn stream
:type t
)
1605 (let ((name (fdefn-name fdefn
)))
1606 ;; It's somewhat unhelpful to print as <FDEFINITION for (SETF #)>
1607 ;; Generalized function names are indivisible.
1608 (if (proper-list-p name
)
1609 (format stream
"(~{~S~^ ~})" name
)
1610 (output-object name stream
)))))
1613 (defmethod print-object ((pack simd-pack
) stream
)
1614 (cond ((and *print-readably
* *read-eval
*)
1615 (multiple-value-bind (format maker extractor
)
1617 ((simd-pack double-float
)
1618 (values "#.(~S ~S ~S)"
1619 '%make-simd-pack-double
#'%simd-pack-doubles
))
1620 ((simd-pack single-float
)
1621 (values "#.(~S ~S ~S ~S ~S)"
1622 '%make-simd-pack-single
#'%simd-pack-singles
))
1624 (values "#.(~S #X~16,'0X #X~16,'0X)"
1625 '%make-simd-pack-ub64
#'%simd-pack-ub64s
)))
1626 (multiple-value-call
1627 #'format stream format maker
(funcall extractor pack
))))
1629 (print-unreadable-object (pack stream
)
1630 (flet ((all-ones-p (value start end
&aux
(mask (- (ash 1 end
) (ash 1 start
))))
1631 (= (logand value mask
) mask
))
1632 (split-num (value start
)
1635 and v
= (ash value
(- start
)) then
(ash v -
8)
1636 collect
(logand v
#xFF
))))
1637 (multiple-value-bind (low high
)
1638 (%simd-pack-ub64s pack
)
1640 ((simd-pack double-float
)
1641 (multiple-value-bind (v0 v1
) (%simd-pack-doubles pack
)
1642 (format stream
"~S~@{ ~:[~,13E~;~*TRUE~]~}"
1644 (all-ones-p low
0 64) v0
1645 (all-ones-p high
0 64) v1
)))
1646 ((simd-pack single-float
)
1647 (multiple-value-bind (v0 v1 v2 v3
) (%simd-pack-singles pack
)
1648 (format stream
"~S~@{ ~:[~,7E~;~*TRUE~]~}"
1650 (all-ones-p low
0 32) v0
1651 (all-ones-p low
32 64) v1
1652 (all-ones-p high
0 32) v2
1653 (all-ones-p high
32 64) v3
)))
1655 (format stream
"~S~@{ ~{ ~2,'0X~}~}"
1657 (split-num low
0) (split-num low
32)
1658 (split-num high
0) (split-num high
32))))))))))
1662 (defun output-fun (object stream
)
1663 (let* ((name (%fun-name object
))
1664 (proper-name-p (and (legal-fun-name-p name
) (fboundp name
)
1665 (eq (fdefinition name
) object
))))
1666 (print-unreadable-object (object stream
:identity
(not proper-name-p
))
1667 (format stream
"~:[FUNCTION~;CLOSURE~]~@[ ~S~]"
1671 ;;;; catch-all for unknown things
1673 (defmethod print-object ((object t
) stream
)
1674 (flet ((output-it (stream)
1675 (print-unreadable-object (object stream
:identity t
)
1676 (let ((lowtag (lowtag-of object
)))
1678 (#.sb
!vm
:other-pointer-lowtag
1679 (let ((widetag (widetag-of object
)))
1681 (#.sb
!vm
:value-cell-header-widetag
1682 (write-string "value cell " stream
)
1683 (output-object (value-cell-ref object
) stream
))
1685 (write-string "unknown pointer object, widetag=" stream
)
1686 (let ((*print-base
* 16) (*print-radix
* t
))
1687 (output-integer widetag stream
))))))
1688 ((#.sb
!vm
:fun-pointer-lowtag
1689 #.sb
!vm
:instance-pointer-lowtag
1690 #.sb
!vm
:list-pointer-lowtag
)
1691 (write-string "unknown pointer object, lowtag=" stream
)
1692 (let ((*print-base
* 16) (*print-radix
* t
))
1693 (output-integer lowtag stream
)))
1695 (case (widetag-of object
)
1696 (#.sb
!vm
:unbound-marker-widetag
1697 (write-string "unbound marker" stream
))
1699 (write-string "unknown immediate object, lowtag=" stream
)
1700 (let ((*print-base
* 2) (*print-radix
* t
))
1701 (output-integer lowtag stream
))
1702 (write-string ", widetag=" stream
)
1703 (let ((*print-base
* 16) (*print-radix
* t
))
1704 (output-integer (widetag-of object
) stream
))))))))))
1706 ;; This block might not be necessary. Not sure, probably can't hurt.
1707 (pprint-logical-block (stream nil
) (output-it stream
))
1708 (output-it stream
))))