Home > Archive > Scheme > June 2004 > SYNTAX-RULES primer for the merely eccentric
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
SYNTAX-RULES primer for the merely eccentric
|
|
| Joe Marshall 2004-05-24, 2:32 pm |
|
I decided to really learn how to program SYNTAX-RULES macros. It was
easy to find both simple examples and incredibly complex examples of
SYNTAX-RULES, but not so easy to find intermediate examples. I also
discovered that `style guides' are underrepresented. So I wrote one.
I tried to keep track of the different pitfalls I encountered and the
ways I found to get around them, and I tried to present it in a manner
that goes from the very simple macros to seriously complex ones that
require multiple helper functions. I am posting it in the hope that
some may find it useful.
In trying to understand SYNTAX-RULES macros, I stumbled across a new
way of reasoning about macro transformations that I call `Stack-machine
Style'. For me, at least, it greatly simplified the process of
writing complex macros. From this I was able to develop a way to
write composable macro `subroutines' that do not need to be called in
continuation-passing-style. As an example, this form:
(macro-call ()
(this is a (! (macro-cdr (a b (! (macro-null? ())) c d))) test))
expands to (assuming no use of pattern variables)
(this is a (b #t c d) test)
This would be used in a template. The action is to fill in the
pattern variables and then expand the macros that are preceeded with
the !. So the (! (macro-null? ())) subform expands to #t, and this
result is inserted into the list (a b <> c d) to form (a b #t c d). The
call to macro-cdr now takes place on the resultant list to form
(b #t c d) which is inserted into (this is a (b #t c d) test).
At the end of the tutorial I have the obligatory hack: a tiny scheme
interpreter written as a macro. (It is probably the slowest
interpreter I've ever seen, though. Not at all practical.)
I have put this at
http://home.comcast.net/~prunesquallor/macro.txt
It is still in the editing phase, but comments are welcome.
| |
|
|
|
|
| Joe Marshall 2004-06-03, 7:27 pm |
| michele.simionato@poste.it (Michele Simionato) writes:
> Joe Marshall <jrm@ccs.neu.edu> wrote in message news:<3c5pemhk.fsf@ccs.neu.edu>...
>
> I have converted it (semiautomatically, so they may be errors) into
> pdf (http://www.phyast.pitt.edu/~micheles/syntax-rules.pdf)
> for the sake of people who like good old fashioned printed paper.
Thanks!
|
|
|
|
|