DEFINE-COMPILER-MACRO(3cl) |
Common Lisp Reference |
DEFINE-COMPILER-MACRO(3cl) |
NAME
define-compiler-macro – define a compiler macro function (macro)
SYNOPSIS
|
ARGUMENTS and VALUES
—a function name.
—a macro lambda list.
—a string, not evaluated.
—a form.
DESCRIPTION
This is the normal mechanism for defining a compiler macro function. Its manner of definition is the same as for defmacro; the only differences are:
The name can be a function name naming any function or macro.
The expander function is installed as a compiler macro function for the , rather than as a macro function.
The &whole argument is bound to the argument that is passed to the compiler macro function. The remaining parameters are specified as if this contained the function name in the car and the actual arguments in the cdr , but if the car of the actual form is the symbol funcall, then the destructuring of the arguments is actually performed using its cddr instead.
documentation is attached as a documentation string to (as kind compiler-macro) and to the compiler macro function.
Unlike an ordinary macro, a compiler macro can decline to provide an expansion merely by returning a form that is the same as the original (which can be obtained by using &whole).
AFFECTED BY
(none)
EXCEPTIONAL SITUATIONS
The consequences are undefined if nil in a use of setf of compiler-macro-function. is non-
NOTES
The consequences of writing a compiler macro definition for a function in the COMMON-LISP package are undefined; it is quite possible that in some implementations such an attempt would override an equivalent or equally important definition. In general, it is recommended that a programmer only write compiler macro definitions for functions he or she personally maintains–writing a compiler macro definition for a function maintained elsewhere is normally considered a violation of traditional rules of modularity and data abstraction.
EXAMPLES
(
defun
(
x
) (
expt
2
))
(
define-compiler-macro
square
(
&whole
)
(
if
(
atom
)
`
(
expt
,
2
)
(
case
(
car
)
(
(
if
(
=
(
length
)
2
)
`
(
expt
,(
nth
1
)
4
)
))
(
expt
(
if
(
=
(
length
)
3
)
(
if
(
numberp
(
nth
2
))
`
(
expt
,(
nth
1
)
,(
*
2
(
nth
2
)))
`
(
expt
,(
nth
1
)
(
*
2
,(
nth
2
))))
))
(
otherwise
`
(
expt
,
2
)))))
(
(
3
))
81
(
macroexpand
’(
))
(
),
false
(
funcall
(
compiler-macro-function
’
) ’(
)
nil
)
(
EXPT
2
)
(
funcall
(
compiler-macro-function
’
)
’(
(
))
nil
)
(
EXPT
4
)
(
funcall
(
compiler-macro-function
’
)
’(
#’
)
nil
)
(
EXPT
2
)
(
defun
(
)
(
sqrt
(
+
(
expt
(
-
)
2
) (
expt
(
-
)
2
))))
(
defun
(
&key
(
0
) (
0
) (
) (
))
(
))
(
define-compiler-macro
(
&whole
&rest
&key
(
0
)
(
0
)
(
)
(
)
&allow-other-keys
&environment
)
(
flet
((
(
)
(
nth
(
*
2
)
))
(
(
)
(
nth
(
1+
(
*
2
))
))
(
(
)
(
let
((
(
macroexpand
)))
(
or
(
constantp
)
(
symbolp
)))))
(
let
((
(
/
(
length
)
2
)))
(
multiple-value-bind
(
)
(
loop
for
(
)
on
by
#’
cddr
count
(
eq
’:
)
into
count
(
eq
’:
)
into
count
(
eq
’:
)
into
count
(
eq
’:
)
into
count
(
not
(
member
’(:
:
:
:
)))
into
finally
(
return
(
values
)))
(
cond
((
and
(
=
4
)
(
eq
(
0
)
:
)
(
eq
(
1
)
:
)
(
eq
(
2
)
:
)
(
eq
(
3
)
:
))
`
(
,
,
,
,
))
((
and
(
if
(
and
(
=
1
)
(
))
t
)
(
if
(and
(
=
1
) (
))
t
)
(
if
(and
(
=
1
) (
))
t
)
(
if
(and
(
=
1
) (
))
t
)
(
zerop
))
`
(
,
,
,
,
))
((
and
(
<
2
)
(
<
2
)
(
<
2
)
(
<
2
)
(
zerop
))
(
let
((
(
loop
repeat
collect
(
gensym
))))
`
(
let
,(
loop
for
below
collect
(
list
(
nth
)
(
)))
(
,@(
loop
for
below
append
(
list
(
)
(
nth
)))))))
(
t
)))))
)
(
dolist
(
’((
:
(
setq
7
) :
(
decf
)
:
(
decf
) :
(
decf
))
(
:
(
setq
7
) :
(
decf
)
:
(
decf
) :
(
decf
))
(
:
(
setq
7
) :
(
incf
))
(
:
(
setq
7
) :
(
incf
)
:
(
incf
))
(
:
:
:
:
)
(
:
:
:
:
)
(
:
:
:
:
:
:
)))
(
print
(
funcall
(
compiler-macro-function
’
)
nil
)))
(
LET
((#:
(
SETQ
7
))
(#:
(
DECF
))
(#:
(
DECF
))
(#:
(
DECF
)))
(
:
#:
:
#:
:
#:
:
#:
))
(
(
SETQ
7
)
(
DECF
) (
DECF
) (
DECF
))
(
LET
((#:
(
SETQ
7
))
(#:
(
INCF
)))
(
:
#:
:
#:
))
(
:
(
SETQ
7
) :
(
INCF
) :
(
INCF
))
(
)
(
)
(
:
:
:
:
:
:
)
NIL
SEE ALSO
compiler-macro-function(3cl), defmacro(3cl), documentation(3cl)
AUTHOR and COPYRIGHT
Substantial portions of this page are taken from draft proposed American National Standard for Information Systems—Programming Language—Common Lisp, X3J13/94-101R, Version 15.17R, Fri 12-Aug-1994 6:35pm EDT; no copyright indicated.
Additional clarification and comments by Michael Marking <marking@tatanka.com>, http://www.tatanka.com/software/cl-manpages/; alternatively, https://github.com/wakinyantanka/cl-manpages/. Copyright 2017 Michael Marking as both an original and a derivative work.
Licensed under Creative Commons Attribution-NoDerivatives 4.0 International (CC BY-ND 4.0).
This page last revised Sunday 26 February 2017.