What you're trying to do is capture some amount of the invocation context of
the macro, which is not what macros are meant for. You're better of making (bar
e) a macro for (+ 3 e). Unfortunately bar is not part of the input, so you'll
need to make it a syntax parameter, so it has well-behaved scope.
(require racket/stxparam)
(define-syntax-parameter bar (lambda (stx) (raise-syntax-error #f "Only for use
in define-foo" stx)))
(define-syntax (define-foo stx)
(syntax-case stx ()
[(_ (name . params) . body)
#'(define (name . params)
(syntax-parameterize ([bar (syntax-rules () [(_ e) (+ 3 e)])])
. body))]))
Unless you have separate macro invocations setting up some compile-time context
(see Macros That Work Together, and Keeping it Clean with Syntax Parameters)
for later use, you should focus on making the macro's meaning only depend on
its input.
-Ian
----- Original Message -----
From: "Chad Albers" <[email protected]>
To: [email protected]
Sent: Tuesday, August 27, 2013 4:33:17 AM GMT -05:00 US/Canada Eastern
Subject: [racket] Macro Assistance
I'm still trying to grok macros - which are amazing, but hard. I'd like to know
if it is possible capture part of the following expression
(display (+ 2 (bar (+ 1 2))))
if "bar" is a literal in the macro, I would like to capture body0 as (+ 1 2)
and body1 as (display (+ 2 ...)).
This is a totally artificial example.
Here's what I have:
(define-syntax (define-foo stx)
(syntax-case stx (bar)
([_ (name param) (body1 (bar . body0))]
(syntax (define (name param)
(body1 (+ 3 . body0)))))))
(define-foo (foof env)
(display (bar (+ 1 2))))
This works...and expands to.
(define (foof env)
(display (+ 3 (- 1 (+ 1 2)))))
(Note that -1 has been substitute for the "bar" literal").
This expression fails with "bad syntax"
(define-foo (foog env)
(display (+3 (bar (- 1 (+ 1 2))))))
I don't seem be to able to capture (display (+3 )) as separate from (bar (-1 (+
1 2)))
Is this possible to capture?
Thanks for any help and insights into macros
Chad
____________________
Racket Users list:
http://lists.racket-lang.org/users
____________________
Racket Users list:
http://lists.racket-lang.org/users