[plt-scheme] syntax-rules version of this...

From: Robby Findler (robby at cs.uchicago.edu)
Date: Fri Jun 20 13:40:42 EDT 2003

Here's one (I believe):

(define-syntax (define-c-const stx)
  (syntax-case stx ()
    [(_ name)
     (identifier? (syntax name)) ;; the `fender' 
     (with-syntax ([str (format "___result = ~a" (syntax-e (syntax name)))])
       (syntax 
         (define name ((c-lambda () int str)))))]))

the fender only allows that branch of the syntax-case to match if name
is an identifier. Technically, this is different than you macro, but
probably close to what you want -- in fact, you probably want more
checking, since I guess that things like this:

  (define-c-const |a b c|)

don't do the right thing, due to the spaces.

The with-syntax form is like `let', except that it binds things inside
`syntax' only. In this case, we constrct `str' at compile time and then
stick it into the output of the macro as a literal. Constrast with this
macro:

(define-syntax (define-c-const stx)
  (syntax-case stx ()
    [(_ name)
     (identifier? (syntax name)) ;; the `fender' 
     (syntax 
       (define name
        ((c-lambda () int 
           (format "___result = ~a" 'name)))))]))

that constructs the string during evaluation.

hth,
Robby


Posted on the users mailing list.