Thursday, February 22, 2007

Why?

Why did I write a compiler?
When I was studying computer programming at Universidad de Belgrano, one of my fellow students mentioned that "at least they don't require us to write a compiler anymore", making reference to earlier years when the students seemingly were required to write compilers in order to finish their degree.
This obviously made me want to write a compiler, not to finish my degree, but to only satisfy my geeky desire to do something nobody else wanted to do.

Why did I write a compiler for Visual Basic.Net?
Visual Basic was the first computer language I learnt (way back at high school in '98 I got my hands on VB4, later on it was upgraded to VB5 and then VB6), and I did actually like it (no comments on this one please :) .
Later on I started learning C++ (but I didn't like it since the relation between "tangible results"/"amount of code written" is way lower than for VB - in other words it took me far longer to do the same thing in C++ than in VB), and Delphi (which after about half an hour of trying things out the compiler started throwing internal errors at me, so I figured Delphi wasn't the language for me either).
This happened about the same time as the first beta of VS2002 came out (VB7/VB.Net), I had ordered a copy so I was trying it out and learning about the new VB.Net language, and I liked it way better than the previous editions of VB, so the choice wasn't really that hard. Besides, I couldn't find any other VB compiler (non-MS and compatible with MS' version of VB) out there, so it always feels good to create something new :)
The original intention was to write a compiler that compiled VB.Net into native code (I did actually get it produce a HelloWorld.exe, it was however mostly hardcoded into the compiler), but when I got to learn more about the managed world I realized that the intelligent thing to do would be to create a compiler that compiled IL to native code and therefore targeting several languages in one hit. The amount of document-reading required to do this (I would have to learn IL, assembler, the PE file format...), made it completely uninteresting, so I rescoped my project into creating a compiler to compile VB.Net into IL, just as vbc.exe does it.

Why did I write the compiler in Visual Basic.Net?
It didn't start out like that, I first tried C++ (yes, even though I didn't like it, but I thought it was a great change to learn it better and maybe make the mentioned result/work relation better). You can actually still see this in the code (check out the comments at the end of this file). The effort didn't last much though, after a couple of days of debugging memory leaks and seeing weird compiler messages all the time I got bored and figured I'd have to change the source language if I was ever going to finish anything at all.
Next try was VB6. I'd read everywhere that no sane person would write a compiler in VB6, so I decided to kill that myth. After a couple of hours the myth had survived. The lack of inheritance in VB6 made me crazy, I saw myself either copy-paste huge amounts of code, or write it all in functions, with no OOP whatsoever, neither very pleasant alternatives. Especially now that I was learning the new Visual Basic.Net language. Once again, the choice wasn't really that hard.

8 comments:

  1. Hi Rolf.
    Can you tell me what documentation needs to be read for such a compiler ?

    ReplyDelete
  2. Keep up the good work. I worked with all versions of VB starting from version 1. Like many VB'ers I got stuck for what to use next after VB6. I tried VB.NET, but I figured I may as well try learning C#, given that it was similar to Java, which I was also dabbling with. We use a lot of VB.NET at work, so this will help a lot. Thnaks.

    ReplyDelete
  3. I think this is really great. Not just from the Mono angle, but now we have a good starting point for writing our own experimental language variants based on VB.NET.


    (I have been dying to try out an attribute-based preprocessor, but I couldn't do it wihout building my own parser and that is just too time consuming with work and school.)

    ReplyDelete
  4. Alex, you probably should start with "the dragon book".

    http://en.wikipedia.org/wiki/Principles_of_Compiler_Design

    ReplyDelete
  5. U rocks !!

    VB.net is a great langage, I work with C# and VB.net but I like VB.net more.

    This is very good news for mono, please continue this excellent work !!

    ReplyDelete
  6. Hi!

    Rolf, just hanks for all the hard work.

    I'm very interested in the topic of compilers for .Net/Mono. If you could just offer some more information of what is needed (apart from the dragon book).
    Which was your path to learn or translate to IL code? Any feedback is greatly appreciated. Thanks!

    ReplyDelete
  7. Excellent work Rolf. I've always wanted to write a compiler too. At university, I did a compiler design module where I got to modify an existing compiler for a very simple language (Triangle).

    A few months ago I started working on a COBOL compiler for .Net and Mono. I've been playing around with using classes from the Framework and external assemblies in my COBOL programs, and have got NUnit working with it.

    ReplyDelete
  8. Thanks for the nice post!

    ReplyDelete