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

defmacro within defmacro



    Date: Wed, 23 May 90 18:47 PDT
    From: Charest@AI-SUN.jpl.nasa.gov (Len Charest)

    I want to define a macro that defines another macro with some complex template that is best
    explained by example. Here goes...

    The macro I am actually writing (let's call it MACRO-MAKER) should have the following syntax:
	    (macro-maker name arglist)
    An example of its use looks like:
	    (macro-maker foo (bar baz ad infinitum))
	    => FOO
    The contract of macro-maker is to define a macro with the given name and arglist. The example above 
    defines a macro named FOO that takes 4 required arguments.
	    (foo 1 2 3 4)
	    => ...some result...

    If I say:
1    (defmacro macro-maker (name arglist) ;name and will become part of the inner macro's template
      `(defmacro ,name ,arglist
0	 ;;the function some-function takes multiple literal patterns (ie. lists) as its arguments
	 ;;assume the lists are self-quoting
1	 `(some-function ,,@(loop for arg in arglist
0				  1collect `(0literal1 ,arg)))))
0    => MACRO-MAKER

    I get:
    (macroexpand-1 '(macro-maker foo (bar baz ad infinitum)))
    => (DEFMACRO FOO (BAR BAZ AD INFINITUM)
	`(SOME-FUNCTION ,(LITERAL BAR)
			,(LITERAL BAZ)
			,(LITERAL AD)
			,(LITERAL INFINITUM)))
       T

    What I want is to define MACRO-MAKER so that its expansion looks EXACTLY like:

    (DEFMACRO FOO (BAR BAZ AD INFINITUM)
	`(SOME-FUNCTION (LITERAL ,BAR)
			(LITERAL ,BAZ)
			(LITERAL ,AD)
			(LITERAL ,INFINITUM)))
    T

    Note the sly placement of commas within the pattern denoted by LITERAL. I have tried dozens of
    backquote-comma combinations but can't find the right one.
    Any responses will be greatly appreciated.

    Len Charest, charest@ai-sun.jpl.nasa.gov

This works for me, although I can't call it perpicuous.

1(defmacro macro-maker (name arglist) ;name and will become part of the inner macro's template
  `(defmacro ,name ,arglist
0     ;;the function some-function takes multiple literal patterns (ie. lists) as its arguments
     ;;assume the lists are self-quoting
1     `(some-function
     0,1,@(loop for arg in arglist
0		1collect0 ``1(0literal1 0,,1arg)))))