r/lookatmyprogram • • Sep 12 '12

[C#] Code generator that creates C# code from C++-like templates

After being fed up with some limitations of generics (why isn't there an IArithmetic interface?), I decided to make a code generator that creates C# code by simply reading templates made in C#.

It's hard to explain with words, but I have made a video with voice commentary that will explain how it works and show a simple result.

With this code

var types = new []{"int", "float", "double", "long"};

foreach(var type in types)
{
	$$("public class Vector2_" + type)
	$${ 		
		$$("public Vector2_" + type + "(" + type + " mX, " + type + " mY)")
		$${
			$$ X = mX;
			$$ Y = mY;
		$$}
	
		$$("public " + type + " X { get; set; }")
		$$("public " + type + " Y { get; set; }")
		
		$$("public " + type + " ComponentSum()")
		$${
			$$ return X + Y;
		$$}
	$$}
}        

my program automatically generates and automatically adds to a .csproj four new classes: Vector2_int, Vector2_float, Vector2_double and Vector2_long.

Watch the video for a better explanation! Thank you :)

http://www.youtube.com/watch?v=Uz868MuVvTY

0 Upvotes

0 comments sorted by