[plt-scheme] how to quote identifier in macro

From: Andre Mayers (andre.mayers at usherbrooke.ca)
Date: Sun Dec 14 10:45:53 EST 2008

I have some difficulty to apply your suggestion.
The obvious way to implement it doesn't work. 
---
(define-for-syntax attr '(a1 a2 a3))

(define-for-syntax slot-value
  (λ (slot-name objet)
    (cadr (assq slot-name objet))))

(define-syntax (tata stx)
  (syntax-case stx ()
    [(_ a (s ps ...) body ...)
     (with-syntax ([(attr-id ...) (map (λ (p-attr) 
                                         (list p-attr 
                                               (slot-value p-attr #'self)))
                                       (syntax->list #'a))])
       (syntax 
        (λ (self ps ...) 
          (let (attr-id ...)
            body ...))))]))

---
because
(tata attr 
      (self p1 p2) 
      (display self) (newline) (+ p2 a1))

==> expand: unbound identifier in module in: attr
---
I can make a copy of attr accessible for the expansion of the last expression
 (define attr '(a1 a2 a3) )

But both definition of attr are not related to each other and I don't know how to make the connexion between them in the tata macro. For example the simplified program
---
(define attr '(a1 a2 a3))
(define-for-syntax attr '(a1 a2 a3))

(define-syntax (tata stx)
  (syntax-case stx ()
    [(_ a (s ps ...) body ...)
     (with-syntax ([(attr-id ...) (syntax->list #'a)])
       (syntax (attr-id ...)))]))

(tata attr 
      (self p1 p2) 
      (display self) (newline) (+ p2 a1))

==> give the following error message
with-syntax: binding match failed in: (attr-id ...)
---


-----Message d'origine-----
De : Noel Welsh [mailto:noelwelsh at gmail.com] 
Envoyé : December-13-08 7:46 AM
À : andre.mayers at usherbrooke.ca
Cc : pltscheme
Objet : Re: [plt-scheme] how to quote identifier in macro

I don't have time right now to work out the full solution, so here's a
sketch of what you need to do:

1. Make attr available at expansion time.  Do this using
define-for-syntax instead of define

2. Map over attr generating the syntax you want to splice into the
expansion.  Introduce the result using with-syntax

So something like:

(define-for-syntax attr '(a1 a2 a3))

(define-syntax ...
  [(_ a (s ps ...) expr ...)
   (with-syntax ([(attr-id ...) (process-attr (syntax a))])
     (syntax ...))])


HTH,
Noel




Posted on the users mailing list.