如何用另一个S表达式包装和执行Lisp S表达式?

时间:2018-11-11 20:10:29

标签: macros common-lisp

我试图用另一个Lisp表达式包装Lisp表达式。我想宏应该可以,但是我不明白。谁能帮助我,谁知道该怎么做?

我的实际目的是编写一个宏,该宏将一些with-open-file表达式包装在某些宏程序体代码周围。

(我想编写一个脚本/程序,该脚本/程序打开一个或两个输入文件,逐行处理它们,而且还将处理结果输出到几个不同的独立输出文件中。为此,我希望拥有{{ 1}}宏调用围绕处理和写入独立输出文件的代码堆积-所有这些都针对宏体代码打开。

由于with-open-file需要输入或输出流的符号(处理程序)以及输出(或输入)文件的路径变量,以及一些其他信息(文件的方向等),因此我想将它们放入列表。

with-open-file

我希望如何调用宏:

;; Output file-paths:
(defparameter *paths* '("~/out1.lisp" "~/out2.lisp" "~/out3.lisp"))

;; stream handlers (symbols for the output streams)
(defparameter *handlers* '(out1 out2 out3))

;; code which I would love to execute in the body
(print "something1" out1)
(print "something2" out2)
(print "something3" out3)

宏调用应扩展的内容:

(with-open-files (*handlers* *paths* '(:direction :output :if-exists :append))
  ;; the third macro argument should be what should be passed to the
  ;; individual `with-open-file` calls
  ;; and it might be without `quote`-ing or with `quote`-ing
  ;; - is there by the way a good-practice for such cases? -
  ;; - is it recommended to have `quote`-ing? Or how would you do that? -
  ;; and then follows the code which should be in the macro body:
  (print "something1" out1)
  (print "something2" out2)
  (print "something3" out3))

作为一个步骤,我认为我必须使一个s表达式包装另一个s表达式。

我的第一个问题是:如何用另一个s表达式包装一个s表达式?但是我现在无法管理它。 我所能做的就是编写一个只会溢出未执行表达式的函数。

如何编写一个宏,该宏既可以执行同样的操作,又可以在执行后扩展代码?

(with-open-file (out1 "~/out1.lisp" :direction :output :if-exists :append)
  (with-open-file (out2 "~/out2.lisp" :direction :output :if-exists :append)
    (with-open-file (out3 "~/out3.lisp" :direction :output :if-exists :append)
      (print "something1" out1)
      (print "something2" out2)
      (print "something3" out3))))

哪个给:

(defun wrap (s-expr-1 s-expr-2)
  (append s-expr-1 (list s-expr-2)))

(wrap '(func1 arg1) '(func2 arg2))
;; => (FUNC1 ARG1 (FUNC2 ARG2))

(wrap '(with-open-files (out1 "~/out1.lisp" :direction :output :if-exists :append))
  '(with-open-files (out2 "~/out2.lisp" :direction :output :if-exists :append) 
      (print "something1" out1)
      (print "something2" out2)
      (print "something3" out3)))

这样,依次应用(WITH-OPEN-FILES (OUT1 "~/out1.lisp" :DIRECTION :OUTPUT :IF-EXISTS :APPEND) (WITH-OPEN-FILES (OUT2 "~/out2.lisp" :DIRECTION :OUTPUT :IF-EXISTS :APPEND) (PRINT "something1" OUT1) (PRINT "something2" OUT2) (PRINT "something3" OUT3))) 函数,遍历输入列表,也许可以构建代码...

但是,这些函数只会生成代码,而不执行代码。 最后,我将被迫使用wrap函数来评估已构建的代码……(但是不知何故,我不应该这样做。而且我只是不太了解如何编写)可以执行此类操作的宏...实际上,宏用于解决此类问题...

在执行死刑时,我遇到了大麻烦。而且由于无法在宏(而不是函数名)上调用evalfuncall,所以我看不到一种明显的解决方案。有人遇到过这种情况吗?

当通过另一个s表达式将s表达式包装在宏中并对其进行评估时,下一个问题将是如何处理列表以使代码扩展为所需的代码,然后进行评估?我只是试了几个小时而没走很远。

我需要有经验的人编写此类宏的帮助...

1 个答案:

答案 0 :(得分:6)

请注意,在Lisp中,“处理程序”通常是函数,而不是符号。您的命名令人困惑。

静态

如果要生成 code ,则应使用 macros ,而不是函数。 假设您在编译时知道哪些文件和流 您将使用的变量:

最简单的方法是使用递归:

(defmacro with-open-files ((streams file-names &rest options &key &allow-other-keys) &body body)
  (if (and streams file-names)
      `(with-open-file (,(pop streams) ,(pop file-names) ,@options)
         (with-open-files (,streams ,file-names ,@options)
           ,@body))
      `(progn ,@body)))

测试:

(macroexpand-1
 '(with-open-files ((a b c) ("f" "g" "h") :direction :output :if-exists :supersede)
   (print "a" a)
   (print "b" b)
   (print "c" c)))
==>
(WITH-OPEN-FILE (A "f" :DIRECTION :OUTPUT :IF-EXISTS :SUPERSEDE)
  (WITH-OPEN-FILES ((B C) ("g" "h") :DIRECTION :OUTPUT :IF-EXISTS :SUPERSEDE)
    (PRINT "a" A) (PRINT "b" B) (PRINT "c" C)))

(macroexpand-1
 '(with-open-files ((a) ("f") :direction :output :if-exists :supersede)
   (print "a" a)))
==>
(WITH-OPEN-FILE (A "f" :DIRECTION :OUTPUT :IF-EXISTS :SUPERSEDE)
  (WITH-OPEN-FILES (NIL NIL :DIRECTION :OUTPUT :IF-EXISTS :SUPERSEDE)
    (PRINT "a" A)))

(macroexpand-1
 '(with-open-files (nil nil :direction :output :if-exists :supersede)
   (print nil)))
==>
(PROGN (PRINT NIL))

动态

如果您在编译时不知道什么是流和文件,例如, 它们存储在*handler*变量中,您不能使用简单 上面的宏-您将必须使用 progv用于绑定和 gensym避免变量 捕获。请注意,反引号内的let如何避免多个 评估(即,参数streamsfile-namesoptions是 被评估一次(一次,不会多次):

(defmacro with-open-files-d ((streams file-names &rest options &key &allow-other-keys) &body body)
  (let ((sv (gensym "STREAM-VARIABLES-"))
        (so (gensym "STREAM-OBJECTS-"))
        (ab (gensym "ABORT-"))
        (op (gensym "OPTIONS-")))
    `(let* ((,sv ,streams)
            (,ab t)
            (,op (list ,@options))
            (,so (mapcar (lambda (fn) (apply #'open fn ,op)) ,file-names)))
       (progv ,sv ,so
         (unwind-protect (multiple-value-prog1 (progn ,@body) (setq ,ab nil))
           (dolist (s ,so)
             (when s
               (close s :abort ,ab))))))))

(macroexpand-1
 '(with-open-files-d ('(a b c) '("f" "g" "h")  :direction :output :if-exists :supersede)
   (print "a" a)
   (print "b" b)
   (print "c" c)))
==>
(LET* ((#:STREAM-VARIABLES-372 '(A B C))
       (#:ABORT-374 T)
       (#:OPTIONS-375 (LIST :DIRECTION :OUTPUT :IF-EXISTS :SUPERSEDE))
       (#:STREAM-OBJECTS-373
        (MAPCAR (LAMBDA (FN) (APPLY #'OPEN FN #:OPTIONS-375)) '("f" "g" "h"))))
  (PROGV
      #:STREAM-VARIABLES-372
      #:STREAM-OBJECTS-373
    (UNWIND-PROTECT
        (MULTIPLE-VALUE-PROG1 (PROGN (PRINT "a" A) (PRINT "b" B) (PRINT "c" C))
          (SETQ #:ABORT-374 NIL))
      (DOLIST (S #:STREAM-OBJECTS-373)
        (WHEN S
          (CLOSE S :ABORT #:ABORT-374))))))

运行时这两个流变量和文件列表 进行评估。

重要

此处重要的实用说明是,静态版本更健壮,因为它保证所有流都被关闭,而动态版本将无法关闭其余流,例如,如果第一个close引发异常(此可以是固定的,但不是琐碎的:我们不能只是ignore-errors,因为实际上应该报告它们,而是哪个应该报告错误?&c&c)。

另一个观察结果是,如果在编译时未知流变量列表,则使用它们的body中的代码将无法正确编译(变量将使用动态绑定&c进行编译),如下所示undefined-variable编译时警告。

基本上,动态版本是宏观的练习,而静态版本则是您在实践中应该使用的版本。

您的具体情况

如果我正确理解了您的要求,则可以执行以下操作 这个(未经测试!):

(defun process-A-line (line stream)
  do something with line,
  stream is an open output stream)

(defun process-file (input-file processors)
  "Read input-file line by line, calling processors,
which is a list of lists (handler destination ...):
 handler is a function like process-A-line,
 destination is a file name and the rest is open options."
  (with-open-file (inf input-file)
    (let ((proc-fd (mapcar (lambda (p)
                             (cons (first p)
                                   (apply #'open (rest p))))
                           processors))
          (abort-p t))
      (unwind-protect
           (loop for line = (read-line inf nil nil)
             while line
             do (dolist (p-f proc-fd)
                  (funcall (car p-f) line (cdr p-f)))
             finally (setq abort-p nil))
        (dolist (p-f proc-fd)
          (close (cdr p-f) :abort abort-p))))))