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

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

INCF, DECF

File size: 3.5 KB
Line 
1;;; define-modify-macro.lisp
2;;;
3;;; Copyright (C) 2003-2005 Peter Graves
4;;; $Id: define-modify-macro.lisp,v 1.2 2005-02-03 02:27:44 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;;; Adapted from CMUCL.
21
22(in-package "SYSTEM")
23
24(defmacro define-modify-macro (name lambda-list function &optional doc-string)
25  "Creates a new read-modify-write macro like PUSH or INCF."
26  (let ((other-args nil)
27  (rest-arg nil)
28  (env (gensym))
29  (reference (gensym)))
30    ;; Parse out the variable names and rest arg from the lambda list.
31    (do ((ll lambda-list (cdr ll))
32   (arg nil))
33  ((null ll))
34      (setq arg (car ll))
35      (cond ((eq arg '&optional))
36      ((eq arg '&rest)
37       (if (symbolp (cadr ll))
38     (setq rest-arg (cadr ll))
39     (error "non-symbol &REST arg in definition of ~S" name))
40       (if (null (cddr ll))
41     (return nil)
42     (error "illegal stuff after &rest arg in DEFINE-MODIFY-MACRO")))
43      ((memq arg '(&key &allow-other-keys &aux))
44       (error "~S not allowed in DEFINE-MODIFY-MACRO lambda list" arg))
45      ((symbolp arg)
46       (push arg other-args))
47      ((and (listp arg) (symbolp (car arg)))
48       (push (car arg) other-args))
49      (t (error "illegal stuff in lambda list of DEFINE-MODIFY-MACRO"))))
50    (setq other-args (nreverse other-args))
51    `(defmacro ,name (,reference ,@lambda-list &environment ,env)
52       ,doc-string
53       (multiple-value-bind (dummies vals newval setter getter)
54   (get-setf-expansion ,reference ,env)
55   (do ((d dummies (cdr d))
56        (v vals (cdr v))
57        (let-list nil (cons (list (car d) (car v)) let-list)))
58       ((null d)
59        (push
60         (list (car newval)
61         ,(if rest-arg
62        `(list* ',function getter ,@other-args ,rest-arg)
63        `(list ',function getter ,@other-args)))
64         let-list)
65        `(let* ,(nreverse let-list)
66     ,setter)))))))
67
68(define-modify-macro incf-complex (&optional (delta 1)) +
69  "The first argument is some location holding a number.  This number is
70   incremented by the second argument, DELTA, which defaults to 1.")
71
72(define-modify-macro decf-complex (&optional (delta 1)) -
73  "The first argument is some location holding a number.  This number is
74   decremented by the second argument, DELTA, which defaults to 1.")
75
76(defmacro incf (place &optional (delta 1))
77  (cond ((symbolp place)
78         `(setq ,place (+ ,place ,delta)))
79        ((and (consp place) (eq (car place) 'THE))
80         (let ((res (gensym)))
81           `(let ((,res (the ,(second place) (+ ,place ,delta))))
82              (setq ,(third place) ,res))))
83        (t
84         `(incf-complex ,place ,delta))))
85
86(defmacro decf (place &optional (delta 1))
87  (cond ((symbolp place)
88         `(setq ,place (- ,place ,delta)))
89        ((and (consp place) (eq (car place) 'THE))
90         (let ((res (gensym)))
91           `(let ((,res (the ,(second place) (- ,place ,delta))))
92              (setq ,(third place) ,res))))
93        (t
94         `(decf-complex ,place ,delta))))
Note: See TracBrowser for help on using the repository browser.