1.0.18.17: Alter some STYLE-WARNING names introduced in 1.0.18.16.
[sbcl/pkhuong.git] / src / code / print.lisp
blobe235f4b2b938979b5fc5ffaca66b1d447f4d7cfd
1 ;;;; the printer
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 ;;;; exported printer control variables
16 ;;; FIXME: Many of these have nontrivial types, e.g. *PRINT-LEVEL*,
17 ;;; *PRINT-LENGTH*, and *PRINT-LINES* are (OR NULL UNSIGNED-BYTE).
19 (defvar *print-readably* nil
20 #!+sb-doc
21 "If true, all objects will printed readably. If readable printing is
22 impossible, an error will be signalled. This overrides the value of
23 *PRINT-ESCAPE*.")
24 (defvar *print-escape* t
25 #!+sb-doc
26 "Should we print in a reasonably machine-readable way? (possibly
27 overridden by *PRINT-READABLY*)")
28 (defvar *print-pretty* nil ; (set later when pretty-printer is initialized)
29 #!+sb-doc
30 "Should pretty printing be used?")
31 (defvar *print-base* 10.
32 #!+sb-doc
33 "the output base for RATIONALs (including integers)")
34 (defvar *print-radix* nil
35 #!+sb-doc
36 "Should base be verified when printing RATIONALs?")
37 (defvar *print-level* nil
38 #!+sb-doc
39 "How many levels should be printed before abbreviating with \"#\"?")
40 (defvar *print-length* nil
41 #!+sb-doc
42 "How many elements at any level should be printed before abbreviating
43 with \"...\"?")
44 (defvar *print-circle* nil
45 #!+sb-doc
46 "Should we use #n= and #n# notation to preserve uniqueness in general (and
47 circularity in particular) when printing?")
48 (defvar *print-case* :upcase
49 #!+sb-doc
50 "What case should the printer should use default?")
51 (defvar *print-array* t
52 #!+sb-doc
53 "Should the contents of arrays be printed?")
54 (defvar *print-gensym* t
55 #!+sb-doc
56 "Should #: prefixes be used when printing symbols with null SYMBOL-PACKAGE?")
57 (defvar *print-lines* nil
58 #!+sb-doc
59 "the maximum number of lines to print per object")
60 (defvar *print-right-margin* nil
61 #!+sb-doc
62 "the position of the right margin in ems (for pretty-printing)")
63 (defvar *print-miser-width* nil
64 #!+sb-doc
65 "If the remaining space between the current column and the right margin
66 is less than this, then print using ``miser-style'' output. Miser
67 style conditional newlines are turned on, and all indentations are
68 turned off. If NIL, never use miser mode.")
69 (defvar *print-pprint-dispatch*)
70 #!+sb-doc
71 (setf (fdocumentation '*print-pprint-dispatch* 'variable)
72 "the pprint-dispatch-table that controls how to pretty-print objects")
74 (defmacro with-standard-io-syntax (&body body)
75 #!+sb-doc
76 "Bind the reader and printer control variables to values that enable READ
77 to reliably read the results of PRINT. These values are:
78 *PACKAGE* the COMMON-LISP-USER package
79 *PRINT-ARRAY* T
80 *PRINT-BASE* 10
81 *PRINT-CASE* :UPCASE
82 *PRINT-CIRCLE* NIL
83 *PRINT-ESCAPE* T
84 *PRINT-GENSYM* T
85 *PRINT-LENGTH* NIL
86 *PRINT-LEVEL* NIL
87 *PRINT-LINES* NIL
88 *PRINT-MISER-WIDTH* NIL
89 *PRINT-PRETTY* NIL
90 *PRINT-RADIX* NIL
91 *PRINT-READABLY* T
92 *PRINT-RIGHT-MARGIN* NIL
93 *READ-BASE* 10
94 *READ-DEFAULT-FLOAT-FORMAT* SINGLE-FLOAT
95 *READ-EVAL* T
96 *READ-SUPPRESS* NIL
97 *READTABLE* the standard readtable"
98 `(%with-standard-io-syntax (lambda () ,@body)))
100 (defun %with-standard-io-syntax (function)
101 (declare (type function function))
102 (let ((*package* (find-package "COMMON-LISP-USER"))
103 (*print-array* t)
104 (*print-base* 10)
105 (*print-case* :upcase)
106 (*print-circle* nil)
107 (*print-escape* t)
108 (*print-gensym* t)
109 (*print-length* nil)
110 (*print-level* nil)
111 (*print-lines* nil)
112 (*print-miser-width* nil)
113 (*print-pretty* nil)
114 (*print-radix* nil)
115 (*print-readably* t)
116 (*print-right-margin* nil)
117 (*read-base* 10)
118 (*read-default-float-format* 'single-float)
119 (*read-eval* t)
120 (*read-suppress* nil)
121 ;; FIXME: It doesn't seem like a good idea to expose our
122 ;; disaster-recovery *STANDARD-READTABLE* here. What if some
123 ;; enterprising user corrupts the disaster-recovery readtable
124 ;; by doing destructive readtable operations within
125 ;; WITH-STANDARD-IO-SYNTAX? Perhaps we should do a
126 ;; COPY-READTABLE? The consing would be unfortunate, though.
127 (*readtable* *standard-readtable*))
128 (funcall function)))
130 ;;;; routines to print objects
132 (defun write (object &key
133 ((:stream stream) *standard-output*)
134 ((:escape *print-escape*) *print-escape*)
135 ((:radix *print-radix*) *print-radix*)
136 ((:base *print-base*) *print-base*)
137 ((:circle *print-circle*) *print-circle*)
138 ((:pretty *print-pretty*) *print-pretty*)
139 ((:level *print-level*) *print-level*)
140 ((:length *print-length*) *print-length*)
141 ((:case *print-case*) *print-case*)
142 ((:array *print-array*) *print-array*)
143 ((:gensym *print-gensym*) *print-gensym*)
144 ((:readably *print-readably*) *print-readably*)
145 ((:right-margin *print-right-margin*)
146 *print-right-margin*)
147 ((:miser-width *print-miser-width*)
148 *print-miser-width*)
149 ((:lines *print-lines*) *print-lines*)
150 ((:pprint-dispatch *print-pprint-dispatch*)
151 *print-pprint-dispatch*))
152 #!+sb-doc
153 "Output OBJECT to the specified stream, defaulting to *STANDARD-OUTPUT*"
154 (output-object object (out-synonym-of stream))
155 object)
157 (defun prin1 (object &optional stream)
158 #!+sb-doc
159 "Output a mostly READable printed representation of OBJECT on the specified
160 STREAM."
161 (let ((*print-escape* t))
162 (output-object object (out-synonym-of stream)))
163 object)
165 (defun princ (object &optional stream)
166 #!+sb-doc
167 "Output an aesthetic but not necessarily READable printed representation
168 of OBJECT on the specified STREAM."
169 (let ((*print-escape* nil)
170 (*print-readably* nil))
171 (output-object object (out-synonym-of stream)))
172 object)
174 (defun print (object &optional stream)
175 #!+sb-doc
176 "Output a newline, the mostly READable printed representation of OBJECT, and
177 space to the specified STREAM."
178 (let ((stream (out-synonym-of stream)))
179 (terpri stream)
180 (prin1 object stream)
181 (write-char #\space stream)
182 object))
184 (defun pprint (object &optional stream)
185 #!+sb-doc
186 "Prettily output OBJECT preceded by a newline."
187 (let ((*print-pretty* t)
188 (*print-escape* t)
189 (stream (out-synonym-of stream)))
190 (terpri stream)
191 (output-object object stream))
192 (values))
194 (defun write-to-string
195 (object &key
196 ((:escape *print-escape*) *print-escape*)
197 ((:radix *print-radix*) *print-radix*)
198 ((:base *print-base*) *print-base*)
199 ((:circle *print-circle*) *print-circle*)
200 ((:pretty *print-pretty*) *print-pretty*)
201 ((:level *print-level*) *print-level*)
202 ((:length *print-length*) *print-length*)
203 ((:case *print-case*) *print-case*)
204 ((:array *print-array*) *print-array*)
205 ((:gensym *print-gensym*) *print-gensym*)
206 ((:readably *print-readably*) *print-readably*)
207 ((:right-margin *print-right-margin*) *print-right-margin*)
208 ((:miser-width *print-miser-width*) *print-miser-width*)
209 ((:lines *print-lines*) *print-lines*)
210 ((:pprint-dispatch *print-pprint-dispatch*)
211 *print-pprint-dispatch*))
212 #!+sb-doc
213 "Return the printed representation of OBJECT as a string."
214 (stringify-object object))
216 (defun prin1-to-string (object)
217 #!+sb-doc
218 "Return the printed representation of OBJECT as a string with
219 slashification on."
220 (let ((*print-escape* t))
221 (stringify-object object)))
223 (defun princ-to-string (object)
224 #!+sb-doc
225 "Return the printed representation of OBJECT as a string with
226 slashification off."
227 (let ((*print-escape* nil)
228 (*print-readably* nil))
229 (stringify-object object)))
231 ;;; This produces the printed representation of an object as a string.
232 ;;; The few ...-TO-STRING functions above call this.
233 (defun stringify-object (object)
234 (let ((stream (make-string-output-stream)))
235 (setup-printer-state)
236 (output-object object stream)
237 (get-output-stream-string stream)))
239 ;;;; support for the PRINT-UNREADABLE-OBJECT macro
241 ;;; guts of PRINT-UNREADABLE-OBJECT
242 (defun %print-unreadable-object (object stream type identity body)
243 (declare (type (or null function) body))
244 (when *print-readably*
245 (error 'print-not-readable :object object))
246 (flet ((print-description ()
247 (when type
248 (write (type-of object) :stream stream :circle nil
249 :level nil :length nil)
250 (write-char #\space stream))
251 (when body
252 (funcall body))
253 (when identity
254 (when (or body (not type))
255 (write-char #\space stream))
256 (write-char #\{ stream)
257 (write (get-lisp-obj-address object) :stream stream
258 :radix nil :base 16)
259 (write-char #\} stream))))
260 (cond ((print-pretty-on-stream-p stream)
261 ;; Since we're printing prettily on STREAM, format the
262 ;; object within a logical block. PPRINT-LOGICAL-BLOCK does
263 ;; not rebind the stream when it is already a pretty stream,
264 ;; so output from the body will go to the same stream.
265 (pprint-logical-block (stream nil :prefix "#<" :suffix ">")
266 (print-description)))
268 (write-string "#<" stream)
269 (print-description)
270 (write-char #\> stream))))
271 nil)
273 ;;;; OUTPUT-OBJECT -- the main entry point
275 ;;; Objects whose print representation identifies them EQLly don't
276 ;;; need to be checked for circularity.
277 (defun uniquely-identified-by-print-p (x)
278 (or (numberp x)
279 (characterp x)
280 (and (symbolp x)
281 (symbol-package x))))
283 ;;; Output OBJECT to STREAM observing all printer control variables.
284 (defun output-object (object stream)
285 (labels ((print-it (stream)
286 (if *print-pretty*
287 (sb!pretty:output-pretty-object object stream)
288 (output-ugly-object object stream)))
289 (check-it (stream)
290 (multiple-value-bind (marker initiate)
291 (check-for-circularity object t)
292 (if (eq initiate :initiate)
293 (let ((*circularity-hash-table*
294 (make-hash-table :test 'eq)))
295 (check-it (make-broadcast-stream))
296 (let ((*circularity-counter* 0))
297 (check-it stream)))
298 ;; otherwise
299 (if marker
300 (when (handle-circularity marker stream)
301 (print-it stream))
302 (print-it stream))))))
303 (cond (;; Maybe we don't need to bother with circularity detection.
304 (or (not *print-circle*)
305 (uniquely-identified-by-print-p object))
306 (print-it stream))
307 (;; If we have already started circularity detection, this
308 ;; object might be a shared reference. If we have not, then
309 ;; if it is a compound object it might contain a circular
310 ;; reference to itself or multiple shared references.
311 (or *circularity-hash-table*
312 (compound-object-p object))
313 (check-it stream))
315 (print-it stream)))))
317 ;;; a hack to work around recurring gotchas with printing while
318 ;;; DEFGENERIC PRINT-OBJECT is being built
320 ;;; (hopefully will go away naturally when CLOS moves into cold init)
321 (defvar *print-object-is-disabled-p*)
323 ;;; Output OBJECT to STREAM observing all printer control variables
324 ;;; except for *PRINT-PRETTY*. Note: if *PRINT-PRETTY* is non-NIL,
325 ;;; then the pretty printer will be used for any components of OBJECT,
326 ;;; just not for OBJECT itself.
327 (defun output-ugly-object (object stream)
328 (typecase object
329 ;; KLUDGE: The TYPECASE approach here is non-ANSI; the ANSI definition of
330 ;; PRINT-OBJECT says it provides printing and we're supposed to provide
331 ;; PRINT-OBJECT methods covering all classes. We deviate from this
332 ;; by using PRINT-OBJECT only when we print instance values. However,
333 ;; ANSI makes it hard to tell that we're deviating from this:
334 ;; (1) ANSI specifies that the user isn't supposed to call PRINT-OBJECT
335 ;; directly.
336 ;; (2) ANSI (section 11.1.2.1.2) says it's undefined to define
337 ;; a method on an external symbol in the CL package which is
338 ;; applicable to arg lists containing only direct instances of
339 ;; standardized classes.
340 ;; Thus, in order for the user to detect our sleaziness in conforming
341 ;; code, he has to do something relatively obscure like
342 ;; (1) actually use tools like FIND-METHOD to look for PRINT-OBJECT
343 ;; methods, or
344 ;; (2) define a PRINT-OBJECT method which is specialized on the stream
345 ;; value (e.g. a Gray stream object).
346 ;; As long as no one comes up with a non-obscure way of detecting this
347 ;; sleaziness, fixing this nonconformity will probably have a low
348 ;; priority. -- WHN 2001-11-25
349 (list
350 (if (null object)
351 (output-symbol object stream)
352 (output-list object stream)))
353 (instance
354 (cond ((not (and (boundp '*print-object-is-disabled-p*)
355 *print-object-is-disabled-p*))
356 (print-object object stream))
357 ((typep object 'structure-object)
358 (default-structure-print object stream *current-level-in-print*))
360 (write-string "#<INSTANCE but not STRUCTURE-OBJECT>" stream))))
361 (funcallable-instance
362 (cond
363 ((not (and (boundp '*print-object-is-disabled-p*)
364 *print-object-is-disabled-p*))
365 (print-object object stream))
366 (t (output-fun object stream))))
367 (function
368 (output-fun object stream))
369 (symbol
370 (output-symbol object stream))
371 (number
372 (etypecase object
373 (integer
374 (output-integer object stream))
375 (float
376 (output-float object stream))
377 (ratio
378 (output-ratio object stream))
379 (ratio
380 (output-ratio object stream))
381 (complex
382 (output-complex object stream))))
383 (character
384 (output-character object stream))
385 (vector
386 (output-vector object stream))
387 (array
388 (output-array object stream))
389 (system-area-pointer
390 (output-sap object stream))
391 (weak-pointer
392 (output-weak-pointer object stream))
393 (lra
394 (output-lra object stream))
395 (code-component
396 (output-code-component object stream))
397 (fdefn
398 (output-fdefn object stream))
400 (output-random object stream))))
402 ;;;; symbols
404 ;;; values of *PRINT-CASE* and (READTABLE-CASE *READTABLE*) the last
405 ;;; time the printer was called
406 (defvar *previous-case* nil)
407 (defvar *previous-readtable-case* nil)
409 ;;; This variable contains the current definition of one of three
410 ;;; symbol printers. SETUP-PRINTER-STATE sets this variable.
411 (defvar *internal-symbol-output-fun* nil)
413 ;;; This function sets the internal global symbol
414 ;;; *INTERNAL-SYMBOL-OUTPUT-FUN* to the right function depending on
415 ;;; the value of *PRINT-CASE*. See the manual for details. The print
416 ;;; buffer stream is also reset.
417 (defun setup-printer-state ()
418 (unless (and (eq *print-case* *previous-case*)
419 (eq (readtable-case *readtable*) *previous-readtable-case*))
420 (setq *previous-case* *print-case*)
421 (setq *previous-readtable-case* (readtable-case *readtable*))
422 (unless (member *print-case* '(:upcase :downcase :capitalize))
423 (setq *print-case* :upcase)
424 (error "invalid *PRINT-CASE* value: ~S" *previous-case*))
425 (unless (member *previous-readtable-case*
426 '(:upcase :downcase :invert :preserve))
427 (setf (readtable-case *readtable*) :upcase)
428 (error "invalid READTABLE-CASE value: ~S" *previous-readtable-case*))
430 (setq *internal-symbol-output-fun*
431 (case *previous-readtable-case*
432 (:upcase
433 (case *print-case*
434 (:upcase #'output-preserve-symbol)
435 (:downcase #'output-lowercase-symbol)
436 (:capitalize #'output-capitalize-symbol)))
437 (:downcase
438 (case *print-case*
439 (:upcase #'output-uppercase-symbol)
440 (:downcase #'output-preserve-symbol)
441 (:capitalize #'output-capitalize-symbol)))
442 (:preserve #'output-preserve-symbol)
443 (:invert #'output-invert-symbol)))))
445 ;;; Output PNAME (a symbol-name or package-name) surrounded with |'s,
446 ;;; and with any embedded |'s or \'s escaped.
447 (defun output-quoted-symbol-name (pname stream)
448 (write-char #\| stream)
449 (dotimes (index (length pname))
450 (let ((char (schar pname index)))
451 (when (or (char= char #\\) (char= char #\|))
452 (write-char #\\ stream))
453 (write-char char stream)))
454 (write-char #\| stream))
456 (defun output-symbol (object stream)
457 (if (or *print-escape* *print-readably*)
458 (let ((package (symbol-package object))
459 (name (symbol-name object)))
460 (cond
461 ;; The ANSI spec "22.1.3.3.1 Package Prefixes for Symbols"
462 ;; requires that keywords be printed with preceding colons
463 ;; always, regardless of the value of *PACKAGE*.
464 ((eq package *keyword-package*)
465 (write-char #\: stream))
466 ;; Otherwise, if the symbol's home package is the current
467 ;; one, then a prefix is never necessary.
468 ((eq package (sane-package)))
469 ;; Uninterned symbols print with a leading #:.
470 ((null package)
471 (when (or *print-gensym* *print-readably*)
472 (write-string "#:" stream)))
474 (multiple-value-bind (symbol accessible)
475 (find-symbol name (sane-package))
476 ;; If we can find the symbol by looking it up, it need not
477 ;; be qualified. This can happen if the symbol has been
478 ;; inherited from a package other than its home package.
479 (unless (and accessible (eq symbol object))
480 (output-symbol-name (package-name package) stream)
481 (multiple-value-bind (symbol externalp)
482 (find-external-symbol name package)
483 (declare (ignore symbol))
484 (if externalp
485 (write-char #\: stream)
486 (write-string "::" stream)))))))
487 (output-symbol-name name stream))
488 (output-symbol-name (symbol-name object) stream nil)))
490 ;;; Output the string NAME as if it were a symbol name. In other
491 ;;; words, diddle its case according to *PRINT-CASE* and
492 ;;; READTABLE-CASE.
493 (defun output-symbol-name (name stream &optional (maybe-quote t))
494 (declare (type simple-string name))
495 (let ((*readtable* (if *print-readably* *standard-readtable* *readtable*)))
496 (setup-printer-state)
497 (if (and maybe-quote (symbol-quotep name))
498 (output-quoted-symbol-name name stream)
499 (funcall *internal-symbol-output-fun* name stream))))
501 ;;;; escaping symbols
503 ;;; When we print symbols we have to figure out if they need to be
504 ;;; printed with escape characters. This isn't a whole lot easier than
505 ;;; reading symbols in the first place.
507 ;;; For each character, the value of the corresponding element is a
508 ;;; fixnum with bits set corresponding to attributes that the
509 ;;; character has. At characters have at least one bit set, so we can
510 ;;; search for any character with a positive test.
511 (defvar *character-attributes*
512 (make-array 160 ; FIXME
513 :element-type '(unsigned-byte 16)
514 :initial-element 0))
515 (declaim (type (simple-array (unsigned-byte 16) (#.160)) ; FIXME
516 *character-attributes*))
518 ;;; constants which are a bit-mask for each interesting character attribute
519 (defconstant other-attribute (ash 1 0)) ; Anything else legal.
520 (defconstant number-attribute (ash 1 1)) ; A numeric digit.
521 (defconstant uppercase-attribute (ash 1 2)) ; An uppercase letter.
522 (defconstant lowercase-attribute (ash 1 3)) ; A lowercase letter.
523 (defconstant sign-attribute (ash 1 4)) ; +-
524 (defconstant extension-attribute (ash 1 5)) ; ^_
525 (defconstant dot-attribute (ash 1 6)) ; .
526 (defconstant slash-attribute (ash 1 7)) ; /
527 (defconstant funny-attribute (ash 1 8)) ; Anything illegal.
529 (eval-when (:compile-toplevel :load-toplevel :execute)
531 ;;; LETTER-ATTRIBUTE is a local of SYMBOL-QUOTEP. It matches letters
532 ;;; that don't need to be escaped (according to READTABLE-CASE.)
533 (defparameter *attribute-names*
534 `((number . number-attribute) (lowercase . lowercase-attribute)
535 (uppercase . uppercase-attribute) (letter . letter-attribute)
536 (sign . sign-attribute) (extension . extension-attribute)
537 (dot . dot-attribute) (slash . slash-attribute)
538 (other . other-attribute) (funny . funny-attribute)))
540 ) ; EVAL-WHEN
542 (flet ((set-bit (char bit)
543 (let ((code (char-code char)))
544 (setf (aref *character-attributes* code)
545 (logior bit (aref *character-attributes* code))))))
547 (dolist (char '(#\! #\@ #\$ #\% #\& #\* #\= #\~ #\[ #\] #\{ #\}
548 #\? #\< #\>))
549 (set-bit char other-attribute))
551 (dotimes (i 10)
552 (set-bit (digit-char i) number-attribute))
554 (do ((code (char-code #\A) (1+ code))
555 (end (char-code #\Z)))
556 ((> code end))
557 (declare (fixnum code end))
558 (set-bit (code-char code) uppercase-attribute)
559 (set-bit (char-downcase (code-char code)) lowercase-attribute))
561 (set-bit #\- sign-attribute)
562 (set-bit #\+ sign-attribute)
563 (set-bit #\^ extension-attribute)
564 (set-bit #\_ extension-attribute)
565 (set-bit #\. dot-attribute)
566 (set-bit #\/ slash-attribute)
568 ;; Mark anything not explicitly allowed as funny.
569 (dotimes (i 160) ; FIXME
570 (when (zerop (aref *character-attributes* i))
571 (setf (aref *character-attributes* i) funny-attribute))))
573 ;;; For each character, the value of the corresponding element is the
574 ;;; lowest base in which that character is a digit.
575 (defvar *digit-bases*
576 (make-array 128 ; FIXME
577 :element-type '(unsigned-byte 8)
578 :initial-element 36))
579 (declaim (type (simple-array (unsigned-byte 8) (#.128)) ; FIXME
580 *digit-bases*))
581 (dotimes (i 36)
582 (let ((char (digit-char i 36)))
583 (setf (aref *digit-bases* (char-code char)) i)))
585 ;;; A FSM-like thingie that determines whether a symbol is a potential
586 ;;; number or has evil characters in it.
587 (defun symbol-quotep (name)
588 (declare (simple-string name))
589 (macrolet ((advance (tag &optional (at-end t))
590 `(progn
591 (when (= index len)
592 ,(if at-end '(go TEST-SIGN) '(return nil)))
593 (setq current (schar name index)
594 code (char-code current)
595 bits (cond ; FIXME
596 ((< code 160) (aref attributes code))
597 ((upper-case-p current) uppercase-attribute)
598 ((lower-case-p current) lowercase-attribute)
599 (t other-attribute)))
600 (incf index)
601 (go ,tag)))
602 (test (&rest attributes)
603 `(not (zerop
604 (the fixnum
605 (logand
606 (logior ,@(mapcar
607 (lambda (x)
608 (or (cdr (assoc x
609 *attribute-names*))
610 (error "Blast!")))
611 attributes))
612 bits)))))
613 (digitp ()
614 `(and (< code 128) ; FIXME
615 (< (the fixnum (aref bases code)) base))))
617 (prog ((len (length name))
618 (attributes *character-attributes*)
619 (bases *digit-bases*)
620 (base *print-base*)
621 (letter-attribute
622 (case (readtable-case *readtable*)
623 (:upcase uppercase-attribute)
624 (:downcase lowercase-attribute)
625 (t (logior lowercase-attribute uppercase-attribute))))
626 (index 0)
627 (bits 0)
628 (code 0)
629 current)
630 (declare (fixnum len base index bits code))
631 (advance START t)
633 TEST-SIGN ; At end, see whether it is a sign...
634 (return (not (test sign)))
636 OTHER ; not potential number, see whether funny chars...
637 (let ((mask (logxor (logior lowercase-attribute uppercase-attribute
638 funny-attribute)
639 letter-attribute)))
640 (do ((i (1- index) (1+ i)))
641 ((= i len) (return-from symbol-quotep nil))
642 (unless (zerop (logand (let* ((char (schar name i))
643 (code (char-code char)))
644 (cond
645 ((< code 160) (aref attributes code))
646 ((upper-case-p char) uppercase-attribute)
647 ((lower-case-p char) lowercase-attribute)
648 (t other-attribute)))
649 mask))
650 (return-from symbol-quotep t))))
652 START
653 (when (digitp)
654 (if (test letter)
655 (advance LAST-DIGIT-ALPHA)
656 (advance DIGIT)))
657 (when (test letter number other slash) (advance OTHER nil))
658 (when (char= current #\.) (advance DOT-FOUND))
659 (when (test sign extension) (advance START-STUFF nil))
660 (return t)
662 DOT-FOUND ; leading dots...
663 (when (test letter) (advance START-DOT-MARKER nil))
664 (when (digitp) (advance DOT-DIGIT))
665 (when (test number other) (advance OTHER nil))
666 (when (test extension slash sign) (advance START-DOT-STUFF nil))
667 (when (char= current #\.) (advance DOT-FOUND))
668 (return t)
670 START-STUFF ; leading stuff before any dot or digit
671 (when (digitp)
672 (if (test letter)
673 (advance LAST-DIGIT-ALPHA)
674 (advance DIGIT)))
675 (when (test number other) (advance OTHER nil))
676 (when (test letter) (advance START-MARKER nil))
677 (when (char= current #\.) (advance START-DOT-STUFF nil))
678 (when (test sign extension slash) (advance START-STUFF nil))
679 (return t)
681 START-MARKER ; number marker in leading stuff...
682 (when (test letter) (advance OTHER nil))
683 (go START-STUFF)
685 START-DOT-STUFF ; leading stuff containing dot without digit...
686 (when (test letter) (advance START-DOT-STUFF nil))
687 (when (digitp) (advance DOT-DIGIT))
688 (when (test sign extension dot slash) (advance START-DOT-STUFF nil))
689 (when (test number other) (advance OTHER nil))
690 (return t)
692 START-DOT-MARKER ; number marker in leading stuff with dot..
693 ;; leading stuff containing dot without digit followed by letter...
694 (when (test letter) (advance OTHER nil))
695 (go START-DOT-STUFF)
697 DOT-DIGIT ; in a thing with dots...
698 (when (test letter) (advance DOT-MARKER))
699 (when (digitp) (advance DOT-DIGIT))
700 (when (test number other) (advance OTHER nil))
701 (when (test sign extension dot slash) (advance DOT-DIGIT))
702 (return t)
704 DOT-MARKER ; number marker in number with dot...
705 (when (test letter) (advance OTHER nil))
706 (go DOT-DIGIT)
708 LAST-DIGIT-ALPHA ; previous char is a letter digit...
709 (when (or (digitp) (test sign slash))
710 (advance ALPHA-DIGIT))
711 (when (test letter number other dot) (advance OTHER nil))
712 (return t)
714 ALPHA-DIGIT ; seen a digit which is a letter...
715 (when (or (digitp) (test sign slash))
716 (if (test letter)
717 (advance LAST-DIGIT-ALPHA)
718 (advance ALPHA-DIGIT)))
719 (when (test letter) (advance ALPHA-MARKER))
720 (when (test number other dot) (advance OTHER nil))
721 (return t)
723 ALPHA-MARKER ; number marker in number with alpha digit...
724 (when (test letter) (advance OTHER nil))
725 (go ALPHA-DIGIT)
727 DIGIT ; seen only ordinary (non-alphabetic) numeric digits...
728 (when (digitp)
729 (if (test letter)
730 (advance ALPHA-DIGIT)
731 (advance DIGIT)))
732 (when (test number other) (advance OTHER nil))
733 (when (test letter) (advance MARKER))
734 (when (test extension slash sign) (advance DIGIT))
735 (when (char= current #\.) (advance DOT-DIGIT))
736 (return t)
738 MARKER ; number marker in a numeric number...
739 ;; ("What," you may ask, "is a 'number marker'?" It's something
740 ;; that a conforming implementation might use in number syntax.
741 ;; See ANSI 2.3.1.1 "Potential Numbers as Tokens".)
742 (when (test letter) (advance OTHER nil))
743 (go DIGIT))))
745 ;;;; *INTERNAL-SYMBOL-OUTPUT-FUN*
746 ;;;;
747 ;;;; case hackery: These functions are stored in
748 ;;;; *INTERNAL-SYMBOL-OUTPUT-FUN* according to the values of
749 ;;;; *PRINT-CASE* and READTABLE-CASE.
751 ;;; called when:
752 ;;; READTABLE-CASE *PRINT-CASE*
753 ;;; :UPCASE :UPCASE
754 ;;; :DOWNCASE :DOWNCASE
755 ;;; :PRESERVE any
756 (defun output-preserve-symbol (pname stream)
757 (declare (simple-string pname))
758 (write-string pname stream))
760 ;;; called when:
761 ;;; READTABLE-CASE *PRINT-CASE*
762 ;;; :UPCASE :DOWNCASE
763 (defun output-lowercase-symbol (pname stream)
764 (declare (simple-string pname))
765 (dotimes (index (length pname))
766 (let ((char (schar pname index)))
767 (write-char (char-downcase char) stream))))
769 ;;; called when:
770 ;;; READTABLE-CASE *PRINT-CASE*
771 ;;; :DOWNCASE :UPCASE
772 (defun output-uppercase-symbol (pname stream)
773 (declare (simple-string pname))
774 (dotimes (index (length pname))
775 (let ((char (schar pname index)))
776 (write-char (char-upcase char) stream))))
778 ;;; called when:
779 ;;; READTABLE-CASE *PRINT-CASE*
780 ;;; :UPCASE :CAPITALIZE
781 ;;; :DOWNCASE :CAPITALIZE
782 (defun output-capitalize-symbol (pname stream)
783 (declare (simple-string pname))
784 (let ((prev-not-alphanum t)
785 (up (eq (readtable-case *readtable*) :upcase)))
786 (dotimes (i (length pname))
787 (let ((char (char pname i)))
788 (write-char (if up
789 (if (or prev-not-alphanum (lower-case-p char))
790 char
791 (char-downcase char))
792 (if prev-not-alphanum
793 (char-upcase char)
794 char))
795 stream)
796 (setq prev-not-alphanum (not (alphanumericp char)))))))
798 ;;; called when:
799 ;;; READTABLE-CASE *PRINT-CASE*
800 ;;; :INVERT any
801 (defun output-invert-symbol (pname stream)
802 (declare (simple-string pname))
803 (let ((all-upper t)
804 (all-lower t))
805 (dotimes (i (length pname))
806 (let ((ch (schar pname i)))
807 (when (both-case-p ch)
808 (if (upper-case-p ch)
809 (setq all-lower nil)
810 (setq all-upper nil)))))
811 (cond (all-upper (output-lowercase-symbol pname stream))
812 (all-lower (output-uppercase-symbol pname stream))
814 (write-string pname stream)))))
817 (defun test1 ()
818 (let ((*readtable* (copy-readtable nil)))
819 (format t "READTABLE-CASE Input Symbol-name~@
820 ----------------------------------~%")
821 (dolist (readtable-case '(:upcase :downcase :preserve :invert))
822 (setf (readtable-case *readtable*) readtable-case)
823 (dolist (input '("ZEBRA" "Zebra" "zebra"))
824 (format t "~&:~A~16T~A~24T~A"
825 (string-upcase readtable-case)
826 input
827 (symbol-name (read-from-string input)))))))
829 (defun test2 ()
830 (let ((*readtable* (copy-readtable nil)))
831 (format t "READTABLE-CASE *PRINT-CASE* Symbol-name Output Princ~@
832 --------------------------------------------------------~%")
833 (dolist (readtable-case '(:upcase :downcase :preserve :invert))
834 (setf (readtable-case *readtable*) readtable-case)
835 (dolist (*print-case* '(:upcase :downcase :capitalize))
836 (dolist (symbol '(|ZEBRA| |Zebra| |zebra|))
837 (format t "~&:~A~15T:~A~29T~A~42T~A~50T~A"
838 (string-upcase readtable-case)
839 (string-upcase *print-case*)
840 (symbol-name symbol)
841 (prin1-to-string symbol)
842 (princ-to-string symbol)))))))
845 ;;;; recursive objects
847 (defun output-list (list stream)
848 (descend-into (stream)
849 (write-char #\( stream)
850 (let ((length 0)
851 (list list))
852 (loop
853 (punt-print-if-too-long length stream)
854 (output-object (pop list) stream)
855 (unless list
856 (return))
857 (when (or (atom list)
858 (check-for-circularity list))
859 (write-string " . " stream)
860 (output-object list stream)
861 (return))
862 (write-char #\space stream)
863 (incf length)))
864 (write-char #\) stream)))
866 (defun output-vector (vector stream)
867 (declare (vector vector))
868 (cond ((stringp vector)
869 (cond ((and *print-readably*
870 (not (eq (array-element-type vector)
871 (load-time-value
872 (array-element-type
873 (make-array 0 :element-type 'character))))))
874 (error 'print-not-readable :object vector))
875 ((or *print-escape* *print-readably*)
876 (write-char #\" stream)
877 (quote-string vector stream)
878 (write-char #\" stream))
880 (write-string vector stream))))
881 ((not (or *print-array* *print-readably*))
882 (output-terse-array vector stream))
883 ((bit-vector-p vector)
884 (write-string "#*" stream)
885 (dovector (bit vector)
886 ;; (Don't use OUTPUT-OBJECT here, since this code
887 ;; has to work for all possible *PRINT-BASE* values.)
888 (write-char (if (zerop bit) #\0 #\1) stream)))
890 (when (and *print-readably*
891 (not (array-readably-printable-p vector)))
892 (error 'print-not-readable :object vector))
893 (descend-into (stream)
894 (write-string "#(" stream)
895 (dotimes (i (length vector))
896 (unless (zerop i)
897 (write-char #\space stream))
898 (punt-print-if-too-long i stream)
899 (output-object (aref vector i) stream))
900 (write-string ")" stream)))))
902 ;;; This function outputs a string quoting characters sufficiently
903 ;;; so that someone can read it in again. Basically, put a slash in
904 ;;; front of an character satisfying NEEDS-SLASH-P.
905 (defun quote-string (string stream)
906 (macrolet ((needs-slash-p (char)
907 ;; KLUDGE: We probably should look at the readtable, but just do
908 ;; this for now. [noted by anonymous long ago] -- WHN 19991130
909 `(or (char= ,char #\\)
910 (char= ,char #\"))))
911 (with-array-data ((data string) (start) (end)
912 :check-fill-pointer t)
913 (do ((index start (1+ index)))
914 ((>= index end))
915 (let ((char (schar data index)))
916 (when (needs-slash-p char) (write-char #\\ stream))
917 (write-char char stream))))))
919 (defun array-readably-printable-p (array)
920 (and (eq (array-element-type array) t)
921 (let ((zero (position 0 (array-dimensions array)))
922 (number (position 0 (array-dimensions array)
923 :test (complement #'eql)
924 :from-end t)))
925 (or (null zero) (null number) (> zero number)))))
927 ;;; Output the printed representation of any array in either the #< or #A
928 ;;; form.
929 (defun output-array (array stream)
930 (if (or *print-array* *print-readably*)
931 (output-array-guts array stream)
932 (output-terse-array array stream)))
934 ;;; Output the abbreviated #< form of an array.
935 (defun output-terse-array (array stream)
936 (let ((*print-level* nil)
937 (*print-length* nil))
938 (print-unreadable-object (array stream :type t :identity t))))
940 ;;; Output the readable #A form of an array.
941 (defun output-array-guts (array stream)
942 (when (and *print-readably*
943 (not (array-readably-printable-p array)))
944 (error 'print-not-readable :object array))
945 (write-char #\# stream)
946 (let ((*print-base* 10)
947 (*print-radix* nil))
948 (output-integer (array-rank array) stream))
949 (write-char #\A stream)
950 (with-array-data ((data array) (start) (end))
951 (declare (ignore end))
952 (sub-output-array-guts data (array-dimensions array) stream start)))
954 (defun sub-output-array-guts (array dimensions stream index)
955 (declare (type (simple-array * (*)) array) (fixnum index))
956 (cond ((null dimensions)
957 (output-object (aref array index) stream))
959 (descend-into (stream)
960 (write-char #\( stream)
961 (let* ((dimension (car dimensions))
962 (dimensions (cdr dimensions))
963 (count (reduce #'* dimensions)))
964 (dotimes (i dimension)
965 (unless (zerop i)
966 (write-char #\space stream))
967 (punt-print-if-too-long i stream)
968 (sub-output-array-guts array dimensions stream index)
969 (incf index count)))
970 (write-char #\) stream)))))
972 ;;; a trivial non-generic-function placeholder for PRINT-OBJECT, for
973 ;;; use until CLOS is set up (at which time it will be replaced with
974 ;;; the real generic function implementation)
975 (defun print-object (instance stream)
976 (default-structure-print instance stream *current-level-in-print*))
978 ;;;; integer, ratio, and complex printing (i.e. everything but floats)
980 (defun %output-radix (base stream)
981 (write-char #\# stream)
982 (write-char (case base
983 (2 #\b)
984 (8 #\o)
985 (16 #\x)
986 (t (%output-reasonable-integer-in-base base 10 stream)
987 #\r))
988 stream))
990 (defun %output-reasonable-integer-in-base (n base stream)
991 (multiple-value-bind (q r)
992 (truncate n base)
993 ;; Recurse until you have all the digits pushed on
994 ;; the stack.
995 (unless (zerop q)
996 (%output-reasonable-integer-in-base q base stream))
997 ;; Then as each recursive call unwinds, turn the
998 ;; digit (in remainder) into a character and output
999 ;; the character.
1000 (write-char
1001 (schar "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" r)
1002 stream)))
1004 ;;; *POWER-CACHE* is an alist mapping bases to power-vectors. It is
1005 ;;; filled and probed by POWERS-FOR-BASE. SCRUB-POWER-CACHE is called
1006 ;;; always prior a GC to drop overly large bignums from the cache.
1008 ;;; It doesn't need a lock, but if you work on SCRUB-POWER-CACHE or
1009 ;;; POWERS-FOR-BASE, see that you don't break the assumptions!
1010 (defvar *power-cache* nil)
1012 (defconstant +power-cache-integer-length-limit+ 2048)
1014 (defun scrub-power-cache ()
1015 (let ((cache *power-cache*))
1016 (dolist (cell cache)
1017 (let ((powers (cdr cell)))
1018 (declare (simple-vector powers))
1019 (let ((too-big (position-if
1020 (lambda (x)
1021 (>= (integer-length x)
1022 +power-cache-integer-length-limit+))
1023 powers)))
1024 (when too-big
1025 (setf (cdr cell) (subseq powers 0 too-big))))))
1026 ;; Since base 10 is overwhelmingly common, make sure it's at head.
1027 ;; Try to keep other bases in a hopefully sensible order as well.
1028 (if (eql 10 (caar cache))
1029 (setf *power-cache* cache)
1030 ;; If we modify the list destructively we need to copy it, otherwise
1031 ;; an alist lookup in progress might be screwed.
1032 (setf *power-cache* (sort (copy-list cache)
1033 (lambda (a b)
1034 (declare (fixnum a b))
1035 (cond ((= 10 a) t)
1036 ((= 10 b) nil)
1037 ((= 16 a) t)
1038 ((= 16 b) nil)
1039 ((= 2 a) t)
1040 ((= 2 b) nil)
1041 (t (< a b))))
1042 :key #'car)))))
1044 ;;; Compute (and cache) a power vector for a BASE and LIMIT:
1045 ;;; the vector holds integers for which
1046 ;;; (aref powers k) == (expt base (expt 2 k))
1047 ;;; holds.
1048 (defun powers-for-base (base limit)
1049 (flet ((compute-powers (from)
1050 (let (powers)
1051 (do ((p from (* p p)))
1052 ((> p limit)
1053 ;; We don't actually need this, but we also
1054 ;; prefer not to cons it up a second time...
1055 (push p powers))
1056 (push p powers))
1057 (nreverse powers))))
1058 ;; Grab a local reference so that we won't stuff consed at the
1059 ;; head by other threads -- or sorting by SCRUB-POWER-CACHE.
1060 (let ((cache *power-cache*))
1061 (let ((cell (assoc base cache)))
1062 (if cell
1063 (let* ((powers (cdr cell))
1064 (len (length powers))
1065 (max (svref powers (1- len))))
1066 (if (> max limit)
1067 powers
1068 (let ((new
1069 (concatenate 'vector powers
1070 (compute-powers (* max max)))))
1071 (setf (cdr cell) new)
1072 new)))
1073 (let ((powers (coerce (compute-powers base) 'vector)))
1074 ;; Add new base to head: SCRUB-POWER-CACHE will later
1075 ;; put it to a better place.
1076 (setf *power-cache* (acons base powers cache))
1077 powers))))))
1079 ;; Algorithm by Harald Hanche-Olsen, sbcl-devel 2005-02-05
1080 (defun %output-huge-integer-in-base (n base stream)
1081 (declare (type bignum n) (type fixnum base))
1082 ;; POWER is a vector for which the following holds:
1083 ;; (aref power k) == (expt base (expt 2 k))
1084 (let* ((power (powers-for-base base n))
1085 (k-start (or (position-if (lambda (x) (> x n)) power)
1086 (bug "power-vector too short"))))
1087 (labels ((bisect (n k exactp)
1088 (declare (fixnum k))
1089 ;; N is the number to bisect
1090 ;; K on initial entry BASE^(2^K) > N
1091 ;; EXACTP is true if 2^K is the exact number of digits
1092 (cond ((zerop n)
1093 (when exactp
1094 (loop repeat (ash 1 k) do (write-char #\0 stream))))
1095 ((zerop k)
1096 (write-char
1097 (schar "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" n)
1098 stream))
1100 (setf k (1- k))
1101 (multiple-value-bind (q r) (truncate n (aref power k))
1102 ;; EXACTP is NIL only at the head of the
1103 ;; initial number, as we don't know the number
1104 ;; of digits there, but we do know that it
1105 ;; doesn't get any leading zeros.
1106 (bisect q k exactp)
1107 (bisect r k (or exactp (plusp q))))))))
1108 (bisect n k-start nil))))
1110 (defun %output-integer-in-base (integer base stream)
1111 (when (minusp integer)
1112 (write-char #\- stream)
1113 (setf integer (- integer)))
1114 ;; The ideal cutoff point between these two algorithms is almost
1115 ;; certainly quite platform dependent: this gives 87 for 32 bit
1116 ;; SBCL, which is about right at least for x86/Darwin.
1117 (if (or (fixnump integer)
1118 (< (integer-length integer) (* 3 sb!vm:n-positive-fixnum-bits)))
1119 (%output-reasonable-integer-in-base integer base stream)
1120 (%output-huge-integer-in-base integer base stream)))
1122 (defun output-integer (integer stream)
1123 (let ((base *print-base*))
1124 (when (and (/= base 10) *print-radix*)
1125 (%output-radix base stream))
1126 (%output-integer-in-base integer base stream)
1127 (when (and *print-radix* (= base 10))
1128 (write-char #\. stream))))
1130 (defun output-ratio (ratio stream)
1131 (let ((base *print-base*))
1132 (when *print-radix*
1133 (%output-radix base stream))
1134 (%output-integer-in-base (numerator ratio) base stream)
1135 (write-char #\/ stream)
1136 (%output-integer-in-base (denominator ratio) base stream)))
1138 (defun output-complex (complex stream)
1139 (write-string "#C(" stream)
1140 ;; FIXME: Could this just be OUTPUT-NUMBER?
1141 (output-object (realpart complex) stream)
1142 (write-char #\space stream)
1143 (output-object (imagpart complex) stream)
1144 (write-char #\) stream))
1146 ;;;; float printing
1148 ;;; FLONUM-TO-STRING (and its subsidiary function FLOAT-STRING) does
1149 ;;; most of the work for all printing of floating point numbers in
1150 ;;; FORMAT. It converts a floating point number to a string in a free
1151 ;;; or fixed format with no exponent. The interpretation of the
1152 ;;; arguments is as follows:
1154 ;;; X - The floating point number to convert, which must not be
1155 ;;; negative.
1156 ;;; WIDTH - The preferred field width, used to determine the number
1157 ;;; of fraction digits to produce if the FDIGITS parameter
1158 ;;; is unspecified or NIL. If the non-fraction digits and the
1159 ;;; decimal point alone exceed this width, no fraction digits
1160 ;;; will be produced unless a non-NIL value of FDIGITS has been
1161 ;;; specified. Field overflow is not considerd an error at this
1162 ;;; level.
1163 ;;; FDIGITS - The number of fractional digits to produce. Insignificant
1164 ;;; trailing zeroes may be introduced as needed. May be
1165 ;;; unspecified or NIL, in which case as many digits as possible
1166 ;;; are generated, subject to the constraint that there are no
1167 ;;; trailing zeroes.
1168 ;;; SCALE - If this parameter is specified or non-NIL, then the number
1169 ;;; printed is (* x (expt 10 scale)). This scaling is exact,
1170 ;;; and cannot lose precision.
1171 ;;; FMIN - This parameter, if specified or non-NIL, is the minimum
1172 ;;; number of fraction digits which will be produced, regardless
1173 ;;; of the value of WIDTH or FDIGITS. This feature is used by
1174 ;;; the ~E format directive to prevent complete loss of
1175 ;;; significance in the printed value due to a bogus choice of
1176 ;;; scale factor.
1178 ;;; Returns:
1179 ;;; (VALUES DIGIT-STRING DIGIT-LENGTH LEADING-POINT TRAILING-POINT DECPNT)
1180 ;;; where the results have the following interpretation:
1182 ;;; DIGIT-STRING - The decimal representation of X, with decimal point.
1183 ;;; DIGIT-LENGTH - The length of the string DIGIT-STRING.
1184 ;;; LEADING-POINT - True if the first character of DIGIT-STRING is the
1185 ;;; decimal point.
1186 ;;; TRAILING-POINT - True if the last character of DIGIT-STRING is the
1187 ;;; decimal point.
1188 ;;; POINT-POS - The position of the digit preceding the decimal
1189 ;;; point. Zero indicates point before first digit.
1191 ;;; NOTE: FLONUM-TO-STRING goes to a lot of trouble to guarantee
1192 ;;; accuracy. Specifically, the decimal number printed is the closest
1193 ;;; possible approximation to the true value of the binary number to
1194 ;;; be printed from among all decimal representations with the same
1195 ;;; number of digits. In free-format output, i.e. with the number of
1196 ;;; digits unconstrained, it is guaranteed that all the information is
1197 ;;; preserved, so that a properly- rounding reader can reconstruct the
1198 ;;; original binary number, bit-for-bit, from its printed decimal
1199 ;;; representation. Furthermore, only as many digits as necessary to
1200 ;;; satisfy this condition will be printed.
1202 ;;; FLOAT-DIGITS actually generates the digits for positive numbers;
1203 ;;; see below for comments.
1205 (defun flonum-to-string (x &optional width fdigits scale fmin)
1206 (declare (type float x))
1207 ;; FIXME: I think only FORMAT-DOLLARS calls FLONUM-TO-STRING with
1208 ;; possibly-negative X.
1209 (setf x (abs x))
1210 (cond ((zerop x)
1211 ;; Zero is a special case which FLOAT-STRING cannot handle.
1212 (if fdigits
1213 (let ((s (make-string (1+ fdigits) :initial-element #\0)))
1214 (setf (schar s 0) #\.)
1215 (values s (length s) t (zerop fdigits) 0))
1216 (values "." 1 t t 0)))
1218 (multiple-value-bind (e string)
1219 (if fdigits
1220 (flonum-to-digits x (min (- (+ fdigits (or scale 0)))
1221 (- (or fmin 0))))
1222 (if (and width (> width 1))
1223 (let ((w (multiple-value-list
1224 (flonum-to-digits x
1225 (max 1
1226 (+ (1- width)
1227 (if (and scale (minusp scale))
1228 scale 0)))
1229 t)))
1230 (f (multiple-value-list
1231 (flonum-to-digits x (- (+ (or fmin 0)
1232 (if scale scale 0)))))))
1233 (cond
1234 ((>= (length (cadr w)) (length (cadr f)))
1235 (values-list w))
1236 (t (values-list f))))
1237 (flonum-to-digits x)))
1238 (let ((e (+ e (or scale 0)))
1239 (stream (make-string-output-stream)))
1240 (if (plusp e)
1241 (progn
1242 (write-string string stream :end (min (length string)
1244 (dotimes (i (- e (length string)))
1245 (write-char #\0 stream))
1246 (write-char #\. stream)
1247 (write-string string stream :start (min (length
1248 string) e))
1249 (when fdigits
1250 (dotimes (i (- fdigits
1251 (- (length string)
1252 (min (length string) e))))
1253 (write-char #\0 stream))))
1254 (progn
1255 (write-string "." stream)
1256 (dotimes (i (- e))
1257 (write-char #\0 stream))
1258 (write-string string stream)
1259 (when fdigits
1260 (dotimes (i (+ fdigits e (- (length string))))
1261 (write-char #\0 stream)))))
1262 (let ((string (get-output-stream-string stream)))
1263 (values string (length string)
1264 (char= (char string 0) #\.)
1265 (char= (char string (1- (length string))) #\.)
1266 (position #\. string))))))))
1268 ;;; implementation of figure 1 from Burger and Dybvig, 1996. As the
1269 ;;; implementation of the Dragon from Classic CMUCL (and previously in
1270 ;;; SBCL above FLONUM-TO-STRING) says: "DO NOT EVEN THINK OF
1271 ;;; ATTEMPTING TO UNDERSTAND THIS CODE WITHOUT READING THE PAPER!",
1272 ;;; and in this case we have to add that even reading the paper might
1273 ;;; not bring immediate illumination as CSR has attempted to turn
1274 ;;; idiomatic Scheme into idiomatic Lisp.
1276 ;;; FIXME: figure 1 from Burger and Dybvig is the unoptimized
1277 ;;; algorithm, noticeably slow at finding the exponent. Figure 2 has
1278 ;;; an improved algorithm, but CSR ran out of energy.
1280 ;;; possible extension for the enthusiastic: printing floats in bases
1281 ;;; other than base 10.
1282 (defconstant single-float-min-e
1283 (nth-value 1 (decode-float least-positive-single-float)))
1284 (defconstant double-float-min-e
1285 (nth-value 1 (decode-float least-positive-double-float)))
1286 #!+long-float
1287 (defconstant long-float-min-e
1288 (nth-value 1 (decode-float least-positive-long-float)))
1290 (defun flonum-to-digits (v &optional position relativep)
1291 (let ((print-base 10) ; B
1292 (float-radix 2) ; b
1293 (float-digits (float-digits v)) ; p
1294 (digit-characters "0123456789")
1295 (min-e
1296 (etypecase v
1297 (single-float single-float-min-e)
1298 (double-float double-float-min-e)
1299 #!+long-float
1300 (long-float long-float-min-e))))
1301 (multiple-value-bind (f e)
1302 (integer-decode-float v)
1303 (let (;; FIXME: these even tests assume normal IEEE rounding
1304 ;; mode. I wonder if we should cater for non-normal?
1305 (high-ok (evenp f))
1306 (low-ok (evenp f)))
1307 (with-push-char (:element-type base-char)
1308 (labels ((scale (r s m+ m-)
1309 (do ((k 0 (1+ k))
1310 (s s (* s print-base)))
1311 ((not (or (> (+ r m+) s)
1312 (and high-ok (= (+ r m+) s))))
1313 (do ((k k (1- k))
1314 (r r (* r print-base))
1315 (m+ m+ (* m+ print-base))
1316 (m- m- (* m- print-base)))
1317 ((not (or (< (* (+ r m+) print-base) s)
1318 (and (not high-ok)
1319 (= (* (+ r m+) print-base) s))))
1320 (values k (generate r s m+ m-)))))))
1321 (generate (r s m+ m-)
1322 (let (d tc1 tc2)
1323 (tagbody
1324 loop
1325 (setf (values d r) (truncate (* r print-base) s))
1326 (setf m+ (* m+ print-base))
1327 (setf m- (* m- print-base))
1328 (setf tc1 (or (< r m-) (and low-ok (= r m-))))
1329 (setf tc2 (or (> (+ r m+) s)
1330 (and high-ok (= (+ r m+) s))))
1331 (when (or tc1 tc2)
1332 (go end))
1333 (push-char (char digit-characters d))
1334 (go loop)
1336 (let ((d (cond
1337 ((and (not tc1) tc2) (1+ d))
1338 ((and tc1 (not tc2)) d)
1339 (t ; (and tc1 tc2)
1340 (if (< (* r 2) s) d (1+ d))))))
1341 (push-char (char digit-characters d))
1342 (return-from generate (get-pushed-string))))))
1343 (initialize ()
1344 (let (r s m+ m-)
1345 (if (>= e 0)
1346 (let* ((be (expt float-radix e))
1347 (be1 (* be float-radix)))
1348 (if (/= f (expt float-radix (1- float-digits)))
1349 (setf r (* f be 2)
1351 m+ be
1352 m- be)
1353 (setf r (* f be1 2)
1354 s (* float-radix 2)
1355 m+ be1
1356 m- be)))
1357 (if (or (= e min-e)
1358 (/= f (expt float-radix (1- float-digits))))
1359 (setf r (* f 2)
1360 s (* (expt float-radix (- e)) 2)
1361 m+ 1
1362 m- 1)
1363 (setf r (* f float-radix 2)
1364 s (* (expt float-radix (- 1 e)) 2)
1365 m+ float-radix
1366 m- 1)))
1367 (when position
1368 (when relativep
1369 (aver (> position 0))
1370 (do ((k 0 (1+ k))
1371 ;; running out of letters here
1372 (l 1 (* l print-base)))
1373 ((>= (* s l) (+ r m+))
1374 ;; k is now \hat{k}
1375 (if (< (+ r (* s (/ (expt print-base (- k position)) 2)))
1376 (* s (expt print-base k)))
1377 (setf position (- k position))
1378 (setf position (- k position 1))))))
1379 (let ((low (max m- (/ (* s (expt print-base position)) 2)))
1380 (high (max m+ (/ (* s (expt print-base position)) 2))))
1381 (when (<= m- low)
1382 (setf m- low)
1383 (setf low-ok t))
1384 (when (<= m+ high)
1385 (setf m+ high)
1386 (setf high-ok t))))
1387 (values r s m+ m-))))
1388 (multiple-value-bind (r s m+ m-) (initialize)
1389 (scale r s m+ m-))))))))
1391 ;;; Given a non-negative floating point number, SCALE-EXPONENT returns
1392 ;;; a new floating point number Z in the range (0.1, 1.0] and an
1393 ;;; exponent E such that Z * 10^E is (approximately) equal to the
1394 ;;; original number. There may be some loss of precision due the
1395 ;;; floating point representation. The scaling is always done with
1396 ;;; long float arithmetic, which helps printing of lesser precisions
1397 ;;; as well as avoiding generic arithmetic.
1399 ;;; When computing our initial scale factor using EXPT, we pull out
1400 ;;; part of the computation to avoid over/under flow. When
1401 ;;; denormalized, we must pull out a large factor, since there is more
1402 ;;; negative exponent range than positive range.
1404 (eval-when (:compile-toplevel :execute)
1405 (setf *read-default-float-format*
1406 #!+long-float 'long-float #!-long-float 'double-float))
1407 (defun scale-exponent (original-x)
1408 (let* ((x (coerce original-x 'long-float)))
1409 (multiple-value-bind (sig exponent) (decode-float x)
1410 (declare (ignore sig))
1411 (if (= x 0.0e0)
1412 (values (float 0.0e0 original-x) 1)
1413 (let* ((ex (locally (declare (optimize (safety 0)))
1414 (the fixnum
1415 (round (* exponent (log 2e0 10))))))
1416 (x (if (minusp ex)
1417 (if (float-denormalized-p x)
1418 #!-long-float
1419 (* x 1.0e16 (expt 10.0e0 (- (- ex) 16)))
1420 #!+long-float
1421 (* x 1.0e18 (expt 10.0e0 (- (- ex) 18)))
1422 (* x 10.0e0 (expt 10.0e0 (- (- ex) 1))))
1423 (/ x 10.0e0 (expt 10.0e0 (1- ex))))))
1424 (do ((d 10.0e0 (* d 10.0e0))
1425 (y x (/ x d))
1426 (ex ex (1+ ex)))
1427 ((< y 1.0e0)
1428 (do ((m 10.0e0 (* m 10.0e0))
1429 (z y (* y m))
1430 (ex ex (1- ex)))
1431 ((>= z 0.1e0)
1432 (values (float z original-x) ex))
1433 (declare (long-float m) (integer ex))))
1434 (declare (long-float d))))))))
1435 (eval-when (:compile-toplevel :execute)
1436 (setf *read-default-float-format* 'single-float))
1438 ;;;; entry point for the float printer
1440 ;;; the float printer as called by PRINT, PRIN1, PRINC, etc. The
1441 ;;; argument is printed free-format, in either exponential or
1442 ;;; non-exponential notation, depending on its magnitude.
1444 ;;; NOTE: When a number is to be printed in exponential format, it is
1445 ;;; scaled in floating point. Since precision may be lost in this
1446 ;;; process, the guaranteed accuracy properties of FLONUM-TO-STRING
1447 ;;; are lost. The difficulty is that FLONUM-TO-STRING performs
1448 ;;; extensive computations with integers of similar magnitude to that
1449 ;;; of the number being printed. For large exponents, the bignums
1450 ;;; really get out of hand. If bignum arithmetic becomes reasonably
1451 ;;; fast and the exponent range is not too large, then it might become
1452 ;;; attractive to handle exponential notation with the same accuracy
1453 ;;; as non-exponential notation, using the method described in the
1454 ;;; Steele and White paper.
1456 ;;; NOTE II: this has been bypassed slightly by implementing Burger
1457 ;;; and Dybvig, 1996. When someone has time (KLUDGE) they can
1458 ;;; probably (a) implement the optimizations suggested by Burger and
1459 ;;; Dyvbig, and (b) remove all vestiges of Dragon4, including from
1460 ;;; fixed-format printing.
1462 ;;; Print the appropriate exponent marker for X and the specified exponent.
1463 (defun print-float-exponent (x exp stream)
1464 (declare (type float x) (type integer exp) (type stream stream))
1465 (let ((*print-radix* nil))
1466 (if (typep x *read-default-float-format*)
1467 (unless (eql exp 0)
1468 (format stream "e~D" exp))
1469 (format stream "~C~D"
1470 (etypecase x
1471 (single-float #\f)
1472 (double-float #\d)
1473 (short-float #\s)
1474 (long-float #\L))
1475 exp))))
1477 (defun output-float-infinity (x stream)
1478 (declare (float x) (stream stream))
1479 (cond (*read-eval*
1480 (write-string "#." stream))
1481 (*print-readably*
1482 (error 'print-not-readable :object x))
1484 (write-string "#<" stream)))
1485 (write-string "SB-EXT:" stream)
1486 (write-string (symbol-name (float-format-name x)) stream)
1487 (write-string (if (plusp x) "-POSITIVE-" "-NEGATIVE-")
1488 stream)
1489 (write-string "INFINITY" stream)
1490 (unless *read-eval*
1491 (write-string ">" stream)))
1493 (defun output-float-nan (x stream)
1494 (print-unreadable-object (x stream)
1495 (princ (float-format-name x) stream)
1496 (write-string (if (float-trapping-nan-p x) " trapping" " quiet") stream)
1497 (write-string " NaN" stream)))
1499 ;;; the function called by OUTPUT-OBJECT to handle floats
1500 (defun output-float (x stream)
1501 (cond
1502 ((float-infinity-p x)
1503 (output-float-infinity x stream))
1504 ((float-nan-p x)
1505 (output-float-nan x stream))
1507 (let ((x (cond ((minusp (float-sign x))
1508 (write-char #\- stream)
1509 (- x))
1511 x))))
1512 (cond
1513 ((zerop x)
1514 (write-string "0.0" stream)
1515 (print-float-exponent x 0 stream))
1517 (output-float-aux x stream -3 8)))))))
1519 (defun output-float-aux (x stream e-min e-max)
1520 (multiple-value-bind (e string)
1521 (flonum-to-digits x)
1522 (cond
1523 ((< e-min e e-max)
1524 (if (plusp e)
1525 (progn
1526 (write-string string stream :end (min (length string) e))
1527 (dotimes (i (- e (length string)))
1528 (write-char #\0 stream))
1529 (write-char #\. stream)
1530 (write-string string stream :start (min (length string) e))
1531 (when (<= (length string) e)
1532 (write-char #\0 stream))
1533 (print-float-exponent x 0 stream))
1534 (progn
1535 (write-string "0." stream)
1536 (dotimes (i (- e))
1537 (write-char #\0 stream))
1538 (write-string string stream)
1539 (print-float-exponent x 0 stream))))
1540 (t (write-string string stream :end 1)
1541 (write-char #\. stream)
1542 (write-string string stream :start 1)
1543 (print-float-exponent x (1- e) stream)))))
1545 ;;;; other leaf objects
1547 ;;; If *PRINT-ESCAPE* is false, just do a WRITE-CHAR, otherwise output
1548 ;;; the character name or the character in the #\char format.
1549 (defun output-character (char stream)
1550 (if (or *print-escape* *print-readably*)
1551 (let ((graphicp (and (graphic-char-p char)
1552 (standard-char-p char)))
1553 (name (char-name char)))
1554 (write-string "#\\" stream)
1555 (if (and name (not graphicp))
1556 (quote-string name stream)
1557 (write-char char stream)))
1558 (write-char char stream)))
1560 (defun output-sap (sap stream)
1561 (declare (type system-area-pointer sap))
1562 (cond (*read-eval*
1563 (format stream "#.(~S #X~8,'0X)" 'int-sap (sap-int sap)))
1565 (print-unreadable-object (sap stream)
1566 (format stream "system area pointer: #X~8,'0X" (sap-int sap))))))
1568 (defun output-weak-pointer (weak-pointer stream)
1569 (declare (type weak-pointer weak-pointer))
1570 (print-unreadable-object (weak-pointer stream)
1571 (multiple-value-bind (value validp) (weak-pointer-value weak-pointer)
1572 (cond (validp
1573 (write-string "weak pointer: " stream)
1574 (write value :stream stream))
1576 (write-string "broken weak pointer" stream))))))
1578 (defun output-code-component (component stream)
1579 (print-unreadable-object (component stream :identity t)
1580 (let ((dinfo (%code-debug-info component)))
1581 (cond ((eq dinfo :bogus-lra)
1582 (write-string "bogus code object" stream))
1584 (write-string "code object" stream)
1585 (when dinfo
1586 (write-char #\space stream)
1587 (output-object (sb!c::debug-info-name dinfo) stream)))))))
1589 (defun output-lra (lra stream)
1590 (print-unreadable-object (lra stream :identity t)
1591 (write-string "return PC object" stream)))
1593 (defun output-fdefn (fdefn stream)
1594 (print-unreadable-object (fdefn stream)
1595 (write-string "FDEFINITION object for " stream)
1596 (output-object (fdefn-name fdefn) stream)))
1598 ;;;; functions
1600 ;;; Output OBJECT as using PRINT-OBJECT if it's a
1601 ;;; FUNCALLABLE-STANDARD-CLASS, or return NIL otherwise.
1603 ;;; The definition here is a simple temporary placeholder. It will be
1604 ;;; overwritten by a smarter version (capable of calling generic
1605 ;;; PRINT-OBJECT when appropriate) when CLOS is installed.
1606 (defun printed-as-funcallable-standard-class (object stream)
1607 (declare (ignore object stream))
1608 nil)
1610 (defun output-fun (object stream)
1611 (let* ((*print-length* 3) ; in case we have to..
1612 (*print-level* 3) ; ..print an interpreted function definition
1613 (name (%fun-name object))
1614 (proper-name-p (and (legal-fun-name-p name) (fboundp name)
1615 (eq (fdefinition name) object))))
1616 (print-unreadable-object (object stream :identity (not proper-name-p))
1617 (format stream "~:[FUNCTION~;CLOSURE~]~@[ ~S~]"
1618 (closurep object)
1619 name))))
1621 ;;;; catch-all for unknown things
1623 (defun output-random (object stream)
1624 (print-unreadable-object (object stream :identity t)
1625 (let ((lowtag (lowtag-of object)))
1626 (case lowtag
1627 (#.sb!vm:other-pointer-lowtag
1628 (let ((widetag (widetag-of object)))
1629 (case widetag
1630 (#.sb!vm:value-cell-header-widetag
1631 (write-string "value cell " stream)
1632 (output-object (value-cell-ref object) stream))
1634 (write-string "unknown pointer object, widetag=" stream)
1635 (let ((*print-base* 16) (*print-radix* t))
1636 (output-integer widetag stream))))))
1637 ((#.sb!vm:fun-pointer-lowtag
1638 #.sb!vm:instance-pointer-lowtag
1639 #.sb!vm:list-pointer-lowtag)
1640 (write-string "unknown pointer object, lowtag=" stream)
1641 (let ((*print-base* 16) (*print-radix* t))
1642 (output-integer lowtag stream)))
1644 (case (widetag-of object)
1645 (#.sb!vm:unbound-marker-widetag
1646 (write-string "unbound marker" stream))
1648 (write-string "unknown immediate object, lowtag=" stream)
1649 (let ((*print-base* 2) (*print-radix* t))
1650 (output-integer lowtag stream))
1651 (write-string ", widetag=" stream)
1652 (let ((*print-base* 16) (*print-radix* t))
1653 (output-integer (widetag-of object) stream)))))))))