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.
My compiler source: http://www.puzzleframework.com/Roger/GoldLinq.zip
(C# VS.NET 2008 solution)
Gold parser: http://www.devincook.com/goldparser/
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
display 'Hello World!'
Have a nice day:
Display 'What is your name?' Read Name
Display 'Hello, ' & Name & '. Have a nice day!'
assign n = 10
while n >= 1 do
assign n = n - 1
display 'Blast off!'
Miles and kilometers:
'Do you want to convert 1) Miles to Kilometers or 2) Kilometers to Miles?'
if Choice == 1 then
Display 'Please enter the number of miles' Read Miles
Display Miles & ' Miles = ' & (Miles * 1.609) & ' Kilometers'
Display 'Please enter the number of kilometers' Read Kilometers
Display Kilometers & ' Kilometers = ' & (Kilometers / 1.609) & ' Miles'
Assign SecretNumber = 64
Display 'Please guess the secret number'
While Guess <> SecretNumber Do
If Guess < SecretNumber Then
Display 'Your guess was too LOW. Try again.' Read Guess
If Guess > SecretNumber Then
Display 'Your guess was too HIGH. Try again.' Read Guess
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:
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.