visual-studio 如何创建 .Net 编程语言?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3857743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a .Net programming language?
提问by Telavian
I have created a few different full programming languages using some of the various parsing tools available. However, how would someone create a programming language that runs the .Net framework? Would I have to output the .Net IL and compile that or is there a higher level of abstraction?
我使用一些可用的各种解析工具创建了几种不同的完整编程语言。但是,有人将如何创建运行 .Net 框架的编程语言?我是否必须输出 .Net IL 并编译它,或者是否有更高级别的抽象?
Also, is there an easy way to get the language working in Visual Studio?
另外,有没有一种简单的方法可以让语言在 Visual Studio 中工作?
采纳答案by Hans Passant
You'll want to take a look at the Microsoft Research Common Compiler Infrastructure (CCI)project. It provides everything you need to generate the metadata and the MSIL for an assembly. And the debugging .pdb file, rather important to get your language going.
您需要查看Microsoft Research Common Compiler Infrastructure (CCI)项目。它提供了为程序集生成元数据和 MSIL 所需的一切。和调试 .pdb 文件,对于让你的语言运行相当重要。
There's a sister project, CCI Code Model and AST, that might be useful as well, depends how far along you got.
有一个姊妹项目,CCI 代码模型和 AST,它可能也很有用,这取决于您的进展情况。
回答by NerdFury
There is a LOLCode compiler built in C# that might be a good starting point.
有一个用 C# 构建的 LOLCode 编译器可能是一个很好的起点。
http://code.google.com/p/lolcode-dot-net/
http://code.google.com/p/lolcode-dot-net/
As for implementing in Visual Studio, there is an open source view engine called Spark (http://www.sparkviewengine.com/) that did some work with getting intellisense to work. The context is different, but the lesson is the same, it's hard, and it's not a very well documented process.
至于在 Visual Studio 中的实现,有一个名为 Spark ( http://www.sparkviewengine.com/) 的开源视图引擎,它做了一些让智能感知工作的工作。上下文不同,但教训是相同的,它很难,而且不是一个很好的记录过程。
回答by Femaref
Your compiler is responsible for converting the code it gets to ILCode. ILCode is the common language any and all .net languages compile to. This code gets interpreted by the JIT-Compiler at runtime. Of course, it can rely on the CodeDom classes in the framework to accomplish that, if you write the compiler in C# for example.
您的编译器负责将它获得的代码转换为 ILCode。ILCode 是任何和所有 .net 语言编译成的通用语言。此代码在运行时由 JIT 编译器解释。当然,它可以依靠框架中的 CodeDom 类来实现,例如,如果您用 C# 编写编译器。
回答by Michael Stum
Not sure if this is what you're looking for, but after I had a Question on SOi've created a .net Compiler for Brainf**kas an excersize (Part 6contains the actual Compiler Source Code and is also on GitHub)
不确定这是否是您要查找的内容,但是在我对 SO 提出问题后,我为 Brainf**k创建了一个.net 编译器作为练习(第 6 部分包含实际的编译器源代码,也在GitHub 上))
Essentially, the actual Compiler can be written in any .net Language and use the System.Reflection.Emitnamespace to create assemblies, classes, methods etc. and emit IL into them.
本质上,实际的编译器可以用任何 .net 语言编写,并使用System.Reflection.Emit命名空间来创建程序集、类、方法等,并将 IL 发送到它们中。
The actual work of creating a new language obviously involves writing a parser/lexer/analyzer/whatever that can dissect your program into classes and "feed" your compiler - sorry, I have no experience in that, but check this list of resourceson how to write a compiler.
创建新语言的实际工作显然涉及编写解析器/词法分析器/分析器/任何可以将您的程序分解为类并“馈送”您的编译器的东西 - 抱歉,我没有这方面的经验,但请查看此资源列表以了解如何写一个编译器。
You do want to read ECMA-335to learn how the CLI works: What datatypes it supports and what commands it actually does.
您确实想阅读ECMA-335以了解 CLI 的工作原理:它支持哪些数据类型以及它实际执行哪些命令。
回答by stakx - no longer contributing
Your compiler could output either CIL assembly language, or an assembly (.dll/ .exe) file.
您的编译器可以输出 CIL 汇编语言或汇编 ( .dll/ .exe) 文件。
Take a look at the CLI (Common Language Infrastructure) standard, which describes the CLI (the target platform), the CIL (assembly language), and the binary file format of assemblies.
看一看CLI(通用语言基础结构)标准,它描述了 CLI(目标平台)、CIL(汇编语言)和程序集的二进制文件格式。
By the way, the standard is also available as a book with annotations.
顺便说一下,该标准也可以作为带注释的书使用。
回答by Mike Schenk
A great example for integrating your own language into Visual Studio is the Lua Language Supportproject.
将您自己的语言集成到 Visual Studio 的一个很好的例子是Lua 语言支持项目。
回答by Henk Holterman
or is there a higher level of abstraction?
还是有更高层次的抽象?
Just to be complete, you could output C# or VB.NET. Both compilers are part of the runtime.
为了完整起见,您可以输出 C# 或 VB.NET。两个编译器都是运行时的一部分。
Disputable if that would be a 'real' compiler though.
不过,这是否是一个“真正的”编译器是有争议的。

