December 18, 2004
C# to C++ translator
Some time ago (oh boy I can't quite remember exactly how long but I think it was about 13 years ago) I started to learn C++. Before that I had been using C. Then in a typical "imperatively thinking programmer tries to learn OO" manner, over the next 13 years, I gradually started to "get OO". I'm still not there but I'm making continued progress.
Now, a couple of years ago I realized that C++ doesn't "get OO" either. To continue my journey in search of OO nirvana, I would have to switch languages. So I actually switched to two languages: Java and C#. I had the "now I get it" moment while reading Martin Fowler's excellent book called "Refactoring", which I think every programmer should read.
For games development, C# appeals to the pragmatist in me. It's sufficiently like C++ that it won't scare a less open-minded programmer in the way a pure-OO language like Smalltalk would. Also, for most applications, it generates code with similar performance to that generated by C++. And performance is, I think, the line of argument most likely to be used against the idea of using a different language. That and "what's wrong with C++, I know it already?".
How about Java? Java would be good as well but I wrote a couple of Java compilers already and I want to try something new.
And hey it's the weekend and I can do whatever I want!
My overall strategy is to take the C# compiler that comes with the Mono project and modify the back-end to output C++ source code instead of MSIL byte-code. This way most of the work is done for me. The Mono C# compiler already does most of the front-end work and any C++ compiler can act as the back-end. All I have to do is glue them together in the middle.
Another option would be to translate byte-code into C++. In fact that was the way my Java compilers worked. The big disadvantage is that a lot of the program structure is lost in the byte-code, so I would have little option but to generate spaghetti code.
Then I can write C# programs for any platform with a C++ compiler, including game consoles.