source: trunk/j/src/org/armedbear/lisp/dotimes.lisp @ 9266

Last change on this file since 9266 was 8239, checked in by piso, 17 years ago

New implementation.

File size: 1.9 KB
Line 
1;;; dotimes.lisp
2;;;
3;;; Copyright (C) 2004 Peter Graves
4;;; $Id: dotimes.lisp,v 1.3 2004-12-11 18:16:08 piso Exp $
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(in-package "SYSTEM")
21
22(defmacro dotimes ((var count &optional (result nil)) &body body)
23  (multiple-value-bind (forms decls) (parse-body body nil)
24    (if (numberp count)
25        (let ((top (gensym "TOP-"))
26              (test (gensym "TEST-")))
27          `(block nil
28             (let ((,var 0))
29               ,@decls
30               (tagbody
31                (go ,test)
32                ,top
33                ,@forms
34                (setq ,var (1+ ,var))
35                ,test
36                (when (< ,var ,count)
37                  (go ,top))
38                (return-from nil (progn ,result))
39                ))))
40        (let ((limit (gensym "LIMIT-"))
41              (top (gensym "TOP-"))
42              (test (gensym "TEST-")))
43          `(block nil
44             (let ((,limit ,count)
45                   (,var 0))
46               ,@decls
47               (tagbody
48                (go ,test)
49                ,top
50                ,@forms
51                (setq ,var (1+ ,var))
52                ,test
53                (when (< ,var ,limit)
54                  (go ,top))
55                (return-from nil (progn ,result))
56                )))))))
Note: See TracBrowser for help on using the repository browser.