Common Lisp:编译与评估 [英] Common Lisp: compilation vs evaluation
问题描述
- 评估:例如与C-M-x eval-defun
- 汇编:例如与Cc Mk编译文件
第二个也生成一个.fasl文件。
两者之间有什么区别?
当我编译定义/一个文件?
每个人的优点和缺点是什么?
首先,有一个函数 eval
[ 1 ],它允许在语言运行时评估(即执行)任意CL表单。 CL实现可能有2种不同的操作模式:编译模式和解释模式。编译模式意味着,在评估之前,表单首先被编译在内存中。在CL评估中也不是在文件级别上,而是在单个表单的级别上。因此 eval
都可以编译和解释表单,具体取决于操作模式。 (例如默认情况下SBCL总是编译,除非你通过设置 sb-ext:* evaluateator-mode *
为:interpret
,而CLISP总是解释)。
现在,还有一个方便的函数 compile-file
[ 2 ],它允许编译某个文件中的所有表单并将结果保存在另一个文件。这个不会触发这些表单的评估。
CL还定义了程序生命周期的3个不同时间:编译时间,加载时间和执行时间处理时间。而且有一种可能性来控制当使用最多的(如果不是最多的)隐含的CL特殊操作符 eval-when
[ CMx eval -defun 将在光标下的窗体上调用 eval
。它不需要编译它,但这是可能的,取决于实施。 C-c M-k编译文件会编译文件
您的缓冲区,但不会评估它的内容。
On Emacs + Slime with sbcl, once I define a function (or more) in a file I have two choices:
- Evaluation: e.g. with C-M-x eval-defun
- Compilation: e.g. with C-c M-k compile-file
The second one produces a .fasl file, too.
What are the differences between the two?
What's going on under the hood when I compile a definition / a file?
What are the Pros and Cons of each one?
First of all, there's a function eval
[1], that allows to evaluate (i.e. execute) arbitrary CL form in the language runtime. CL implementations may have 2 different modes of operation: compilation mode and interpretation mode. Compilation mode implies, that before evaluation the form is first compiled in-memory. Also in CL evaluation happens not on the file-level, but on the level of individual forms. So eval
may both compile and interpret the form, depending on the mode of operation. (For example SBCL by default always compiles, unless you instruct it not to by setting sb-ext:*evaluator-mode*
to :interpret
, while CLISP always interprets).
Now, there's also a convenience function compile-file
[2] that allows to compile all the forms in some file and save the results in another file. This doesn't trigger evaluation of these forms.
Also CL defines 3 distinct times of program lifecycle: compile-time, load-time and execution time. And there's a possibility to control what happens when with one of the most (if not the most) cryptic CL special operators eval-when
[3].
To sum up, C-M-x eval-defun will call eval
on the form under cursor. It will not necessary compile it, but that is possible, depending on implementation. C-c M-k compile-file will compile-file
your buffer, but not evaluate it's contents.
这篇关于Common Lisp:编译与评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!