如何为 .NET 制作并行编译器
Nikhil Kothari 的 Script# 很可能是我在 JavaScript 领域见过的最令人惊奇的概念之一很长一段时间。 这个问题与 JavaScript 无关,而是与 .NET 运行时中的语言编译有关。
我一直对如何使用 .NET 平台为一种已经有编译器(如 C#)的语言编写编译器非常感兴趣,该编译器将从原始编译器生成单独的输出,同时允许原始编译器为以下内容生成输出:在相同的构建操作期间使用相同的源,同时也引用/使用其他编译器的输出。
我不完全确定我是否足够了解该过程,是否能够以正确的细节提出问题,但这是我目前查看该过程的方式,根据 Script# 文档中的图表。 我考虑过许多涉及复杂语言设计和编译的事情,这些事情可能能够利用这样的概念,并且我对其他人对这些概念的看法感兴趣。
--
编辑:感谢您的评论,到目前为止; 您的信息本身非常有趣,我想对其进行更多研究,但我的问题实际上是关于我如何编写自己的编译器,该编译器可以同时在同一源上运行使用 CLR 生成多种不同类型(可能)相互依赖的输出。 Script# 就是一个例子,因为它使用相同的 C# 源代码生成 JavaScript 和 Assembly,同时使编译后的 Assembly 与 JavaScript 配合。 我很好奇设计这种性质的东西的各种方法和理论概念是什么。
Nikhil Kothari's Script# is quite possibly one of the most amazing concepts I've seen in the JavaScript arena for quite some time. This question isn't about JavaScript, but rather about language compilation in the .NET runtime.
I've been rather interested in how, using the .NET platform, one can write a compiler for a language that already has a compiler (like C#) that will generate separate output from the original compiler while allowing the original compiler to generate output for the same source during the same build operation, all the while referencing/using the output of the other compiler as well.
I'm not entirely sure I even understand the process well enough to ask the question with the right details, but this is the way I currently see the process, as per diagrams in the Script# docs. I've thought about many things involving complex language design and compilation that may be able to take advantage of concepts like this and I'm interested in what other people think about the concepts.
--
Edit: Thanks for commenting,so far; your information is, in it's own right, very intriguing and I should like to research it more, but my question is actually about how I would be able to write my own compiler/s that can be run on the same source at the same time producing multiple different types of (potentially) interdependent output using the CLR. Script# serves as an example since it generates JavaScript and an Assembly using the same C# source, all the while making the compiled Assembly cooperate with the JavaScript. I'm curious what the various approaches and theoretical concepts are in designing something of this nature.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
重要的是要认识到,编译器所做的一切就是获取源语言(在本例中为 C#),对其进行解析,以便编译器具有对它有意义的表示形式,而不是对人类有意义的表示形式(这是抽象语法树),然后执行目标语言的原生代码生成(msil 是在 .NET 运行时上运行的语言的目标)。
现在,如果 script# 代码转换为程序集并与其他 .NET 代码交互,则意味着该编译器必须生成 msil。 script# 为此使用 csc.exe,它只是标准的 c# 编译器。 现在要生成 javascript,它必须使用 c# 或 msil,解析它,并生成 javascript 发送到浏览器。 文档说它有一个自定义的 c# -> Node.js 编译器称为 ssc.exe。
为了使客户端和服务器端的交互一致,它有一组用 .NET 编写但也编译为 JavaScript 的参考程序集。 但这不是编译器特定的问题,这些引用程序集是 script# 运行时。 不过,运行时可能是造成您所感知的许多 script# 魔法的原因。
It's important to realize that all a compiler does is take a source language (C# in this case), parse it so the compiler has a representation that makes sense to it and not humans (this is the abstract syntax tree), and then does a naive code generation to the target language (msil is the target for languages that run on the .NET runtime).
Now if the script# code is turned into an assembly and interacts with other .NET code, that means this compiler must be generating msil. script# is using csc.exe for this, which is just the standard c# comiler. Now to generate the javascript, it must take either c# or msil, parse it, and generate javascript to send to the browser. The docs says it has a custom c# -> js compiler called ssc.exe.
To make things interact consistently on both the client side and the server side it has a set of reference assemblies that are written in .NET but are also compiled to javascript. This is not a compiler specific issue though, those reference assemblies are the script# runtime. The runtime is probably responsible for a lot of the script# magic you're perceiving though.
假设您想将 C# 编译为 Javascript。 您问是否可以利用现有的 C# 编译器,而不是直接将 C# 编译为 Javascript,而是将 C# 编译器生成的 MSIL 转换为 Javascript?
当然,你可以这样做。 一旦获得 MSIL 二进制文件,您就可以对其进行任何操作。
So let's say you want to compile C# into Javascript. You are asking whether you can take advantage of the existing C# compilers, so instead of compiling C# into Javascript directly you actually convert the MSIL generated by the C# compiler into Javascript?
Sure, you can do that. Once you have the MSIL binary you can do whatever you want to it.
微软有一个名为 Volta 的研究项目,其中包括将 msil 编译为 JavaScript。
Microsoft has a research project called Volta which, amongst other things, compiles msil to JavaScript.