String-Based Macro Systems
Sujet : String-Based Macro Systems
De : ldo (at) *nospam* nz.invalid (Lawrence D'Oliveiro)
Groupes : comp.lang.miscDate : 13. Apr 2024, 03:29:56
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <uvcqn3$2pju0$1@dont-email.me>
User-Agent : Pan/0.155 (Kherson; fc5a80b8)
I think most of us are familiar with the “#define” preprocessor in C and
C++. There are more powerful macro processors around, like GNU m4. They
all have the same basic concept: pass input text straight through to
output, until something triggers a macro substitution on the text.
The original m4 was created by the Unix folks at Bell Labs, modelled on an
earlier concept called “Macrogenerator” by Christopher Strachey (one of
the brains behind the programming language CPL, which led to BCPL, which
led to B and then C). Macrogenerator had special symbols to indicate macro
definition, and macro and argument expansion:
§DEF,«name»,<«definition»>;
where the “<” and “>” are actual quote symbols in the notation, while I
use “«” and ”»” as metasyntactic brackets. Within the «definition»,
occurrences of “~1”, “~2” etc are replaced with the first, second etc
actual argument specified in the call. You then use this macro as
§«name»,«args»;
where multiple arguments are comma-separated.
Simple example: given
§DEF,greetings,<Hello, ~1!>;
then
I would just like to say, “§greetings,world;” to anybody listening
should expand to
I would just like to say, “Hello, world!” to anybody listening
Here is a moderately interesting example, from the Bryan Higman book where
I first heard about this. It uses a builtin called §UPDATE, which does
assignment to an existing macro name, and also note the occurrence of
§DEFs within §DEFs, for local (temporary) macro definitions (since the
auxiliary macro §Q has to persist between invocations, it cannot be one of
these):
§DEF,Q,A;
§DEF,AORB,<§§Q;;>,§DEF,A,<A§UPDATE,Q,B;>;,§DEF,B,<B§UPDATE,Q,A;>;;
What this does is, each time you write “§AORB;”, it expands to alternately
“A” or “B”.
The big difference with m4 is that it does away with these special
symbols; the mere occurrence of a name matching a defined macro (or an
argument of the macro currently being expanded) is sufficient to trigger
substitution. Do you think this is a good idea?
There are all kinds of pitfalls with such macro systems. The original
Macrogenerator could not cope with substitutions containing unpaired
“< ... >” quote symbols, and even GNU m4 lacks something as simple as a
backslash-style “escape next single character, whatever it is”. While m4
lets you switch the quoting symbols, it still insists that they occur in
pairs.
Would adding such an escape character be useful?
Haut de la page
Les messages affichés proviennent d'usenet.
NewsPortal