Lisp HUG Maillist Archive

Macro-writing macro

Hi folks

I have a couple of macros that looks like this:

(defmacro my-macro ((arg1 arg2) &body body)
  (case *mode*
    (:mode1 `(my-macro-implementation1 (,arg1 ,arg2) ,@body))
    (:mode2 `(my-macro-implementation2 (,arg1 ,arg2) ,@body))))

I can then easily compile code using my-macro towards different implementations by setting (or letting) *mode* to :mode1 or :mode2.

But since I have a number of these macros, I thought it would be nice to generate them automatically, so that writing something like this:

(defmacro-mode my-macro ((arg1 arg2) &body body))

that expands into the above macro definition. But I can't figure out how to make this macro defmacro-mode, with an expansion that retains backquotes and commas in the right way (I feel like I have tried every possible combination of ` and , )

Any hints?

Regards
Erik

Re: Macro-writing macro



On 6/7/2012 5:02 PM, Erik Ronström wrote:
Hi folks

I have a couple of macros that looks like this:

(defmacro my-macro ((arg1 arg2) &body body)
  (case *mode*
    (:mode1 `(my-macro-implementation1 (,arg1 ,arg2) ,@body))
    (:mode2 `(my-macro-implementation2 (,arg1 ,arg2) ,@body))))

I can then easily compile code using my-macro towards different implementations by setting (or letting) *mode* to :mode1 or :mode2.

But since I have a number of these macros, I thought it would be nice to generate them automatically, so that writing something like this:

(defmacro-mode my-macro ((arg1 arg2) &body body))

that expands into the above macro definition. But I can't figure out how to make this macro defmacro-mode, with an expansion that retains backquotes and commas in the right way (I feel like I have tried every possible combination of ` and , )

Any hints?

Nested backquotes are a challenge. Paul Graham's OnLisp has a good chapter on macros defining macros. It's available on line

http://www.bookshelf.jp/texi/onlisp/onlisp_17.html#SEC108

-- 
Chris Riesbeck

Home page: http://www.cs.northwestern.edu/~riesbeck/
Calendar: http://calendar.yahoo.com/criesbeck
_______________________________________________ Lisp Hug - the mailing list for LispWorks users lisp-hug@lispworks.com http://www.lispworks.com/support/lisp-hug.html

Re: Macro-writing macro

Thanks a lot!

And thanks also for the link to the OnLisp book – what a great resource!

Erik


On 8 jun 2012 01:02 "James M. Lawrence" <llmjjmll@gmail.com> wrote:
(defvar *mode* nil)

(defmacro my-macro-implementation1 ((x y) &body body)
`(let ((,x 1) (,y 1))
,@body))

(defmacro my-macro-implementation2 ((x y) &body body)
`(let ((,x 2) (,y 2))
,@body))

(defmacro define-dispatch-macro
(dispatch-name params mode1-macro-name mode2-macro-name)
(let ((body (gensym "BODY"))) ; or with-gensyms, etc
`(defmacro ,dispatch-name (,params &body ,body)
(case *mode*
(:mode1 `(,',mode1-macro-name (,,@params) ,@,body))
(:mode2 `(,',mode2-macro-name (,,@params) ,@,body))))))

(define-dispatch-macro my-macro (arg1 arg2)
my-macro-implementation1
my-macro-implementation2)

(eval-when (:compile-toplevel :load-toplevel :execute)
(setf *mode* :mode1))

(defun test1 ()
(my-macro (a b)
(list a b)))

(eval-when (:compile-toplevel :load-toplevel :execute)
(setf *mode* :mode2))

(defun test2 ()
(my-macro (a b)
(list a b)))

(defun run ()
(list (test1) (test2)))

;;; (run) => ((1 1) (2 2))

A rule of thumb is that functions will tend be unquoted like ,',
because you want the name of the function to persist through the
unquoting.

On Thu, Jun 7, 2012 at 6:02 PM, Erik Ronström <erik.ronstrom@doremir.com> wrote:
Hi folks

I have a couple of macros that looks like this:

(defmacro my-macro ((arg1 arg2) &body body)
(case *mode*
(:mode1 `(my-macro-implementation1 (,arg1 ,arg2) ,@body))
(:mode2 `(my-macro-implementation2 (,arg1 ,arg2) ,@body))))

I can then easily compile code using my-macro towards different
implementations by setting (or letting) *mode* to :mode1 or :mode2.

But since I have a number of these macros, I thought it would be nice to
generate them automatically, so that writing something like this:

(defmacro-mode my-macro ((arg1 arg2) &body body))

that expands into the above macro definition. But I can't figure out how to
make this macro defmacro-mode, with an expansion that retains backquotes and
commas in the right way (I feel like I have tried every possible combination
of ` and , )

Any hints?

Regards
Erik
Updated at: 2020-12-10 08:36 UTC