CSR is a small compiler which generates .NET executables. I wrote it for my CS bachelor's degree thesis, Generarea de cod executabil pe platforma .NET which translates to Generating Executable Code on the .NET Framework. For lack of a better name, I called it CSR which stands for Compilator Scris de Riscutia which translates to Compiler Written by Riscutia :)
I am releasing the sources under the FreeBSD license as (1) it might be of academic interest to someone and (2) I don't want to worry about backing it up whenever I reimage my PC and if I'm uploading them, I might as well make them public.
The code is untouched as it was written in March-April of 2008. Some strings are still in Romanian in the code samples as I had to present it to my teachers though everything else - source and comments - is in English.
The goal of the project was to write a compiler end-to-end, from processing the source text to generating executable code. It is a very simple compiler for a simple Pascal-like language.
The language supports the following data types: boolean, double, int, string, void (as return type) and multidimensional arrays of these types. It supports function declarations and basic CLR interop - calls can be made to static functions from external assemblies.
The front end is generated using Coco/R and uses snytax-directed translation to create an abstract syntax tree.
The back end walks the syntax tree and, using Reflection.Emit, creates a .NET executable file. It also does some basic optimizations: constant folding and dead code elimination.
Description of the source tree: :
/src
CSR.sln Visual Studio 2005 solution file
V.ATG Coco/R grammar definition
/CSR
/AST
Declarations.cs Function and variable declaration nodes
Expressions.cs Expressions nodes
Scopes.cs Visibility scopes
Signatures.cs Function singatures and signature matching
Statements.cs Statement nodes
Types.cs Data types
/Compiler
Compiler.cs Compiler
/Generated
Parser.cs Generated by Coco/R
Scanner.cs Generated by Coco/R
/Tests
BinaryOpTest.txt Binary operations involving implicit typecasts
CallTest.txt Function calls (signature matching)
ExprTest.txt Various expressions
ParserError.txt An invalid expression
ReturnEval.txt Evaluating function return value
Test.txt Backtraking to find 4-permutations of 4
The AST nodes also contain functions to emit the IL code.
Parser.cs and Scanner.cs are generated by Coco/R. To rebuild them, Coco/R should be run on the V.ATG file.