Adding Matt operator to Roslyn - Emitter
10 Jun 2017The last missing piece to get a new operator: m@
(matt) into C# and Roslyn - Emitter. That’s the piece that translates C# code into IL, which is than run by the runtime.
by Marcin Juraszek
The last missing piece to get a new operator: m@
(matt) into C# and Roslyn - Emitter. That’s the piece that translates C# code into IL, which is than run by the runtime.
Following on my previous post where I showed how to add a new operator to C# and Roslyn today I’m going to describe how to make further progress on implementing matt operator: m@
. I already extender the Lexer and Parser to understand that new language construct. The next step is to make Binder aware of our new operator, to make sure it can only be used in the right places and with the right types. We also need to teach the compiler that <int> m@ <int>
returns an int
.
I read a very interesting blog post by Matt Warren yesterday morning: Adding a new Bytecode Instruction to the CLR. It was very eye-opening to see how easy it is to add a new instruction to .NET CLR. In his blogpost Matt started a challange about adding support for his new matt
operator into C# (Roslyn):
The other reason for naming it matt is that I’d really like someone to make a version of the C# (Roslyn) compiler that allows you to write code like this:
Console.WriteLine("{0} m@ {1} = {2}", 1, 7, 1 m@ 7)); // prints '1 m@ 7 = 7'
I definitely want the
m@
operator to be a thing (pronounced ‘matt’, not ‘m-at’), maybe the other ‘Matt Warren’ who works at Microsoft on the C# Language Design Team can help out!! Seriously though, if anyone reading this would like to write a similar blog post, showing how you’d add them@
operator to the Roslyn compiler, please let me know I’d love to read it.
Because I always wanted to learn more about Roslyn I decided to explore it a little bit and see how far I can get. I invite you to join me on that journey.
Today was a big day for entire .NET Framework community. So much amazing stuff announced on VisualStudio Connect(); event in NY: open sourcing Core .NET Framework code (including CLR, JIT, FC, BLC and more), Visual Studio 2015 Preview with free and extensible Visual Studio Community Edition, new Visual Studio Online features and more. Also quite a few announcements connected to ASP.NET 5. The new version of ASP.NET, known before as ASP.NET vNext, that will revolutionize the way people create Web Applications using .NET Framework stack. Particular feature I’d like to focus on today is great integration with Grunt – JavaScript task runner that can be used to automate routine development tasks, e.g. compiling LESS/SASS files to CSS, CoffeeScript/TypeScript to JavaScript and more.
How many times in your code do you need to parse strings to primitive values? I would guess pretty often. How often do you use TryParse
method, to avoid unnecessary exception handling? The same guess – probably pretty often. How many times did you wish that TryParse
didn’t use out method parameter? Yes, the same answer again. But how many times did you think how to improve that scenario? Probably never. I think you should have done that! There is quite a lot of room for improvement here. Simple tricks that may make your parsing logic much simpler, cleaner and more verbatim.