Racket Macro如何将Ellipses传递给Helper功能?(Racket Macro How to Pass Ellipses to Helper function?)

编程入门 行业动态 更新时间:2024-10-22 16:46:37
Racket Macro如何将Ellipses传递给Helper功能?(Racket Macro How to Pass Ellipses to Helper function?)

鉴于:

(define-syntax (test stx) (syntax-case stx () [(_ body ...) (with-syntax ([body0 (process-body #'(body ...))]) #'body0)]))

我应该如何在助手中收到模式和省略号? 我甚至不确定是否包裹身体...内部()是正确的,但我已经看到它,它是唯一不会崩溃的东西。

process-body过程最终会使用extra()包装它的语法。 我可以试着打破这一点,但我只是想知道这样做的正确方法是什么。

process-body使用一些代码在之前和之后包装body模式。 并且,与define类似,我希望能够在一个列表中为宏提供多个表单而不是所有表单。 因此,如果给定(form1)(form2),其中form2是省略号,则process-body应该(do-something)(form1)(form2)(do-something-else)。

(define-for-syntax (process-body body-syntax) (with-syntax ([stx body-syntax]) (syntax/loc body-syntax (λ (request) stx))))

当然,当我在线定义模板时,我有这个工作,我想我可以在这里做,但有时模板变得笨拙,并且调用帮助器很好。

非常感谢。

作为尝试dyoo的第一个例子的编辑,我提供以下内容:

#lang racket (define-syntax (test2 stx) (syntax-case stx () [(_ body ...) (with-syntax ([(body0 ...) (process-body2 #'(body ...))]) #'(begin body0 ...))])) (define-for-syntax (process-body2 bodies) (with-syntax ([(body ...) bodies]) (syntax/loc bodies (λ (request) body ...)))) (test2 (print "hi"))

λ:语法错误

Given:

(define-syntax (test stx) (syntax-case stx () [(_ body ...) (with-syntax ([body0 (process-body #'(body ...))]) #'body0)]))

How should I receive the pattern and the ellipses in the helper? I'm not even sure if wrapping the body ... inside () is correct, but I've seen it around and it's the only thing that doesn't crash.

The process-body procedure ends up with syntax that has extra () wrapping it. I can try and break this apart, but I'm just wondering what the correct way to do this is.

process-body wraps the body pattern with some code before AND after. And, similar to define, I want to be able to provide the macro with multiple forms rather than all forms in one list. So, if given (form1) (form2), where form2 is the ellipses, process-body should (do-something) (form1) (form2) (do-something-else).

ie,

(define-for-syntax (process-body body-syntax) (with-syntax ([stx body-syntax]) (syntax/loc body-syntax (λ (request) stx))))

Of course I have this working when I define the template in-line, and I suppose I could do that here, but sometimes the template becomes unwieldy and it's nice to call a helper.

Thanks a lot.

As an edit to try dyoo's first example, I'm providing the following:

#lang racket (define-syntax (test2 stx) (syntax-case stx () [(_ body ...) (with-syntax ([(body0 ...) (process-body2 #'(body ...))]) #'(begin body0 ...))])) (define-for-syntax (process-body2 bodies) (with-syntax ([(body ...) bodies]) (syntax/loc bodies (λ (request) body ...)))) (test2 (print "hi"))

λ: bad syntax

最满意答案

with-syntax模式的左侧也可以有省略号,因此可以使用以下内容:

(define-syntax (test stx) (syntax-case stx () [(_ body ...) (with-syntax ([(body0 ...) (process-body #'(body ...))]) #'(begin body0 ...))]))

基本思想是如果process-body返回转换后的body元素,我们就可以将它们与begin一起引入。


您的process-body定义也可以使用带有省略号with-syntax 。 所以你可以这样做:

(define-for-syntax (process-body bodies) (with-syntax ([(body ...) bodies]) (syntax/loc bodies (λ (request) body ...))))

如果这是process-body的定义,我们应该修改test因为process-body的结果形状现在是一个完整的lambda表达式,所以我们可以直接返回它的结果:

(define-syntax (test stx) (syntax-case stx () [(_ body ...) (process-body (syntax/loc stx (body ...)))]))

作为一个独立的例子:

#lang racket (define-syntax (test stx) (syntax-case stx () [(_ body ...) (process-body (syntax/loc stx (body ...)))])) (define-for-syntax (process-body bodies) (with-syntax ([(body ...) bodies]) (syntax/loc bodies (λ (request) (printf "before the body\n") body ... (printf "after the body\n"))))) ;; Let's try it: (define p (test (displayln "hello") (displayln "world"))) (p 'should-be-a-request)

The left hand side of a with-syntax pattern can also have ellipses, so that the following is possible:

(define-syntax (test stx) (syntax-case stx () [(_ body ...) (with-syntax ([(body0 ...) (process-body #'(body ...))]) #'(begin body0 ...))]))

The basic idea is that if process-body returns the transformed body elements, we can then introduce them all together with a begin.


Your process-body definition can also use with-syntax with ellipses too. So you can do something like this:

(define-for-syntax (process-body bodies) (with-syntax ([(body ...) bodies]) (syntax/loc bodies (λ (request) body ...))))

If that's the definition of process-body, we should amend test since the shape of the result from process-body is now a complete lambda expression, so we can just return its result directly:

(define-syntax (test stx) (syntax-case stx () [(_ body ...) (process-body (syntax/loc stx (body ...)))]))

As a self-contained example:

#lang racket (define-syntax (test stx) (syntax-case stx () [(_ body ...) (process-body (syntax/loc stx (body ...)))])) (define-for-syntax (process-body bodies) (with-syntax ([(body ...) bodies]) (syntax/loc bodies (λ (request) (printf "before the body\n") body ... (printf "after the body\n"))))) ;; Let's try it: (define p (test (displayln "hello") (displayln "world"))) (p 'should-be-a-request)

更多推荐

本文发布于:2023-08-02 19:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379650.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   功能   Racket   Macro   Ellipses

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!