C compiler written in Python Was: An idea.

Dan Bishop danb_83 at yahoo.com
Thu Jul 22 02:47:41 CEST 2004


"Thomas Guettler" <guettli at thomas-guettler.de> wrote in message news:<pan.2004.07.21.12.43.33.694021 at thomas-guettler.de>...
> Am Wed, 21 Jul 2004 03:59:37 -0700 schrieb winlinch:
> 
> > Hi!
> > I use Python, and writing some extension modules I think which could
> > be written an
> > C compiler, useful only to compile extension modules (I not think an
> > GCC!!!!), so that the user not have to use GCC, Microsoft Visual C++,
> > or other.
> > It must have an common API to all platforms, even if obviously the
> > implementation is various.
> > Could be write in 100% Python pure.
> > 
> > It is a bad idea?
> 
> Hi,
> 
> I like the idea very much. A C compiler
> written in python. Someone already started
> this. There was a post in this newsgroup, 
> maybe one month ago.

The link to this compiler is here:

http://people.cs.uchicago.edu/~varmaa/mini_c/

Another good idea would be to write an improved C preprocessor that

* is more consistent with the C language.  For example,
     macro SQUARE(x) = x * x;
  would be equivalent to
     #define SQUARE(x) ((x) * (x))
* in addition to MACRO and MACRO(args), allows forms as complex as:
     macro NEW T[arraysize] = calloc(arraysize, sizeof(T));
     macro NEW T(...) = T ## _new(...);
     macro ALLOCATOR typename(...) {init} =
        T* T##_new(...) {
           T* self = malloc(sizeof(T));
           init;
           return self;
        }
     ;
   which would allow you to write code like
      ALLOCATOR String(char *s) {
         self->length = strlen(self);
         self->data = NEW char[self->length + 1];
         strcpy(self->data, s);
      }
      int foo() {
         s = NEW String("Hello, world!");
         // ...
      }
* Has "typeof" as part of the preprocessor, so that code like
     macro DESTRUCTOR T {cleanup} =
        void T##_delete(T* self) {
           cleanup;
           free(self);
        }
     ;
     macro DELETE p = typeof(p)##_delete(p);
  will work.



More information about the Python-list mailing list