[gambit-list] Syntax-case

Christian Jaeger christian at pflanze.mine.nu
Tue Feb 12 12:18:01 EST 2008


Joel J. Adamson wrote:
> Christian Jaeger <christian at pflanze.mine.nu> writes:
> 
>> Joel J. Adamson wrote:
>>> ... syntax-rules macros
>>>   
>> syntax-rules is incompatible with most Gambit specialities. 
> 
> Interesting:
> * I have had a lot of trouble understanding the pattern language anyway,
> and I have a hard time seeing what the advantages of define-syntax over
> define-macro anyway.  Any thoughts?

The disadvantages of define-macro:

* with define-macro you have to care about hygiene yourself (e.g. you
have to use (gensym) to introduce new identifyers into the transformed
code, and you have to fully qualify symbols that you want to refer to a
particular package instead of to the identifyers in the package of the
user of the macro)

* with define-macro you're working with sexpr's that have lost their
source location information; as a result, when an error happens in code
which has been built by a macro, the Gambit debugger will just points at
the start of the region with the generated code; with macros spanning
big code parts, this makes tracking errors hard. syntax-{case,rules} is
said to keep source location information automatically.

Gambit has an undocumented define-macro variant which gives you the
sexpr's with location information; thus with some care, you can still
implement macros that keep location information. (I started implementing
the same in chjmodule before I knew about the gambit lowlevel
define-macro, and actually I can't find the name of the latter right now.)

> * Can you elaborate on Gambit's specialties that are incompatible with
> syntax-rules? 

Read the comments at the top of /usr/local/Gambit-C/current/syntax-case.scm

> 
> I don't have my heart set on using syntax-rules; I was under the
> impression that there is some huge advantage of it since in other
> implementations (MzScheme especially) it is used A LOT.
> 
> However, last night after figuring out the namespaces thing, I wrote a
> syntax-rules macro that looked exactly like I could have written it in
> define-macro language instead.  Also after doing a quick find-grep of
> the examples and finding no instances of syntax-rules, and many examples
> of define-macro, I think I'm alright with using define-macro.
> 
>> whole-sourcefile transformers
> 
> Can you tell me more about this technique?

See the bottom of the file /usr/local/Gambit-C/current/syntax-case.scm,
namely the ##expand-source and c#expand-source hooks.

e.g.

> (set! ##expand-source (lambda (v) (pp v) v))
> (define a "hello")
#(#(source1)
   (#(#(source1) define (console) 65537)
    #(#(source1) a (console) 524289)
    #(#(source1) "hello" (console) 655361))
   (console)
   1)
> a
#(#(source1) a (console) 2)
"hello"

I've written a library of functions for dealing with the source location
information which is wrapped around the data (it is to be released
sooner than RSN™ with the rest of my modules and the chjmodule system).

Christian.





More information about the Gambit-list mailing list