简体   繁体   English

扩展Mono C#编译器:有没有任何文档或先例?

[英]Extending the Mono C# compiler: is there any documentation or precedent?

I am currently involved in some interesting programming language research which has, up until now, centred around extending the upcoming Java 7.0 compiler with some very powerful programmer-productivity-based features. 我目前正在参与一些有趣的编程语言研究,到目前为止,该研究主要围绕使用一些非常强大的基于程序员生产力的功能扩展即将推出的Java 7.0编译器。 The work should be equally applicable to related programming languages such as C#. 该工作应同样适用于相关的编程语言,如C#。

I'm currently scoping out the options for prototyping a C# port of the functionality. 我目前正在寻找原型化C#端口功能的选项。 I would prefer open-source options so that the fruits of this work can be shared with the broadest-possible audience. 我更喜欢开源选项,以便可以与最广泛的受众分享这项工作的成果。 Thus the Mono C# compiler seems to be the most obvious starting point. 因此,Mono C#编译器似乎是最明显的起点。 I'm an experienced C# developer so writing the code isn't the problem. 我是一位经验丰富的C#开发人员,所以编写代码不是问题。 I'm mainly concerned about extending the compiler in a maintainable and supported fashion. 我主要关心的是以可维护和支持的方式扩展编译器。 In the Mono FAQ on the subject ( link ) it is stated that "Mono has already been used as a foundation for trying out new ideas for the C# language (there are three or four compilers derived from Mono's C# compiler)". 在关于这个主题的单声道常见问题解答( 链接 )中,声称“Mono已被用作尝试C#语言的新思想的基础(有三个或四个编译器派生自Mono的C#编译器)”。 Unfortunately, there are no further pointers than this and, so far, Google searches have not turned anything up. 不幸的是,没有比此更多的指示,到目前为止,谷歌的搜索没有任何改变。

I'm wondering if anybody out there has any information on this. 我想知道是否有任何人有这方面的任何信息。 Do mcs / gmcs / dmcs have a standard extensibility model? mcs / gmcs / dmcs是否具有标准的可扩展性模型? Specifically, I will be performing some interesting transformations on a program's abstract syntax tree. 具体来说,我将在程序的抽象语法树上执行一些有趣的转换。 Is there a standard mechanism for inserting functionality into the compiler chain between abstract syntax tree generation and the type checker and then code generation? 是否有一种标准机制,用于在抽象语法树生成和类型检查器之间插入功能到编译器链中,然后生成代码?

Up until now I've written some ad-hoc extensions to the code (primarily in the code generator) but this doesn't seem to be a maintainable solution especially given that I intend to keep my extensions up to date with the Git trunk of Mono as much as possible. 到目前为止,我已经为代码编写了一些特别的扩展(主要是在代码生成器中),但这似乎不是一个可维护的解决方案,特别是考虑到我打算让我的扩展更新为Git主干单声道尽可能多。 Furthermore it would be nice to be able to make updates to my extensions without having to recompile the whole compiler every time I make a change. 此外,能够更新我的扩展,而不必每次进行更改时都重新编译整个编译器,这将是很好的。 I would like to be able to wrap all my AST manipulations into a single .NET assembly that could be dynamically loaded by mcs / gmcs / dmcs without having to hack at the core compiler code directly. 我希望能够将我的所有AST操作包装到一个可以由mcs / gmcs / dmcs动态加载的.NET程序gmcs而不必直接破解核心编译器代码。

Any thoughts or pointers on extending the Mono C# compiler would be gratefully received! 任何有关扩展Mono C#编译器的想法或指示都将非常感激!

UPDATES (23 October 2010) 更新(2010年10月23日)

In response to the responses to my question, I decided that I would start working on a branch of Mono in order to create a simple extensibility model for the compiler. 在回答我的问题时,我决定开始研究Mono的一个分支,以便为编译器创建一个简单的可扩展性模型。 It's in its very early stages, but here it is at GitHub: 这是在它的早期阶段,但它在GitHub:

http://github.com/rcook/mono-extensibility http://github.com/rcook/mono-extensibility

And the main commit is: http://github.com/rcook/mono-extensibility/commit/a0456c852e48f6822e6bdad7b4d12a357ade0d01 主要提交是: http//github.com/rcook/mono-extensibility/commit/a0456c852e48f6822e6bdad7b4d12a357ade0d01

If anybody would be interested in collaborating on this project, please let me know! 如果有人有兴趣合作这个项目,请告诉我!

Unfortunately, I cannot adequately answer your question, but if you look at the examples of C# extensions on Miguel de Icaza's blog, you will notice that all of them take the form of patches to the compiler, not plugins or extensions. 不幸的是,我无法充分回答你的问题,但如果你看看Miguel de Icaza博客上的C#扩展示例,你会发现所有这些都采用补丁的形式,而不是插件或扩展。 This seems to indicate that there is no such API. 这似乎表明没有这样的API。

Note that all of these examples are of much smaller scope than what you seem to be working on: 请注意,所有这些示例的范围都比您似乎正在处理的范围小得多:

These are mostly localized syntactic sugar, with no "interesting" behavior. 这些大多是局部的语法糖,没有“有趣”的行为。 The fourth patch, for example, implements Cω's syntactic sugar for IEnumerable s, but without any of Cω's semantics that make this syntax interesting. 例如,第四个补丁为IEnumerable实现了Cω的语法糖,但是没有任何Cω的语义使这个语法变得有趣。 If you look at the patch you can see that it literally does stupid syntactical expansion of ~TIEnumerable<T> , as opposed to Cω, where member access and method invocation are properly lifted over streams. 如果你看一下补丁你可以看到它实际上是愚蠢的语法扩展~TIEnumerable<T> ,而不是Cω,其中成员访问和方法调用在流上正确解除。

Microsoft Research's Phoenix Compiler Pipeline was once explicitly touted as the solution to such extensibility problems, but it seems that it now focuses mostly on optimizations and analysis on the IR level in a code generation backend. 微软研究院的凤凰编译器管道曾经被明确地吹捧为这种可扩展性问题的解决方案,但它现在似乎主要集中在代码生成后端的IR级别上的优化和分析。 In fact, I'm not even sure if the project is even still alive. 事实上,我甚至不确定这个项目是否还活着。

The mono C# compiler is a bit of a hack. 单声道C#编译器有点像黑客。 I spent around a week figuring out how to use information from the parse tree. 我花了大约一周的时间来弄清楚如何使用解析树中的信息。 The compiler does not produce any intermediate representation and code generation may break parts of the parse tree. 编译器不会产生任何中间表示,代码生成可能会破坏部分解析树。 Still, the parser and tokenizer might prove useful to you and you just take it from there. 仍然,解析器和标记器可能对您有用,您只需从那里获取它。 SharpDevelop also provides a C# parser . SharpDevelop还提供了一个C#解析器 The SharpDevelop parser is easier to use than the mono C# parser. SharpDevelop解析器比单声道C#解析器更容易使用。 If F# also works for you, I would recommended. 如果F#也适合你,我建议。 The source much cleaner than mono and available under open source license. 源代码比单声道更清晰,可在开源许可下使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM