What's important though is that the expressions received by the compiler contain a mixture of user- and macro-written code. For example, given the following hygienic OR macro:
(define-syntax or (form1 form2)The code
`(let ((tmp ,form1))
(if tmp
tmp
,form2)))
(or foo bar)is translated to:
(let ((tmp foo))The blue parts are user-written, the white parts are macro-written.
(if tmp
tmp
bar))
The important thing is that parts with different colors do not interact.
So even if we use the variable name TMP, the code still works as expected because a white TMP doesn't interact with a blue TMP.
(or foo tmp)is translated to:
(let ((tmp foo))Without the colors, the code would always return the value of FOO.
(if tmp
tmp
tmp))
This is one part of the equation: the macro-written white TMP doesn't shadow the user-written blue TMP.
The other part is that user-written code mustn't shadow macro-written variables. If you have a (stupid) macro:
(define-syntax my-list (&rest args)and a user uses it thusly:
`(list ,@args))
(let ((list 12))This expands to:
(my-list 1 2 3))
(let ((list 12))Again, this causes no problem, because different colors don't interact.
(list 1 2 3))
Everytime you create a piece of code with quasiquote, the system generates a new unique color.
3 comments:
There's now a part 2.
"user-written code mustn't shadow macro-written variables"
If I understand correctly, the word
"mustn't" must be written from the
impementor's point of view, right?
For the programmer, it's "will not",
a guarantee, right?
Correct.
Post a Comment