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

Last change on this file since 11297 was 11297, checked in by ehuelsmann, 13 years ago

Set Id keyword for expansion.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.3 KB
Line 
1;;; dotimes.lisp
2;;;
3;;; Copyright (C) 2004-2005 Peter Graves
4;;; $Id: dotimes.lisp 11297 2008-08-31 13:26:45Z ehuelsmann $
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    (let ((index (gensym "INDEX-"))
25          (top   (gensym "TOP-")))
26      (if (numberp count)
27          `(block nil
28             (let ((,var 0)
29                   (,index 0))
30               (declare (type (integer 0 ,count) ,index))
31               (declare (ignorable ,var))
32               ,@decls
33               (when (> ,count 0)
34                 (tagbody
35                  ,top
36                  ,@forms
37                  (setq ,index (1+ ,index))
38                  (setq ,var ,index)
39                  (when (< ,index ,count)
40                    (go ,top))))
41               (progn ,result)))
42          (let ((limit (gensym "LIMIT-")))
43            ;; Annotations for the compiler.
44            (setf (get limit 'dotimes-limit-variable-p) t)
45            (setf (get limit 'dotimes-index-variable-name) index)
46            (setf (get index 'dotimes-index-variable-p) t)
47            (setf (get index 'dotimes-limit-variable-name) limit)
48            `(block nil
49               (let ((,var 0)
50                     (,limit ,count)
51                     (,index 0))
52                 (declare (ignorable ,var))
53                 ,@decls
54                 (when (> ,limit 0)
55                   (tagbody
56                    ,top
57                    ,@forms
58                    (setq ,index (1+ ,index))
59                    (setq ,var ,index)
60                    (when (< ,index ,limit)
61                      (go ,top))))
62                 (progn ,result))))))))
Note: See TracBrowser for help on using the repository browser.