[plt-scheme] Struggling with macro...

From: Paulo J. Matos (pocm at soton.ac.uk)
Date: Wed May 23 10:08:54 EDT 2007

On 5/23/07, Jos K oot <jos.koot at telefonica.net> wrote:
> Hi
> The ellipsis in the inner macro, it is handled by the outer macro. In order
> to push the ellipsis into the inner macro replace it by (... ...) which is a
> kind of quoting an ellipsis within a pattern or template.

Yes, I've read about it... so I guess the following should work even
though is pretty cryptic:
(define-syntax (machine stx)
    (syntax-case stx ()
      [(_ mname sexps ...)
       (identifier? #'mname) ; fender
       #`(let ([mc (make-mc () () () () ())])
           #,(with-syntax ([variables (syntax-case #'(sexps ...) (variables)
                                        (((variables (name value) (...
...)) (... ...))
                                         (andmap identifier? #'(name (... ...)))
                                         ((add-variable-to-mc #'mc
#'name #'value) (... ...))))])
               #'variables
               #'mc))]))

Because if (... ...) transforms to ... in the inner macro, the inner macro is:
[variables (syntax-case #'(sexps ...) (variables)
                                        (((variables (name value) ...) ...)
                                         (andmap identifier? #'(name ...))
                                         ((add-variable-to-mc #'mc
#'name #'value) ...)))]

It would probably be easier to dump with-syntax and just define a
separate macro for this but I can't for a very simple reason (to which
I don't know a workaround), I need to access the mc variable from the
macro, so the macro needs to be declared after mc is defined and in
the same environment, due to hygiene.

Any ideas?

> Jos Koot
>
> ----- Original Message -----
> From: "Paulo J. Matos" <pocm at soton.ac.uk>
> To: <plt-scheme at list.cs.brown.edu>
> Sent: Wednesday, May 23, 2007 2:03 PM
> Subject: [plt-scheme] Struggling with macro...
>
>
> > Hi all,
> >
> > Even though from time to time I can write a macro, unfortunately I
> > feel they are not yet clear enough for me.
> >
> > I'm doing a macro to recognise the following:
> > (machine <name>
> >       (variables (<varname> <value>)
> >                         (<varname> <value>)
> >                         (<varname> <value>)
> >                         etc.)
> >      ... (other syntax))
> >
> > I've done some simple structure to keep the information of machine
> > macro called mc and function to save information to structure.
> > So, macro is something like this:
> > (require-for-syntax "mc-utils.scm")
> >
> >  (define-syntax (machine stx)
> >    (syntax-case stx ()
> >      [(_ mname sexps ...)
> >       (identifier? #'mname) ; fender
> >       #'(let ([mc (make-mc () () () () ())])
> >           #,(with-syntax ([variables (syntax-case #'(sexps ...)
> > (variables)
> >                                        ((variables (name value) ...)
> >                                         (andmap identifier? #'(name ...))
> >                                         ((add-variable-to-mc #'name
> > #'value) ...)))])
> >               #'variables
> >               #'mc))]))
> >
> > This gives me: syntax: no pattern variables before ellipses in template
> > in: ...
> > in the second ellipsis of with-syntax.
> > I want to expand:
> > (machine hello
> >       (variables (x 1)
> >                         (y 2)
> >                         (z 3)))
> >
> > to
> > (let ([mc (make-mc () () () () ())])
> >     (add-variable-to-mc x 1)
> >     (add-variable-to-mc y 2)
> >     (add-variable-to-mc z 3)
> >     mc)
> >
> > It seems that no matter what I change I get some kind of wierd error.
> > Can you please point me out in the right direction? (sorry for
> > presenting such ugly and probably full or error macro).
> >
> > Cheers,
> > --
> > Paulo Jorge Matos - pocm at soton.ac.uk
> > http://www.personal.soton.ac.uk/pocm
> > PhD Student @ ECS
> > University of Southampton, UK
> > _________________________________________________
> >  For list-related administrative tasks:
> >  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> >
>
>
>
>


-- 
Paulo Jorge Matos - pocm at soton.ac.uk
http://www.personal.soton.ac.uk/pocm
PhD Student @ ECS
University of Southampton, UK


Posted on the users mailing list.