7

I know it might not be worth it but just for education purposes I want to know if there is a way to inject your own keywords to .NET languages.

For example I thought it's good to have C++ asm keyword in C#.
Remember I'm not talking about how to implement asm keyword but a general way to add keyword to C#.

My imagined code :

asm{
    mov ax,1
    add ax,4
}

So is there a way to achieve this ?
The answers which cover implementing keyword{ } suits enough for this question.

Mohsen Sarkar
  • 5,910
  • 7
  • 47
  • 86
  • 8
    Only if you ready to write your own C# compiler. – Hamlet Hakobyan Mar 13 '13 at 14:44
  • The only way I know is assigning aliases to known types. – Denys Denysenko Mar 13 '13 at 14:47
  • I think this needs some variant of an "Intermediate Language" tag... all .NET goes to IL code – makerofthings7 Mar 13 '13 at 14:47
  • @HamletHakobyan You would only need to write your own preprocessor, still hard, but not nearly as complex as writing a full compiler. Roslyn makes that a lot easier too. – Servy Mar 13 '13 at 14:47
  • @makerofthings7 It wouldn't need to, if you used a proprocessor, rather than a postprocessor. – Servy Mar 13 '13 at 14:48
  • [Possible duplicate](http://stackoverflow.com/a/161484/328397) like this `using CustomerList = System.Collections.Generic.List;` – makerofthings7 Mar 13 '13 at 14:49
  • @Servy preprocessor is a part of compiler, isn't it? – Hamlet Hakobyan Mar 13 '13 at 14:49
  • @HamletHakobyan The compiler does have it's own preprocessor. You can create your own that runs before that. – Servy Mar 13 '13 at 14:50
  • @Servy Is it possible in this case? – Hamlet Hakobyan Mar 13 '13 at 14:51
  • @HamletHakobyan Sure. Have you ever written a program that modifies a file? You take a file, do whatever you want to it, and then when you're done, feed it to the real compiler. It doesn't need to have anything to do with the real compiler, as long as the output is valid C# code. – Servy Mar 13 '13 at 14:52
  • @Servy Okay, now we process the C# code and found `asm{ mov ax,1 add ax,4 }` What is your action? – Hamlet Hakobyan Mar 13 '13 at 14:55
  • @HamletHakobyan No idea. You can, in theory, do whatever you want. The OP hasn't given any indication of what it might be. Imagine C++ macro, you could, if you felt like it, implement your own version of that, or even just some sort of text replacement, or even something as complex as what an iterator block does, as it refactors the code into more C# code, rather than directly into IL. You could write an extension that allowed iterator blocks on C# 1.0, or `await` on C# 2.0, etc. – Servy Mar 13 '13 at 15:03
  • For the Microsoft .NET languages - no, not yet. But some third party .NET languages do provide such a functionality. Take a look at Nemerle, for example. – SK-logic Mar 13 '13 at 19:15

4 Answers4

4

This isn't possible at the moment. However, there's a Microsoft project in development called Roslyn that can be summarised as "the compiler as a service." It allows you, amongst other things, to extend or modify the behaviour of the compiler through an API.

When Roslyn becomes available, I believe this should be something that (with caution!) is quite doable.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
2

Unfortunately this is not possible. You can't extend or alter the languages in any way.

You could in some obscure way use PostSharp to read and parse strings and transform them to custom code at compile time (a pre processor). But you would not get very happy with that, as it is very error prone and you won't get any kind of intellisense or code completion for your magic strings.

2

You can use whatever tools you would like to pre-process your code before sending it to the C# compiler. For example, you might use VS macros to do the pre-processing, mapping a given syntax that you invented into something that does compile into C# code, possibly generating an error if there is a problem. If VS macros aren't powerful enough for you then you can always use your own IDE that does whatever you code it to do to the text before sending it to the compiler.

There is no built in support in the compiler for specifying your own keywords/syntax; you would need to handle it entirely independent of the compiler.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • There isn't yet, but there will be soon - Roslyn will let you modify the compiler behaviour if you choose. The samples I've seen so far are... well, pretty scary, mostly! ;-) – Dan Puzey Mar 13 '13 at 15:38
  • How sad is that macros are retired since VS 2012 – Apocatastasis Mar 13 '13 at 15:50
  • 1
    @Apocatastasis Well, you can use an add in, or some other form of an extension, even if it's not the same flavor of macros. – Servy Mar 13 '13 at 15:52
0

According to MSDN keywords are predefined and cannot be altered. So you can't add any, because you would need to tell the compiler how to handle them. Insofar, no you can't.

bash.d
  • 13,029
  • 3
  • 29
  • 42