CIL – Compiler construction

I’ve created a little sample on how to make your own .NET compiler.
The compiler uses Gold parser for parsing and Reflection.Emit to generate the compiled .exe file.

Initially I intended to make a sample on how to use Gold parser to parse and then compile Linq expressions, thus the name GoldLinq, however, Linq have now been replaced with Reflection.Emit.

Links:
My compiler source: http://www.puzzleframework.com/Roger/GoldLinq.zip
(C# VS.NET 2008 solution)

Gold parser: http://www.devincook.com/goldparser/
Grammar: http://www.devincook.com/goldparser/grammars/index.htm

How it works:

  • Gold parser – Calitha engine is used to parse the source code into a parse tree
  • The parse tree is transformed into a typed AST
  • The AST is verified using visitor pattern, the verifier handles type inferrence and auto casts.
  • The AST is optimized using visitor pattern, the optimizer replaces constant expressions and statements.
  • The AST is compiled into CIL/MSIL using visitor pattern.
  • If successful, the compiler will generate a file called “output.exe” in the same folder as the compiler

Samples:

Hello world 

display 'Hello World!'

 Have a nice day:

Display 'What is your name?' Read Name 
Display 'Hello, ' & Name & '. Have a nice day!'

Blastoff: 

assign n = 10 
while n >= 1 do 
    display n 
    assign n = n - 1 
end 
display 'Blast off!'

Miles and kilometers: 

Display 
'Do you want to convert 1) Miles to Kilometers or 2) Kilometers to Miles?' 
Read Choice        

if Choice == 1 then 
    Display 'Please enter the number of miles' Read Miles 
    Display Miles & ' Miles = ' & (Miles * 1.609)  & ' Kilometers' 
else 
    Display 'Please enter the number of kilometers' Read Kilometers 
    Display Kilometers & ' Kilometers = ' & (Kilometers / 1.609)  & ' Miles' 
end

Secret number: 

Assign SecretNumber = 64 

Display 'Please guess the secret number' 
Read Guess          

While Guess <> SecretNumber Do 
    If Guess < SecretNumber Then 
        Display 'Your guess was too LOW. Try again.' Read Guess 
    End     
If Guess > SecretNumber Then 
        Display 'Your guess was too HIGH. Try again.' Read Guess 
    End 
End     

Display 'Correct!'

How to compile the samples:

First you need a source file to compile, just create a normal textfile and paste one of the samples above into it.
Once you have the code file you can compile it using the compiler:

c:whatever\bin\debug> GoldSample.exe mysource.txt

When the compiler is done, you can start the compiled application with:

c:\whatever\bin\debug> output.exe

The code is somewhat too big to cover in a blog post, so if you have any questions feel free to ask in the comment section.

//Roger

3 thoughts on “CIL – Compiler construction”

  1. Hi Roger,
    I still can’t use the forums on the Puzzel framework site. At one point I was able to login but posting of messages failed.

    I have a propostion for you. I really like the SyntaxBox control and it seems to be stagnating on the puzzel site as part of the framework. Would you be interested in allowing me to put the code on CodePlex as a stand alone project? Technically I could just fork it as long as I follow the OS license guidlines, but would much rather have you on board.

    I compared SyntaxBox to the latest from http://www.actiprosoftware.com/ and SyntaxBox is not that far behind it and as a seperate project could do very well.

    I could also convert some of the old docs on from the compona site for use on this new codeplex project.

    In case you are intersted here is a screenshot of SyntaxBox in my upcoming database Admin tool:
    http://www.amsoftwaredesign.com/node/34

    Let me know what you think.

    Tony Caduto
    AM Software
    http://www.amsoftwaredesign.com

  2. Bestill my heart, someone beat me to it! Yours is better than mine would have been anyway! Nice work! I especially like the demo of the optimization. That’s something that’s not covered in very many places, at least not in code.

  3. A very late entry for CIL – Compiler construction. I’m trying to learn about compilers and build a simple one and this sounded like an interesting example (plus I’ve seen your stuff before (SyntaxBox) and it rocks) so I was hoping to get a look but following the link goes to your PuzzleFramework.com page where I had no idea what I was meant to do. How could I get the GoldLinq.zip file? Is it still available somewhere?

    Thanks for your help.
    Jim

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s