Common Lisp内置函数可能在C中实现。但是我想象宏在Lisp中实现(很抱歉,如果我对两个句子中的任何一个有误,都可以。)有什么办法(通过某些函数或宏)来查看Common Lisp中内置宏的实现?我正在使用CLisp。
检查功能和宏定义的能力是您开发环境的功能。如今,通常将带有emacs的SLIME或SLY用作Lisp开发环境的基础。我个人使用SLIME,但我也听说过SLY的好处。
在SLIME中,您可以调用slime-edit-definition
(通过键入M-x slime-edit-definition
或使用键绑定M-.
)来访问源文件中光标下方的符号定义。在源文件中或从REPL中进行编辑时,此方法均有效。当您要检查正在使用的某些库代码时,此功能非常有用,但是您也可以通过这种方式查看许多内置定义。您甚至可以从正在检查的任何定义中找到的新符号跳到新定义。
查看完定义后,您可以使用M-x slime-pop-find-definition-stack
或更容易记住的键绑定M-,
(M-*
也可以使用)来退出以前查看的定义,最终返回到开始点。
这里是一个示例,在SBCL中:
CL-USER> with-open-file[press M-.]
(请注意,上面的“ [press M-。]”未键入,仅用于提醒您在此处采取了什么操作)。将光标放在符号with-open-file
上或之后,按M-.
查看定义:
(sb-xc:defmacro with-open-file ((stream filespec &rest options)
&body body)
(multiple-value-bind (forms decls) (parse-body body nil)
(let ((abortp (gensym)))
`(let ((,stream (open ,filespec ,@options))
(,abortp t))
,@decls
(unwind-protect
(multiple-value-prog1
(progn ,@forms)
(setq ,abortp nil))
(when ,stream
(close ,stream :abort ,abortp)))))))
这次在按下M-.
之后,SLIME提供了可供选择的定义以供查看:
CL-USER> and[press M-.]
显示在emacs缓冲区中:
/path-to-source/sbcl-2.0.4/src/code/macros.lisp
(DEFMACRO AND)
/path-to-source/sbcl-2.0.4/src/pcl/ctypes.lisp
(DEFINE-METHOD-COMBINATION AND)
我们想查看宏定义,因此将光标移至显示(DEFMACRO AND)
的行,并显示以下定义:
;; AND and OR are defined in terms of IF.
(sb-xc:defmacro and (&rest forms)
(named-let expand-forms ((nested nil) (forms forms) (ignore-last nil))
(cond ((endp forms) t)
((endp (rest forms))
(let ((car (car forms)))
(cond (nested
car)
(t
;; Preserve non-toplevelness of the form!
`(the t ,car)))))
((and ignore-last
(endp (cddr forms)))
(car forms))
;; Better code that way, since the result will only have two
;; values, NIL or the last form, and the precedeing tests
;; will only be used for jumps
((and (not nested) (cddr forms))
`(if ,(expand-forms t forms t)
,@(last forms)))
(t
`(if ,(first forms)
,(expand-forms t (rest forms) ignore-last))))))
这里还有更多内容,因为您现在实际上已经在包含and
定义的源文件中;如果向下滚动,还可以找到or
的定义。
许多SBCL函数是用Lisp编写的; SBCL具有非常高质量的编译器,因此,您可以期望用C编写的很多内容都可以用Lisp编写,而不会降低性能。这是函数list-length
的定义:
CL-USER> list-length[press M-.]
(defun list-length (list)
"Return the length of the given List, or Nil if the List is circular."
(do ((n 0 (+ n 2))
(y list (cddr y))
(z list (cdr z)))
(())
(declare (type fixnum n)
(type list y z))
(when (endp y) (return n))
(when (endp (cdr y)) (return (+ n 1)))
(when (and (eq y z) (> n 0)) (return nil))))
将CLISP与SLIME一起使用时,可以完成相同的操作。这是CLISP中定义的with-open-file
:
CL-USER> with-open-file[press M-.]
(defmacro with-open-file ((stream &rest options) &body body)
(multiple-value-bind (body-rest declarations) (SYSTEM::PARSE-BODY body)
`(LET ((,stream (OPEN ,@options)))
(DECLARE (READ-ONLY ,stream) ,@declarations)
(UNWIND-PROTECT
(MULTIPLE-VALUE-PROG1
(PROGN ,@body-rest)
;; Why do we do a first CLOSE invocation inside the protected form?
;; For reliability: Because the stream may be a buffered file stream,
;; therefore (CLOSE ,stream) may produce a disk-full error while
;; writing the last block of the file. In this case, we need to erase
;; the file again, through a (CLOSE ,stream :ABORT T) invocation.
(WHEN ,stream (CLOSE ,stream)))
(WHEN ,stream (CLOSE ,stream :ABORT T))))))
但是,许多CLISP函数都是用C编写的,并且这些定义无法以与以前相同的方式进行检查:
CL-USER> list-length[press M-.]
No known definition for: list-length (in COMMON-LISP-USER)