Consider the following macro definition in R7RS scheme:
(define-syntax foo
(syntax-rules ()
((_ bar)
(begin
(define baz 42)
(define-syntax bar
(syntax-rules ()
((_) baz)))))))
I have loaded this file into the repl of chibi-scheme and entered:
> (foo bar)
> (bar)
Instead of the expected output 42, I got:
ERROR: undefined variable: baz
Why is this so and how can I pass the defined value of baz in the outer macro to the inner macro?
This is an error in chibi-scheme. A macro definition needs to capture its environment; for
barthe environment consists ofbaritself andbaz. Then when you expandbarin another environment, the macro expansion needs to recognize thatbazis bound in the env-of-definition. chibi-scheme apparently doesn't recognize thatbazis actually defined.Additionally, another related problem, which you haven't seen in your post, is that even if the expansion of
barrecognizesbazas bound, the loading/running of the code needs to find the value ofbaz.Here is R6RS Ikarus Scheme: