如何在麻省理工学院方案中扩展宏

发布于 2025-02-02 23:54:38 字数 394 浏览 4 评论 0原文

我写了一个简单的宏:

(define-syntax myif
  (syntax-rules ()
    ((_ condition a b)
     (if condition a b))))

用法示例:(myif#t“ yes”“否”)

在MIT方案中,如何显示上述示例的宏扩展?是否有类似于Common Lisp的MacroexpandMacroexpand-1或球拍的展开Expand-once-once

(麻省理工学院方案版本:11.2)

I have written a simple macro:

(define-syntax myif
  (syntax-rules ()
    ((_ condition a b)
     (if condition a b))))

Usage example: (myif #t "yes" "no").

In MIT Scheme, how do I show the macro expansion of the example above? Is there something similar to Common Lisp's macroexpand and macroexpand-1 or Racket's expand and expand-once?

(MIT Scheme version: 11.2)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不喜欢何必死缠烂打 2025-02-09 23:54:38
% cat macro.scm 

(define-syntax myif
  (syntax-rules ()
    ((_ condition a b)
     (if condition a b))))


% mit-scheme --silent
(sf "macro.scm")

;Generating SCode for file: "macro.scm" => "macro.bin"...
;  This program does not have a USUAL-INTEGRATIONS declaration.
;  Without this declaration, the compiler will be unable to perform
;  many optimizations, and as a result the compiled program will be
;  slower and perhaps larger than it could be.  Please read the MIT
;  Scheme User's Guide for more information about USUAL-INTEGRATIONS.
;... done
(pp (fasload "macro.bin"))
;Loading "macro.bin"... done
(define-syntax myif
  (er-macro-transformer
   (lambda (form rename compare)
     (if (and (pair? form)
              (let ((temp (cdr form)))
                (and (pair? temp)
                     (let ((temp (cdr temp)))
                       (and (pair? temp)
                            (let ((temp (cdr temp)))
                              (and (pair? temp)
                                   (null? (cdr temp)))))))))
         (list (rename 'if)
               (car (cdr form))
               (car (cdr (cdr form)))
               (car (cdr (cdr (cdr form)))))
         (ill-formed-syntax form)))))

将打印SCODE。这足以调试。

正如它在汇编警告中所表达的那样,重要的是不要激活优化,否则您将不会再将其字面翻译成SCODE。

这是我调试(不仅宏)的起点。

% cat macro.scm 

(define-syntax myif
  (syntax-rules ()
    ((_ condition a b)
     (if condition a b))))


% mit-scheme --silent
(sf "macro.scm")

;Generating SCode for file: "macro.scm" => "macro.bin"...
;  This program does not have a USUAL-INTEGRATIONS declaration.
;  Without this declaration, the compiler will be unable to perform
;  many optimizations, and as a result the compiled program will be
;  slower and perhaps larger than it could be.  Please read the MIT
;  Scheme User's Guide for more information about USUAL-INTEGRATIONS.
;... done
(pp (fasload "macro.bin"))
;Loading "macro.bin"... done
(define-syntax myif
  (er-macro-transformer
   (lambda (form rename compare)
     (if (and (pair? form)
              (let ((temp (cdr form)))
                (and (pair? temp)
                     (let ((temp (cdr temp)))
                       (and (pair? temp)
                            (let ((temp (cdr temp)))
                              (and (pair? temp)
                                   (null? (cdr temp)))))))))
         (list (rename 'if)
               (car (cdr form))
               (car (cdr (cdr form)))
               (car (cdr (cdr (cdr form)))))
         (ill-formed-syntax form)))))

will print the Scode. This is enough for debugging.

As it's expressed in the warning of compilation, it is important not to activate the optimizations, otherwise you won't see any more the literal translation into Scode.

This is the starting point when I debug (not only macros).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文