AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79529685
Accepted
Demihm Seinname
Demihm Seinname
Asked: 2025-03-24 04:57:27 +0800 CST2025-03-24 04:57:27 +0800 CST 2025-03-24 04:57:27 +0800 CST

是否可以通过 macroexpand(-1) 以外的方式清楚地表明 Common Lisp 宏的内部工作原理?

  • 772

我有这个宏:

(defmacro if (test-form &body body)
  (let ((test (gensym)))
    `(let ((,test (cl:if (booleanp ,test-form)
                         (cl-truth ,test-form)
                         ,test-form)))
       (cl:if ,test
              ,@body))))

使用它的结果是:

LO> (if (wordp (wordify ""))
        (print 1)
        (print 0))
NIL

(您可以看到,我在一个由lo组成的包中工作,但在 中。——该函数用于创建对应的 的实例。谓词检查谓词的输出是否为。然后返回第二个值。):usecommon-lispcl:if:shadowwordifymake-worddefstructbooleanp=> TRUE, t=> FALSE, nilcl-truth

macroexpand-1在这里没有用。它返回=> NIL, NIL

如果我尝试考虑可能的输出,我会发现destructuring-bind一个有用的工具:

(destructuring-bind (test-form &body body)
    '((wordp (wordify "")) (print 1) (print 0))
  (let ((test (gensym)))
    `(let ((,test (cl:if (booleanp ,test-form)
                         (cl-truth ,test-form)
                         ,test-form)))
       (cl:if ,test
              ,@body))))

输出为:

(LET ((#:G698
       (COMMON-LISP:IF (BOOLEANP (WORDP (WORDIFY "")))
                       (CL-TRUTH
                         (WORDP (WORDIFY "")))
                       (WORDP (WORDIFY "")))))
  (COMMON-LISP:IF #:G698
                  (PRINT
                    1)
                  (PRINT
                    0)))

结果就是期望的:

1
; No value

print(这是使用princ没有返回值的习惯。)

你能告诉我为什么宏的行为不同吗?

非常感谢。

编辑:

只是添加一个可能更合适的宏版本,但没有解决奇怪的行为。

(defmacro if-c (test-form instructionlist1 &optional instructionlist2)
  (let ((test (gensym)))
    `(let (;; The general Logo testform will return => TRUE, T / FALSE, NIL
           (,test (destructuring-bind (logo &optional cl)
                      (multiple-value-list ,test-form)
                    (cl:if (booleanp logo)
                           cl
                           (error "if doesn't like ~a as input"
                                  logo)))))
       (cl:if ,test
              ,instructionlist1
              ,instructionlist2))))    

编辑2:

我忘了说了:是的,奇怪的行为已经解决了。简单地说,包上方的宏LC并没有在包中展开,LO而是展开了下面的宏:

(defmacro if (test-form &body body)
  ())

LO在我决定将它移至 之前,它是我准备定义它的地方的残留部分LC。

macros
  • 1 1 个回答
  • 68 Views

1 个回答

  • Voted
  1. Best Answer
    Gwang-Jin Kim
    2025-03-25T00:08:14+08:002025-03-25T00:08:14+08:00

    MACROEXPAND-1实际上足以研究 Common Lisp 中的宏扩展。

    在宏扩展期间有可能使用*MACROEXPAND-HOOK*来让事情发生。

    我正在考虑TRACE宏。

    可以打印出宏调用期间发生的宏扩展。(还可以计算扩展的深度。)

    (defun tracing-macroexpand-hook (expander form &optional env)
        (format t "[expanding] ~S~%" form)
        (let ((result (funcall expander form env)))
          (format t "=> ~S~%" result)
          result))
    
    ;; for convenience, let's define also a `with-macroexpansion-trace`
    (defmacro with-macroexpansion-trace (&body body)
      `(let ((*macroexpand-hook* #'tracing-macroexpand-hook))
         ,@body))
    

    现在,你可以定义一些在层中调用的宏:

    (defmacro m1 (x)
      `(m2 (+ ,x 1)))
    
    (defmacro m2 (x)
      `(m3 (* ,x 2)))
    
    (defmacro m3 (x)
      `(+ ,x 42))
    

    我们可以去:

    (with-macroexpansion-trace
      (macroexpand '(m1 5)))
    

    输出结果如下:

    [expanding] (M1 5)
    => (M2 (+ 5 1))
    [expanding] (M2 (+ 5 1))
    => (M3 (* (+ 5 1) 2))
    [expanding] (M3 (* (+ 5 1) 2))
    => (+ (* (+ 5 1) 2) 42)
    (+ (* (+ 5 1) 2) 42) ;
    T
    

    您现在还可以定义一个with-macroexpansion-result适用的with-macroexpansion-trace但最后还会显示宏扩展后的评估结果 - 并计算总宏调用次数并返回结果:

    (defmacro with-macroexpansion-result (form &key (evaluate nil))
      `(let ((*macroexpand-hook* #'tracing-macroexpand-hook))
         (let ((expanded (macroexpand ',form)))
           ,(if evaluate
                `(let ((result (eval expanded)))
                   (format t "~&[result] ~S~%" result)
                   result)
                'expanded))))
    

    让我们尝试一下:

    (with-macroexpansion-result (m1 5) :evaluate t)
    
    ;; printing out:
    [expanding] (M1 5)
    => (M2 (+ 5 1))
    [expanding] (M2 (+ 5 1))
    => (M3 (* (+ 5 1) 2))
    [expanding] (M3 (* (+ 5 1) 2))
    => (+ (* (+ 5 1) 2) 42)
    [result] 54
    54
    
    
    (defmacro with-macroexpansion-result (form &key (evaluate nil))
      `(let ((macro-call-count 0))
         (flet ((hook (expander f &optional env)
                  (incf macro-call-count)
                  (format t "~&[expanding] ~S~%" f)
                  (let ((result (funcall expander f env)))
                    (format t "=> ~S~%" result)
                    result)))
           (let ((*macroexpand-hook* #'hook))
             (let ((expanded (macroexpand ',form)))
               (format t "~&[macro-calls-total] ~D~%" macro-call-count)
               ,(if evaluate
                    `(let ((result (eval expanded)))
                       (format t "~&[result] ~S~%" result)
                       result)
                    'expanded))))))
    
    (with-macroexpansion-result (m1 5) :evaluate t)
    [expanding] (M1 5)
    => (M2 (+ 5 1))
    [expanding] (M2 (+ 5 1))
    => (M3 (* (+ 5 1) 2))
    [expanding] (M3 (* (+ 5 1) 2))
    => (+ (* (+ 5 1) 2) 42)
    [macro-calls-total] 3
    [result] 54
    54
    
    

    我想这就是你想要的工具,不是吗?某种宏跟踪。

    • 2

相关问题

  • ImageJ 宏帮助 - 将 ROI 标签存储到摘要中

  • CL 宏无法正确解析

  • 为什么不能定义名称为“if”的Scheme宏?

  • 使用 nnkDotExpr 在 Nim 中创建一个 ident

  • 1920x1080 中的 XY 像素坐标转换为 1366x768 的公式

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve