[Small-cl-src] macroexpand-most

Lars Brinkhoff lars at nocrew.org
Thu Jun 10 09:29:25 UTC 2004


;;; A function that expands most macros in a form and its subforms.
;;; It doesn't expand symbol macros or macros implemented as special
;;; operators.

;;; Exercises:
;;; 1. (easy) What's the problem with symbol macros?
;;; 2. (hard) Add support for symbol macros.

(defvar *substitutions*)

(defun make-hook (old-hook)
  (lambda (fn form env)
    (let ((expansion (funcall old-hook fn form env)))
      (unless (symbolp form)
	(push (cons expansion form) *substitutions*))
      expansion)))

(defun macroexpand-most (form)
  (let ((*substitutions* nil)
	(*error-output* (make-broadcast-stream))
	(*macroexpand-hook* (make-hook *macroexpand-hook*)))
    (setq form (copy-tree form))
    (compile nil `(lambda () ,form))
    (dolist (s (nreverse *substitutions*) form)
      (setq form (nsubst (car s) (cdr s) form :test #'eq)))))

(eval-when (:execute)
  (mapc #'compile '(make-hook macroexpand-most)))





More information about the Small-cl-src mailing list