base: Fix bug in `while'.
[lisp] / mdw-base.lisp
CommitLineData
861345b4 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
02866e07
MW
26;;;--------------------------------------------------------------------------
27;;; Package things.
28
861345b4 29(defpackage #:mdw.base
30 (:use #:common-lisp)
31 (:export #:compile-time-defun
32 #:show
33 #:stringify #:listify #:fix-pair #:pairify
34 #:whitespace-char-p
35 #:slot-uninitialized
0375133b 36 #:nlet #:while
861345b4 37 #:with-gensyms #:let*/gensyms #:with-places
e979e568
MW
38 #:locp #:locf #:ref #:with-locatives
39 #:update-place #:update-place-after
40 #:incf-after #:decf-after))
861345b4 41(in-package #:mdw.base)
42
02866e07
MW
43;;;--------------------------------------------------------------------------
44;;; Some simple macros to get things going.
45
861345b4 46(defmacro compile-time-defun (name args &body body)
47 "Define a function which can be used by macros during the compilation
48process."
49 `(eval-when (:compile-toplevel :load-toplevel)
50 (defun ,name ,args ,@body)))
51
52(defmacro show (x)
53 "Debugging tool: print the expression X and its value."
54 (let ((tmp (gensym)))
55 `(let ((,tmp ,x))
56 (format t "~&~S: ~S~%" ',x ,tmp)
57 ,tmp)))
58
59(defun stringify (str)
60 "Return a string representation of STR. Strings are returned unchanged;
61symbols are converted to their names (unqualified!). Other objects are
62converted to their print representations."
63 (typecase str
64 (string str)
65 (symbol (symbol-name str))
66 (t (with-output-to-string (s)
67 (princ str s)))))
02866e07 68
861345b4 69(compile-time-defun listify (x)
70 "If X is a (possibly empty) list, return X; otherwise return (list X)."
71 (if (listp x) x (list x)))
02866e07 72
861345b4 73(compile-time-defun do-fix-pair (x y defaultp)
74 "Helper function for fix-pair and pairify."
75 (flet ((singleton (x) (values x (if defaultp y x))))
76 (cond ((atom x) (singleton x))
77 ((null (cdr x)) (singleton (car x)))
78 ((atom (cdr x)) (values (car x) (cdr x)))
79 ((cddr x) (error "Too many elements for a pair."))
80 (t (values (car x) (cadr x))))))
02866e07 81
861345b4 82(compile-time-defun fix-pair (x &optional (y nil defaultp))
83 "Return two values extracted from X. It works as follows:
84 (A) -> A, Y
85 (A B) -> A, B
86 (A B . C) -> error
87 (A . B) -> A, B
88 A -> A, Y
89where Y defaults to A if not specified."
90 (do-fix-pair x y defaultp))
02866e07 91
861345b4 92(compile-time-defun pairify (x &optional (y nil defaultp))
93 "As for fix-pair, but returns a list instead of two values."
94 (multiple-value-call #'list (do-fix-pair x y defaultp)))
95
96(defun whitespace-char-p (ch)
97 "Return whether CH is a whitespace character or not."
98 (case ch
99 ((#\space #\tab #\newline #\return #\vt #\formfeed) t)
100 (t nil)))
101
102(declaim (ftype (function nil ()) slot-unitialized))
103(defun slot-uninitialized ()
104 "A function which signals an error. Can be used as an initializer form in
105structure definitions without doom ensuing."
106 (error "No initializer for slot."))
107
02866e07
MW
108;;;--------------------------------------------------------------------------
109;;; Generating symbols.
110
861345b4 111(defmacro with-gensyms (syms &body body)
112 "Everyone's favourite macro helper."
113 `(let (,@(mapcar (lambda (sym) `(,sym (gensym ,(symbol-name sym))))
114 (listify syms)))
115 ,@body))
116
117(defmacro let*/gensyms (binds &body body)
118 "A macro helper. BINDS is a list of binding pairs (VAR VALUE), where VALUE
119defaults to VAR. The result is that BODY is evaluated in a context where
120each VAR is bound to a gensym, and in the final expansion, each of those
121gensyms will be bound to the corresponding VALUE."
122 (labels ((more (binds)
123 (let ((tmp (gensym "TMP")) (bind (car binds)))
124 `((let ((,tmp ,(cadr bind))
125 (,(car bind) (gensym ,(symbol-name (car bind)))))
126 `(let ((,,(car bind) ,,tmp))
127 ,,@(if (cdr binds)
128 (more (cdr binds))
129 body)))))))
130 (if (null binds)
131 `(progn ,@body)
132 (car (more (mapcar #'pairify (listify binds)))))))
133
02866e07 134;;;--------------------------------------------------------------------------
f2d46aaa
MW
135;;; Some simple yet useful control structures.
136
137(defmacro nlet (name binds &body body)
138 "Scheme's named let."
139 (multiple-value-bind (vars vals)
140 (loop for bind in binds
141 for (var val) = (pairify bind nil)
142 collect var into vars
143 collect val into vals
144 finally (return (values vars vals)))
145 `(labels ((,name ,vars
146 ,@body))
147 (,name ,@vals))))
148
149(defmacro while (cond &body body)
150 "If COND is false, evaluate to nil; otherwise evaluate BODY and try again."
151 `(loop
93f04724 152 (unless ,cond (return))
f2d46aaa
MW
153 ,@body))
154
155;;;--------------------------------------------------------------------------
02866e07
MW
156;;; with-places
157
861345b4 158(defmacro %place-ref (getform setform newtmp)
159 "Grim helper macro for with-places."
160 (declare (ignore setform newtmp))
161 getform)
02866e07 162
861345b4 163(define-setf-expander %place-ref (getform setform newtmp)
164 "Grim helper macro for with-places."
165 (values nil nil newtmp setform getform))
02866e07 166
861345b4 167(defmacro with-places ((&key environment) places &body body)
168 "A hairy helper, for writing setf-like macros. PLACES is a list of binding
169pairs (VAR PLACE), where PLACE defaults to VAR. The result is that BODY is
170evaluated in a context where each VAR is bound to a gensym, and in the final
171expansion, each of those gensyms will be bound to a symbol-macro capable of
172reading or setting the value of the corresponding PLACE."
173 (if (null places)
174 `(progn ,@body)
175 (let*/gensyms (environment)
176 (labels
177 ((more (places)
178 (let ((place (car places)))
179 (with-gensyms (tmp valtmps valforms
180 newtmps setform getform)
181 `((let ((,tmp ,(cadr place))
182 (,(car place)
183 (gensym ,(symbol-name (car place)))))
184 (multiple-value-bind
185 (,valtmps ,valforms
186 ,newtmps ,setform ,getform)
187 (get-setf-expansion ,tmp
188 ,environment)
189 (list 'let*
190 (mapcar #'list ,valtmps ,valforms)
191 `(symbol-macrolet ((,,(car place)
192 (%place-ref ,,getform
193 ,,setform
194 ,,newtmps)))
195 ,,@(if (cdr places)
196 (more (cdr places))
197 body))))))))))
198 (car (more (mapcar #'pairify (listify places))))))))
199
02866e07
MW
200;;;--------------------------------------------------------------------------
201;;; Update-in-place macros built using with-places.
202
e979e568
MW
203(defmacro update-place (op place arg &environment env)
204 "Update PLACE with the value of OP PLACE ARG, returning the new value."
205 (with-places (:environment env) (place)
206 `(setf ,place (,op ,place ,arg))))
02866e07 207
e979e568
MW
208(defmacro update-place-after (op place arg &environment env)
209 "Update PLACE with the value of OP PLACE ARG, returning the old value."
210 (with-places (:environment env) (place)
211 (with-gensyms (x)
212 `(let ((,x ,place))
02866e07
MW
213 (setf ,place (,op ,x ,arg))
214 ,x))))
215
e979e568
MW
216(defmacro incf-after (place &optional (by 1))
217 "Increment PLACE by BY, returning the old value."
218 `(update-place-after + ,place ,by))
02866e07 219
e979e568
MW
220(defmacro decf-after (place &optional (by 1))
221 "Decrement PLACE by BY, returning the old value."
222 `(update-place-after - ,place ,by))
223
02866e07
MW
224;;;--------------------------------------------------------------------------
225;;; Locatives.
e979e568 226
861345b4 227(defstruct (loc (:predicate locp) (:constructor make-loc (reader writer)))
228 "Locative data type. See `locf' and `ref'."
229 (reader (slot-uninitialized) :type function)
230 (writer (slot-uninitialized) :type function))
02866e07 231
861345b4 232(defmacro locf (place &environment env)
233 "Slightly cheesy locatives. (locf PLACE) returns an object which, using
234the `ref' function, can be used to read or set the value of PLACE. It's
235cheesy because it uses closures rather than actually taking the address of
236something. Also, unlike Zetalisp, we don't overload `car' to do our dirty
237work."
238 (multiple-value-bind
239 (valtmps valforms newtmps setform getform)
240 (get-setf-expansion place env)
241 `(let* (,@(mapcar #'list valtmps valforms))
242 (make-loc (lambda () ,getform)
243 (lambda (,@newtmps) ,setform)))))
02866e07 244
861345b4 245(declaim (inline loc (setf loc)))
02866e07 246
861345b4 247(defun ref (loc)
248 "Fetch the value referred to by a locative."
249 (funcall (loc-reader loc)))
02866e07 250
861345b4 251(defun (setf ref) (new loc)
252 "Store a new value in the place referred to by a locative."
253 (funcall (loc-writer loc) new))
02866e07 254
861345b4 255(defmacro with-locatives (locs &body body)
256 "LOCS is a list of items of the form (SYM [LOC-EXPR]), where SYM is a
257symbol and LOC-EXPR evaluates to a locative. If LOC-EXPR is omitted, it
258defaults to SYM. As an abbreviation for a common case, LOCS may be a symbol
259instead of a list. The BODY is evaluated in an environment where each SYM is
260a symbol macro which expands to (ref LOC-EXPR) -- or, in fact, something
261similar which doesn't break if LOC-EXPR has side-effects. Thus, references,
262including `setf' forms, fetch or modify the thing referred to by the
263LOC-EXPR. Useful for covering over where something uses a locative."
264 (setf locs (mapcar #'pairify (listify locs)))
265 (let ((tt (mapcar (lambda (l) (declare (ignore l)) (gensym)) locs))
266 (ll (mapcar #'cadr locs))
267 (ss (mapcar #'car locs)))
268 `(let (,@(mapcar (lambda (tmp loc) `(,tmp ,loc)) tt ll))
269 (symbol-macrolet (,@(mapcar (lambda (sym tmp)
270 `(,sym (ref ,tmp))) ss tt))
271 ,@body))))
272
273;;;----- That's all, folks --------------------------------------------------