Only do macro env lookup once

This commit is contained in:
Justin Ethier 2016-02-02 22:43:24 -05:00
parent 0542b57cfd
commit 6a4e8032b7

View file

@ -52,23 +52,27 @@
result result
(append begin-exprs (cdr exp)) (append begin-exprs (cdr exp))
env))) env)))
(else
(let ((macro #f))
(when (and (app? (car exp))
(symbol? (caar exp)))
(set! macro (env:lookup (caar exp) env #f)))
(if (tagged-list? 'macro macro)
;; Expand macro here so we can catch begins in the expanded code, ;; Expand macro here so we can catch begins in the expanded code,
;; including nested begins ;; including nested begins
((and (app? (car exp)) (let ((expanded (macro:expand (car exp) macro env)))
(symbol? (caar exp)) ;; Call with expanded macro in case we need to expand again
(tagged-list? 'macro (env:lookup (caar exp) env #f)))
(let ((expanded (macro:expand (car exp) (env:lookup (caar exp) env #f) env)))
(expand-body (expand-body
result result
(cons expanded (cdr exp)) (cons expanded (cdr exp))
env))) env))
(else ;; No macro, use main expand function to process
(expand-body (expand-body
(cons (cons
(inner-expand (car exp) env) (inner-expand (car exp) env)
result) result)
(cdr exp) (cdr exp)
env)))) env))))))
(define (my-expand exp env) (define (my-expand exp env)
(inner-expand exp env)) (inner-expand exp env))