38

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?

Telavian
  • 3,752
  • 6
  • 36
  • 60
  • 2
    You would output the .NET IL. The .NET runtime would handle compiling and running that code. Not sure about the other pieces. – Nelson Rothermel Oct 04 '10 at 17:47
  • 1
    Similar [resources-to-learn-how-to-create-a-compiler-interpreter-for-the-net](http://stackoverflow.com/questions/1448643/resources-to-learn-how-to-create-a-compiler-interpreter-for-the-net-framework) – nawfal Jul 21 '14 at 13:02

7 Answers7

17

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.

There's a sister project, CCI Code Model and AST, that might be useful as well, depends how far along you got.

Mika Sundland
  • 18,120
  • 16
  • 38
  • 50
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    Has this been deprecated by Roslyn or is it still relevant? – Aaron Anodide Oct 11 '14 at 23:16
  • CCI is helpful to create *any* language, Roslyn targets only C# and VB.NET – Hans Passant Oct 11 '14 at 23:34
  • This has been discontinued: "Moving forward, there will only be servicing investments in CCI to support existing uses. " – Aaron Franke May 23 '19 at 07:56
  • 1
    Hmm, no, there is a distinction between "discontinued" and "done". It is discontinued when you can't download it anymore. Lots of Microsoft projects are done, they still service it to provide critical bug fixes but won't add to it anymore. Winforms for example has been done for 14 years now, lots of people still use it. It actually gets more servicing updates than WPF, also done. – Hans Passant May 23 '19 at 08:58
11

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.

Dinah
  • 52,922
  • 30
  • 133
  • 149
NerdFury
  • 18,876
  • 5
  • 38
  • 41
10

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.

Community
  • 1
  • 1
Michael Stum
  • 177,530
  • 117
  • 400
  • 535
7

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.

Dinah
  • 52,922
  • 30
  • 133
  • 149
Femaref
  • 60,705
  • 7
  • 138
  • 176
  • 1
    Check out this article from MSDN magazine on implementing a compiler in C# for a given language: http://msdn.microsoft.com/en-us/magazine/cc136756.aspx – Justin Niessner Oct 04 '10 at 17:58
  • 2
    The link is dead. Here is an archive.org snapshot: https://web.archive.org/web/20150111033016/http://msdn.microsoft.com/en-us/magazine/cc136756.aspx – IInspectable Aug 25 '17 at 07:49
  • How do you take IL code and compile/run it? Do you need to generate a project which .NET Core, Framework, and/or Mono could run? – Aaron Franke Jul 05 '19 at 23:20
5

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.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
3

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

Mike Schenk
  • 1,512
  • 9
  • 21
2

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.

H H
  • 263,252
  • 30
  • 330
  • 514