Managed BF Language Compiler
As usual, I received my February 2008 copy of MSDN magazine and
proceeded to devour it. An article that I found quite interesting was "Create a Language Compiler for the .NET Framework" by Joel Pobar. It talks about using the code generation features of the CLR to create a compiler.
A
co-worker of mine saw my copy laying around and said "Hey, you should
make your own compiler." So I proceeded to do just that. Looking
back, I think that he was joking.
The first thing that I needed
was a language to use. I thought for a minute about writing my own
language specification, but then remembered a hobby language that I
used to dabble with that would be perfect for my first attempt at a
compiler. There is a language called BrainF***
(I will leave the stars to your imagination) that was created to be a
simple functional language. A BF program consists of 30000 8-bit cells
(think sbyte[]) that are all initialized to 0 and is essentially based on pointer arithmetic. BF has only eight
possible commands:
- > : Go to the next cell
- < : Go to the previous cell
- + : Increment the value in the current cell
- - : Decrement the value in the current cell
- [ : Start a loop
- ] : End a loop (if the value in the current cell is 0]
- . : Output the char value of the current cell to the standard output
- , : Read a char value into the current cell from the standard input
If
you take a minute to process this, you will soon realize why it was
given its name. Programs written in BF quickly become very complex to
read and understand. However, in all of its simplicity (and the headaches that come with writing programs), it can be used
to perform just about any functional task that can be thought of. A
good place to find some interesting programs written in BF can be found
here.
The
compiler for this was actually pretty easy to implement and I have
attached the source for your viewing pleasure. The current
implementation just compiles the BF source as it is written without any
checks or warnings if the source is bad (i.e.- overflow, etc). The
next version will have a interpreter that basically runs the BF source
prior to compilation to validate it. There is a sample Hello World
source file in the attached code, and many more can be found online.
The source was written in VS 2008.
NOTE: This is not an
interpreter that runs the source in from a C# application. The
application is actually a compiler that takes the BF source and
generates an assembly that can then be independently executed.