| 1 | ;;; compiler-macro.lisp |
|---|
| 2 | ;;; |
|---|
| 3 | ;;; Copyright (C) 2003-2007 Peter Graves |
|---|
| 4 | ;;; $Id: compiler-macro.lisp 13597 2011-09-17 20:46:45Z 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 'compiler-macroexpand) |
|---|
| 35 | |
|---|
| 36 | (defvar *compiler-macros* (make-hash-table :test #'equal)) |
|---|
| 37 | |
|---|
| 38 | (defun compiler-macro-function (name &optional environment) |
|---|
| 39 | (declare (ignore environment)) |
|---|
| 40 | (gethash1 name (the hash-table *compiler-macros*))) |
|---|
| 41 | |
|---|
| 42 | (defun (setf compiler-macro-function) (new-function name &optional environment) |
|---|
| 43 | (declare (ignore environment)) |
|---|
| 44 | (setf (gethash name (the hash-table *compiler-macros*)) new-function)) |
|---|
| 45 | |
|---|
| 46 | (defmacro define-compiler-macro (name lambda-list &rest body) |
|---|
| 47 | (let* ((form (gensym)) |
|---|
| 48 | (env (gensym)) |
|---|
| 49 | (block-name (fdefinition-block-name name))) |
|---|
| 50 | (multiple-value-bind (body decls) |
|---|
| 51 | (parse-defmacro lambda-list form body name 'defmacro :environment env |
|---|
| 52 | ;; when we encounter an error |
|---|
| 53 | ;; parsing the arguments in the call |
|---|
| 54 | ;; (not in the difinition!), return |
|---|
| 55 | ;; the arguments unmodified -- ie skip the |
|---|
| 56 | ;; transform (see also source-transform.lisp) |
|---|
| 57 | :error-fun `(lambda (&rest ignored) |
|---|
| 58 | (declare (ignore ignored)) |
|---|
| 59 | (return-from ,block-name ,form))) |
|---|
| 60 | (let ((expander `(lambda (,form ,env) |
|---|
| 61 | (declare (ignorable ,env)) |
|---|
| 62 | (block ,block-name ,body)))) |
|---|
| 63 | `(progn |
|---|
| 64 | (setf (compiler-macro-function ',name) (function ,expander)) |
|---|
| 65 | ',name))))) |
|---|
| 66 | |
|---|
| 67 | ;;; Adapted from OpenMCL. |
|---|
| 68 | (defun compiler-macroexpand-1 (form &optional env) |
|---|
| 69 | (let ((expander nil) |
|---|
| 70 | (new-form nil)) |
|---|
| 71 | (if (and (consp form) |
|---|
| 72 | (symbolp (%car form)) |
|---|
| 73 | (setq expander (compiler-macro-function (%car form) env))) |
|---|
| 74 | (values (setq new-form (funcall expander form env)) |
|---|
| 75 | (neq new-form form)) |
|---|
| 76 | (values form |
|---|
| 77 | nil)))) |
|---|
| 78 | |
|---|
| 79 | (defun compiler-macroexpand (form &optional env) |
|---|
| 80 | (let ((expanded-p nil)) |
|---|
| 81 | (loop |
|---|
| 82 | (multiple-value-bind (expansion exp-p) |
|---|
| 83 | (compiler-macroexpand-1 form env) |
|---|
| 84 | (if exp-p |
|---|
| 85 | (setf form expansion |
|---|
| 86 | expanded-p t) |
|---|
| 87 | (return)))) |
|---|
| 88 | (values form expanded-p))) |
|---|
| 89 | |
|---|