什么是"3D语法"? [英] What is "3D syntax"?

查看:112
本文介绍了什么是"3D语法"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写Racket宏的情况下,"3D语法"是什么意思?

In the context of writing Racket macros, what does "3D syntax" mean?

我已经听过几次这个短语.包括一次引用宏 I 的过程.但这是前一阵子.我修复了它,现在我已经不记得最初在做什么错了.

I've heard the phrase a few times. Including once in reference to a macro I was writing. But that was awhile ago; I fixed it, and now I can't remember exactly what I was doing wrong originally.

还:3D语法是否总是错误?还是像eval(如果您认为需要使用它,可能是错误的,但是在专家手中有一些有效用法)?

Also: Is 3D syntax always bad? Or is it like eval (where if you think you need to use it, you're probably wrong, but there are some valid uses in expert hands)?

推荐答案

语法对象通常应该只是

Syntax objects are usually supposed to be just serializable data. 3D-syntax weakens this condition: it allows us to sneak in arbitrary values, and not just plain data. That's what makes them "3d": they are values that rise above the regular flat things you'd expect out of syntax objects.

例如,我们可以潜入lambda值!

For example, we can sneak in lambda values!

#lang racket

(define ns (make-base-namespace))
(define (set-next! n)
  (parameterize ([current-namespace ns])
    (eval #`(define next #,n))))    ;; <--  3d-syntax here

(define (compute s)
  (parameterize ([current-namespace ns])
    (eval s)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define counter 0)
(set-next! (lambda ()
             (set! counter (add1 counter))
             counter))

(compute '(+ (next)
             (next)
             (next)
             (next)))

执行此操作通常是一件坏事,因为此类值的存在可能意味着在编译阶段泄漏信息的错误依据.结果是可能无法单独编译.如果您看到的错误听起来像是:

Doing this is usually a bad thing, because the presence of such values probably means an ill-founded attempt to leak information across phases of compilation. The result is something that's likely not separately-compilable. If you see an error that sounds something like:

write: cannot marshal value that is embedded in compiled code value

那么这很可能是由于宏产生了一段无法序列化为字节码的3d语法.

then that is most likely due to a macro having produced a piece of 3d-syntax that can't be serialized to bytecode.

有时候,在极少数情况下,我们确实确实希望在动态评估环境中使用3d语法.举一个具体的例子,DrRacket中的调试器可能想注释程序的语法,以便函数应用程序直接回调调试器的函数,以便我们可以在程序编辑器中进行交互式代码覆盖着色等操作.从这个意义上讲,3d语法可以充当动态评估的代码与其周围环境之间的沟通渠道.

Sometimes, in rare situations, we really do want 3d-syntax, often in dynamic evaluation contexts. As a concrete example, a debugger in DrRacket may want to annotate the syntax of a program so that function applications directly call back into functions of the debugger, so that we can do things like interactive code coverage coloring in the program editor. In that sense, 3d-syntax can act as a communication channel between dynamically-evaluated code and its ambient environment.

这篇关于什么是"3D语法"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆