gpt4 book ai didi

macros - 为什么这些嵌套宏无法在导入这些宏的包中创建绑定(bind)?

转载 作者:行者123 更新时间:2023-12-04 09:26:06 29 4
gpt4 key购买 nike

我尝试构建一个函数模板,我可以在具有特定于包的参数的其他包中使用该模板。我试图实现这一目标的要点如下:

;;; FSM

(in-package #:fsm)

(defmacro %cas (flag old new)
#+sbcl `(sb-ext:compare-and-swap ,flag ,old ,new)
#+ecl `(mp:compare-and-swap ,flag ,old ,new)
)

(defmacro control! (fsm task flag)
`(let ((*task-category* (tag ,task)))
(unless (%cas ,flag nil t)
(lambda () (submit-task (channel (cqueue-prio-out ,fsm)) (fn ,task))))))


;;; REPL

(in-package #:repl)

(defparameter *controller-active* nil)

(control! fsm control-task *controller-active*)

;;; USB-SP

(in-package #:usb-sp)

(defparameter *controller-active* nil)

(control! fsm control-task *controller-active*)

显然,这不起作用:
Unhandled SIMPLE-ERROR in thread #<SB-THREAD:THREAD "main thread" RUNNING {1001640703}>:
Invalid place to CAS: CNC-HOST/FSM::FLAG -> CNC-HOST/FSM::FLAG
这个结构是如何正确完成的?

最佳答案

在收到有关 freenode lisp channel 的反馈后,我很清楚宏构造按预期工作:

(defpackage #:fsm    (:use #:cl)       (:export #:control!! #:%cas))
(defpackage #:repl (:use #:cl #:fsm) (:export #:test-in-repl))
(defpackage #:usb-sp (:use #:cl #:fsm) (:export #:test-in-usb-sp))

;;; FSM

(in-package #:fsm)

(defmacro %cas (flag old new)
#+sbcl `(sb-ext:compare-and-swap ,flag ,old ,new)
#+ecl `(mp:compare-and-swap ,flag ,old ,new))

(defmacro control!! (flag pkg)
`(lambda () (if (%cas ,flag nil t)
(format nil "~A : skip task" ,pkg)
(format nil "~A : task run" ,pkg))))


;;; REPL

(in-package #:repl)

(defparameter *controller-active* nil)
(defun test-in-repl (pkg) (funcall (control!! *controller-active* pkg)))
(assert (string= "repl : task run" (test-in-repl "repl")))
(assert *controller-active*)

;;; USB-SP

(in-package #:usb-sp)

(defparameter *controller-active* nil)
(defun test-in-usb-sp (pkg) (funcall (control!! usb-sp::*controller-active* pkg)))
(assert (string= "usb-sp : task run" (test-in-usb-sp "usb-sp")))
(assert *controller-active*)

(in-package #:cl-user)

(assert (string= "repl : skip task" (repl:test-in-repl "repl")))
(assert (string= "usb-sp : skip task" (usb-sp:test-in-usb-sp "usb-sp")))
编译器消息让我认为我在宏中有一个错误 - 相反,我在我的用例 control!! 中忽略了这一点应该返回函数调用结果而不是 lambda .

关于macros - 为什么这些嵌套宏无法在导入这些宏的包中创建绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63001750/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com