Eduardo Cavazos
2009-02-13 23:37:54 UTC
Hello,
So, I've seen a few "define-macro using syntax-case" implementations
floating around. This one loads and works for simple cases. But it has
trouble when you reference non-rnrs-base functions in the body.
For example, this works:
----------------------------------------------------------------------
----------------------------------------------------------------------
but this doesn't:
----------------------------------------------------------------------
No binding available for sq in library (define-macro)
----------------------------------------------------------------------
Any suggestions?
Ed
----------------------------------------------------------------------
(library
(define-macro)
(export define-macro)
(import
(for (rnrs base) run expand)
(for (rnrs syntax-case) run expand)
)
(define-syntax define-macro
(lambda (x)
(syntax-case x ()
((_ (name . args) . body)
(syntax (define-macro name (lambda args . body))))
((_ name transformer)
(syntax
(define-syntax name
(lambda (y)
(syntax-case y ()
((_ . args)
(datum->syntax
(syntax _)
(apply transformer
(syntax->datum (syntax args)))))))))))))
)
----------------------------------------------------------------------
So, I've seen a few "define-macro using syntax-case" implementations
floating around. This one loads and works for simple cases. But it has
trouble when you reference non-rnrs-base functions in the body.
For example, this works:
----------------------------------------------------------------------
(define-macro (test-macro a) `(list ,a))
(test-macro 10)
(10)(test-macro 10)
----------------------------------------------------------------------
but this doesn't:
----------------------------------------------------------------------
(define (sq n) (* n n))
(define-macro (test-macro-sq a) `(list (sq ,a)))
(test-macro-sq 10)
Syntax violation: invalid reference(define-macro (test-macro-sq a) `(list (sq ,a)))
(test-macro-sq 10)
No binding available for sq in library (define-macro)
----------------------------------------------------------------------
Any suggestions?
Ed
----------------------------------------------------------------------
(library
(define-macro)
(export define-macro)
(import
(for (rnrs base) run expand)
(for (rnrs syntax-case) run expand)
)
(define-syntax define-macro
(lambda (x)
(syntax-case x ()
((_ (name . args) . body)
(syntax (define-macro name (lambda args . body))))
((_ name transformer)
(syntax
(define-syntax name
(lambda (y)
(syntax-case y ()
((_ . args)
(datum->syntax
(syntax _)
(apply transformer
(syntax->datum (syntax args)))))))))))))
)
----------------------------------------------------------------------