Macro by Example
================
`(require 'macro-by-example)'
A vanilla implementation of `Macro by Example' (Eugene Kohlbecker,
R4RS) by Dorai Sitaram, (dorai @ cs.rice.edu) using `defmacro'.
* generating hygienic global `define-syntax' Macro-by-Example macros
*cheaply*.
* can define macros which use `...'.
* needn't worry about a lexical variable in a macro definition
clashing with a variable from the macro use context
* don't suffer the overhead of redefining the repl if `defmacro'
natively supported (most implementations)
Caveat
------
These macros are not referentially transparent (*note Macros:
(r4rs)Macros.). Lexically scoped macros (i.e., `let-syntax' and
`letrec-syntax') are not supported. In any case, the problem of
referential transparency gains poignancy only when `let-syntax' and
`letrec-syntax' are used. So you will not be courting large-scale
disaster unless you're using system-function names as local variables
with unintuitive bindings that the macro can't use. However, if you
must have the full `r4rs' macro functionality, look to the more
featureful (but also more expensive) versions of syntax-rules available
in slib Note:Macros That Work, Note:Syntactic Closures, and Note:Syntax-Case Macros.
- Macro: define-syntax keyword transformer-spec
The KEYWORD is an identifier, and the TRANSFORMER-SPEC should be
an instance of `syntax-rules'.
The top-level syntactic environment is extended by binding the
KEYWORD to the specified transformer.
(define-syntax let*
(syntax-rules ()
((let* () body1 body2 ...)
(let () body1 body2 ...))
((let* ((name1 val1) (name2 val2) ...)
body1 body2 ...)
(let ((name1 val1))
(let* (( name2 val2) ...)
body1 body2 ...)))))
- Macro: syntax-rules literals syntax-rule ...
LITERALS is a list of identifiers, and each SYNTAX-RULE should be
of the form
`(PATTERN TEMPLATE)'
where the PATTERN and TEMPLATE are as in the grammar above.
An instance of `syntax-rules' produces a new macro transformer by
specifying a sequence of hygienic rewrite rules. A use of a macro
whose keyword is associated with a transformer specified by
`syntax-rules' is matched against the patterns contained in the
SYNTAX-RULEs, beginning with the leftmost SYNTAX-RULE. When a
match is found, the macro use is trancribed hygienically according
to the template.
Each pattern begins with the keyword for the macro. This keyword
is not involved in the matching and is not considered a pattern
variable or literal identifier.