chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] macro systems and chicken (long)


From: Alaric Snell-Pym
Subject: Re: [Chicken-users] macro systems and chicken (long)
Date: Tue, 8 Apr 2008 12:04:45 +0100


Actually, here's a more twisted test case with lots of shadowing of
names with different values at different levels:

(use riaxpander)

(define-macro (my-assert test error)
        `(if (not ,test) (error ,error)))

(define-syntax foo
        (syntax-rules ()
                ((_ x) (let ((tmp x)) (my-assert tmp "failed")))))

(macroexpand '(let ((x 1) (tmp 2)) (foo (+ x tmp))))


This expands (IMHO correctly) to:

((lambda (x#0 tmp#1) ((lambda (tmp#2) (if (not tmp#2) (error
"failed"))) (+ x#0 tmp#1))) 1 2)

On a side note, it's illuminating to see how redefining 'if' breaks
my-assert (since it's a dirty macro), but even if I put an if into
the foo macro, it doesn't get broken, thanks to the hygienic renaming:

(define-syntax foo
        (syntax-rules ()
                ((_ x) (let ((tmp x)) (if #t (my-assert tmp "failed"))))))

(macroexpand '(let ((if 3) (x 1) (tmp 2)) (foo (+ x tmp))))

produces:

((lambda (if#0 x#1 tmp#2) ((lambda (tmp#3) (if #t (if#0 (not tmp#3)
(error "failed")))) (+ x#1 tmp#2))) 3 1 2)

Note how my rebinding of if becomes if#0, while foo still refers to
plain old if, but the expanded body of assert picks up if#0.

ABS

--
Alaric Snell-Pym
Work: http://www.snell-systems.co.uk/
Play: http://www.snell-pym.org.uk/alaric/
Blog: http://www.snell-pym.org.uk/?author=4






reply via email to

[Prev in Thread] Current Thread [Next in Thread]