Shawn Driscoll's Tech Blog


Stuff That Happened in the Past

Friday, May 4, 2012

Converting Python Code to C Code

I know this will sound crazy to most people.

I need a program that will convert Python code to C code. Yes, I know that Python can make calls to other programs (like C) when machine code speed is needed. Yes, I know that I'm going against the grain, since nearly all converters translate from C to Python.

All I can say is, "It's a science experiment."

Anyway, I already have the Python-2-C translator code thought out in my head (in Python, of course, because I love working with Python) in case I end up doing the programming myself. It's going to be a two-pass converter so far.

The first pass converts each Python line of code into a C line of code (notice how I'm not saying C++). The second pass will add in the variables and declare them.

The resulting C code should work for a specific C compiler I have for a specific CPU I've been wanting to play more with. So why not write the programs in C in the first place, instead of writing a converter to do it? Because I prefer to program in Python instead of in C. And I can write programs much faster in Python. And if the converter spits out C code just as fast I can write Phython code for it, then my C programs get done faster than if I hand-type them and go through all the debugging, etc.

I know I'll need to find a balance somewhere between writing Python code that easily translates into C code while not taking the fun out of programming in Python because I'm structuring my programs as though I'm writing in C. I might as well just write in C if Python becomes too much like it. I'm talking crazy talk now. I'll find a good middle ground to stay in. And it should be a fun science project.

No comments:

Post a Comment