Rationale
This module implements only one macro, with-prefix, and one procedure, a variant of gensym with memory, which is needed for testing. The former does the renaming in explicit-renaming-macros automatically.
Writing explicit-renaming macros, you either have to replace each symbol, sym, you want renamed in a quasiquote template with ,(rename sym) -- which makes the code hard to read -- or you have to wrap the whole template into a -- possibly big -- let of the form
(let ((%sym (rename 'sym)) ...) `(... ,%sym ...))
This is much easier to read, but in writing the macro, you have always to add a new definition to the let, whenever you add a new symbol to be renamed in the template.
The current module automizes this: instead of the let, you only wrap the quasiquote-template in a (with-prefix (% rename) ...), the macro then selects the prefixed names from the template, removes duplicates and generates the corresponding let. You, as the writer, needn't bother, if you already defined the renamed symbol or not, the macro does it for you. The resulting macro is automatically hygienic, if you prefix each symbol, which is not a macro argument, accordingly. The necessary let defining the prefixed symbols as renamed ones than replaces with-prefix.
An example will make this more clear:
(with-prefix (% gensym*) `(,%let ((,%a "a") (,%b 'b)) (,%begin (,%print ,%a) (,%print ,%b))))
rewrites to
(let ((%let (gensym* 'let)) (%a (gensym* 'a)) (%b (gensym* 'b)) (%begin (gensym* 'begin)) (%print (gensym* 'print))) `(,%let ((,%a "a") (,%b 'b)) (,%begin (,%print ,%a) (,%print ,%b))))
The prefix in this example is %, but you can choose whatever you like. I've locally defined the routines, which do the processing of the symbols to rename, so the macro is easy to port. Notice, that this routines run all at compile-time.
To be able to test this replacement, you need gensym* instead of gensym.
API
with-prefix
[syntax] (with-prefix (prefix mapper) symtree)
extracts those symbols from symtree, which are prefixed with prefix, and prepends symtree with bindings of the prefixed syms to prefix stripped versions mapped with mapper, e.g. gensym, rename, inject.
gensym*
[procedure] (gensym* sym)
a variant of gensym with memory, i.e. it will always produce the same gensym for its only argument.
prefixes
[procedure] (prefixes)
[procedure] (prefixes sym)
with sym: documentation of exported symbol without sym: list of exported symbols
Examples
(import prefixes) (with-prefix (% gensym*) `(1 ,%a (,%b ,%a) ,%c d)) ;-> (let ((%a (gensym* 'a)) (%b (gensym* 'b)) (%c (gensym* 'c))) `(1 ,%a (,%b ,%a) ,%c d)) (with-prefix (% gensym*) `(,%let ((,%a "a") (,%b 'b)) (,%begin (,%print ,%a) (,%print ,%b)))) ;-> (let ((%let (gensym* let)) (%a (gensym* 'a)) (%b (gensym* 'b)) (%begin (gensym* 'begin)) (%print (gensym* 'print))) `(,%let ((,%a "a") (,%b 'b)) (,%begin (,%print ,%a) (,%print ,%b))))
Requirements
none
Last update
Dec 22, 2024
Author
Juergen Lorenz
License
Copyright (c) 2022-2024 , Juergen Lorenz, ju (at) jugilo (dot) de All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Version history
- 2.0
- port to chicken-6, remove dependencies, new test suite
- 1.0
- initial check in