使用动态分段的建筑代码
Building Code with Dynamic Staging
摘要:
当创建一个新的领域特定语言(DSL)时,通常将其作为灵活的宿主语言的一部分嵌入,而不是完全从头开始创建。嵌入式DSL(EDSL)的语义要么直接作为一组函数(浅嵌入)给出,要么构造一个AST,然后进行处理(深嵌入)。通常,当EDSL以AST转换的形式指定特定于域的优化(DSO)时,使用深度嵌入。在本文中,我们表明,深度嵌入是没有必要指定大多数优化。我们将语言语义定义为在解析过程中执行的动作函数。这些动作逐步构建一个新的、任意复杂的程序函数。EDSL设计器能够将语义的许多方面指定为可运行代码,例如变量范围规则、自定义类型检查、任意控制流结构或DSO。一个足够强大的staging机制有助于从不同的动作中组装代码,并在任意多个阶段中评估语义。最后,我们得到的代码与手工编写的代码一样高效。我们从不创建代码的任何对象表示。不使用外部遍历算法来处理代码。所有的程序片段都是函数,它们的整个语义都嵌入在函数体中。这种方法允许在EDSL和宿主语言之间重用代码,以及组合许多不同EDSL的操作。
英文摘要:
When creating a new domain-specific language (DSL) it is common to embed it as a part of a flexible host language, rather than creating it entirely from scratch. The semantics of an embedded DSL (EDSL) is either given directly as a set of functions (shallow embedding), or an AST is constructed that is later processed (deep embedding). Typically, the deep embedding is used when the EDSL specifies domain-specific optimizations (DSO) in a form of AST transformations. In this paper we show that deep embedding is not necessary to specify most optimizations. We define language semantics as action functions that are executed during parsing. These actions build incrementally a new, arbitrary complex program function. The EDSL designer is able to specify many aspects of the semantics as a runnable code, such as variable scoping rules, custom type checking, arbitrary control flow structures, or DSO. A sufficiently powerful staging mechanism helps assembling the code from different actions, as well as evaluate the semantics in arbitrarily many stages. In the end, we obtain code that is as efficient as one written by hand. We never create any object representation of the code. No external traversing algorithm is used to process the code. All program fragments are functions with their entire semantics embedded within the function bodies. This approach allows reusing the code between EDSL and the host language, as well as combining actions of many different EDSLs.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。