0.7.13.pcl-class.1
[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         (apply (fdefinition def-con) (rest body))))))
134 \f
135 ;;;; reading numbers: the #B, #C, #O, #R, and #X readmacros
136
137 (defun sharp-B (stream sub-char numarg)
138   (ignore-numarg sub-char numarg)
139   (sharp-r stream sub-char 2))
140
141 (defun sharp-C (stream sub-char numarg)
142   (ignore-numarg sub-char numarg)
143   ;; The next thing had better be a list of two numbers.
144   (let ((cnum (read stream t nil t)))
145     (when *read-suppress* (return-from sharp-c nil))
146     (if (and (listp cnum) (= (length cnum) 2))
147         (complex (car cnum) (cadr cnum))
148         (%reader-error stream "illegal complex number format: #C~S" cnum))))
149
150 (defun sharp-O (stream sub-char numarg)
151   (ignore-numarg sub-char numarg)
152   (sharp-r stream sub-char 8))
153
154 (defun sharp-R (stream sub-char radix)
155   (cond (*read-suppress*
156          (read-extended-token stream)
157          nil)
158         ((not radix)
159          (%reader-error stream "radix missing in #R"))
160         ((not (<= 2 radix 36))
161          (%reader-error stream "illegal radix for #R: ~D." radix))
162         (t
163          (let ((res (let ((*read-base* radix))
164                       (read stream t nil t))))
165            (unless (typep res 'rational)
166              (%reader-error stream
167                             "#~A (base ~D.) value is not a rational: ~S."
168                             sub-char
169                             radix
170                             res))
171            res))))
172
173 (defun sharp-X (stream sub-char numarg)
174   (ignore-numarg sub-char numarg)
175   (sharp-r stream sub-char 16))
176 \f
177 ;;;; reading circular data: the #= and ## readmacros
178
179 ;;; objects already seen by CIRCLE-SUBST
180 (defvar *sharp-equal-circle-table*)
181 (declaim (type hash-table *sharp-equal-circle-table*))
182
183 ;; This function is kind of like NSUBLIS, but checks for circularities and
184 ;; substitutes in arrays and structures as well as lists. The first arg is an
185 ;; alist of the things to be replaced assoc'd with the things to replace them.
186 (defun circle-subst (old-new-alist tree)
187   (cond ((not (typep tree '(or cons (array t) structure-object standard-object)))
188          (let ((entry (find tree old-new-alist :key #'second)))
189            (if entry (third entry) tree)))
190         ((null (gethash tree *sharp-equal-circle-table*))
191          (setf (gethash tree *sharp-equal-circle-table*) t)
192          (cond ((typep tree '(or structure-object standard-object))
193                 (do ((i 1 (1+ i))
194                      (end (%instance-length tree)))
195                     ((= i end))
196                   (let* ((old (%instance-ref tree i))
197                          (new (circle-subst old-new-alist old)))
198                     (unless (eq old new)
199                       (setf (%instance-ref tree i) new)))))
200                ((arrayp tree)
201                 (with-array-data ((data tree) (start) (end))
202                   (declare (fixnum start end))
203                   (do ((i start (1+ i)))
204                       ((>= i end))
205                     (let* ((old (aref data i))
206                            (new (circle-subst old-new-alist old)))
207                       (unless (eq old new)
208                         (setf (aref data i) new))))))
209                (t
210                 (let ((a (circle-subst old-new-alist (car tree)))
211                       (d (circle-subst old-new-alist (cdr tree))))
212                   (unless (eq a (car tree))
213                     (rplaca tree a))
214                   (unless (eq d (cdr tree))
215                     (rplacd tree d)))))
216          tree)
217         (t tree)))
218
219 ;;; Sharp-equal works as follows. When a label is assigned (i.e. when
220 ;;; #= is called) we GENSYM a symbol is which is used as an
221 ;;; unforgeable tag. *SHARP-SHARP-ALIST* maps the integer tag to this
222 ;;; gensym.
223 ;;;
224 ;;; When SHARP-SHARP encounters a reference to a label, it returns the
225 ;;; symbol assoc'd with the label. Resolution of the reference is
226 ;;; deferred until the read done by #= finishes. Any already resolved
227 ;;; tags (in *SHARP-EQUAL-ALIST*) are simply returned.
228 ;;;
229 ;;; After reading of the #= form is completed, we add an entry to
230 ;;; *SHARP-EQUAL-ALIST* that maps the gensym tag to the resolved
231 ;;; object. Then for each entry in the *SHARP-SHARP-ALIST, the current
232 ;;; object is searched and any uses of the gensysm token are replaced
233 ;;; with the actual value.
234 (defvar *sharp-sharp-alist* ())
235
236 (defun sharp-equal (stream ignore label)
237   (declare (ignore ignore))
238   (when *read-suppress* (return-from sharp-equal (values)))
239   (unless label
240     (%reader-error stream "missing label for #=" label))
241   (when (or (assoc label *sharp-sharp-alist*)
242             (assoc label *sharp-equal-alist*))
243     (%reader-error stream "multiply defined label: #~D=" label))
244   (let* ((tag (gensym))
245          (*sharp-sharp-alist* (acons label tag *sharp-sharp-alist*))
246          (obj (read stream t nil t)))
247     (when (eq obj tag)
248       (%reader-error stream
249                      "must tag something more than just #~D#"
250                      label))
251     (push (list label tag obj) *sharp-equal-alist*)
252     (let ((*sharp-equal-circle-table* (make-hash-table :test 'eq :size 20)))
253       (circle-subst *sharp-equal-alist* obj))))
254
255 (defun sharp-sharp (stream ignore label)
256   (declare (ignore ignore))
257   (when *read-suppress* (return-from sharp-sharp nil))
258   (unless label
259     (%reader-error stream "missing label for ##" label))
260
261   (let ((entry (assoc label *sharp-equal-alist*)))
262     (if entry
263         (third entry)
264         (let ((pair (assoc label *sharp-sharp-alist*)))
265           (unless pair
266             (%reader-error stream "object is not labelled #~S#" label))
267           (cdr pair)))))
268 \f
269 ;;;; conditional compilation: the #+ and #- readmacros
270
271 (flet ((guts (stream not-p)
272          (unless (if (handler-case
273                          (let ((*package* *keyword-package*)
274                                (*read-suppress* nil))
275                            (featurep (read stream t nil t)))
276                        (reader-package-error
277                         (condition)
278                         (declare (ignore condition))
279                         nil))
280                      (not not-p)
281                      not-p)
282            (let ((*read-suppress* t))
283              (read stream t nil t)))
284          (values)))
285
286   (defun sharp-plus (stream sub-char numarg)
287     (ignore-numarg sub-char numarg)
288     (guts stream nil))
289
290   (defun sharp-minus (stream sub-char numarg)
291     (ignore-numarg sub-char numarg)
292     (guts stream t)))
293 \f
294 ;;;; reading miscellaneous objects: the #P, #\, and #| readmacros
295
296 (defun sharp-P (stream sub-char numarg)
297   (ignore-numarg sub-char numarg)
298   (let ((namestring (read stream t nil t)))
299     (unless *read-suppress*
300       (parse-namestring namestring))))
301
302 (defun sharp-backslash (stream backslash numarg)
303   (ignore-numarg backslash numarg)
304   (let ((charstring (read-extended-token-escaped stream)))
305     (declare (simple-string charstring))
306     (cond (*read-suppress* nil)
307           ((= (the fixnum (length charstring)) 1)
308            (char charstring 0))
309           ((name-char charstring))
310           (t
311            (%reader-error stream "unrecognized character name: ~S"
312                           charstring)))))
313
314 (defun sharp-vertical-bar (stream sub-char numarg)
315   (ignore-numarg sub-char numarg)
316   (let ((stream (in-synonym-of stream)))
317     (if (ansi-stream-p stream)
318         (prepare-for-fast-read-char stream
319           (do ((level 1)
320                (prev (fast-read-char) char)
321                (char (fast-read-char) (fast-read-char)))
322               (())
323             (cond ((and (char= prev #\|) (char= char #\#))
324                    (setq level (1- level))
325                    (when (zerop level)
326                      (done-with-fast-read-char)
327                      (return (values)))
328                    (setq char (fast-read-char)))
329                   ((and (char= prev #\#) (char= char #\|))
330                    (setq char (fast-read-char))
331                    (setq level (1+ level))))))
332         ;; fundamental-stream
333         (do ((level 1)
334              (prev (read-char stream t) char)
335              (char (read-char stream t) (read-char stream t)))
336             (())
337           (cond ((and (char= prev #\|) (char= char #\#))
338                  (setq level (1- level))
339                  (when (zerop level)
340                    (return (values)))
341                  (setq char (read-char stream t)))
342                 ((and (char= prev #\#) (char= char #\|))
343                  (setq char (read-char stream t))
344                  (setq level (1+ level))))))))
345 \f
346 ;;;; a grab bag of other sharp readmacros: #', #:, and #.
347
348 (defun sharp-quote (stream sub-char numarg)
349   (ignore-numarg sub-char numarg)
350   ;; The fourth arg tells READ that this is a recursive call.
351   `(function ,(read stream t nil t)))
352
353 (defun sharp-colon (stream sub-char numarg)
354   (ignore-numarg sub-char numarg)
355   (multiple-value-bind (token escapep colon) (read-extended-token stream)
356     (declare (simple-string token) (ignore escapep))
357     (cond
358      (*read-suppress* nil)
359      (colon
360       (%reader-error stream
361                      "The symbol following #: contains a package marker: ~S"
362                      token))
363      (t
364       (make-symbol token)))))
365
366 (defvar *read-eval* t
367   #!+sb-doc
368   "If false, then the #. read macro is disabled.")
369
370 (defun sharp-dot (stream sub-char numarg)
371   (ignore-numarg sub-char numarg)
372   (let ((token (read stream t nil t)))
373     (unless *read-suppress*
374       (unless *read-eval*
375         (%reader-error stream "can't read #. while *READ-EVAL* is NIL"))
376       (eval token))))
377 \f
378 (defun sharp-illegal (stream sub-char ignore)
379   (declare (ignore ignore))
380   (%reader-error stream "illegal sharp macro character: ~S" sub-char))
381
382 ;;; for cold init: Install SHARPM stuff in the current *READTABLE*.
383 (defun !sharpm-cold-init ()
384   (make-dispatch-macro-character #\# t)
385   (set-dispatch-macro-character #\# #\\ #'sharp-backslash)
386   (set-dispatch-macro-character #\# #\' #'sharp-quote)
387   (set-dispatch-macro-character #\# #\( #'sharp-left-paren)
388   (set-dispatch-macro-character #\# #\* #'sharp-star)
389   (set-dispatch-macro-character #\# #\: #'sharp-colon)
390   (set-dispatch-macro-character #\# #\. #'sharp-dot)
391   (set-dispatch-macro-character #\# #\R #'sharp-R)
392   (set-dispatch-macro-character #\# #\r #'sharp-R)
393   (set-dispatch-macro-character #\# #\B #'sharp-B)
394   (set-dispatch-macro-character #\# #\b #'sharp-B)
395   (set-dispatch-macro-character #\# #\O #'sharp-O)
396   (set-dispatch-macro-character #\# #\o #'sharp-O)
397   (set-dispatch-macro-character #\# #\X #'sharp-X)
398   (set-dispatch-macro-character #\# #\x #'sharp-X)
399   (set-dispatch-macro-character #\# #\A #'sharp-A)
400   (set-dispatch-macro-character #\# #\a #'sharp-A)
401   (set-dispatch-macro-character #\# #\S #'sharp-S)
402   (set-dispatch-macro-character #\# #\s #'sharp-S)
403   (set-dispatch-macro-character #\# #\= #'sharp-equal)
404   (set-dispatch-macro-character #\# #\# #'sharp-sharp)
405   (set-dispatch-macro-character #\# #\+ #'sharp-plus)
406   (set-dispatch-macro-character #\# #\- #'sharp-minus)
407   (set-dispatch-macro-character #\# #\C #'sharp-C)
408   (set-dispatch-macro-character #\# #\c #'sharp-C)
409   (set-dispatch-macro-character #\# #\| #'sharp-vertical-bar)
410   (set-dispatch-macro-character #\# #\p #'sharp-p)
411   (set-dispatch-macro-character #\# #\P #'sharp-p)
412   (set-dispatch-macro-character #\# #\) #'sharp-illegal)
413   (set-dispatch-macro-character #\# #\< #'sharp-illegal)
414   (set-dispatch-macro-character #\# #\Space #'sharp-illegal)
415   (dolist (cc '#.(list tab-char-code form-feed-char-code return-char-code
416                        line-feed-char-code backspace-char-code))
417     (set-dispatch-macro-character #\# (code-char cc) #'sharp-illegal)))