1 | ;;; do.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2004-2006 Peter Graves <peter@armedbear.org> |
---|
4 | ;;; |
---|
5 | ;;; This program is free software; you can redistribute it and/or |
---|
6 | ;;; modify it under the terms of the GNU General Public License |
---|
7 | ;;; as published by the Free Software Foundation; either version 2 |
---|
8 | ;;; of the License, or (at your option) any later version. |
---|
9 | ;;; |
---|
10 | ;;; This program is distributed in the hope that it will be useful, |
---|
11 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | ;;; GNU General Public License for more details. |
---|
14 | ;;; |
---|
15 | ;;; You should have received a copy of the GNU General Public License |
---|
16 | ;;; along with this program; if not, write to the Free Software |
---|
17 | ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
18 | ;;; |
---|
19 | ;;; As a special exception, the copyright holders of this library give you |
---|
20 | ;;; permission to link this library with independent modules to produce an |
---|
21 | ;;; executable, regardless of the license terms of these independent |
---|
22 | ;;; modules, and to copy and distribute the resulting executable under |
---|
23 | ;;; terms of your choice, provided that you also meet, for each linked |
---|
24 | ;;; independent module, the terms and conditions of the license of that |
---|
25 | ;;; module. An independent module is a module which is not derived from |
---|
26 | ;;; or based on this library. If you modify this library, you may extend |
---|
27 | ;;; this exception to your version of the library, but you are not |
---|
28 | ;;; obligated to do so. If you do not wish to do so, delete this |
---|
29 | ;;; exception statement from your version. |
---|
30 | |
---|
31 | ;;; Adapted from CMUCL. |
---|
32 | |
---|
33 | (in-package "SYSTEM") |
---|
34 | |
---|
35 | (defun do-do-body (varlist endlist decls-and-code bind step name block) |
---|
36 | (let* ((inits ()) |
---|
37 | (steps ()) |
---|
38 | (L1 (gensym)) |
---|
39 | (L2 (gensym))) |
---|
40 | ;; Check for illegal old-style do. |
---|
41 | (when (or (not (listp varlist)) (atom endlist)) |
---|
42 | (error "Ill-formed ~S -- possibly illegal old style DO?" name)) |
---|
43 | ;; Parse the varlist to get inits and steps. |
---|
44 | (dolist (v varlist) |
---|
45 | (cond ((symbolp v) (push v inits)) |
---|
46 | ((listp v) |
---|
47 | (unless (symbolp (first v)) |
---|
48 | (error "~S step variable is not a symbol: ~S" name (first v))) |
---|
49 | (case (length v) |
---|
50 | (1 (push (first v) inits)) |
---|
51 | (2 (push v inits)) |
---|
52 | (3 (push (list (first v) (second v)) inits) |
---|
53 | (setq steps (list* (third v) (first v) steps))) |
---|
54 | (t (error "~S is an illegal form for a ~S varlist." v name)))) |
---|
55 | (t (error "~S is an illegal form for a ~S varlist." v name)))) |
---|
56 | ;; Construct the new form. |
---|
57 | (multiple-value-bind (code decls) (parse-body decls-and-code nil) |
---|
58 | `(block ,block |
---|
59 | (,bind ,(nreverse inits) |
---|
60 | ,@decls |
---|
61 | (tagbody |
---|
62 | (go ,L2) |
---|
63 | ,L1 |
---|
64 | ,@code |
---|
65 | (,step ,@(nreverse steps)) |
---|
66 | ,L2 |
---|
67 | (unless ,(car endlist) (go ,L1)) |
---|
68 | (return-from ,block (progn ,@(cdr endlist))))))))) |
---|
69 | |
---|
70 | (defmacro do (varlist endlist &rest body) |
---|
71 | (do-do-body varlist endlist body 'let 'psetq 'do nil)) |
---|
72 | |
---|
73 | (defmacro do* (varlist endlist &rest body) |
---|
74 | (do-do-body varlist endlist body 'let* 'setq 'do* nil)) |
---|