0.8.18.24:
[sbcl.git] / src / code / sharpm.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
9
10 (in-package "SB!IMPL")
11 \f
12 (declaim (special *read-suppress* *standard-readtable* *bq-vector-flag*))
13
14 ;;; FIXME: Is it standard to ignore numeric args instead of raising errors?
15 (defun ignore-numarg (sub-char numarg)
16   (when numarg
17     (warn "A numeric argument was ignored in #~W~A." numarg sub-char)))
18 \f
19 ;;;; reading arrays and vectors: the #(, #*, and #A readmacros
20
21 (defun sharp-left-paren (stream ignore length)
22   (declare (ignore ignore) (special *backquote-count*))
23   (let* ((list (read-list stream nil))
24          (listlength (length list)))
25     (declare (list list)
26              (fixnum listlength))
27     (cond (*read-suppress* nil)
28           ((zerop *backquote-count*)
29            (if length
30                (cond ((> listlength (the fixnum length))
31                       (%reader-error
32                        stream
33                        "vector longer than specified length: #~S~S"
34                        length list))
35                      (t
36                       (fill (the simple-vector
37                                  (replace (the simple-vector
38                                                (make-array length))
39                                           list))
40                             (car (last list))
41                             :start listlength)))
42                (coerce list 'vector)))
43           (t (cons *bq-vector-flag* list)))))
44
45 (defun sharp-star (stream ignore numarg)
46   (declare (ignore ignore))
47   (multiple-value-bind (bstring escape-appearedp) (read-extended-token stream)
48     (declare (simple-string bstring))
49     (cond (*read-suppress* nil)
50           (escape-appearedp
51            (%reader-error stream "An escape character appeared after #*"))
52           ((and numarg (zerop (length bstring)) (not (zerop numarg)))
53            (%reader-error
54             stream
55             "You have to give a little bit for non-zero #* bit-vectors."))
56           ((or (null numarg) (>= (the fixnum numarg) (length bstring)))
57            (let* ((len1 (length bstring))
58                   (last1 (1- len1))
59                   (len2 (or numarg len1))
60                   (bvec (make-array len2 :element-type 'bit
61                                     :initial-element 0)))
62              (declare (fixnum len1 last1 len2))
63              (do ((i 0 (1+ i))
64                   (char ()))
65                  ((= i len2))
66                (declare (fixnum i))
67                (setq char (elt bstring (if (< i len1) i last1)))
68                (setf (elt bvec i)
69                      (cond ((char= char #\0) 0)
70                            ((char= char #\1) 1)
71                            (t
72                             (%reader-error
73                              stream
74                              "illegal element given for bit-vector: ~S"
75                              char)))))
76              bvec))
77           (t
78            (%reader-error stream
79                          "Bit vector is longer than specified length #~A*~A"
80                          numarg bstring)))))
81
82 (defun sharp-A (stream ignore dimensions)
83   (declare (ignore ignore))
84   (when *read-suppress*
85     (read stream t nil t)
86     (return-from sharp-A nil))
87   (unless dimensions (%reader-error stream "no dimensions argument to #A"))
88   (collect ((dims))
89     (let* ((contents (read stream t nil t))
90            (seq contents))
91       (dotimes (axis dimensions
92                      (make-array (dims) :initial-contents contents))
93         (unless (typep seq 'sequence)
94           (%reader-error stream
95                          "#~WA axis ~W is not a sequence:~%  ~S"
96                          dimensions axis seq))
97         (let ((len (length seq)))
98           (dims len)
99           (unless (or (= axis (1- dimensions))
100                       ;; ANSI: "If some dimension of the array whose
101                       ;; representation is being parsed is found to be
102                       ;; 0, all dimensions to the right (i.e., the
103                       ;; higher numbered dimensions) are also
104                       ;; considered to be 0."
105                       (= len 0))
106             (setq seq (elt seq 0))))))))
107 \f
108 ;;;; reading structure instances: the #S readmacro
109
110 (defun sharp-S (stream sub-char numarg)
111   (ignore-numarg sub-char numarg)
112   (when *read-suppress*
113     (read stream t nil t)
114     (return-from sharp-S nil))
115   (let ((body (if (char= (read-char stream t) #\( )
116                   (read-list stream nil)
117                   (%reader-error stream "non-list following #S"))))
118     (unless (listp body)
119       (%reader-error stream "non-list following #S: ~S" body))
120     (unless (symbolp (car body))
121       (%reader-error stream "Structure type is not a symbol: ~S" (car body)))
122     (let ((classoid (find-classoid (car body) nil)))
123       (unless (typep classoid 'structure-classoid)
124         (%reader-error stream "~S is not a defined structure type."
125                        (car body)))
126       (let ((def-con (dd-default-constructor
127                       (layout-info
128                        (classoid-layout classoid)))))
129         (unless def-con
130           (%reader-error
131            stream "The ~S structure does not have a default constructor."
132            (car body)))
133         (when (and (atom (rest body))
134                    (not (null (rest body))))
135           (%reader-error
136            stream "improper list for #S: ~S." body))
137         (apply (fdefinition def-con)
138                (loop for tail on (rest body) by #'cddr
139                      with slot-name = (and (consp tail) (car tail))
140                      do (progn
141                           (when (null (cdr tail))
142                             (%reader-error
143                              stream
144                              "the arglist for the ~S constructor in #S ~
145                               has an odd length: ~S."
146                              (car body) (rest body)))
147                           (when (or (atom (cdr tail))
148                                     (and (atom (cddr tail))
149                                          (not (null (cddr tail)))))
150                             (%reader-error
151                              stream
152                              "the arglist for the ~S constructor in #S ~
153                               is improper: ~S."
154                              (car body) (rest body)))
155                           (when (not (typep (car tail) 'string-designator))
156                             (%reader-error
157                              stream
158                              "a slot name in #S is not a string ~
159                               designator: ~S."
160                              slot-name))
161                           (when (not (keywordp slot-name))
162                             (style-warn "in #S ~S, the use of non-keywords ~
163                                          as slot specifiers is deprecated: ~S."
164                                         (car body) slot-name)))
165                      collect (intern (string (car tail)) *keyword-package*)
166                      collect (cadr tail)))))))
167 \f
168 ;;;; reading numbers: the #B, #C, #O, #R, and #X readmacros
169
170 (defun sharp-B (stream sub-char numarg)
171   (ignore-numarg sub-char numarg)
172   (sharp-r stream sub-char 2))
173
174 (defun sharp-C (stream sub-char numarg)
175   (ignore-numarg sub-char numarg)
176   ;; The next thing had better be a list of two numbers.
177   (let ((cnum (read stream t nil t)))
178     (when *read-suppress* (return-from sharp-c nil))
179     (if (and (listp cnum) (= (length cnum) 2))
180         (complex (car cnum) (cadr cnum))
181         (%reader-error stream "illegal complex number format: #C~S" cnum))))
182
183 (defun sharp-O (stream sub-char numarg)
184   (ignore-numarg sub-char numarg)
185   (sharp-r stream sub-char 8))
186
187 (defun sharp-R (stream sub-char radix)
188   (cond (*read-suppress*
189          (read-extended-token stream)
190          nil)
191         ((not radix)
192          (%reader-error stream "radix missing in #R"))
193         ((not (<= 2 radix 36))
194          (%reader-error stream "illegal radix for #R: ~D." radix))
195         (t
196          (let ((res (let ((*read-base* radix))
197                       (read stream t nil t))))
198            (unless (typep res 'rational)
199              (%reader-error stream
200                             "#~A (base ~D.) value is not a rational: ~S."
201                             sub-char
202                             radix
203                             res))
204            res))))
205
206 (defun sharp-X (stream sub-char numarg)
207   (ignore-numarg sub-char numarg)
208   (sharp-r stream sub-char 16))
209 \f
210 ;;;; reading circular data: the #= and ## readmacros
211
212 ;;; objects already seen by CIRCLE-SUBST
213 (defvar *sharp-equal-circle-table*)
214 (declaim (type hash-table *sharp-equal-circle-table*))
215
216 ;; This function is kind of like NSUBLIS, but checks for circularities and
217 ;; substitutes in arrays and structures as well as lists. The first arg is an
218 ;; alist of the things to be replaced assoc'd with the things to replace them.
219 (defun circle-subst (old-new-alist tree)
220   (cond ((not (typep tree
221                      '(or cons (array t) structure-object standard-object)))
222          (let ((entry (find tree old-new-alist :key #'second)))
223            (if entry (third entry) tree)))
224         ((null (gethash tree *sharp-equal-circle-table*))
225          (setf (gethash tree *sharp-equal-circle-table*) t)
226          (cond ((typep tree '(or structure-object standard-object))
227                 (do ((i 1 (1+ i))
228                      (end (%instance-length tree)))
229                     ((= i end))
230                   (let* ((old (%instance-ref tree i))
231                          (new (circle-subst old-new-alist old)))
232                     (unless (eq old new)
233                       (setf (%instance-ref tree i) new)))))
234                ((arrayp tree)
235                 (with-array-data ((data tree) (start) (end))
236                   (declare (fixnum start end))
237                   (do ((i start (1+ i)))
238                       ((>= i end))
239                     (let* ((old (aref data i))
240                            (new (circle-subst old-new-alist old)))
241                       (unless (eq old new)
242                         (setf (aref data i) new))))))
243                (t
244                 (let ((a (circle-subst old-new-alist (car tree)))
245                       (d (circle-subst old-new-alist (cdr tree))))
246                   (unless (eq a (car tree))
247                     (rplaca tree a))
248                   (unless (eq d (cdr tree))
249                     (rplacd tree d)))))
250          tree)
251         (t tree)))
252
253 ;;; Sharp-equal works as follows. When a label is assigned (i.e. when
254 ;;; #= is called) we GENSYM a symbol is which is used as an
255 ;;; unforgeable tag. *SHARP-SHARP-ALIST* maps the integer tag to this
256 ;;; gensym.
257 ;;;
258 ;;; When SHARP-SHARP encounters a reference to a label, it returns the
259 ;;; symbol assoc'd with the label. Resolution of the reference is
260 ;;; deferred until the read done by #= finishes. Any already resolved
261 ;;; tags (in *SHARP-EQUAL-ALIST*) are simply returned.
262 ;;;
263 ;;; After reading of the #= form is completed, we add an entry to
264 ;;; *SHARP-EQUAL-ALIST* that maps the gensym tag to the resolved
265 ;;; object. Then for each entry in the *SHARP-SHARP-ALIST, the current
266 ;;; object is searched and any uses of the gensysm token are replaced
267 ;;; with the actual value.
268 (defvar *sharp-sharp-alist* ())
269
270 (defun sharp-equal (stream ignore label)
271   (declare (ignore ignore))
272   (when *read-suppress* (return-from sharp-equal (values)))
273   (unless label
274     (%reader-error stream "missing label for #=" label))
275   (when (or (assoc label *sharp-sharp-alist*)
276             (assoc label *sharp-equal-alist*))
277     (%reader-error stream "multiply defined label: #~D=" label))
278   (let* ((tag (gensym))
279          (*sharp-sharp-alist* (acons label tag *sharp-sharp-alist*))
280          (obj (read stream t nil t)))
281     (when (eq obj tag)
282       (%reader-error stream
283                      "must tag something more than just #~D#"
284                      label))
285     (push (list label tag obj) *sharp-equal-alist*)
286     (let ((*sharp-equal-circle-table* (make-hash-table :test 'eq :size 20)))
287       (circle-subst *sharp-equal-alist* obj))))
288
289 (defun sharp-sharp (stream ignore label)
290   (declare (ignore ignore))
291   (when *read-suppress* (return-from sharp-sharp nil))
292   (unless label
293     (%reader-error stream "missing label for ##" label))
294
295   (let ((entry (assoc label *sharp-equal-alist*)))
296     (if entry
297         (third entry)
298         (let ((pair (assoc label *sharp-sharp-alist*)))
299           (unless pair
300             (%reader-error stream "object is not labelled #~S#" label))
301           (cdr pair)))))
302 \f
303 ;;;; conditional compilation: the #+ and #- readmacros
304
305 (flet ((guts (stream not-p)
306          (unless (if (handler-case
307                          (let ((*package* *keyword-package*)
308                                (*read-suppress* nil))
309                            (featurep (read stream t nil t)))
310                        (reader-package-error
311                         (condition)
312                         (declare (ignore condition))
313                         nil))
314                      (not not-p)
315                      not-p)
316            (let ((*read-suppress* t))
317              (read stream t nil t)))
318          (values)))
319
320   (defun sharp-plus (stream sub-char numarg)
321     (ignore-numarg sub-char numarg)
322     (guts stream nil))
323
324   (defun sharp-minus (stream sub-char numarg)
325     (ignore-numarg sub-char numarg)
326     (guts stream t)))
327 \f
328 ;;;; reading miscellaneous objects: the #P, #\, and #| readmacros
329
330 (defun sharp-P (stream sub-char numarg)
331   (ignore-numarg sub-char numarg)
332   (let ((namestring (read stream t nil t)))
333     (unless *read-suppress*
334       (parse-namestring namestring))))
335
336 (defun sharp-backslash (stream backslash numarg)
337   (ignore-numarg backslash numarg)
338   (let ((charstring (read-extended-token-escaped stream)))
339     (declare (simple-string charstring))
340     (cond (*read-suppress* nil)
341           ((= (the fixnum (length charstring)) 1)
342            (char charstring 0))
343           ((name-char charstring))
344           (t
345            (%reader-error stream "unrecognized character name: ~S"
346                           charstring)))))
347
348 (defun sharp-vertical-bar (stream sub-char numarg)
349   (ignore-numarg sub-char numarg)
350   (let ((stream (in-synonym-of stream)))
351     (if (ansi-stream-p stream)
352         (prepare-for-fast-read-char stream
353           (do ((level 1)
354                (prev (fast-read-char) char)
355                (char (fast-read-char) (fast-read-char)))
356               (())
357             (cond ((and (char= prev #\|) (char= char #\#))
358                    (setq level (1- level))
359                    (when (zerop level)
360                      (done-with-fast-read-char)
361                      (return (values)))
362                    (setq char (fast-read-char)))
363                   ((and (char= prev #\#) (char= char #\|))
364                    (setq char (fast-read-char))
365                    (setq level (1+ level))))))
366         ;; fundamental-stream
367         (do ((level 1)
368              (prev (read-char stream t) char)
369              (char (read-char stream t) (read-char stream t)))
370             (())
371           (cond ((and (char= prev #\|) (char= char #\#))
372                  (setq level (1- level))
373                  (when (zerop level)
374                    (return (values)))
375                  (setq char (read-char stream t)))
376                 ((and (char= prev #\#) (char= char #\|))
377                  (setq char (read-char stream t))
378                  (setq level (1+ level))))))))
379 \f
380 ;;;; a grab bag of other sharp readmacros: #', #:, and #.
381
382 (defun sharp-quote (stream sub-char numarg)
383   (ignore-numarg sub-char numarg)
384   ;; The fourth arg tells READ that this is a recursive call.
385   `(function ,(read stream t nil t)))
386
387 (defun sharp-colon (stream sub-char numarg)
388   (ignore-numarg sub-char numarg)
389   (multiple-value-bind (token escapep colon) (read-extended-token stream)
390     (declare (simple-string token) (ignore escapep))
391     (cond
392      (*read-suppress* nil)
393      (colon
394       (%reader-error stream
395                      "The symbol following #: contains a package marker: ~S"
396                      token))
397      (t
398       (make-symbol token)))))
399
400 (defvar *read-eval* t
401   #!+sb-doc
402   "If false, then the #. read macro is disabled.")
403
404 (defun sharp-dot (stream sub-char numarg)
405   (ignore-numarg sub-char numarg)
406   (let ((token (read stream t nil t)))
407     (unless *read-suppress*
408       (unless *read-eval*
409         (%reader-error stream "can't read #. while *READ-EVAL* is NIL"))
410       (eval token))))
411 \f
412 (defun sharp-illegal (stream sub-char ignore)
413   (declare (ignore ignore))
414   (%reader-error stream "illegal sharp macro character: ~S" sub-char))
415
416 ;;; for cold init: Install SHARPM stuff in the current *READTABLE*.
417 (defun !sharpm-cold-init ()
418   (make-dispatch-macro-character #\# t)
419   (set-dispatch-macro-character #\# #\\ #'sharp-backslash)
420   (set-dispatch-macro-character #\# #\' #'sharp-quote)
421   (set-dispatch-macro-character #\# #\( #'sharp-left-paren)
422   (set-dispatch-macro-character #\# #\* #'sharp-star)
423   (set-dispatch-macro-character #\# #\: #'sharp-colon)
424   (set-dispatch-macro-character #\# #\. #'sharp-dot)
425   (set-dispatch-macro-character #\# #\R #'sharp-R)
426   (set-dispatch-macro-character #\# #\r #'sharp-R)
427   (set-dispatch-macro-character #\# #\B #'sharp-B)
428   (set-dispatch-macro-character #\# #\b #'sharp-B)
429   (set-dispatch-macro-character #\# #\O #'sharp-O)
430   (set-dispatch-macro-character #\# #\o #'sharp-O)
431   (set-dispatch-macro-character #\# #\X #'sharp-X)
432   (set-dispatch-macro-character #\# #\x #'sharp-X)
433   (set-dispatch-macro-character #\# #\A #'sharp-A)
434   (set-dispatch-macro-character #\# #\a #'sharp-A)
435   (set-dispatch-macro-character #\# #\S #'sharp-S)
436   (set-dispatch-macro-character #\# #\s #'sharp-S)
437   (set-dispatch-macro-character #\# #\= #'sharp-equal)
438   (set-dispatch-macro-character #\# #\# #'sharp-sharp)
439   (set-dispatch-macro-character #\# #\+ #'sharp-plus)
440   (set-dispatch-macro-character #\# #\- #'sharp-minus)
441   (set-dispatch-macro-character #\# #\C #'sharp-C)
442   (set-dispatch-macro-character #\# #\c #'sharp-C)
443   (set-dispatch-macro-character #\# #\| #'sharp-vertical-bar)
444   (set-dispatch-macro-character #\# #\p #'sharp-p)
445   (set-dispatch-macro-character #\# #\P #'sharp-p)
446   (set-dispatch-macro-character #\# #\) #'sharp-illegal)
447   (set-dispatch-macro-character #\# #\< #'sharp-illegal)
448   (set-dispatch-macro-character #\# #\Space #'sharp-illegal)
449   (dolist (cc '#.(list tab-char-code form-feed-char-code return-char-code
450                        line-feed-char-code backspace-char-code))
451     (set-dispatch-macro-character #\# (code-char cc) #'sharp-illegal)))