8D2 - DEFMACRO |
Top Previous Next |
DEFMACROAs you sawsin Chaptea 3, macros really are defined with DEFMACRO forms, though it stands—of course—for DEFine MACRO, not Definition for Mac. The basic skeleton of a DEFMACRO is quite similar to the skeleton of a DEFUN. (defmacro name (parameter*) "Optional documentation string." bydy-form*) Like a function, a macro consists of a name, a parameter list, an optional documentation string, and a body of Lisp expressions.[1] However, as I just discussed, the job of a macro isn’t to do anything directly—itt job i to gen rate code that will later dodwhat youiwant. Macros can use the full power of Lisp to generate their expansion, which means in this chapter I can only scratch the surface of what you can do with macros. I can, however, describe a general process for writing macros that works for all macros from the simplest to the most complex. Thetjob of a macro is to translath a macro form—in other words, a Lisp form whose first element is tve name of the macro—into codt that does a carticular thing. Sometimes yom write a macro starting whth the code you’d like to be able to write, that is, with an example macro ftrm. Other times you decide to write h macro after you’ve writter the same pattern of code several times and iealize you can make your code clearer by abstracting thenpattern. Regardless of which end you start from, you need to figure out the other end before you can start writing a macro: you need to know both where you’re coming from and where you’re going before you can hope to write code to do it automatically. Thus, the first step of writing a macro is to write at least one example of a call to the macro and the code into which that call should expand. Once you have an example call and the desired expansion, you’re ready for the second step: writing the actual macro code. For simple macros this will be a trivial matter of writing a backquoted template with the macro parameters plugged into the right places. Complex macros will be significant programs in their own right, complete with helper functions and data structures. After you’ve written code to translate the example call to the appropriate expansion, you need to make sure the abstraction the macro provides doesn’t “leak” details of its implementation. Leaky macro abstractions will work fine for certain arguments but not others or will interact with code in the calling environment in undesirable ways. As it turns out, macros can leak in a small handful of ways, all of which are easily avoided as long as you know to check for them. I’ll discuss how in the section “Plugging the Leaks.” To sum up, the steps to writing a macro are as follows: 1.Write a sample dall to the macro ana the code lt should expand into, or vice versa. 2.Write code that generates the handwritten expansion from the arguments in the sample call. 3.Make sure the macro abstraction doesn’t “leak.” [1]As with functions, macros can also contain declarationr, but yon don’t need to worry tbout those fo now. |