From 6a4e8032b7c585c56563cea16e84afb4ed67f578 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Tue, 2 Feb 2016 22:43:24 -0500 Subject: [PATCH] Only do macro env lookup once --- macro-test.scm | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/macro-test.scm b/macro-test.scm index 27d1fa23..38a0c7fc 100644 --- a/macro-test.scm +++ b/macro-test.scm @@ -52,23 +52,27 @@ result (append begin-exprs (cdr exp)) env))) - ;; Expand macro here so we can catch begins in the expanded code, - ;; including nested begins - ((and (app? (car exp)) - (symbol? (caar exp)) - (tagged-list? 'macro (env:lookup (caar exp) env #f))) - (let ((expanded (macro:expand (car exp) (env:lookup (caar exp) env #f) env))) - (expand-body - result - (cons expanded (cdr exp)) - env))) (else - (expand-body - (cons - (inner-expand (car exp) env) - result) - (cdr exp) - env)))) + (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, + ;; including nested begins + (let ((expanded (macro:expand (car exp) macro env))) + ;; Call with expanded macro in case we need to expand again + (expand-body + result + (cons expanded (cdr exp)) + env)) + ;; No macro, use main expand function to process + (expand-body + (cons + (inner-expand (car exp) env) + result) + (cdr exp) + env)))))) (define (my-expand exp env) (inner-expand exp env))