Is it possible to write a macro in Scheme that expands differently depending upon whether the macro was used at the top level or internally? I'm primarily interested in a solution that works in Guile Scheme.
Is it possible to write a macro in Scheme that expands differently depending upon whether the macro was used at the top level or internally? I'm primarily interested in a solution that works in Guile Scheme.
If you mean "internal" in the sense of internal definitions, then it might work to have the macro redefine itself in the nested scope.
(define inner-transformer
(syntax-rules ()
[(_ (id a ...) e1 e2 ...)
(define (id a ...)
(format #t "inner func ~a\n" 'id)
(let () e1 e2 ...))]))
(define-syntax func
(lambda (x)
(syntax-case x ()
[(k (id a ...) e1 e2 ...)
#'(define (id a ...)
(let-syntax ([k inner-transformer])
(format #t "top func ~a\n" 'id)
(let () e1 e2 ...)))])))
(func (foo x)
(func (bar y)
(func (baz z)
(+ x y z))
(baz y))
(bar x))
(foo 1)
Guile result:
top func foo
inner func bar
inner func baz
$1 = 3