Hack a doodle, do.

By Darius Bacon

Tail: eaten. (Well, almost.)

I aimed to get my Python compiler to compile my Python compiler by today. Why? Because work’s going to be busy this week. No, why should it compile itself? No practical reason, but when the whole language won’t fit in 500 lines, this goal helps to decide which parts of Python make it in.

(A full Python compiler could certainly fit in the budget of 500 lines: that is, going from abstract syntax trees to CPython bytecode. But we also want a readable program explainable to early-career programmers; this code is already too intricate.)

Tonight’s result: it works! Incompletely in two senses:

  • This compiler takes a source-code module and produces a code object, which you can then run. If in the running it imports other modules, this invokes the usual Python import machinery, which calls the built-in Python compiler. Since my compiler has several modules, to compile and run them all together would require messing with the import machinery. There are in fact hooks to do this, but I haven’t bothered; instead I tested each module by itself,

  • One of my smaller modules, the assembler, uses a couple of unsupported features (the -= and += operators, plus assignment to nonlocal variables). There’s a straightforward fix, but I might instead redesign the whole assembler along lines suggested earlier by Dave Long.

Perhaps more interesting: why’d it take three weeks from the last announcement to get here? Partly because I’m busy and human, partly since much of the work has been reverse engineering, with incomplete and wrong documents (which I ought to submit fixes for Real Soon Now) – and partly I never asked for help. It wouldn’t have hurt!

  • 17 March 2014