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