【问题标题】:How to create a macro that generates another macro in SISC / Scheme?如何在 SISC/Scheme 中创建一个生成另一个宏的宏?
【发布时间】:2014-07-06 06:17:32
【问题描述】:

在 Guile 或使用 SRFI-46 中,可以像 Specifying a Custom Ellipsis Identifier 中所示。但在 SISC 或“纯方案”R5RS 中是否可行?

我知道不使用省略号也是可能的,但是如果我需要像下面的示例那样使用内部省略号怎么办?

(define-syntax define-quotation-macros
  (syntax-rules ()
    ((_ (macro-name head-symbol) ...)
     (begin (define-syntax macro-name
              (syntax-rules ::: ()
                ((_ x :::)
                 (quote (head-symbol x :::)))))
            ...))))
(define-quotation-macros (quote-a a) (quote-b b) (quote-c c))
(quote-a 1 2 3) ⇒ (a 1 2 3)

【问题讨论】:

    标签: macros scheme r5rs guile srfi


    【解决方案1】:

    SISC 中使用的宏扩展器 psyntax 通过使用 ... 宏来支持一种不同的内省略方式。您可以通过将... 宏应用于您要使用的每个内椭圆来编写此代码:

    (define-syntax define-quotation-macros
      (syntax-rules ()
        ((_ (macro-name head-symbol) ...)
         (begin (define-syntax macro-name
                  (syntax-rules ()
                    ((_ x (... ...))
                     '(head-symbol x (... ...)))))
                ...))))
    

    或者您可以将其应用于外部形式,其中所有椭圆都应该是内部的:

    (define-syntax define-quotation-macros
      (syntax-rules ()
        ((_ (macro-name head-symbol) ...)
         (begin (define-syntax macro-name
                  (... (syntax-rules ()
                         ((_ x ...)
                          '(head-symbol x ...)))))
                ...))))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多