71 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
			
		
		
	
	
			71 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
| To: alan@lcs.mit.edu
 | |
| Subject: scoping macros
 | |
| Reply-to: shivers@ai.mit.edu
 | |
| --text follows this line--
 | |
| So, I'd like to write a macro that introduces a lexical contour.
 | |
| Here's my toy problem:
 | |
| 
 | |
| (color)	- macro form that produces the current lexical color.
 | |
| 
 | |
| (with-green body ...)
 | |
| (with-blue  body ...)
 | |
|     Evaluate BODY forms in a lexical color contour that is green/blue,
 | |
|     respectively.
 | |
| 
 | |
| The default, top-level color is green.
 | |
| 
 | |
| This doesn't work:
 | |
| 
 | |
| ---
 | |
| (define-syntax color (syntax-rules () ((color) 'green)))
 | |
| 
 | |
| (define-syntax with-blue
 | |
|   (syntax-rules (color)
 | |
|     ((with-blue body ...)
 | |
|      (let-syntax ((color (syntax-rules () ((color) 'blue))))
 | |
|        body ...))))
 | |
| 
 | |
| (define-syntax with-green
 | |
|   (syntax-rules (color)
 | |
|     ((with-blue body ...)
 | |
|      (let-syntax ((color (syntax-rules () ((color) 'green))))
 | |
|        body ...))))
 | |
| ---
 | |
| 
 | |
| Everything comes out green.  Removing COLOR from the syntax-rules keyword list
 | |
| doesn't fix it.
 | |
| 
 | |
| This *does* work:
 | |
| ---
 | |
| (define-syntax with-blue
 | |
|   (syntax-rules (color)
 | |
|     ((with-blue body ...)
 | |
|      (let-syntax ((color (syntax-rules (color) ((color) 'blue))))
 | |
|        body ...))))
 | |
| 
 | |
| (with-blue (color))
 | |
| 'green
 | |
| 
 | |
| (define-syntax with-blue
 | |
|   (lambda (exp r c)
 | |
|     `(,(r 'let-syntax) ((color (,(r 'syntax-rules) ()
 | |
|                                  ((color) 'blue))))
 | |
|        . ,(cdr exp))))
 | |
| 
 | |
| > (with-blue (color))
 | |
| 'blue
 | |
| 
 | |
| > (list (color) (with-blue (color)))
 | |
| '(green blue)
 | |
| 
 | |
| > (define-syntax with-green
 | |
|     (lambda (exp r c)
 | |
|       `(,(r 'let-syntax) ((color (,(r 'syntax-rules) ()
 | |
|                                    ((color) 'green))))
 | |
|          . ,(cdr exp))))
 | |
| 
 | |
| > (cons (color) (with-blue (list (color) (with-green (color)))))
 | |
| '(green blue green)
 | |
| ---
 | |
| 
 |