Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.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 为什么这些嵌套宏无法在导入这些宏的包中创建绑定?_Macros_Common Lisp - Fatal编程技术网

Macros 为什么这些嵌套宏无法在导入这些宏的包中创建绑定?

Macros 为什么这些嵌套宏无法在导入这些宏的包中创建绑定?,macros,common-lisp,Macros,Common Lisp,我试图构造一个函数模板,我可以在具有包特定参数的其他包中使用它。我试图实现这一目标的要点如下: ;;; 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) `(

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

;;; 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
线程中未处理的简单错误#:
CAS的位置无效:CNC-HOST/FSM::FLAG->CNC-HOST/FSM::FLAG

这个构造是如何正确完成的?

在收到freenode lisp通道的反馈后,我清楚地意识到宏构造是按预期工作的:

(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")))
编译器的消息让我觉得宏中有错误——相反,我在用例
控件中忽略了这一点
应该返回函数调用结果,而不是
lambda