Home > Stuff > Code Generator

Code Generator

December 29th, 2011 admin Leave a comment Go to comments

The code generator is a tool / language that is intended to generate repetitious code, or code that relies on permutations. A great example is C++ meta binding code, especially when it comes to emulating variadic templates (permute based on the number of arguments, whether it has a return type, whether it’s a static or instance method, etc).

Lets take a look at that language! Here we have our first “Hello world!” example:

The generated text basically looks like:

Hello world!
Hello world!

Hello world is actually printed 11 times, because we created a permutation that goes from 35 to 45 and includes both ends. The text that is repeated is all the text that comes after “source%” on the very next line. This example isn’t very interesting or useful, lets look at another!

The output:


Hello world!
Over the hill!
Hello world!

In the above example, we added the concept of handlers. A handler is basically a block of replacements that will be made, only if a condition is met. A handler’s condition is checked for every possible permutation. The <? ?> syntax means that actual C# code goes between there. The condition we used was ‘Age == 40′ (C#), which will only evaluate when we permute from 35 to 45 and hit 40 in between. Once that condition is met, the replacement is made from “Hello world!” to “Over the hill!”. In this next example, we’ll introduce the concept of code blocks for replacement:

The output:

Hello 35 year old person!
Hello 36 year old person!
Hello 37 year old person!

In this last example, we made a more complicated replacement. Instead of replacing one string with another, we actually replaced a sub-string with a string that was built by code. In the handler case, the code between the <? ?> needed to return a Boolean, as it was a condition (in this case we used ‘All’, which means true or always). In the replacement case, the code needs to return a String. Notice also in the code we were able to utilize the permutation variable ‘Age’. This next example doesn’t really introduce much, but it highlights the potential of permutations:

The output:

Hello 35 year old person!
Goodbye 35 year old person!
Hello 36 year old person!
Goodbye 36 year old person!

Note that by also permuting bool (which has 2 values), we’ve doubled the number of output lines. The types ‘int’ and ‘bool’ are special in the language, as they are the only primitive types that can be permuted (without going into the obviously unnecessary usage of uint, short, ushort, etc). The int, as we’ve seen before has its own syntax for declaring a range. The bool doesn’t need one since it’s always only true or false. The last type we have to introduce is the array type (as well as a special replacement syntax):

The output:

Hello 35 year old person!
Bonjour 35 year old person!
Guten Tag 35 year old person!
Bon Giorno 35 year old person!
Namaste 35 year old person!
Ni Hao 35 year old person!
Hello 36 year old person!
Bonjour 36 year old person!

The array type allows us to iterate through an array of any type of object constructed in C#. The syntax is a little bizarre (for reasons I’d rather not go into), however, it is a very powerful concept. The syntax basically goes “array C#-Type[dimensions] = <? {Initializer List} ?>”. Note also that it can be a multidimensional array. In the future, array may be changed to anything enumerable (if time and necessity permits). The other thing that this last concept introduced was a special syntax for replacements. Notice we use First and Second without quotes “”. This basically is just syntactic sugar for “$First$”, which you’ll notice appears in the source. This last example shows everything else in the language at this point:

The C# file from the import statement:

The above example is pretty terrible and contrived, but until I come up with a better one that shows off all the features, this will be it. The keyword ‘alias’ allows for an alias of the variable name (you can refer to it by both names).

That’s about it! I’ll be adding more, but as for an introduction, this will have to do. Hope you like it!

Categories: Stuff Tags:
  1. No comments yet.
  1. No trackbacks yet.