Initial checkin.
[lisp] / mdw-base.lisp
1 ;;; -*-lisp-*-
2 ;;;
3 ;;; $Id$
4 ;;;
5 ;;; Basic definitions
6 ;;;
7 ;;; (c) 2005 Mark Wooding
8 ;;;
9
10 ;;;----- Licensing notice ---------------------------------------------------
11 ;;;
12 ;;; This program is free software; you can redistribute it and/or modify
13 ;;; it under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 2 of the License, or
15 ;;; (at your option) any later version.
16 ;;;
17 ;;; This program is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
21 ;;;
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with this program; if not, write to the Free Software Foundation,
24 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26 (defpackage #:mdw.base
27 (:use #:common-lisp)
28 (:export #:compile-time-defun
29 #:show
30 #:stringify #:listify #:fix-pair #:pairify
31 #:whitespace-char-p
32 #:slot-uninitialized
33 #:with-gensyms #:let*/gensyms #:with-places
34 #:locp #:locf #:ref #:with-locatives))
35 (in-package #:mdw.base)
36
37 (defmacro compile-time-defun (name args &body body)
38 "Define a function which can be used by macros during the compilation
39 process."
40 `(eval-when (:compile-toplevel :load-toplevel)
41 (defun ,name ,args ,@body)))
42
43 (defmacro show (x)
44 "Debugging tool: print the expression X and its value."
45 (let ((tmp (gensym)))
46 `(let ((,tmp ,x))
47 (format t "~&~S: ~S~%" ',x ,tmp)
48 ,tmp)))
49
50 (defun stringify (str)
51 "Return a string representation of STR. Strings are returned unchanged;
52 symbols are converted to their names (unqualified!). Other objects are
53 converted to their print representations."
54 (typecase str
55 (string str)
56 (symbol (symbol-name str))
57 (t (with-output-to-string (s)
58 (princ str s)))))
59 (compile-time-defun listify (x)
60 "If X is a (possibly empty) list, return X; otherwise return (list X)."
61 (if (listp x) x (list x)))
62 (compile-time-defun do-fix-pair (x y defaultp)
63 "Helper function for fix-pair and pairify."
64 (flet ((singleton (x) (values x (if defaultp y x))))
65 (cond ((atom x) (singleton x))
66 ((null (cdr x)) (singleton (car x)))
67 ((atom (cdr x)) (values (car x) (cdr x)))
68 ((cddr x) (error "Too many elements for a pair."))
69 (t (values (car x) (cadr x))))))
70 (compile-time-defun fix-pair (x &optional (y nil defaultp))
71 "Return two values extracted from X. It works as follows:
72 (A) -> A, Y
73 (A B) -> A, B
74 (A B . C) -> error
75 (A . B) -> A, B
76 A -> A, Y
77 where Y defaults to A if not specified."
78 (do-fix-pair x y defaultp))
79 (compile-time-defun pairify (x &optional (y nil defaultp))
80 "As for fix-pair, but returns a list instead of two values."
81 (multiple-value-call #'list (do-fix-pair x y defaultp)))
82
83 (defun whitespace-char-p (ch)
84 "Return whether CH is a whitespace character or not."
85 (case ch
86 ((#\space #\tab #\newline #\return #\vt #\formfeed) t)
87 (t nil)))
88
89 (declaim (ftype (function nil ()) slot-unitialized))
90 (defun slot-uninitialized ()
91 "A function which signals an error. Can be used as an initializer form in
92 structure definitions without doom ensuing."
93 (error "No initializer for slot."))
94
95 (defmacro with-gensyms (syms &body body)
96 "Everyone's favourite macro helper."
97 `(let (,@(mapcar (lambda (sym) `(,sym (gensym ,(symbol-name sym))))
98 (listify syms)))
99 ,@body))
100
101 (defmacro let*/gensyms (binds &body body)
102 "A macro helper. BINDS is a list of binding pairs (VAR VALUE), where VALUE
103 defaults to VAR. The result is that BODY is evaluated in a context where
104 each VAR is bound to a gensym, and in the final expansion, each of those
105 gensyms will be bound to the corresponding VALUE."
106 (labels ((more (binds)
107 (let ((tmp (gensym "TMP")) (bind (car binds)))
108 `((let ((,tmp ,(cadr bind))
109 (,(car bind) (gensym ,(symbol-name (car bind)))))
110 `(let ((,,(car bind) ,,tmp))
111 ,,@(if (cdr binds)
112 (more (cdr binds))
113 body)))))))
114 (if (null binds)
115 `(progn ,@body)
116 (car (more (mapcar #'pairify (listify binds)))))))
117
118 (defmacro %place-ref (getform setform newtmp)
119 "Grim helper macro for with-places."
120 (declare (ignore setform newtmp))
121 getform)
122 (define-setf-expander %place-ref (getform setform newtmp)
123 "Grim helper macro for with-places."
124 (values nil nil newtmp setform getform))
125 (defmacro with-places ((&key environment) places &body body)
126 "A hairy helper, for writing setf-like macros. PLACES is a list of binding
127 pairs (VAR PLACE), where PLACE defaults to VAR. The result is that BODY is
128 evaluated in a context where each VAR is bound to a gensym, and in the final
129 expansion, each of those gensyms will be bound to a symbol-macro capable of
130 reading or setting the value of the corresponding PLACE."
131 (if (null places)
132 `(progn ,@body)
133 (let*/gensyms (environment)
134 (labels
135 ((more (places)
136 (let ((place (car places)))
137 (with-gensyms (tmp valtmps valforms
138 newtmps setform getform)
139 `((let ((,tmp ,(cadr place))
140 (,(car place)
141 (gensym ,(symbol-name (car place)))))
142 (multiple-value-bind
143 (,valtmps ,valforms
144 ,newtmps ,setform ,getform)
145 (get-setf-expansion ,tmp
146 ,environment)
147 (list 'let*
148 (mapcar #'list ,valtmps ,valforms)
149 `(symbol-macrolet ((,,(car place)
150 (%place-ref ,,getform
151 ,,setform
152 ,,newtmps)))
153 ,,@(if (cdr places)
154 (more (cdr places))
155 body))))))))))
156 (car (more (mapcar #'pairify (listify places))))))))
157
158 (defstruct (loc (:predicate locp) (:constructor make-loc (reader writer)))
159 "Locative data type. See `locf' and `ref'."
160 (reader (slot-uninitialized) :type function)
161 (writer (slot-uninitialized) :type function))
162 (defmacro locf (place &environment env)
163 "Slightly cheesy locatives. (locf PLACE) returns an object which, using
164 the `ref' function, can be used to read or set the value of PLACE. It's
165 cheesy because it uses closures rather than actually taking the address of
166 something. Also, unlike Zetalisp, we don't overload `car' to do our dirty
167 work."
168 (multiple-value-bind
169 (valtmps valforms newtmps setform getform)
170 (get-setf-expansion place env)
171 `(let* (,@(mapcar #'list valtmps valforms))
172 (make-loc (lambda () ,getform)
173 (lambda (,@newtmps) ,setform)))))
174 (declaim (inline loc (setf loc)))
175 (defun ref (loc)
176 "Fetch the value referred to by a locative."
177 (funcall (loc-reader loc)))
178 (defun (setf ref) (new loc)
179 "Store a new value in the place referred to by a locative."
180 (funcall (loc-writer loc) new))
181 (defmacro with-locatives (locs &body body)
182 "LOCS is a list of items of the form (SYM [LOC-EXPR]), where SYM is a
183 symbol and LOC-EXPR evaluates to a locative. If LOC-EXPR is omitted, it
184 defaults to SYM. As an abbreviation for a common case, LOCS may be a symbol
185 instead of a list. The BODY is evaluated in an environment where each SYM is
186 a symbol macro which expands to (ref LOC-EXPR) -- or, in fact, something
187 similar which doesn't break if LOC-EXPR has side-effects. Thus, references,
188 including `setf' forms, fetch or modify the thing referred to by the
189 LOC-EXPR. Useful for covering over where something uses a locative."
190 (setf locs (mapcar #'pairify (listify locs)))
191 (let ((tt (mapcar (lambda (l) (declare (ignore l)) (gensym)) locs))
192 (ll (mapcar #'cadr locs))
193 (ss (mapcar #'car locs)))
194 `(let (,@(mapcar (lambda (tmp loc) `(,tmp ,loc)) tt ll))
195 (symbol-macrolet (,@(mapcar (lambda (sym tmp)
196 `(,sym (ref ,tmp))) ss tt))
197 ,@body))))
198
199 ;;;----- That's all, folks --------------------------------------------------