[racket] macros whose meaning is dependent on context?
On Oct 16, 2012, at 3:25 PM, Jon Rafkind wrote:
> 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
Okay, this makes sense; I'm just deeply freaked out (temporarily, I presume) that the syntax-parameterize occurs in the phase 0 code, and not the phase 1 code; rather than doing the transformation that I want to do during the evaluation of the macro, I'm embedding it in another macro, to be evaluated later.
Sure! I'm okay with that. Um… I think.
Thanks,
John
>
> 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
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4800 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20121016/54fba567/attachment.p7s>