| johnnyb@eskimo.com 2005-05-31, 4:01 am |
| I came up with a handy macro and I thought I'd see what your comments
on it were. It's a Scheme replacement for Lisp's eval-when, called
"at-expand-time". Although Scheme doesn't have an explicit compile
stage, it usually corresponds with macroexpand time. Anyway, this
little syntax-case macro does compile-time evaluation. I've tested it
with Chicken, and wondered what you all thought of it.
I'm posting from Google, so I apologize if this comes through jumbled.
;;Define our macro
(define-syntax at-compile-time
;;x is the syntax object to be transformed
(lambda (x)
(syntax-case x ()
(
;;Pattern just like a syntax-rules pattern
(at-compile-time expression)
;;with-syntax allows us to build syntax objects
;;dynamically
(with-syntax
(
;this is the syntax object we are building
(expression-value
;after computing expression, transform it into a syntax
object
(datum->syntax-object
;syntax domain
(syntax at-compile-time)
;quote the value so that its a literal value
(list 'quote
;compute the value to transform
(eval
;;convert the expression from the syntax
;;representation to a list representation
(syntax-object->datum (syntax expression)))))))
;;Just return the generated value as the result
(syntax expression-value))))))
(define calculated-at-compile-time (at-compile-time (sqrt 5)))
Anyway, I'm still pretty new to macros, so let me know what you think!
|