Friday, June 02, 2006

Compiling velocity

So i started to write a Velocity compiler :-). It will convert a Velocity template into a java class which when compiled will produce a generator which will have the output comparable (ideally the same) with the original output of the velocity template.

The main reason for doing this is to see if there is any performance improvement. Right now .. the way the templates are used is very inconvenient. Another issue is the fact that the templates are still being edited so a "one shot" conversion will not do any good. Yet another reason would the coolness factor :-).

The task is quite challenging since velocity is not a typed language and the target language is typed. The way to do it properly is to have some sort of type inferrence engine to be applied for expressions to find the most likely type for it. It actually works quite nice. Also you have issues with variable scopes because of certain usage practices of velocity macros. For example you can write something like this:

#set($var = "");
#set($var = $expression);

If you translate this to:

String var = "";
var = expression;

and expression has a different type than String you are in trouble with javac.

More next time.

No comments: