1 | ;;; macros.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2003-2007 Peter Graves |
---|
4 | ;;; $Id: macros.lisp 14447 2013-03-24 19:15:26Z 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., 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 | `(%defconstant ',name ,initial-value ,docstring)) |
---|
54 | |
---|
55 | (defmacro defparameter (name initial-value &optional docstring) |
---|
56 | `(%defparameter ',name ,initial-value ,docstring)) |
---|
57 | |
---|
58 | (defmacro truly-the (type value) |
---|
59 | `(the ,type ,value)) |
---|
60 | |
---|
61 | (defmacro %car (x) |
---|
62 | `(car (truly-the cons ,x))) |
---|
63 | |
---|
64 | (defmacro %cdr (x) |
---|
65 | `(cdr (truly-the cons ,x))) |
---|
66 | |
---|
67 | (defmacro %cadr (x) |
---|
68 | `(%car (%cdr ,x))) |
---|
69 | |
---|
70 | (defmacro %caddr (x) |
---|
71 | `(%car (%cdr (%cdr ,x)))) |
---|
72 | |
---|
73 | (defmacro prog1 (first-form &rest forms) |
---|
74 | (let ((result (gensym))) |
---|
75 | `(let ((,result ,first-form)) |
---|
76 | ,@forms |
---|
77 | ,result))) |
---|
78 | |
---|
79 | (defmacro prog2 (first-form second-form &rest forms) |
---|
80 | `(prog1 (progn ,first-form ,second-form) ,@forms)) |
---|
81 | |
---|
82 | ;; Adapted from SBCL. |
---|
83 | (defmacro push (&environment env item place) |
---|
84 | (if (and (symbolp place) |
---|
85 | (eq place (macroexpand place env))) |
---|
86 | `(setq ,place (cons ,item ,place)) |
---|
87 | (multiple-value-bind (dummies vals newval setter getter) |
---|
88 | (get-setf-expansion place env) |
---|
89 | (let ((g (gensym))) |
---|
90 | `(let* ((,g ,item) |
---|
91 | ,@(mapcar #'list dummies vals) |
---|
92 | (,(car newval) (cons ,g ,getter))) |
---|
93 | ,setter))))) |
---|
94 | |
---|
95 | ;; Adapted from SBCL. |
---|
96 | (defmacro pushnew (&environment env item place &rest keys) |
---|
97 | (if (and (symbolp place) |
---|
98 | (eq place (macroexpand place env))) |
---|
99 | `(setq ,place (adjoin ,item ,place ,@keys)) |
---|
100 | (multiple-value-bind (dummies vals newval setter getter) |
---|
101 | (get-setf-expansion place env) |
---|
102 | (let ((g (gensym))) |
---|
103 | `(let* ((,g ,item) |
---|
104 | ,@(mapcar #'list dummies vals) |
---|
105 | (,(car newval) (adjoin ,g ,getter ,@keys))) |
---|
106 | ,setter))))) |
---|
107 | |
---|
108 | ;; Adapted from SBCL. |
---|
109 | (defmacro pop (&environment env place) |
---|
110 | (if (and (symbolp place) |
---|
111 | (eq place (macroexpand place env))) |
---|
112 | `(prog1 (car ,place) |
---|
113 | (setq ,place (cdr ,place))) |
---|
114 | (multiple-value-bind (dummies vals newval setter getter) |
---|
115 | (get-setf-expansion place env) |
---|
116 | (do* ((d dummies (cdr d)) |
---|
117 | (v vals (cdr v)) |
---|
118 | (let-list nil)) |
---|
119 | ((null d) |
---|
120 | (push (list (car newval) getter) let-list) |
---|
121 | `(let* ,(nreverse let-list) |
---|
122 | (prog1 (car ,(car newval)) |
---|
123 | (setq ,(car newval) (cdr ,(car newval))) |
---|
124 | ,setter))) |
---|
125 | (push (list (car d) (car v)) let-list))))) |
---|
126 | |
---|
127 | (defmacro psetq (&environment env &rest args) |
---|
128 | (do ((l args (cddr l)) |
---|
129 | (forms nil) |
---|
130 | (bindings nil)) |
---|
131 | ((endp l) (list* 'let* (reverse bindings) (reverse (cons nil forms)))) |
---|
132 | (if (and (symbolp (car l)) |
---|
133 | (eq (car l) (macroexpand-1 (car l) env))) |
---|
134 | (let ((sym (gensym))) |
---|
135 | (push (list sym (cadr l)) bindings) |
---|
136 | (push (list 'setq (car l) sym) forms)) |
---|
137 | (multiple-value-bind |
---|
138 | (dummies vals newval setter getter) |
---|
139 | (get-setf-expansion (macroexpand-1 (car l) env) env) |
---|
140 | (declare (ignore getter)) |
---|
141 | (do ((d dummies (cdr d)) |
---|
142 | (v vals (cdr v))) |
---|
143 | ((null d)) |
---|
144 | (push (list (car d) (car v)) bindings)) |
---|
145 | (push (list (car newval) (cadr l)) bindings) |
---|
146 | (push setter forms))))) |
---|
147 | |
---|
148 | (defmacro time (form) |
---|
149 | `(%time #'(lambda () ,form))) |
---|
150 | |
---|
151 | (defmacro with-open-stream (&rest args) |
---|
152 | (let ((var (caar args)) |
---|
153 | (stream (cadar args)) |
---|
154 | (forms (cdr args)) |
---|
155 | (abortp (gensym))) |
---|
156 | `(let ((,var ,stream) |
---|
157 | (,abortp t)) |
---|
158 | (unwind-protect |
---|
159 | (multiple-value-prog1 |
---|
160 | (progn ,@forms) |
---|
161 | (setq ,abortp nil)) |
---|
162 | (when ,var |
---|
163 | (close ,var :abort ,abortp)))))) |
---|
164 | |
---|
165 | (defun ansi-loop (exps) |
---|
166 | (let ((*warn-on-redefinition* nil)) |
---|
167 | (require 'loop)) |
---|
168 | (fmakunbound 'ansi-loop) |
---|
169 | `(loop ,@exps)) |
---|
170 | |
---|
171 | (defmacro loop (&rest exps) |
---|
172 | (dolist (exp exps) |
---|
173 | (when (atom exp) |
---|
174 | (return-from loop (ansi-loop exps)))) |
---|
175 | (let ((tag (gensym))) |
---|
176 | `(block nil (tagbody ,tag ,@exps (go ,tag))))) |
---|
177 | |
---|
178 | (defmacro defvar (var &optional (val nil valp) (doc nil docp)) |
---|
179 | `(progn |
---|
180 | (%defvar ',var) |
---|
181 | ,@(when valp |
---|
182 | `((unless (boundp ',var) |
---|
183 | (setq ,var ,val)))) |
---|
184 | ,@(when docp |
---|
185 | `((%set-documentation ',var 'variable ',doc))) |
---|
186 | ',var)) |
---|
187 | |
---|
188 | (defmacro defconst (name value) |
---|
189 | `(defconstant ,name |
---|
190 | (if (boundp ',name) |
---|
191 | (symbol-value ',name) |
---|
192 | ,value))) |
---|