In the spirit of wanting to learn not only the C FFI, but also gambit macros, I decided to write a macro which generates accessors for C structs. I decided to post it here for others to review, critique, etc. Any feedback on style, methodology, etc is welcome. Also, I put it on pastebin as well as in the e-mail body:
pastebin: http://paste.lisp.org/display/87730
(define-macro (define-c-struct name . members) ;(define (define-c-struct-test name members)
; -- internal utility functions
(define (res-type-gen t) (let ((real-type (if (list? t) (car t) t))) (cond ((member real-type '(union type pointer nonnull-pointer function nonnull-function)) "___result_voidstar = ") ((member real-type '(pointer nonnull-pointer function nonnull-function)) "___result_voidstar = (void*)") ((eq? real-type 'struct) "___result_voidstar = &") (else "___result = "))))
(define (mem-acc-gen name-s mem type) `(define ,(string->symbol (string-append name-s "." mem)) (c-lambda ((pointer ,name-s)) ,type ,(string-append (res-type-gen type) "___arg1->" mem ";"))))
; -- expansion
(let* ((name-s (symbol->string name))) `(begin ,@(let loop ((ms members)) (let* ((mem (symbol->string (caar ms))) (type (cadar ms)) (res-type (res-type-gen type))) (cons (mem-acc-gen name-s mem type) (if (null? (cdr ms)) '() (loop (cdr ms)))))))))