;;; 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)))