source: trunk/abcl/src/org/armedbear/lisp/macros.lisp

Last change on this file was 15569, checked in by Mark Evenson, 2 years ago

Untabify en masse

Results of running style.org source blocks on tree

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1;;; macros.lisp
2;;;
3;;; Copyright (C) 2003-2007 Peter Graves
4;;; $Id: macros.lisp 15569 2022-03-19 12:50:18Z mevenson $
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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(in-package "SYSTEM")
33
34(export 'defconst)
35
36(defmacro in-package (name)
37  `(%in-package ,(string name)))
38
39(defmacro when (test-form &rest body)
40  (if (cdr body)
41      `(if ,test-form (progn ,@body))
42      `(if ,test-form ,(car body))))
43
44(defmacro unless (test-form &rest body)
45  (if (cdr body)
46      `(if (not ,test-form) (progn ,@body))
47      `(if (not ,test-form) ,(car body))))
48
49(defmacro return (&optional result)
50  `(return-from nil ,result))
51
52(defmacro defconstant (name initial-value &optional docstring)
53  `(progn
54     (record-source-information-for-type ',name :constant)
55     (%defconstant ',name ,initial-value ,docstring)))
56
57(defmacro defparameter (name initial-value &optional docstring)
58  `(progn
59     (record-source-information-for-type ',name :variable)
60     (%defparameter ',name ,initial-value ,docstring)))
61
62(defmacro truly-the (type value)
63  `(the ,type ,value))
64
65(defmacro %car (x)
66  `(car (truly-the cons ,x)))
67
68(defmacro %cdr (x)
69  `(cdr (truly-the cons ,x)))
70
71(defmacro %cadr (x)
72  `(%car (%cdr ,x)))
73
74(defmacro %caddr (x)
75  `(%car (%cdr (%cdr ,x))))
76
77(defmacro prog1 (first-form &rest forms)
78  (let ((result (gensym)))
79    `(let ((,result ,first-form))
80       ,@forms
81       ,result)))
82
83(defmacro prog2 (first-form second-form &rest forms)
84  `(prog1 (progn ,first-form ,second-form) ,@forms))
85
86;; Adapted from SBCL.
87(defmacro push (&environment env item place)
88  (if (and (symbolp place)
89           (eq place (macroexpand place env)))
90      `(setq ,place (cons ,item ,place))
91      (multiple-value-bind (dummies vals newval setter getter)
92        (get-setf-expansion place env)
93        (let ((g (gensym)))
94          `(let* ((,g ,item)
95                  ,@(mapcar #'list dummies vals)
96                  (,(car newval) (cons ,g ,getter)))
97             ,setter)))))
98
99;; Adapted from SBCL.
100(defmacro pushnew (&environment env item place &rest keys)
101  (if (and (symbolp place)
102           (eq place (macroexpand place env)))
103      `(setq ,place (adjoin ,item ,place ,@keys))
104      (multiple-value-bind (dummies vals newval setter getter)
105        (get-setf-expansion place env)
106        (let ((g (gensym)))
107          `(let* ((,g ,item)
108                  ,@(mapcar #'list dummies vals)
109                  (,(car newval) (adjoin ,g ,getter ,@keys)))
110             ,setter)))))
111
112;; Adapted from SBCL.
113(defmacro pop (&environment env place)
114  (if (and (symbolp place)
115           (eq place (macroexpand place env)))
116      `(prog1 (car ,place)
117              (setq ,place (cdr ,place)))
118      (multiple-value-bind (dummies vals newval setter getter)
119        (get-setf-expansion place env)
120        (do* ((d dummies (cdr d))
121              (v vals (cdr v))
122              (let-list nil))
123             ((null d)
124              (push (list (car newval) getter) let-list)
125              `(let* ,(nreverse let-list)
126                 (prog1 (car ,(car newval))
127                        (setq ,(car newval) (cdr ,(car newval)))
128                        ,setter)))
129          (push (list (car d) (car v)) let-list)))))
130
131(defmacro psetq (&environment env &rest args)
132  (do ((l args (cddr l))
133       (forms nil)
134       (bindings nil))
135    ((endp l) (list* 'let* (reverse bindings) (reverse (cons nil forms))))
136    (if (and (symbolp (car l))
137             (eq (car l) (macroexpand-1 (car l) env)))
138        (let ((sym (gensym)))
139          (push (list sym (cadr l)) bindings)
140          (push (list 'setq (car l) sym) forms))
141        (multiple-value-bind
142              (dummies vals newval setter getter)
143            (get-setf-expansion (macroexpand-1 (car l) env) env)
144          (declare (ignore getter))
145          (do ((d dummies (cdr d))
146               (v vals (cdr v)))
147              ((null d))
148            (push (list (car d) (car v)) bindings))
149          (push (list (car newval) (cadr l)) bindings)
150          (push setter forms)))))
151
152(defmacro time (form)
153  `(%time #'(lambda () ,form)))
154
155(defmacro with-open-stream (&rest args)
156  (let ((var (caar args))
157        (stream (cadar args))
158        (forms (cdr args))
159        (abortp (gensym)))
160    `(let ((,var ,stream)
161           (,abortp t))
162       (unwind-protect
163        (multiple-value-prog1
164         (progn ,@forms)
165         (setq ,abortp nil))
166        (when ,var
167          (close ,var :abort ,abortp))))))
168
169(defun ansi-loop (exps)
170  (let ((*warn-on-redefinition* nil))
171    (require 'loop))
172  (fmakunbound 'ansi-loop)
173  `(loop ,@exps))
174
175(defmacro loop (&rest exps)
176  (dolist (exp exps)
177    (when (atom exp)
178      (return-from loop (ansi-loop exps))))
179  (let ((tag (gensym)))
180    `(block nil (tagbody ,tag ,@exps (go ,tag)))))
181
182(defmacro defvar (var &optional (val nil valp) (doc nil docp))
183  `(progn
184     (sys::record-source-information-for-type ',var :variable)
185     (%defvar ',var)
186     ,@(when valp
187         `((unless (boundp ',var)
188             (setq ,var ,val))))
189     ,@(when docp
190         `((%set-documentation ',var 'variable ',doc)))
191     ',var))
192
193(defmacro defconst (name value)
194  `(defconstant ,name
195     (if (boundp ',name)
196         (symbol-value ',name)
197         ,value)))
Note: See TracBrowser for help on using the repository browser.