[racket] macros whose meaning is dependent on context?

From: Jon Rafkind (rafkind at cs.utah.edu)
Date: Tue Oct 16 18:25:10 EDT 2012

This is what syntax-parameters are for.

(require racket/stxparam)
(define-syntax-parameter m (lambda (stx) (raise-syntax-error 'm "dont use this outside deeper")))

(define-syntax-rule (deeper e)
   (syntax-parameterize ([m (lambda (stx) #'1)])
      e))

(deeper (m)) -> 1

On 10/16/2012 04:18 PM, John Clements wrote:
> This seems like a straightforward question; apologies if I just haven't dug deep enough to find the answer.
>
> I want to define a macro whose meaning depends on its context. More specifically, I want to define an "outer" macro that gives a particular meaning to the "inner" macro. 
>
> I can see how to get the job done in a yucky way, using mutation:
>
> #lang racket
>
> ;; I want m's meaning to depend on its context
> (define-syntax (m stx)
>   #`#,(unbox the-box))
>
> (define-syntax (deeper stx)
>   (syntax-case stx ()
>     [(_ arg)
>      ;; I want to change the meaning of m, here:
>      (set-box! the-box 16)
>      #'arg]))
>
> ;; yucky mutation-based way to get the job done
> (define-for-syntax the-box (box 13))
>
>
> (deeper (m 134))
>
>
> ... but it seems like there must be a simple way for the outer binding to say "whatever meaning the inner macro wants it to have".
>
> Don't tell me, it's define-for-inner-syntax/local-expand. I just made that up, so I hope it's wrong :).
>
> John
>
>
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121016/6a20f760/attachment-0001.html>

Posted on the users mailing list.