Re: Trying to understand the "rx" macro.
"Brian D. Carlstrom" <[email protected]> Sun, 02 Nov 2008 12:21:49 -0800
| Newsgroups | gmane.lisp.scheme.scsh |
|---|---|
| Message-ID | <ley701op42.wl%[email protected]> |
Looking at:
http://en.wikipedia.org/wiki/Hygienic_macros
this macro mechanism is referred to as "explicit renaming"
I've worked on porting macros to and from Scheme 48 based systems
include scsh I've run into this mechanism before.
basically a function such as expand-rx is used to rewrite the synatx.
the function receives three arguments: 1.) the syntax an an s-exp, 2.) a
renaming function, and 3.) a comparing function. so in the expand-rx
case, the first argument as you can see from your debugging is '(| ax).
the second argument is a gensym function you can use to make unique
symbols. the third comparator argument function is less frequently used
as the gensym function, but I believe it is used to compare symbols for
equality taking into account any newly gensymed symbols, etc.
I found the explicit renaming macros in Scheme 48 pretty easy to work
with, but it probably been a decade since I've touched them so my
details are a little fuzzy.
-bri
At Sun, 2 Nov 2008 17:59:22 -0200,
"Igor Hjelmstrom Vinhas Ribeiro" <[email protected]> wrote:
>
> [1 <text/plain; ISO-8859-1 (7bit)>]
> All,
>
> First of all: thanks for Scsh. I used it for over a year now, and it is a
> really nice tool - SRE's, specifically, are wonderful.
>
> Second, I am trying to understand the "rx" macro that can be found
> in SCSH, with the purpose of porting it and the associated
> functionality over to another Scheme system.
>
> I don't understand how its declaration and use
> are working. In packages.scm I found the following line:
>
> (define-syntax rx expand-rx)
>
> Then, expand-rx is declared in re-syntax.scm like this:
>
> (define (expand-rx exp r c)...
>
> How can this be? How can it simply associate define-syntax with
> a procedure like this? Did I miss anything in the source code? In
> R5RS itself? Is this something Scheme48 specific?
>
> Here is a test I did. I changed Scsh source code so that the expand-rx
> definition starts like this:
>
> (define (expand-rx exp r c)
> (display "\nexpand-rx: p1=")(display exp)(display "\n")
> (display "\nexpand-rx: p2=")(display r)(display "\n")
> (display "\nexpand-rx: p3=")(display c)(display "\n")
> (let ((re (simplify-regexp (parse-sres (cdr exp) r c))))
> ...
>
> When I run (rx (| "a" "x")) I get the following output:
>
> expand-rx: p1=(#{Name rx} (| a x))
>
> expand-rx: p2=#{Procedure 2610 (unnamed in make-name-generator in names)}
>
> expand-rx: p3=#{Procedure 2607 (unnamed in make-keyword-comparator in
> names)}
> '#{re-char-set}
>
> The two procedures (p2 and p3) - where did they came from? And how
> did my (rx ...) ended up translated to this expand-rx call?
>
> Any help / pointers to assist me in understanding all of this will be
> much appreciated.
>
> Regards,
> Igor.
> [2 <text/html; ISO-8859-1 (7bit)>]
>