chicken-users
[Top][All Lists]
Advanced

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

[Chicken-users] Some macros to help people migrating from define-macro t


From: Alaric Snell-Pym
Subject: [Chicken-users] Some macros to help people migrating from define-macro to syntactic closures or explicit renaming:
Date: Mon, 14 Apr 2008 18:22:00 +0100


I've been messing around with macros that give you the DSSSL argument
list destructuring of define-macro in the above, based on the
technique used to implement define-macro in hygienic macro systems.

(use riaxpander)

(define-syntax sc-macro-transformer-with-dsssl-args
        (sc-macro-transformer
                (lambda (form environment)
                        `(sc-macro-transformer
                                (lambda (f e)
                                        (apply
                                                ,(make-syntactic-closure 
environment '() (cadr form))
                                                (cons e (cdr f))))))))

(define-syntax rsc-macro-transformer-with-dsssl-args
        (sc-macro-transformer
                (lambda (form environment)
                        `(rsc-macro-transformer
                                (lambda (f e)
                                        (apply
                                                ,(make-syntactic-closure 
environment '() (cadr form))
                                                (cons e (cdr f))))))))

(define-syntax er-macro-transformer-with-dsssl-args
        (sc-macro-transformer
                (lambda (form environment)
                        `(er-macro-transformer
                                (lambda (f r c)
                                        (apply
                                                ,(make-syntactic-closure 
environment '() (cadr form))
                                                (cons r (cons c (cdr f)))))))))


Basically, it creates a new macro transformer type that's like a sc,
rsc, or er, except that the form argument is removed, and replaced
with an arbitrary number of extra arguments that get the arguments to
the macro invocation.

Here are some examples:

(define-syntax sc-foo (sc-macro-transformer-with-dsssl-args
        (lambda (env a b #!key (c 3))
                (list 'list
                        (make-syntactic-closure env '() a)
                        (make-syntactic-closure env '() b)
                        (make-syntactic-closure env '() c)))))

(define-syntax rsc-foo (rsc-macro-transformer-with-dsssl-args
        (lambda (env a b #!key (c 3))
                `(,(make-syntactic-closure env '() 'list) ,a ,b ,c))))

(define-syntax er-foo (er-macro-transformer-with-dsssl-args
        (lambda (rename compare a b #!key (c 3))
                `(,(rename 'list) ,a ,b ,c))))

(list (sc-foo 1 2) (sc-foo 1 2 c: 5))
> => ((1 2 3) (1 2 5))
(list (rsc-foo 1 2) (rsc-foo 1 2 c: 5))
> => ((1 2 3) (1 2 5))
(list (er-foo 1 2) (er-foo 1 2 c: 5))
> => ((1 2 3) (1 2 5))

This means you can do basic form deconstruction with simple and
familiar argument list syntax, just as with lambdas, meaning you
needn't break out the pattern matching for simple things (and
bringing more of the simplicity of syntax-rules without the
limitations).

In particular, the er-macro-transformer-with-dsssl-args (suggestions
for a shorter name on a postcard, please) and rsc-macro-transformer-
with-dsssl-args are good replacements for define-macro with identical
semantics, except that you get the extra environment or rename/
compare params with which to start cleaning up your unhygienic
symbols when you're ready.

(define-macro (foo ...args...) ...body...)

=>

(define-syntax foo (rsc-macro-transformer-with-dssl-args
        (lambda (env ...args...) ...body...)))

Or you can go straight to sc-macro-transformer-with-dssl-args then go
and syntactically-enclose those things that you want to be considered
in the environment of the macro call.

If these are welcomed by people, shall I put them in an egg? They
seem too trivial for a whole egg, though - perhaps they should go
into an existing egg, either stuck on the end of riaxpander itself,
or in a misc-hygienic-macros egg?

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]