[plt-scheme] ffi equivalent of array in struct
> This seems repetitive, so maybe a macro will help.
>
> Here's one that I just cooked up (starting to learn about quasiquotation
> now...):
[code cut]
Hi Jon,
Whoops! I made an error in forgetting to require-for-syntax the
foreign.ss library, since it's being used by build-int-stx-list. I'm
still trying to keep my macro-expansion phases straight...
I'm pretty sure I have to do a require-for-syntax to make things kosher.
The corrected code should be:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module n-bytes mzscheme
(require (lib "foreign.ss"))
(require-for-syntax (lib "foreign.ss"))
(define 4bytes _int)
(define 8bytes (make-cstruct-type (list 4bytes 4bytes)))
(define-for-syntax (multiple-of-four? n)
(and (number? n) (= 0 (remainder n 4))))
(define-for-syntax (build-int-stx-list stx n)
(let loop ([n n]
[acc #'()])
(cond
[(= n 0) acc]
[else
(loop (- n 4)
(quasisyntax/loc stx
(_int . #,acc)))])))
(provide n-bytes)
(define-syntax (n-bytes stx)
(syntax-case stx ()
[(_ n)
(cond
[(multiple-of-four? (syntax-e #'n))
(quasisyntax/loc stx
(make-cstruct-type
(list #,@(build-int-stx-list stx (syntax-e #'n)))))]
[else
(raise-syntax-error
#f
"expected numeric literal divisible by four"
stx)])])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
But that's odd, though: I wonder why my tests appeared to work, even when
the reference to "_int" within build-int-stx-list should have broke! I
guess my test cases weren't tight enough.
My apologies!