0.pre7.38:
[sbcl.git] / src / cold / shebang.lisp
1 ;;;; cold-boot-only readmacro syntax
2
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.
11
12 (in-package "SB-COLD")
13 \f
14 ;;;; definition of #!+ and #!- as a mechanism analogous to #+/#-, but
15 ;;;; for *SHEBANG-FEATURES* instead of CL:*FEATURES*. (This is handy
16 ;;;; when cross-compiling, so that we can make a distinction between
17 ;;;; features of the host Common Lisp and features of the target
18 ;;;; SBCL.)
19
20 ;;; the feature list for the target system
21 (export '*shebang-features*)
22 (declaim (type list *shebang-features*))
23 (defvar *shebang-features*)
24
25 (defun feature-in-list-p (feature list)
26   (etypecase feature
27     (symbol (member feature list :test #'eq))
28     (cons (flet ((subfeature-in-list-p (subfeature)
29                    (feature-in-list-p subfeature list)))
30             (ecase (first feature)
31               (:or  (some  #'subfeature-in-list-p (rest feature)))
32               (:and (every #'subfeature-in-list-p (rest feature)))
33               (:not (let ((rest (cdr feature)))
34                       (if (or (null (car rest)) (cdr rest))
35                         (error "wrong number of terms in compound feature ~S"
36                                feature)
37                         (not (subfeature-in-list-p (second feature)))))))))))
38 (compile 'feature-in-list-p)
39
40 (defun shebang-reader (stream sub-character infix-parameter)
41   (declare (ignore sub-character))
42   (when infix-parameter
43     (error "illegal read syntax: #~D!" infix-parameter))
44   (let ((next-char (read-char stream)))
45     (unless (find next-char "+-")
46       (error "illegal read syntax: #!~C" next-char))
47     ;; When test is not satisfied
48     ;; FIXME: clearer if order of NOT-P and (NOT NOT-P) were reversed? then
49     ;; would become "unless test is satisfied"..
50     (when (let* ((*package* (find-package "KEYWORD"))
51                  (*read-suppress* nil)
52                  (not-p (char= next-char #\-))
53                  (feature (read stream)))
54             (if (feature-in-list-p feature *shebang-features*)
55                 not-p
56                 (not not-p)))
57       ;; Read (and discard) a form from input.
58       (let ((*read-suppress* t))
59         (read stream t nil t))))
60   (values))
61 (compile 'shebang-reader)
62
63 (set-dispatch-macro-character #\# #\! #'shebang-reader)
64 \f
65 ;;;; FIXME: Would it be worth implementing this?
66 #|
67 ;;;; readmacro syntax to remove spaces from FORMAT strings at compile time
68 ;;;; instead of leaving them to be skipped over at runtime
69
70 ;;; a counter of the number of bytes that we think we've avoided having to
71 ;;; compile into the system by virtue of doing compile-time processing
72 (defvar *shebang-double-quote--approx-bytes-saved* 0)
73
74 ;;; Read a string, strip out any #\~ #\NEWLINE whitespace sequence,
75 ;;; and return the result. (This is a subset of the processing performed
76 ;;; by FORMAT, but we perform it at compile time instead of postponing
77 ;;; it until run-time.
78 (defun shebang-double-quote (stream)
79   (labels ((rc () (read-char stream))
80            (white-p (char)
81              ;; Putting non-standard characters in the compiler source is
82              ;; generally a bad idea, since we'd like to be really portable.
83              ;; It's specifically a bad idea in strings intended to be
84              ;; processed by SHEBANG-DOUBLE-QUOTE, because there seems to be no
85              ;; portable way to test a non-STANDARD-CHAR for whitespaceness.
86              ;; (The most common problem would be to put a #\TAB -- which is
87              ;; not a STANDARD-CHAR -- into the string. If this is part of the
88              ;; to-be-skipped-over whitespace after a #\~ #\NEWLINE sequence in
89              ;; the string, it won't work, because it won't be recognized as
90              ;; whitespace.)
91              (unless (typep char 'standard-char)
92                (warn "non-STANDARD-CHAR in #!\": ~C" result))
93              (or (char= char #\newline)
94                  (char= char #\space)))
95            (skip-white ()
96              (do ((char (rc) (rc))
97                   (count 0 (1+ count)))
98                  ((not (white-p char))
99                   (unread-char char stream)
100                   count))))
101     (do ((adj-string (make-array 0 :element-type 'char :adjustable t))
102          (char (rc) (rc)))
103         ((char= char #\") (coerce adj-string 'simple-string))
104       (cond ((char= char #\~)
105              (let ((next-char (read-char stream)))
106                (cond ((char= next-char #\newline)
107                       (incf *shebang-double-quote--approx-bytes-saved*
108                             (+ 2 (skip-white))))
109                      (t
110                       (vector-push-extend      char adj-string)
111                       (vector-push-extend next-char adj-string)))))
112             ((char= char #\\)
113              (vector-push-extend char adj-string)
114              (vector-push-extend (rc) adj-string))
115             (t (vector-push-extend char adj-string))))))
116
117 (setf (gethash #\" *shebang-dispatch*)
118       #'shebang-double-quote)
119 |#