Python Formatted C Converter (PfCC)

Alex McHale daimun at home.com
Sun Oct 22 03:24:03 EDT 2000


Hi there,
  I have started work on a project that I *think* is unique.  If not,
someone please send me a URL to such a project's site.
  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.
  First off, let me say; I have a working prototype, with extremely limited
functionality.
  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.  For example, consider this bastardized looking simple
program below:
---
main( ):
  int x=3, y=2
  if ( x < y ):
    printf( "%d %d\n", x,y )
  else:
    printf("heres another condition\n")
---
This compiles using my prototype preprocessor.
  The big question may be 'WHY?'  Easy.  I love the structure of programming
in python.  I think it is a very beautiful style, and wonderfully easy to
read.
  The simple preprocessor I have now was fairly easy coding, and will act as
the groundwork for the rest of the project.  The preprocessor is written in
python.  Below are my thoughts on what will be required.
  1) Full debug information (the most of the debugging information
neccessary will be parse error detection and location)
  2) Automatic prototyping of functions.  I've got a few ideas on what to do
with this, but don't have a fully structured image of exactly what I want
yet.
  3) Problem areas:  anywhere that uses :'s, such as switch statements.  I
believe this shouldn't be an issue, based on the current preprocessor
prototype.
***
The Details of My Algorithm in the current preprocessor:
  Please don't dismiss this algorithm for gaping holes such as switch
statements issues.  The idea is to get the basic preprocessor working, and
expand.  The code I have will be flexible enough to not turn to bloat
(assuming its not too bad now).
  The way the preprocessor works [abstractly], is that I use var =
fd.readlines( ) to load the whole file into a list, then do a
roc_block( var ).  proc_block is a recursive function (though a somewhat
bastardized one) that takes one argument.
  The base case is when it's argument (which will always be a list of lines)
has a length of 0.  The function consists mainly of a while x <
len(argument) loop.  Inside the loop, argument[x] is taken as line.  line is
stripped of its newline characters at the end.  Then, it is stripped of any
trailing whitespace.
  Next, we check if line ends in :.  If so, use a loop to determine the end
of the block nested by line.  We taper the value we will return such that it
goes statement { proc_block( block_nested_by_statement ) }.  If the line
doesn't end in :, we taper the value we will return by statement + '\n'.
***
  That is *basically* the way it goes, as it stands.  The aim is to
eventually be able to code something like this:
---
def main( ):
   int x, y = 2, 3
   int w,z
   w,z = 4,5
   if z < y:
      printf('%d %d\n', w,z)
   else:
      printf('another else\n')
---
  You get the idea.  The def call will probably be used - as an aide in
prototyping.
  Sorry for the newsgroup plague - this hellish post.  If I have garnered
any interest, please let me know.  I am personally very interested to see
what I come up with.

  Alex





More information about the Python-list mailing list