[Python-Dev] Crude Python->Parrot compiler

Andrew Kuchling akuchlin@mems-exchange.org
Wed, 26 Sep 2001 10:00:56 -0400


Over the last few days I've been experimenting with a simple
Python->Parrot compiler.  This morning I finally implemented binding
variables to a register, so it now can actually compute something.

See http://www.mems-exchange.org/software/files/parrot-gen.py for
the code.

Limitations:
* Currently this only understands a *really* limited subset of Python.

* The only allowable type is integer; strings, floats, long ints, &c.,
  aren't supported at all.  

* It will die with an assertion if you feed it a language construct it
  doesn't handle (def, class, most operators).

* Code structure is suboptimal; this is just a quick-and-dirty hack.
        
Example usage:
ute parrot>cat euclid.py
 
# Python implementation of Euclid's algorithm
 
m = 96
n = 64
print m,n
 
r = m % n
while r != 0:
    m = n
    n = r
    r = m % n
 
print n
ute parrot>python euclid.py
96 64
32
ute parrot>python parrot-gen.py -r euclid.py
96 64
32
ute parrot>cat euclid.pasm
main:
        set I3, 96
        set I10, 64
        set I9, 0
        add I0, I3, I9
   ... <rest deleted; you get the idea> ...

Currently the Parrot interpreter only supports integer, floating
point, and string registers.  There's no way to store the contents of
a register in memory as far as I can tell, and PMCs -- the polymorphic
objects that would correspond to PyObjects -- aren't implemented
either.  This means it's not possible to handle general variables, and
therefore we'll have to wait for PMCs before general Python programs
can be handled.

--amk