[plt-scheme] C enum syntax

From: Taylor Campbell (campbell at evdev.ath.cx)
Date: Sat Jan 25 16:20:42 EST 2003

dvanhorn at emba.uvm.edu wrote:

 >  For list-related administrative tasks:
 >  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
 >I've been using the C FFI and handling enumerated constants like so, 
which seems
 >to work fine:
 >   (define some-const ((c-lambda () int "___result = SOME_CONST;")))
 >But I would rather do something like:
 >   (define some-const (c-enum "SOME_CONST"))
 >So I tried:
 >   (define-syntax (c-enum stx)
 >     (syntax-case stx ()
 >       ((_ c)
 >        (quasisyntax
 >         ((c-lambda () int
 >            (unsyntax (format "___result = ~a;" (syntax c)))))))))
 >Which is apparently not correct.  Could someone shed some light on 
what's wrong
 >with the `c-enum' syntax above?  Also, is there a better approach to 
enums in
To your first question, I believe you have to turn the string created with
format into a syntax object with datum->syntax-object; as well, the second
argument to format should be a string, not a syntax object, so you need to
use syntax-object->datum.  Here is the corrected version:

(define-syntax (c-enum stx)
   (syntax-case stx ()
     ((_ ?c)
      #`((c-lambda () int
               stx (format "___result = ~A;"
                           (syntax-object->datum #'?c))))))))

To your second question, I don't know; I've never actually used the FFI.

[oops: didn't send it to the list at first]

Posted on the users mailing list.