> The best solution I have found, disregarding of language, is to use the same language to write a program that generates the code you need.
Sounds like a pitch for lisp; I haven't really used any lisp variant much, but I always hear about people saying they basically write a program to generate their program rather than actually just solving the problem directly.