Re: Compilation order when compiling and loading a file
- From: Rainer Joswig <joswig@xxxxxxx>
- Date: Fri, 30 Nov 2007 15:49:50 +0100
In article
<8d0db54f-a172-4856-97c7-7e8bc8a78705@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"peter.hildebrandt@xxxxxxxxx" <peter.hildebrandt@xxxxxxxxx> wrote:
Hello *,
I'd like to do the following within one file
- defun a helper function
- defmacro a macro which uses the helper function
- use the macro
Just like this
-----------
(in-package :common-lisp-user)
(defun helper (p)
`(print ,p))
(defmacro my-macro (a)
(helper a))
(my-macro 'done)
------------
However, at the time my-macro is compiled, it complains about helper
being undefined. (In case it matters: This happens in SBCL 1.0.11
with today's slime cvs)
Is there a way to make sure helper is defined when the expansion of
(my-macro ...) is compiled? (other than splitting the source into
two files and using e.g. asdf:defsystem)
Any hints are highly appreciated.
Peter
Edi gave you the pointer to EVAL-WHEN.
Just to mention it, above is to expect, when you compile that
file. The Common Lisp compiler sees all the definitions
when compiling the file. It will note that there is a
function HELPER in the file, but will not store its code.
If it sees a macro, it will store the macro code during
the compilation, since the macro can be used later in
the file and then the compiler needs to run the macro.
After compilation the macro is gone. It is only remembered
during compilation of the file. If you want the compiler
to execute code when it compiles a file, you need
to wrap EVAL-WHEN with :COMPILE-TOPLEVEL around that code.
When you wrap it around the HELPER definition, the
compiler will run the DEFUN during compilation.
The mechanism is useful to understand. The interaction
of file compilation with the Lisp system is a bit
unique to Common Lisp. The effect is that the compiler
itself is a full Common Lisp environment which you
can use for computation during compile time. Two
different scenarios are typical:
* the file compiler runs inside the environment. You develop
everything from one Lisp system. The file compiler
is running in the same Lisp where you load the code.
You compile and load file after file into the Lisp system.
* the file compiler is running as batch or slave compiler.
Then you have the effect that for each compilation
session you have a fresh Lisp compiler and every time you
call the file compiler you need to make sure that the compiler
knows ALL the definitions it needs at compile time.
--
http://lispm.dyndns.org/
.
- References:
- Compilation order when compiling and loading a file
- From: peter.hildebrandt@xxxxxxxxx
- Compilation order when compiling and loading a file
- Prev by Date: Re: OT: "frgo" is not frog! WAS: Re: A simple debugging macro
- Next by Date: Re: The origins of CL conditions system
- Previous by thread: Re: Compilation order when compiling and loading a file
- Index(es):
Relevant Pages
|
|