[racket] variables within macros
Folks,
I would like to write a module that uses a macro to transform "define"s
and accumulate a value based on those transformations.
In a fairly simple way, I want to do something like the below. However, it
seems the module-begin is being transformed before the my-defines. How do
I get counter to be incremented before I need it?
Tim
===================================================================================
"counted-defines.rkt" (similar to what's on the 17.1.2 Using #lang s-exp
Guide Page)
===================================================================================
#lang racket
(provide (except-out (all-from-out racket) #%module-begin define)
(rename-out (module-begin #%module-begin) (my-define define)))
(require (for-syntax syntax/parse))
(define-for-syntax counter 0)
(define-syntax (my-define stx)
(syntax-parse
stx
[(_ i v) (set! counter (add1 counter)) #`(define i v)]
[(_ (i args ...) v ...+) (set! counter (add1 counter))
#`(define (i args ...) v ...)]))
(define-syntax (module-begin stx)
(syntax-parse
stx
[(_ expr ...)
#`(#%module-begin expr ...
(define defines-count #,counter)
(provide defines-count))]))
==========================
"test-counted-defines.rkt"
==========================
#lang s-exp "counted-defines.rkt"
(define woo 2) ; should have pushed counter up by one?
==========================
I then run: racket -e '(require "test-counted-defines.rkt") defines-count'
Which returns me "0"
--
Tim Brown <tim.brown at cityc.co.uk> | City Computing Limited |
T: +44 20 8770 2110 | City House, Sutton Park Road |
F: +44 20 8770 2130 | Sutton, Surrey, SM1 2AE, GB |
-----------------------------------------------------------------------|
BEAUTY: What's in your eye when you have a bee in your hand |
-----------------------------------------------------------------------'
City Computing Limited registered in London No. 1767817.
Registered Office: City House, Sutton Park Road, Sutton, Surrey, SM1 2AE
VAT number 372 8290 34.