如何为.NET制作并行编译器
Nikhil Kothari的Scriptis很可能是我在JavaScript领域看到很长时间以来最惊人的概念之一。这个问题不是关于JavaScript,而是关于.NET运行时中的语言编译。
我一直对使用.NET平台如何为一种已经具有编译器(例如C#)的语言编写编译器感兴趣,该语言将生成与原始编译器不同的输出,同时允许原始编译器为在相同的构建操作中使用相同的源代码,同时还要引用/使用其他编译器的输出。
我不能完全确定我是否足够理解该过程,以提出具有正确细节的问题,但是根据Scriptdocs中的图表,这是我当前看到该过程的方式。我考虑过很多涉及复杂语言设计和编译的事情,这些事情可能能够利用这样的概念,并且我对其他人对这些概念的看法很感兴趣。
--
编辑:到目前为止,感谢评论;信息本身就是非常有趣的,我想进一步研究,但是我的问题实际上是关于如何编写自己的可以在同一源上同时运行的编译器使用CLR生成多种不同类型的(可能)相互依赖的输出。 Scriptserve是一个示例,因为它使用相同的Csource生成JavaScript和Assembly,同时使编译后的Assembly与JavaScript协同工作。我很好奇在设计这种性质的东西时会使用各种方法和理论概念。
解决方案
回答
假设我们要编译Cinto Javascript。我们是在问是否可以利用现有的Ccompiler,因此,不是直接将Cinto Javascript编译,而是实际上将Ccompiler生成的MSIL转换为Javascript?
当然可以。拥有MSIL二进制文件后,我们就可以对其进行任何操作。
回答
微软有一个名为Volta的研究项目,该项目除其他外将msil编译为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.
回答
重要的是要意识到,编译器所做的全部工作都是采用源语言(在这种情况下为C),对其进行解析,以便编译器具有一种对它有意义而不是对人类有意义的表示形式(这是抽象语法树),然后进行幼稚处理代码生成为目标语言(msil是.NET运行时上运行的语言的目标)。
现在,如果将脚本代码转换为程序集并与其他.NET代码进行交互,则意味着该编译器必须正在生成msil。 scriptis为此使用csc.exe,这只是标准的ccomiler。现在要生成javascript,必须使用cor msil进行解析,然后生成javascript以发送到浏览器。文档说它有一个名为ssc.exe的自定义c-> js编译器。
为了使事物在客户端和服务器端都能始终如一地交互,它具有一组用.NET编写但也已编译为javascript的引用程序集。但是,这不是编译器特定的问题,那些参考程序集是scriptruntime。但是,运行时可能是导致我们感觉到的许多脚本魔术的原因。