[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Expanding macros

It was pointed out to me that my recent T-DISCUSSION message used the
procedure FULLY-EXPAND-MACRO without saying what it is.  I've given its
definition below.  It's a brute-force definition without alot of hackery,
but it will only really be used at top-level anyway.

(lset user-syntax-table (env-syntax-table user-env))

(define (fully-expand-macro orig-exp . syntax-table)
  (let ((syn-tab (cond (syntax-table => car) (else user-syntax-table))) )
    (iterate zowie ((prev-exp orig-exp))
      (let ((exp (macro-expand prev-exp syn-tab)))
	(cond ((neq? exp prev-exp)
	       (zowie exp))
	      ((not (pair? exp))
	      ((or (eq? (car exp) 'lambda) 
		   (eq? (car exp) (syntax-table-entry syn-tab 'lambda)))
	       (cons* (car exp) (cadr exp) 
		      (map (lambda (x) (fully-expand-macro x syn-tab))
			   (cddr exp))))
	      ((or (eq? (car exp) 'labels) 
		   (eq? (car exp) (syntax-table-entry syn-tab 'labels)))
	       (list (car exp)
		     (map (lambda (spec) 
			    (list (car spec)
				  (fully-expand-macro (cadr spec) syn-tab)))
			  (cadr exp))
		     (fully-expand-macro (caddr exp) syn-tab)))
	      ((pair? exp)
	       (let ((mapped-exp (map (lambda (x) 
					(fully-expand-macro x syn-tab))
		 (if (equal? mapped-exp exp)
		     (zowie mapped-exp))))
	      (else exp)
	      )  ))))


Bruce Krulwich