source: trunk/j/src/org/armedbear/lisp/define-modify-macro.lisp @ 11391

Last change on this file since 11391 was 11391, checked in by vvoutilainen, 13 years ago

ABCL license is GPL + Classpath exception. This was intended
by Peter Graves, the original author. For reference, see
http://sourceforge.net/mailarchive/forum.php?thread_name=20040721115302.839%40prufrock&forum_name=armedbear-j-announce

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1;;; define-modify-macro.lisp
2;;;
3;;; Copyright (C) 2003-2005 Peter Graves
4;;; $Id: define-modify-macro.lisp 11391 2008-11-15 22:38:34Z vvoutilainen $
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 SBCL.
33
34(in-package #:system)
35
36;; FIXME See section 5.1.3.
37(defmacro define-modify-macro (name lambda-list function &optional doc-string)
38  "Creates a new read-modify-write macro like PUSH or INCF."
39  (let ((other-args nil)
40  (rest-arg nil)
41  (env (gensym))
42  (reference (gensym)))
43    ;; Parse out the variable names and &REST arg from the lambda list.
44    (do ((ll lambda-list (cdr ll))
45   (arg nil))
46  ((null ll))
47      (setq arg (car ll))
48      (cond ((eq arg '&optional))
49      ((eq arg '&rest)
50       (if (symbolp (cadr ll))
51     (setq rest-arg (cadr ll))
52     (error "Non-symbol &REST arg in definition of ~S." name))
53       (if (null (cddr ll))
54     (return nil)
55     (error "Illegal stuff after &REST argument in DEFINE-MODIFY-MACRO.")))
56      ((memq arg '(&key &allow-other-keys &aux))
57       (error "~S not allowed in DEFINE-MODIFY-MACRO lambda list." arg))
58      ((symbolp arg)
59       (push arg other-args))
60      ((and (listp arg) (symbolp (car arg)))
61       (push (car arg) other-args))
62      (t (error "Illegal stuff in DEFINE-MODIFY-MACRO lambda list."))))
63    (setq other-args (nreverse other-args))
64    `(defmacro ,name (,reference ,@lambda-list &environment ,env)
65       ,doc-string
66       (multiple-value-bind (dummies vals newval setter getter)
67           (get-setf-expansion ,reference ,env)
68   (do ((d dummies (cdr d))
69        (v vals (cdr v))
70        (let-list nil (cons (list (car d) (car v)) let-list)))
71       ((null d)
72        (push (list (car newval)
73                          ,(if rest-arg
74                               `(list* ',function getter ,@other-args ,rest-arg)
75                               `(list ',function getter ,@other-args)))
76                    let-list)
77        `(let* ,(nreverse let-list)
78     ,setter)))))))
79
80(define-modify-macro incf-complex (&optional (delta 1)) +
81  "The first argument is some location holding a number.  This number is
82   incremented by the second argument, DELTA, which defaults to 1.")
83
84(define-modify-macro decf-complex (&optional (delta 1)) -
85  "The first argument is some location holding a number.  This number is
86   decremented by the second argument, DELTA, which defaults to 1.")
87
88(defmacro incf (place &optional (delta 1))
89  (cond ((symbolp place)
90         (cond ((constantp delta)
91                `(setq ,place (+ ,place ,delta)))
92               (t
93                ;; See section 5.1.3.
94                (let ((temp (gensym)))
95                  `(let ((,temp ,delta))
96                     (setq ,place (+ ,place ,temp)))))))
97        ((and (consp place) (eq (car place) 'THE))
98         (let ((res (gensym)))
99           `(let ((,res (the ,(second place) (+ ,place ,delta))))
100              (setf ,(third place) ,res))))
101        (t
102         `(incf-complex ,place ,delta))))
103
104(defmacro decf (place &optional (delta 1))
105  (cond ((symbolp place)
106         (cond ((constantp delta)
107                `(setq ,place (- ,place ,delta)))
108               (t
109                ;; See section 5.1.3.
110                (let ((temp (gensym)))
111                  `(let ((,temp ,delta))
112                     (setq ,place (- ,place ,temp)))))))
113        ((and (consp place) (eq (car place) 'THE))
114         (let ((res (gensym)))
115           `(let ((,res (the ,(second place) (- ,place ,delta))))
116              (setf ,(third place) ,res))))
117        (t
118         `(decf-complex ,place ,delta))))
Note: See TracBrowser for help on using the repository browser.