2

我正在尝试编写一个宏来定义数组结构数据结构的一些帮助程序(基于代码段)。在该宏中,我定义了另一个宏,它有助于遍历 struct 中的所有槽值。问题是我无法使双重取消引用正常工作。这是代码:

(defmacro defcomponent (name-and-options &body slots)
  (setf name-and-options (ensure-list name-and-options))
  (let ((struct (first name-and-options))
        (slot-names (iter (for s in slots)
                          (collecting
                           (ematch s
                             ((or (and name (symbol)
                                       (<> _ '*)
                                       (<> _ nil))
                                  (list* name _ (plist :type _ :read-only _)))
                              name))))))
    `(progn (defstruct ,name-and-options
              ;; some task-specific stuff omitted here
              )
            (defmacro ,(symbolicate 'with- struct) (components &rest body)
              `(loop
                 ,@',(iter (for s in slot-names)
                          (appending `(for ,s across (,(symbolicate struct '- s) components))))
                 do ,@body)))))

因此,例如(defcomponent buzz x y)宏扩展为

(PROGN
 (DEFSTRUCT (BUZZ)
   X Y)  ;; details omitted
 (DEFMACRO WITH-BUZZ (COMPONENTS &REST BODY)
   `(LOOP ,@'(FOR X ACROSS (BUZZ-X COMPONENTS) FOR Y ACROSS (BUZZ-Y COMPONENTS))
          DO ,@BODY))

这有点工作,但我想访问components内部with-buzz宏的参数,即像这样

(DEFMACRO WITH-BUZZ (COMPONENTS &REST BODY)
   `(LOOP FOR X ACROSS (BUZZ-X ,COMPONENTS) FOR Y ACROSS (BUZZ-Y ,COMPONENTS)
          DO ,@BODY))

我怎么可能做到这一点?我已经尝试了很多的组合,,@无济于事。

4

2 回答 2

4

有时不使用反引号模式会有所帮助。然后在编译器的帮助下可以更容易地理解范围问题,这会警告通常的变量范围问题。

作为一个稍微简化的练习,我们将编写一个生成代码的函数。生成的代码是一个宏定义,它本身会生成代码。

(defun makeit (name slots)
  (labels ((symbolicate (pattern &rest things)
             (intern (apply #'format nil pattern things)))
           (compute-for-clauses (slots)
             (loop for s in slots
                   append (list ''for (list 'quote s)
                                ''across (list 'list
                                               (list 'quote
                                                     (symbolicate "~a-~a" name s))
                                               'components)))))

    (list 'progn
          (list 'defmacro
                (symbolicate "WITH-~a" name)
                '(components &rest body)
                (append '(list* 'loop)
                        (compute-for-clauses slots)
                        (list ''do 'body))))))

例子

CL-USER 51 > (pprint (makeit 'buzz '(x y)))

(PROGN
  (DEFMACRO WITH-BUZZ (COMPONENTS &REST BODY)
    (LIST* 'LOOP
           'FOR
           'X
           'ACROSS
           (LIST 'BUZZ-X COMPONENTS)
           'FOR
           'Y
           'ACROSS
           (LIST 'BUZZ-Y COMPONENTS)
           'DO
           BODY)))

CL-USER 52 > (eval *)
NIL

CL-USER 53 > (macroexpand-1 '(with-buzz a (+ 12) (+ 30)))
(LOOP FOR X ACROSS (BUZZ-X A) FOR Y ACROSS (BUZZ-Y A) DO (+ 12) (+ 30))
T
于 2019-11-09T10:53:05.320 回答
0

好吧,我已经设法通过手动构建列表 +eval而不是 quasiquoting 来做到这一点,但是亲爱的上帝之母,它看起来很自欺欺人。

;; skip
(defmacro ,(symbolicate 'with- struct) (components &rest body)
    (append
    '(loop)
    (eval
    `(iter (for s in ',',slot-names)
            (appending `(for ,s across (,(symbolicate ',',struct '- ,'s) ,,components)))))
    '(do)
    body))

我很乐意接受任何其他更惯用地解决问题的答案。

于 2019-11-08T16:19:34.037 回答