非 C++ 生成式编程语言?

发布于 2024-07-05 01:10:59 字数 272 浏览 12 评论 0原文

C++ 可能是静态元编程Java不支持

除了C++之外还有其他语言支持生成式编程(创建程序的程序)吗?

C++ is probably the most popular language for static metaprogramming and Java doesn't support it.

Are there any other languages besides C++ that support generative programming (programs that create programs)?

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

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

发布评论

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

评论(15

乙白 2024-07-12 01:11:00

无论您使用什么语言,它们中的任何一种都能够进行异构生成元编程。 采用任何动态语言,例如 Python 或 Clojure,或者 Haskell(如果您是打字迷),并使用该宿主语言编写能够自行编译的模型转换成您的团队/雇主想要或强制使用的某种主流语言。

我发现对象图是内部模型表示的一个很好的模型。 该图可以在单个节点中混合属性和有序子图,这是属性语法和 AST 所固有的。 因此,对象图解释可以是宿主语言和目标语言之间的有效层,并且可以充当在数据结构上定义的某种无语法语言。

最接近的模型是 AST:以 Python(宿主语言)为目标描述 AST 树到 C++ 语法(目标语言):

# from metaL import *
class Object:
    def __init__(self, V):
        self.val = V
        self.slot = {}
        self.nest = []


class Module(Object):
    def cc(self):
        c = '// \ %s\n' % self.head(test=True)
        for i in self.nest:
            c += i.cc()
        c += '// / %s\n' % self.head(test=True)
        return c

hello = Module('hello')

# <module:hello> #a04475a2

class Include(Object):
    def cc(self):
        return '#include <%s.h>\n' % self.val

stdlib = Include('stdlib')
hello // stdlib

# <module:hello> #b6efb657
#     0: <include:stdlib> #f1af3e21

class Fn(Object):
    def cc(self):
        return '\nvoid %s() {\n}\n\n' % self.val

main = Fn('main')
hello // main

print(hello.cc())
// \ <module:hello>
#include <stdlib.h>

void main() {
}

// / <module:hello>

但是您不受构造对象图的抽象级别的限制:您不仅可以自由添加自己的类型,还可以对象图可以解释自身,因此可以像 Lisp 中的列表一样修改自身。

It does not matter what language you are using -- any of them is able to do Heterogeneous Generative Metaprogramming. Take any dynamic language such as Python or Clojure, or Haskell if you are a type-fan, and write models in this host language that are able to compile themself into some mainstream language you want or forced to use by your team/employer.

I found object graphs a good model for internal model representation. This graph can mix attributes and ordered subgraphs in a single node, which is native to attribute grammar and AST. So, object graph interpretation can be an effective layer between your host and target languages and can act itself as some sort of no-syntax language defined over data structures.

The closest model is an AST: describe AST trees in Python (host language) targets to C++ syntax (target language):

# from metaL import *
class Object:
    def __init__(self, V):
        self.val = V
        self.slot = {}
        self.nest = []


class Module(Object):
    def cc(self):
        c = '// \ %s\n' % self.head(test=True)
        for i in self.nest:
            c += i.cc()
        c += '// / %s\n' % self.head(test=True)
        return c

hello = Module('hello')

# <module:hello> #a04475a2

class Include(Object):
    def cc(self):
        return '#include <%s.h>\n' % self.val

stdlib = Include('stdlib')
hello // stdlib

# <module:hello> #b6efb657
#     0: <include:stdlib> #f1af3e21

class Fn(Object):
    def cc(self):
        return '\nvoid %s() {\n}\n\n' % self.val

main = Fn('main')
hello // main

print(hello.cc())
// \ <module:hello>
#include <stdlib.h>

void main() {
}

// / <module:hello>

But you are not limited with the level of abstraction of constructed object graph: you not only can freely add your own types but object graph can interpret itself, can thus can modify itself the same way as lists in a Lisp can do.

居里长安 2024-07-12 01:11:00

Lisp 支持某种形式的“元编程”,尽管与 C++ 模板元编程的意义不同。 另外,你的术语“静态”在这种情况下可能意味着不同的东西,但 Lisp 也支持静态类型,如果这就是你的意思的话。

Lisp supports a form of "metaprogramming", although not in the same sense as C++ template metaprogramming. Also, your term "static" could mean different things in this context, but Lisp also supports static typing, if that's what you mean.

你穿错了嫁妆 2024-07-12 01:11:00

“元编程”对于这个特定功能来说确实是一个不好的名字,至少当你讨论不止一种语言时,因为这个功能只需要一小部分语言,这些语言是:

  • 静态
  • 编译为机器语言
  • 为性能而进行了高度优化编译时
  • 可通过用户定义的数据类型(C++ 中的 OOP)进行扩展,
  • 非常流行,

去掉其中任何一种,“静态元编程”就没有意义了。 因此,如果任何远程主流语言具有类似 C++ 所理解的功能,我会感到惊讶。

当然,动态语言和几种函数式语言支持完全不同的概念,也可以称为元编程。

'metaprogramming' is really a bad name for this specific feature, at least when you're discussing more than one language, since this feature is only needed for a narrow slice of languages that are:

  • static
  • compiled to machine language
  • heavily optimised for performance at compile time
  • extensible with user-defined data types (OOP in C++'s case)
  • hugely popular

take out any one of these, and 'static metaprogramming', just doesn't make sense. therefore, i would be surprised if any remotely mainstream language had something like that, as understood on C++.

of course, dynamic languages, and several functional languages support totally different concepts that could also be called metaprogramming.

浪推晚风 2024-07-12 01:11:00

ML 语言系列是专门为此目的而设计的。 OCaml 最著名的成功案例之一是用于高性能 FFT 的 FFTW 库,该库几乎完全由 OCaml 生成的 C 代码程序。

干杯,
乔恩·哈罗普。

The ML family of languages were designed specifically for this purpose. One of OCaml's most famous success stories is the FFTW library for high-performance FFTs that is C code generated almost entirely by an OCaml program.

Cheers,
Jon Harrop.

蒲公英的约定 2024-07-12 01:11:00

大多数人试图找到一种具有“终极反思”的语言
用于自我检查和类似“eval”的东西用于具体化新代码。
这样的语言很难找到(LISP 就是一个主要的反例)
而且它们当然不是主流。

但另一种方法是使用一组可以检查的工具,
生成和操作程序代码。 头奖就是这样一个工具
专注于Java。 http://jackpot.netbeans.org/

我们的 DMS 软件重新设计工具包是
这样的工具,适用于 C、C++、C#、Java、COBOL、PHP、
Javascript、Ada、Verilog、VHDL 和各种其他语言。
(它使用生产质量前端使其能够读取
所有这些语言)。
更好的是,它可以同时使用多种语言来完成此操作。
请参阅http://www.semdesigns.com/Products/DMS/DMSToolkit.html

DMS 之所以成功,是因为它提供了常规方法和支持基础设施,可以像 AST 那样完全访问程序结构,并且在大多数情况下还提供符号表、类型信息、控制和数据流分析等附加数据,所有这些都是进行复杂程序操作所必需的。

Most people try to find a language that has "ultimate reflection"
for self-inspection and something like "eval" for reifying new code.
Such languages are hard to find (LISP being a prime counterexample)
and they certainly aren't mainstream.

But another approach is to use a set of tools that can inspect,
generate, and manipulate program code. Jackpot is such a tool
focused on Java. http://jackpot.netbeans.org/

Our DMS software reengineering toolkit is
such a tool, that works on C, C++, C#, Java, COBOL, PHP,
Javascript, Ada, Verilog, VHDL and variety of other languages.
(It uses production quality front ends to enable it to read
all these langauges).
Better, it can do this with multiple languages at the same instant.
See http://www.semdesigns.com/Products/DMS/DMSToolkit.html

DMS succeeds because it provides a regular method and support infrastructure for complete access to the program structure as ASTs, and in most cases additional data such a symbol tables, type information, control and data flow analysis, all necessary to do sophisticated program manipulation.

瞳孔里扚悲伤 2024-07-12 01:11:00

我推荐 Haskell。 这是一篇描述其编译时元编程功能的论文

I recommend Haskell. Here is a paper describing its compile-time metaprogramming capabilities.

零時差 2024-07-12 01:11:00

Haskell 中的大量工作:领域特定语言 (DSL)、可执行规范、程序转换、部分应用程序、分阶段计算。 几个帮助您入门的链接:

Lots of work in Haskell: Domain Specific Languages (DSL's), Executable Specifications, Program Transformation, Partial Application, Staged Computation. Few links to get you started:

庆幸我还是我 2024-07-12 01:11:00

"D" 编程语言 与 C++ 类似,但具有更好的性能元编程支持。 下面是仅使用编译时元编程编写的光线跟踪器的示例:

Ctrace

此外,还有一个名为“Concept GCC”的 gcc 分支,支持 C++ 不支持的元编程结构(至少目前还没有)。

概念 GCC

The "D" programming language is C++-like but has much better metaprogramming support. Here's an example of a ray-tracer written using only compile-time metaprogramming:

Ctrace

Additionally, there is a gcc branch called "Concept GCC" that supports metaprogramming contructs that C++ doesn't (at least not yet).

Concept GCC

浅听莫相离 2024-07-12 01:11:00

Common Lisp 支持以多种不同方式编写程序的程序。

1) 程序数据和程序“抽象语法树”是统一的(S 表达式!)

2) defmacro

3) Reader 宏。

4) MOP

其中,真正令人兴奋的是MOP。 阅读“元对象协议的艺术”。 我保证,这将为您带来改变!

Common Lisp supports programs that write programs in several different ways.

1) Program data and program "abstract syntax tree" are uniform (S-expressions!)

2) defmacro

3) Reader macros.

4) MOP

Of these, the real mind-blower is MOP. Read "The Art of the Metaobject Protocol." It will change things for you, I promise!

谁的年少不轻狂 2024-07-12 01:11:00

Nim 是一种相对较新的编程语言,它广泛支持静态元编程并生成高效的(类似 C++)编译代码。

http://nim-lang.org/

它支持编译时函数求值、类似 lisp 的 AST 代码转换通过宏、编译时反射、可以用任意值参数化的泛型类型以及可用于创建用户定义的高级类型感知窥孔优化的术语重写。 甚至可以在编译过程中执行外部程序,从而影响代码生成。 例如,考虑与本地运行的数据库服务器进行通信,以验证代码中的 ORM 定义(通过某些 DSL 提供)是否与数据库的架构匹配。

Nim is a relatively new programming language that has extensive support for static meta-programming and produces efficient (C++ like) compiled code.

http://nim-lang.org/

It supports compile-time function evaluation, lisp-like AST code transformations through macros, compile-time reflection, generic types that can be parametrized with arbitrary values, and term rewriting that can be used to create user-defined high-level type-aware peephole optimizations. It's even possible to execute external programs during the compilation process that can influence the code generation. As an example, consider talking to a locally running database server in order to verify that the ORM definition in your code (supplied through some DSL) matches the schema of the database.

难得心□动 2024-07-12 01:11:00

NemerleBoo 是我个人最喜欢的此类东西。 Nemerle 有一个非常优雅的宏语法,尽管它的文档很差。 Boo 的文档非常出色,但它的宏有点不太优雅。 然而,两者都工作得非常好。

两者都以 .NET 为目标,因此它们可以轻松地与 C# 和其他 .NET 语言互操作 - 如果您使用 IKVM,甚至可以与 Java 二进制文件互操作。

编辑:为了澄清,我指的是 Lisp 意义上的宏,而不是 C 的预处理器宏。 这些允许在编译时定义新语法和大量元编程。 例如,Nemerle 附带了宏,可以在编译时根据 SQL 服务器验证 SQL 查询。

Nemerle and Boo are my personal favorites for such things. Nemerle has a very elegant macro syntax, despite its poor documentation. Boo's documentation is excellent but its macros are a little less elegant. Both work incredibly well, however.

Both target .NET, so they can easily interoperate with C# and other .NET languages -- even Java binaries, if you use IKVM.

Edit: To clarify, I mean macros in the Lisp sense of the word, not C's preprocessor macros. These allow definition of new syntax and heavy metaprogramming at compiletime. For instance, Nemerle ships with macros that will validate your SQL queries against your SQL server at compiletime.

忘年祭陌 2024-07-12 01:11:00

模板元编程本质上是对模板机制的滥用。 我的意思是,你基本上从一个意外副作用的功能中得到了你所期望的东西——这是一团糟,并且(尽管工具正在变得更好)真正令人痛苦,因为语言不 你这样做(我应该指出,我在这方面最先进的经验已经过时了,因为我基本上放弃了这种方法。不过,我没有听说取得任何重大进展)

支持 98 年左右的这个问题促使我寻找更好的解决方案。 我可以编写依赖它的有用系统,但它们是地狱般的。 经过摸索最终我找到了 Common Lisp。 当然,模板机制是图灵完备的,但 Intercal 也是如此。

Common Lisp 的元编程“正确”。 在执行此操作时,您可以充分利用可用语言的功能,无需特殊语法,而且由于该语言非常动态,您可以用它做更多事情。

当然还有其他选择。 我使用过的任何其他语言都没有比 Lisp 更好的元编程能力,这就是我使用它来研究代码的原因。 尽管有很多理由让你想要尝试其他东西,但这一切都需要权衡。 你可以看看 Haskell/ML/OCaml 等。许多函数式语言都有一些接近 Lisp 宏的功能。 您可以找到一些针对 .NET 的内容,但它们都非常边缘(就用户群等而言)。 事实上,工业使用语言的大玩家都没有这样的东西。

Template metaprogramming is essentially abuse of the template mechanism. What I mean is that you get basically what you'd expect from a feature that was an unplanned side-effect --- it's a mess, and (although tools are getting better) a real pain in the ass because the language doesn't support you in doing it (I should note that my experience with state-of-the-art on this is out of date, since I essentially gave up on the approach. I've not heard of any great strides made, though)

Messing around with this in about '98 was what drove me to look for better solutions. I could write useful systems that relied on it, but they were hellish. Poking around eventually led me to Common Lisp. Sure, the template mechanism is Turing complete, but then again so is intercal.

Common Lisp does metaprogramming `right'. You have the full power of the language available while you do it, no special syntax, and because the language is very dynamic you can do more with it.

There are other options of course. No other language I've used does metaprogramming better than Lisp does, which is why I use it for research code. There are lots of reasons you might want to try something else though, but it's all going to be tradeoffs. You can look at Haskell/ML/OCaml etc. Lots of functional languages have something approaching the power of Lisp macros. You can find some .NET targeted stuff, but they're all pretty marginal (in terms of user base etc.). None of the big players in industrially used languages have anything like this, really.

氛圍 2024-07-12 01:11:00

让我列出一些关于元编程如何在 lisp 中工作的一些重要细节(或方案,或slate,或选择您最喜欢的“动态”语言):

  • 在 lisp 中进行元编程时,您不必处理两种语言。 元级代码是用与其生成的对象级代码相同的语言编写的。 元编程不限于两个级别,而且对大脑来说也更容易。
  • 在 lisp 中,您可以在运行时使用编译器。 事实上,编译时/运行时的区别感觉非常人为,并且很大程度上取决于您的观点。 在 lisp 中,只需一个函数调用,您就可以将函数编译为机器指令,从那时起您就可以将其用作第一类对象; 即它们可以是未命名的函数,您可以将其保存在局部变量或全局哈希表等中...
  • Lisp 中的非常简单:一堆函数填充在哈希表中并赋予编译器。 对于编译器要编译的每种形式,它都会查阅该哈希表。 如果它找到一个函数,则在编译时以原始形式调用它,并编译该函数返回的形式来代替原始形式。 (对一些不重要的细节取模)所以 lisp 宏基本上是编译器的插件
  • 用 lisp 编写一个计算 lisp 代码的 lisp 函数大约需要两页代码(这通常称为eval)。 在这样的函数中,您拥有在元级别引入任何您想要的新规则的权力。 (不过要让它运行得更快,需要付出一些努力……与引导一门新语言差不多……:)

使用 lisp 元编程可以实现为用户库的随机示例(这些是 Common Lisp 库的实际示例):

  • 使用分隔延续(hu.dwim.delico)扩展语言,
  • 实现一个 js-to-lisp-rpc 宏,您可以在 javascript 中使用(由 lisp 生成)。 它扩展为 js/lisp 代码的混合体,自动发布(在 http 请求中)所有引用的局部变量,在服务器端对它们进行解码,在服务器上运行 lisp 代码主体,并将返回值返回给 javascript边。
  • 添加像回溯这样的序言到与“正常”lisp 代码无缝集成的语言(参见尖叫器)
  • XML 模板扩展到 common lisp(包括作为 lisp 解析器插件的阅读器宏示例)
  • 大量小型 DSL,例如 循环迭代以方便循环

let me list a few important details about how metaprogramming works in lisp (or scheme, or slate, or pick your favorite "dynamic" language):

  • when doing metaprogramming in lisp you don't have to deal with two languages. the meta level code is written in the same language as the object level code it generates. metaprogramming is not limited to two levels, and it's easier on the brain, too.
  • in lisp you have the compiler available at runtime. in fact the compile-time/run-time distinction feels very artificial there and is very much subject to where you place your point of view. in lisp with a mere function call you can compile functions to machine instructions that you can use from then on as first class objects; i.e. they can be unnamed functions that you can keep in a local variable, or a global hashtable, etc...
  • macros in lisp are very simple: a bunch of functions stuffed in a hashtable and given to the compiler. for each form the compiler is about to compile, it consults that hashtable. if it finds a function then calls it at compile-time with the original form, and in place of the original form it compiles the form this function returns. (modulo some non-important details) so lisp macros are basically plugins for the compiler.
  • writing a lisp function in lisp that evaluates lisp code is about two pages of code (this is usually called eval). in such a function you have all the power to introduce whatever new rules you want on the meta level. (making it run fast is going to take some effort though... about the same as bootstrapping a new language... :)

random examples of what you can implement as a user library using lisp metaprogramming (these are actual examples of common lisp libraries):

  • extend the language with delimited continuations (hu.dwim.delico)
  • implement a js-to-lisp-rpc macro that you can use in javascript (which is generated from lisp). it expands into a mixture of js/lisp code that automatically posts (in the http request) all the referenced local variables, decodes them on the server side, runs the lisp code body on the server, and returns back the return value to the javascript side.
  • add prolog like backtracking to the language that very seamlessly integrates with "normal" lisp code (see screamer)
  • an XML templating extension to common lisp (includes an example of reader macros that are plugins for the lisp parser)
  • a ton of small DSL's, like loop or iterate for easy looping
惟欲睡 2024-07-12 01:10:59

模板样式元编程的替代方案是您在各种 Lisp 实现中看到的宏样式。 我建议下载 Paul Graham 的 On Lisp 并看看Clojure 如果您对在 JVM 上运行的带有宏的 Lisp 感兴趣。

Lisp 中的宏比 C/C++ 风格强大得多,并且本身就构成了一种语言——它们用于元编程。

The alternative to template style meta-programming is Macro-style that you see in various Lisp implementations. I would suggest downloading Paul Graham's On Lisp and also taking a look at Clojure if you're interested in a Lisp with macros that runs on the JVM.

Macros in Lisp are much more powerful than C/C++ style and constitute a language in their own right -- they are meant for meta-programming.

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