[plt-scheme] define-syntax by itself

From: Blake McBride (blake at mcbride.name)
Date: Sat Sep 9 17:31:27 EDT 2006

At 02:09 PM 9/9/2006, Eli Barzilay wrote:
>On Sep  9, Blake McBride wrote:
> >
> > I probably mean something like procedural macros that allow you to
> > specify exactly what you want without trying to do it in some higher
> > level 4th generational descriptive pattern language.  I believe
> > there is an SRFI about this.
>
>Well then, `define-syntax' does just that.  When you write:
>
>   (define-syntax foo
>     (lambda (stx)
>       ...))
>
>you bind `foo' -- as a syntax -- to a syntax->syntax transformer
>procedure.  How you implement the procedure is up to you --
>`syntax-rules' can construct simple transformers very easily, and
>`syntax-case' gives you only the pattern matching part in an easy
>way.  But nothing prevents you to write your own body in the above
>definition, and you can use the basic syntax manipulation tools
>(syntax-e, syntax-object->datum and datum->syntax-object).

This is very interesting to me.  If I could deal with syntax objects
the way I want and go back and forth between syntax objects
and scheme objects I would be able to do anything I wanted.
Once I get a good understanding of all this I could better understand
what the heck is going on with syntax-case.

I'd like to play with this.  I did try for a few minutes and couldn't get
anywhere.  For starters, on entry stx, I presume, is a syntax object
representing my call.  I presume the lambda must also return a syntax object.
Since stx is already a syntax object I tried:

(define-syntax foo
   (lambda (stx)
        stx))

(foo 6)

and the system hangs.

If I try:


(define-syntax foo
   (lambda (stx)
     (syntax-object->datum stx)))

(foo 6)

I get:  foo: return value from syntax expander was not syntax: (foo 6)

If I try:


(define-syntax foo
   (lambda (stx)
     (datum->syntax-object stx (car (syntax-object->datum stx)))))

(foo 6)

I get:  car: expects argument of type <pair>; given foo

So if stx represents the "foo" token, how do you get to the 6?

Of course if I try:


(define-syntax foo
   (lambda (stx)
     (datum->syntax-object stx (syntax-object->datum stx))))

(foo 6)

I get the system hang again (I'm now full loop).

I really like your suggestion.  I just can't figure our how to get it 
to work at all.
(I'd prefer to avoid syntax-e since it doesn't seem to be standard.)

Any examples to help me grok this would be greatly appreciated.

Blake McBride


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20060909/f97a5208/attachment.html>

Posted on the users mailing list.