如何为 .NET 制作并行编译器

发布于 2024-07-06 01:47:32 字数 711 浏览 11 评论 0原文

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 技术交流群。

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

发布评论

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

评论(3

So尛奶瓶 2024-07-13 01:47:32

重要的是要认识到,编译器所做的一切就是获取源语言(在本例中为 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.

唐婉 2024-07-13 01:47:32

假设您想将 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.

倾`听者〃 2024-07-13 01:47:32

微软有一个名为 Volta 的研究项目,其中包括将 msil 编译为 JavaScript。

用于构建的开发人员工具集
多层 Web 应用程序使用
现有且熟悉的工具,
技术和模式。 沃尔特的
声明式分层使得
开发商推迟建筑
关于分配的决定,直到
最后可能的负责任的时刻。
另外,感谢共享编程
跨多层模型,Volta
启用新的端到端分析和
测试更高水平的
应用性能、稳健性、
和可靠性。 使用声明式
分层,开发者可细化
基于此的架构决策
分析数据。 这节省了时间并且
与人工相关的成本
重构。 实际上,Volta 延长了
.NET 平台进一步启用
软件+服务的发展
应用程序,使用现有的和
熟悉的工具和技术。

您设计并构建您的
作为 .NET 客户端的应用程序
应用程序,分配的部分
在服务器上运行的应用程序
后期的层和客户层
发展过程。 你可以瞄准
Web 浏览器或 CLR 作为
客户和 Volta 负责处理
分层的复杂性。 这
编译器创建跨浏览器
用于客户端层、Web 的 JavaScript
服务器层的服务,以及所有
通信、序列化、
同步、安全等
用于绑定各层的样板代码
一起。 实际上,Volta 提供了
多个尽力而为的体验
环境无需
定制应用程序。

Microsoft has a research project called Volta which, amongst other things, compiles msil to JavaScript.

a developer toolset for building
multi-tier web applications using
existing and familiar tools,
techniques and patterns. Volta’s
declarative tier-splitting enables
developers to postpone architectural
decisions about distribution until the
last possible responsible moment.
Also, thanks to a shared programming
model across multiple-tiers, Volta
enables new end-to-end profiling and
testing for higher levels of
application performance, robustness,
and reliability. Using the declarative
tier-splitting, developers can refine
architectural decisions based on this
profiling data. This saves time and
costs associated with manual
refactoring. In effect, Volta extends
the .NET platform to further enable
the development of software+services
applications, using existing and
familiar tools and techniques.

You architect and build your
application as a .NET client
application, assigning the portions of
the application that run on the server
tier and client tier late in the
development process. You can target
either web browsers or the CLR as
clients and Volta handles the
complexities of tier-splitting. The
compiler creates cross-browser
JavaScript for the client tier, web
services for the server tier, and all
communication, serialization,
synchronization, security, and other
boilerplate code to tie the tiers
together. In effect, Volta offers a
best-effort experience in multiple
environments without requiring
tailoring of the application.

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