Python Formatted C Converter (PfCC)

Will Ware wware at world.std.com
Mon Oct 23 08:36:54 EDT 2000


Gareth McCaughan (Gareth.McCaughan at pobox.com) wrote:
> Find some
> simple subset of Python... Write a
> *translator* that turns this subset into C. That way, what
> you're writing is more or less real Python; if you do this
> right, you'll even be able to run the code in Python for
> prototyping purposes...
> ...even this probably
> isn't worth the effort it would take...

I've heard of people doing this sort of thing in Lisp/Scheme. This
actually sounds like quite a valuable idea. One thing that would be
quite helpful in constraining Python to make the translation easier
would be to avoid dynamic typing, for instance by using some sort of
Hungarian notation. I despise it as much as the next person, but it
would be a convenient way to signal to the translator the type of a
variable. Prefixes like "i", "u", "l", "f", "d" could stand for int,
unsigned int, long, float, and double. When such a thing was assigned
a constant value, it would be easy to check whether it fit within the
range (oops, assigned a negative value to an unsigned int, flag a
warning). Where Python allows lists, you probably want to declare
fixed-length arrays of fundamental types or pointers.

It would also be handy if the process of translation, compilation,
and linking could all be controlled from within Python. It's possible
within Python to get a parse tree for a function, which can be used
for identifying blocks (translating indentation into braces), knowing
where to declare variables, knowing their scope, etc. So the translator
could take a function as an argument and return a string of C source
code. The name might be mapped in some predictable way, e.g. "foobar()"
might become "c_foobar()". Then it's not too hard to put a wrapper
around it to turn it into a module, write it to a file, and call the
compiler and linker.
-- 
# - - - - - - - - - - - - - - - - - - - - - - - -
# Resistance is futile. Capacitance is efficacious.
# Will Ware	email:    wware @ world.std.com



More information about the Python-list mailing list