source: trunk/abcl/src/org/armedbear/lisp/destructuring-bind.lisp @ 15004

Last change on this file since 15004 was 15004, checked in by Mark Evenson, 7 years ago

Fix DESTRUCTURING-BIND with &rest arguments

(Olof-Joachim Frahm)

Fixes <http://abcl.org/trac/ticket/417> aka
<https://github.com/armedbear/abcl/issues/8>.

Merges <https://github.com/armedbear/abcl/pull/42>.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 14.3 KB
Line 
1;;; destructuring-bind.lisp
2;;;
3;;; Copyright (C) 2003-2005 Peter Graves
4;;; $Id: destructuring-bind.lisp 15004 2017-05-15 20:37:26Z mevenson $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19;;;
20;;; As a special exception, the copyright holders of this library give you
21;;; permission to link this library with independent modules to produce an
22;;; executable, regardless of the license terms of these independent
23;;; modules, and to copy and distribute the resulting executable under
24;;; terms of your choice, provided that you also meet, for each linked
25;;; independent module, the terms and conditions of the license of that
26;;; module.  An independent module is a module which is not derived from
27;;; or based on this library.  If you modify this library, you may extend
28;;; this exception to your version of the library, but you are not
29;;; obligated to do so.  If you do not wish to do so, delete this
30;;; exception statement from your version.
31
32;;;; Adapted from CMUCL/SBCL.
33
34(in-package #:system)
35
36(export '(parse-body))
37
38(defun parse-body (body &optional (doc-string-allowed t))
39  (let ((decls ())
40  (doc nil))
41    (do ((tail body (cdr tail)))
42  ((endp tail)
43   (values tail (nreverse decls) doc))
44      (let ((form (car tail)))
45  (cond ((and (stringp form) (cdr tail))
46         (if doc-string-allowed
47       (setq doc form
48       ;; Only one doc string is allowed.
49       doc-string-allowed nil)
50       (return (values tail (nreverse decls) doc))))
51        ((not (and (consp form) (symbolp (car form))))
52         (return (values tail (nreverse decls) doc)))
53        ((eq (car form) 'declare)
54         (push form decls))
55        (t
56         (return (values tail (nreverse decls) doc))))))))
57
58;; We don't have DEFVAR yet...
59(eval-when (:compile-toplevel :load-toplevel :execute)
60  (%defvar '*arg-tests* ())
61  (%defvar '*system-lets* ())
62  (%defvar '*user-lets* ())
63  (%defvar '*ignorable-vars* ())
64  (%defvar '*env-var* nil))
65
66(defun arg-count-error (error-kind name arg lambda-list minimum maximum)
67  (declare (ignore error-kind arg lambda-list minimum maximum))
68  (error 'program-error
69         :format-control "Wrong number of arguments for ~S."
70         :format-arguments (list name)))
71
72;;; Return, as multiple values, a body, possibly a DECLARE form to put
73;;; where this code is inserted, the documentation for the parsed
74;;; body, and bounds on the number of arguments.
75(defun parse-defmacro (lambda-list arg-list-name body name context
76           &key
77           (anonymousp nil)
78           (doc-string-allowed t)
79           ((:environment env-arg-name))
80           (error-fun 'error)
81                                   (wrap-block t))
82  (multiple-value-bind (forms declarations documentation)
83      (parse-body body doc-string-allowed)
84    (let ((*arg-tests* ())
85    (*user-lets* ())
86    (*system-lets* ())
87    (*ignorable-vars* ())
88          (*env-var* nil))
89      (multiple-value-bind (env-arg-used minimum maximum)
90    (parse-defmacro-lambda-list lambda-list arg-list-name name
91              context error-fun (not anonymousp)
92              nil)
93  (values `(let* (,@(when env-arg-used
94                            `((,*env-var* ,env-arg-name)))
95                        ,@(nreverse *system-lets*))
96       ,@(when *ignorable-vars*
97           `((declare (ignorable ,@*ignorable-vars*))))
98       ,@*arg-tests*
99       (let* ,(nreverse *user-lets*)
100         ,@declarations
101                     ,@(if wrap-block
102                           `((block ,(fdefinition-block-name name) ,@forms))
103                           forms)))
104    `(,@(when (and env-arg-name (not env-arg-used))
105                      `((declare (ignore ,env-arg-name)))))
106    documentation
107    minimum
108    maximum)))))
109
110(defun defmacro-error (problem name)
111  (error 'type-error "~S is not of type ~S~%" problem name))
112
113(defun verify-keywords (key-list valid-keys allow-other-keys)
114  (do ((already-processed nil)
115       (unknown-keyword nil)
116       (remaining key-list (cddr remaining)))
117      ((null remaining)
118       (if (and unknown-keyword
119    (not allow-other-keys)
120    (not (lookup-keyword :allow-other-keys key-list)))
121     (values :unknown-keyword (list unknown-keyword valid-keys))
122     (values nil nil)))
123    (cond ((not (and (consp remaining) (listp (cdr remaining))))
124     (return (values :dotted-list key-list)))
125    ((null (cdr remaining))
126     (return (values :odd-length key-list)))
127    ((or (eq (car remaining) :allow-other-keys)
128         (memql (car remaining) valid-keys))
129     (push (car remaining) already-processed))
130    (t
131     (setq unknown-keyword (car remaining))))))
132
133(defun lookup-keyword (keyword key-list)
134  (do ((remaining key-list (cddr remaining)))
135      ((endp remaining))
136    (when (eq keyword (car remaining))
137      (return (cadr remaining)))))
138
139(defun keyword-supplied-p (keyword key-list)
140  (do ((remaining key-list (cddr remaining)))
141      ((endp remaining))
142    (when (eq keyword (car remaining))
143      (return t))))
144
145(defun dot-length (cons)
146  (do ((rest cons (cdr rest))
147       (length 0 (1+ length)))
148      ((or (null rest) (atom rest)) length)))
149
150(defun parse-defmacro-lambda-list
151       (lambda-list arg-list-name name error-kind error-fun
152        &optional top-level env-illegal ;;env-arg-name
153                    )
154  (let* ((path-0 (if top-level `(cdr ,arg-list-name) arg-list-name))
155         (path path-0)
156         (now-processing :required)
157         (maximum 0)
158         (minimum 0)
159         (keys ())
160         rest-name restp allow-other-keys-p env-arg-used)
161    ;; This really strange way to test for &WHOLE is necessary because MEMBER
162    ;; does not have to work on dotted lists, and dotted lists are legal
163    ;; in lambda lists.
164    (when (and (do ((list lambda-list (cdr list)))
165       ((atom list) nil)
166     (when (eq (car list) '&WHOLE) (return t)))
167         (not (eq (car lambda-list) '&WHOLE)))
168      (error "&Whole must appear first in ~S lambda-list." error-kind))
169    (do ((rest-of-args lambda-list (cdr rest-of-args)))
170  ((atom rest-of-args)
171   (cond ((null rest-of-args) nil)
172         ;; Varlist is dotted, treat as &rest arg and exit.
173         (t (push-let-binding rest-of-args path nil)
174      (setq restp t))))
175      (let ((var (car rest-of-args)))
176  (cond ((eq var '&whole)
177         (cond ((and (cdr rest-of-args) (symbolp (cadr rest-of-args)))
178          (setq rest-of-args (cdr rest-of-args))
179          (push-let-binding (car rest-of-args) arg-list-name nil))
180         ((and (cdr rest-of-args) (consp (cadr rest-of-args)))
181          (pop rest-of-args)
182          (let* ((destructuring-lambda-list (car rest-of-args))
183           (sub (gensym "WHOLE-SUBLIST")))
184      (push-sub-list-binding
185       sub arg-list-name destructuring-lambda-list
186       name error-kind error-fun)
187      (parse-defmacro-lambda-list
188       destructuring-lambda-list sub name error-kind error-fun)))
189         (t
190          (defmacro-error "&WHOLE" name))))
191        ((eq var '&environment)
192         (cond (env-illegal
193          (error "&ENVIRONMENT is not valid with ~S." error-kind))
194         ((not top-level)
195          (error "&ENVIRONMENT is only valid at top level of lambda list.")))
196         (cond ((and (cdr rest-of-args) (symbolp (cadr rest-of-args)))
197          (setq rest-of-args (cdr rest-of-args))
198                      (setq *env-var* (car rest-of-args)
199                            env-arg-used t))
200         (t
201          (defmacro-error "&ENVIRONMENT" name))))
202        ((or (eq var '&rest) (eq var '&body))
203         (cond ((and (cdr rest-of-args) (symbolp (cadr rest-of-args)))
204          (setq rest-of-args (cdr rest-of-args))
205          (setq restp t)
206          (push-let-binding (car rest-of-args) path nil))
207         ((and (cdr rest-of-args) (consp (cadr rest-of-args)))
208          (pop rest-of-args)
209          (setq restp t)
210          (let* ((destructuring-lambda-list (car rest-of-args))
211           (sub (gensym "REST-SUBLIST")))
212      (push-sub-list-binding sub path destructuring-lambda-list
213                                               name error-kind error-fun)
214      (parse-defmacro-lambda-list
215       destructuring-lambda-list sub name error-kind error-fun)))
216         (t
217          (defmacro-error (symbol-name var) name))))
218        ((eq var '&optional)
219         (setq now-processing :optionals))
220        ((eq var '&key)
221         (setq now-processing :keywords)
222         (setq rest-name (gensym "KEYWORDS-"))
223         (push rest-name *ignorable-vars*)
224         (setq restp t)
225         (push-let-binding rest-name path t))
226        ((eq var '&allow-other-keys)
227         (setq allow-other-keys-p t))
228        ((eq var '&aux)
229         (setq now-processing :auxs))
230        ((listp var)
231         (case now-processing
232     (:required
233      (let ((sub-list-name (gensym "SUBLIST-")))
234        (push-sub-list-binding sub-list-name `(car ,path) var
235             name error-kind error-fun)
236        (parse-defmacro-lambda-list var sub-list-name name
237            error-kind error-fun))
238      (setq path `(cdr ,path))
239      (incf minimum)
240      (incf maximum))
241     (:optionals
242      (when (> (length var) 3)
243        (error "more than variable, initform, and suppliedp in &optional binding ~S"
244                           var))
245      (push-optional-binding (car var) (cadr var) (caddr var)
246           `(not (null ,path)) `(car ,path)
247           name error-kind error-fun)
248      (setq path `(cdr ,path))
249      (incf maximum))
250     (:keywords
251      (let* ((keyword-given (consp (car var)))
252       (variable (if keyword-given
253               (cadar var)
254               (car var)))
255       (keyword (if keyword-given
256              (caar var)
257              (make-keyword variable)))
258       (supplied-p (caddr var)))
259        (push-optional-binding variable (cadr var) supplied-p
260             `(keyword-supplied-p ',keyword
261                ,rest-name)
262             `(lookup-keyword ',keyword
263                  ,rest-name)
264             name error-kind error-fun)
265        (push keyword keys)))
266     (:auxs (push-let-binding (car var) (cadr var) nil))))
267        ((symbolp var)
268         (case now-processing
269     (:required
270      (incf minimum)
271      (incf maximum)
272      (push-let-binding var `(car ,path) nil)
273      (setq path `(cdr ,path)))
274     (:optionals
275      (incf maximum)
276      (push-let-binding var `(car ,path) nil `(not (null ,path)))
277      (setq path `(cdr ,path)))
278     (:keywords
279      (let ((key (make-keyword var)))
280        (push-let-binding var `(lookup-keyword ,key ,rest-name)
281              nil)
282        (push key keys)))
283     (:auxs
284      (push-let-binding var nil nil))))
285        (t
286         (error "non-symbol in lambda-list: ~S" var)))))
287    ;; Generate code to check the number of arguments.
288    (push `(unless (<= ,minimum
289                       (dot-length ,path-0)
290                       ,@(unless restp
291                           (list maximum)))
292             ,(if (eq error-fun 'error)
293                  `(arg-count-error ',error-kind ',name ,path-0
294                                    ',lambda-list ,minimum
295                                    ,(unless restp maximum))
296                  `(,error-fun 'arg-count-error
297                    :kind ',error-kind
298                    ,@(when name `(:name ',name))
299                    :argument ,path-0
300                    :lambda-list ',lambda-list
301                    :minimum ,minimum
302                    ,@(unless restp `(:maximum ,maximum)))))
303          *arg-tests*)
304    (if keys
305        (let ((problem (gensym "KEY-PROBLEM-"))
306              (info (gensym "INFO-")))
307          (push `(multiple-value-bind (,problem ,info)
308                     (verify-keywords ,rest-name ',keys ',allow-other-keys-p)
309                   (when ,problem
310                     ,(if (eq error-fun 'error)
311                          `(error 'program-error
312                                  "Unrecognized keyword argument ~S"
313                                  (car ,info))
314                          `(,error-fun
315                           'defmacro-lambda-list-broken-key-list-error
316                           :kind ',error-kind
317                           ,@(when name `(:name ',name))
318                           :problem ,problem
319                           :info ,info))))
320                *arg-tests*)))
321    (values env-arg-used minimum (if (null restp) maximum nil))))
322
323
324(defun push-sub-list-binding (variable path object name error-kind error-fun)
325  (let ((var (gensym "TEMP-")))
326    (push `(,variable
327      (let ((,var ,path))
328        (if (listp ,var)
329      ,var
330      (,error-fun 'defmacro-bogus-sublist-error
331            :kind ',error-kind
332            ,@(when name `(:name ',name))
333            :object ,var
334            :lambda-list ',object))))
335    *system-lets*)))
336
337(defun push-let-binding (variable path systemp &optional condition
338          (init-form nil))
339  (let ((let-form (if condition
340          `(,variable (if ,condition ,path ,init-form))
341          `(,variable ,path))))
342    (if systemp
343  (push let-form *system-lets*)
344  (push let-form *user-lets*))))
345
346(defun push-optional-binding (value-var init-form supplied-var condition path
347          name error-kind error-fun)
348  (unless supplied-var
349    (setq supplied-var (gensym "SUPPLIEDP-")))
350  (push-let-binding supplied-var condition t)
351  (cond ((consp value-var)
352   (let ((whole-thing (gensym "OPTIONAL-SUBLIST-")))
353     (push-sub-list-binding whole-thing
354          `(if ,supplied-var ,path ,init-form)
355          value-var name error-kind error-fun)
356     (parse-defmacro-lambda-list value-var whole-thing name
357               error-kind error-fun)))
358  ((symbolp value-var)
359   (push-let-binding value-var path nil supplied-var init-form))
360  (t
361   (error "Illegal optional variable name: ~S" value-var))))
362
363(defmacro destructuring-bind (lambda-list arg-list &rest body)
364  (let* ((arg-list-name (gensym "ARG-LIST-")))
365    (multiple-value-bind (body local-decls)
366  (parse-defmacro lambda-list arg-list-name body nil 'destructuring-bind
367      :anonymousp t
368                        :doc-string-allowed nil
369                        :wrap-block nil)
370      `(let ((,arg-list-name ,arg-list))
371   ,@local-decls
372   ,body))))
373
374;; Redefine SYS:MAKE-MACRO-EXPANDER to use PARSE-DEFMACRO.
375(defun make-macro-expander (definition)
376  (let* ((name (car definition))
377         (lambda-list (cadr definition))
378         (form (gensym "WHOLE-"))
379         (env (gensym "ENVIRONMENT-"))
380         (body (parse-defmacro lambda-list form (cddr definition) name 'defmacro
381                               :environment env)))
382    `(lambda (,form ,env) (block ,name ,body))))
Note: See TracBrowser for help on using the repository browser.