; Copyright (c) 2022-2023 , 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. #|[ This module implements only one macro, with-prefix, and ony procedure, a variant of gensym with memory. The former does the renaming in explicit-renaming-macros automatically. You need only prefix each symbol you want to be renamed of the macro-code and wrap the whole shebang with that macro. The necessary let defining the prefixed symbols as renamed ones than replaces with-prefix. An example will make this more clear: (with-prefix (% gensym) `(1 ,%a (,%b ,%a) ,%c d)) rewrites to (let ((%a (gensym 'a)) (%b (gensym 'b)) (%c (gensym 'c))) `(1 ,%a (,%b ,%a) ,%c d)) To be able to test this replacement, you need gensym* instead of gensym. ]|# (module prefixes ( with-prefix gensym* prefixes ) (import scheme (only (chicken base) case-lambda print gensym) (only (chicken plist) get put!) ) (import-for-syntax (only tree-walkers tree->pathes filter dups-remove)) #|[ (with-prefix (prefix mapper) symtree) --- macro --- 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. ]|# (define-syntax with-prefix (er-macro-transformer (lambda (form rename compare?) (let ((pair (cadr form)) (tree (caddr form))) (let ((prefix (car pair)) (mapper (cadr pair)) (%let (rename 'let))) (letrec ( (prefixed? (lambda (prefix sym) (and (symbol? sym) (let ((sfix (symbol->string prefix)) (ssym (symbol->string sym))) (let ((fixlen (string-length sfix)) (symlen (string-length ssym))) (and (< fixlen symlen) (string=? sfix (substring ssym 0 fixlen)))))))) (strip-prefix (lambda (prefix sym) (and (prefixed? prefix sym) (string->symbol (substring (symbol->string sym) (string-length (symbol->string prefix))))))) ) (let ((syms (filter (lambda (sym) (prefixed? prefix sym)) (dups-remove eq? ;;; tree->pathes not available ;;; at compile time !!!! (map car (tree->pathes tree)))))) `(,%let ,(map (lambda (var) `(,var (,mapper ',(strip-prefix prefix var)))) syms) ,tree)))))))) #|[ (gensym* sym) --- procedure --- a variant of gensym with memory, i.e. it will always produce the same gensym for its only argument. ]|# (define (gensym* sym) (or (get sym 'gensym) (put! sym 'gensym (gensym sym)))) #|[ (prefixes) (prefixes sym) --- procedure --- documentation procedure ]|# (define prefixes (let ( (alist '( (with-prefix macro: (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) (prefixes sym) "with sym: documentation of exported symbol" "without sym: list of exported symbols" ) )) ) (case-lambda (() (map car alist)) ((sym) (let ((pair (assq sym alist))) (if pair (for-each print (cdr pair)) (print "Choose one of " (map car alist)))))))) )