Python Formatted C Converter (PfCC)

Gareth McCaughan Gareth.McCaughan at pobox.com
Sun Oct 22 18:43:14 EDT 2000


Alex McHale wrote:

>   The object may sound like an odd one.  But I'll give the rundown.  If you
> think it sounds stupid and pointless, good for you -- consider this all
> academic.  Just tell me so - no need to flame me if it's a horrible idea.
> If you like the idea, awesome, let me know.
..
>   The project is, as I call it, a Python Formatted C Converter.  What this
> means, is that it takes a python-style coded C program and converts it into
> compilable C code.

I think it's a horrible idea :-). Here are some reasons.

  - A C program written in "Python style", with indentation
    instead of braces etc, is likely to be very painful for
    most C programmers to read or modify.

  - Debugging such a program will be horrible, since the
    debugger will presumably work on the derived C code
    rather than the original Python-style code.

  - C written with Python syntax will be nasty for language-aware
    text editors. Emacs and Vim, for instance, have special modes
    for editing C and Python: but none for editing 0.5*(C+Python).

  - Some features, as you've discovered, don't *have* natural
    translations. Switch statements. The preprocessor. goto.
    I don't mean that it's impossible to find equivalents for
    them; I mean that there isn't a "canonical" equivalent and
    you'll find that anything you do feels wrong.

  - If you write something substantial in this blend of C and
    Python, I predict that you'll find that the surface Pythonesque
    appearance misleads you and makes you do things that aren't
    legal in C, or that are legal but mean something other than
    what you think.

  - The things that really hurt about programming in C rather
    than in Python are mostly semantic, not syntactic. Slapping
    a Python-looking surface syntax on will only make you get
    more frustrated about the things you really can't do.

If you have to write C, and find the Python syntax so much
nicer than native C syntax, here's a suggestion. Find some
simple subset of Python. Augment it with as many new constructs
as you need (and as few as you can get away with). 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 (make the new constructs look like comments
to Python). This is a much bigger undertaking than writing a
simple preprocessor to turn almost-C into C; but it's also
much more likely to be useful.

In this case, your sample program would turn into something like

    #!return-type int
    def main():
      #! type int x,y
      x,y = 3,2
      if x<y:
        print "%d %d" % (x,y)
      else:
        print "Here's another condition."

which is actually real Python. Note that some of the apparent
generality here might be bogus; for instance, you might only
support the % operator in special contexts, and not support all
its features.

My insufficiently humble opinion is that even this probably
isn't worth the effort it would take; but it's much more likely
to be worth it than a C-in-Python-syntax-to-C-in-C-syntax
translator.

Please note that I have no objection at all to you writing a
translator of the sort you're planning. It's up to you. My
aim here is to discourage you from doing something that you'll
almost certainly regret later.

-- 
Gareth McCaughan  Gareth.McCaughan at pobox.com
sig under construc



More information about the Python-list mailing list