Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macros 有没有办法在Common Lisp中查看内置宏的实现?_Macros_Common Lisp_Clisp - Fatal编程技术网

Macros 有没有办法在Common Lisp中查看内置宏的实现?

Macros 有没有办法在Common Lisp中查看内置宏的实现?,macros,common-lisp,clisp,Macros,Common Lisp,Clisp,常见的Lisp内置函数可能是用C实现的,但我认为宏是用Lisp实现的(如果我对两句话中的任何一句有错误,请原谅)。有没有办法(通过一些函数或宏)查看Common Lisp中内置宏的实现?我正在使用CLisp。检查函数和宏定义的能力是开发环境的一项功能。现在,使用或使用emacs作为Lisp开发环境的基础是很典型的。我个人使用粘液,但我也听说了斯莱的好消息 在SLIME中,您可以调用SLIME edit definition(通过键入M-x SLIME edit definition或使用keyb

常见的Lisp内置函数可能是用C实现的,但我认为宏是用Lisp实现的(如果我对两句话中的任何一句有错误,请原谅)。有没有办法(通过一些函数或宏)查看Common Lisp中内置宏的实现?我正在使用CLisp。

检查函数和宏定义的能力是开发环境的一项功能。现在,使用或使用emacs作为Lisp开发环境的基础是很典型的。我个人使用粘液,但我也听说了斯莱的好消息

在SLIME中,您可以调用
SLIME edit definition
(通过键入
M-x SLIME edit definition
或使用keybinding
M-。
)来访问源文件中光标下的符号定义。无论是在源文件中编辑,还是从REPL中编辑,这都有效。当您想要检查正在使用的某些库代码时,此功能非常有用,但您也可以通过这种方式查看许多内置定义。您甚至可以从当前正在检查的任何定义中找到的新符号跳转到新定义

查看完定义后,您可以使用
M-x slime pop find definition stack
,或更容易记住的keybinding
M-,
M-*
也会起作用)退出先前查看的定义,最终返回到起点

以下是SBCL中的一个示例:

CL-USER> with-open-file[press M-.]
(请注意,上面的“[press M-.]”不是键入的,只是用来提醒此处所采取的操作)。将光标放在打开文件的符号
上或后面,按
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))))))
这里还有更多内容,因为您现在实际位于包含
定义的源文件中;如果向下滚动一点,您还可以找到
的定义

许多SBCL函数都是用Lisp编写的;SBCL有一个非常高质量的编译器,因此许多您可能希望用C编写的东西都可以用Lisp编写,而不会损失性能。下面是函数
列表长度的定义:

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和粘液时也可以做同样的事情。以下是CLISP中定义的打开文件的

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)

CLisp是开源的,您可以查看源代码。如果您使用SLIME,您可以使用标识符上的点执行
ALT-。
,以查看定义是否可用;这是查看库定义的一种非常方便的方式,但您也可以通过这种方式查看一些内置定义。最常见宏的源代码位于
src/macros[12].lisp
。为了理解宏是如何工作的,调用
(MACROEXPAND-1“使用宏的某种形式”)是很有用的。