How to create a .Net programming language?

2019-01-16 10:23发布

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?

Also, is there an easy way to get the language working in Visual Studio?

7条回答
仙女界的扛把子
2楼-- · 2019-01-16 10:35

There is a LOLCode compiler built in C# that might be a good starting point.

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.

查看更多
可以哭但决不认输i
3楼-- · 2019-01-16 10:37

A great example for integrating your own language into Visual Studio is the Lua Language Support project.

查看更多
虎瘦雄心在
4楼-- · 2019-01-16 10:46

Not sure if this is what you're looking for, but after I had a Question on SO i've created a .net Compiler for Brainf**k as an excersize (Part 6 contains the actual Compiler Source Code and is also on GitHub)

Essentially, the actual Compiler can be written in any .net Language and use the System.Reflection.Emit namespace to create assemblies, classes, methods etc. and emit IL into them.

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 resources on how to write a compiler.

You do want to read ECMA-335 to learn how the CLI works: What datatypes it supports and what commands it actually does.

查看更多
家丑人穷心不美
5楼-- · 2019-01-16 10:49

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.

查看更多
贪生不怕死
6楼-- · 2019-01-16 10:51

Your compiler could output either CIL assembly language, or an assembly (.dll / .exe) file.

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.

By the way, the standard is also available as a book with annotations.

查看更多
Bombasti
7楼-- · 2019-01-16 10:55

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.

Disputable if that would be a 'real' compiler though.

查看更多
登录 后发表回答