Source code generation using Python

Philip Semanchuk philip at semanchuk.com
Sat Dec 6 17:19:27 EST 2008


On Dec 6, 2008, at 4:47 PM, ats wrote:

> Hello,
>
> This is my first posting to a Python group (and I'm starting with
> Python seriously only now) , so bear with me if I make some mistakes.
>
> I want to generate 3 different versions of a C++ source code,
> basically injecting different flavours of inline assembler depending
> on target compiler/CPU.
>
> Code generation should be integrated into a 'master source file' which
> is the processed and generates the right code for GCC / MSVC or other
> cases. Something like:
>
>  int FastAdd( int t1, int t2 ){
>    int r;
>    ##if USE_INLINE_ASM
>      #ARG( eax, "t1")
>      #ARG( ebx, "t2")
>      #ASM( "add", ebx, eax )
>      #RES( eax, "r" )
>    ##else
>      r = t1+t2;
>    ##endif
>    return r;
>  }
>
> On processing, given constant USE_INLINE_ASM (or not) the right code
> is generated to a target file, which goes into the build process.
>
> I was looking for packages that can do this and came up with some
> candidates:
>
> - "empy" - http://www.alcyone.com/pyos/empy/ - It looks like it could
> do the job, but appears non-maintained since 2003.
> - "Cheetah" - Looks like more of a tool to do fix replacements of code
> snippets.
>
>
> There is some logic going on in the "ARG", "ASM" and "RES" sections,
> so I need to link code generation with true Python functions.

Hi Arne,
There are *lots* of packages for Python that replace chunks of  
predefined templates. Most are HTML-focused, some more so than others.  
I've used Mako (http://www.makotemplates.org/) to generate both HTML  
and Apache config files. It could certainly do C++. Some alternatives  
to Mako are mentioned in the documentation -- Kid, Genshi and Cheetah.

Rather than invite a flame war as to which is a better templating  
engine, I'll just say that I'm happy with how Mako addresses *my*  
needs. =) Good luck finding something that addresses yours.

Cheers
Philip


>
>
> The situation is really quite similar to HTML/PHP except, here we
> would have C++/Python.
>
> Any suggestions?
>
> Thanks,
> //Arne S.
> --
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list