Suppose I have the following:
(define global "foo")
(define-macro (magic ...))
(magic "bar" 1); <-- I want this to expand to (define foo-bar 1)
How can I make this happen? The macro needs to access the value of "global", but it can't at compile time; if the macro outputs some code that takes advantage of things at run time, I can get to:
(define (string->symbol (string-append "foo" "-" "bar")) 1)
but this, of course, tries to define a function string->symbol rather than what I want.
Thanks!
Afficher les réponses par date
May be you need this:
(define-macro (define-at-expand-time-and-runtime . body) (eval `(define ,@body)) `(define ,@body))
(define-at-expand-time-and-runtime global "foo")
(define-macro (magic name value) `(define ,(string->symbol (string-append global "-" name)) ,value))
(magic "bar" 1)
foo-bar
1
Vasil
Suppose I have the following:
(define global "foo")
(define-macro (magic ...))
(magic "bar" 1); <-- I want this to expand to (define foo-bar 1)
How can I make this happen? The macro needs to access the value of "global", but it can't at compile time; if the macro outputs some code that takes advantage of things at run time, I can get to:
(define (string->symbol (string-append "foo" "-" "bar")) 1)
but this, of course, tries to define a function string->symbol rather than what I want.
Thanks! _______________________________________________ Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
On 7-Aug-09, at 11:51 AM, vasil wrote:
May be you need this:
(define-macro (define-at-expand-time-and-runtime . body) (eval `(define ,@body)) `(define ,@body))
(define-at-expand-time-and-runtime global "foo")
(define-macro (magic name value) `(define ,(string->symbol (string-append global "-" name)) ,value))
(magic "bar" 1)
Here's a stylistic improvement on your idea. The advantage is that the nature of the expression is more apparent (for example you can grep for "(define global" to find the definition of global).
Marc
(define-macro (at-expand-time-and-runtime expr) (eval expr) `,expr)
(at-expand-time-and-runtime (define global "foo"))
(define-macro (magic name value) `(define ,(string->symbol (string-append global "-" name)) ,value))
(magic "bar" 1)
Exactly what I needed. Thanks!
On Fri, Aug 7, 2009 at 9:00 AM, Marc Feeleyfeeley@iro.umontreal.ca wrote:
On 7-Aug-09, at 11:51 AM, vasil wrote:
May be you need this:
(define-macro (define-at-expand-time-and-runtime . body) (eval `(define ,@body)) `(define ,@body))
(define-at-expand-time-and-runtime global "foo")
(define-macro (magic name value) `(define ,(string->symbol (string-append global "-" name)) ,value))
(magic "bar" 1)
Here's a stylistic improvement on your idea. The advantage is that the nature of the expression is more apparent (for example you can grep for "(define global" to find the definition of global).
Marc
(define-macro (at-expand-time-and-runtime expr) (eval expr) `,expr)
(at-expand-time-and-runtime (define global "foo"))
(define-macro (magic name value) `(define ,(string->symbol (string-append global "-" name)) ,value))
(magic "bar" 1)