Making a Python interpreter in 1024 bytes

266 points · 96 comments on HN · read original →

Points and comments are a snapshot, not live.

A code golf project fits a minimal Python interpreter into 1024 bytes of C.

Austin Henley wrote a Python interpreter in 1024 bytes of golfed C, using recursive descent parsing without an AST. It supports single-letter variables, arithmetic, if/while/for loops, function definitions, and print. The interpreter jumps backward in the source for loops and functions, using the C call stack for block structure. It makes no error checks and assumes correct input. The final golfed source is 1024 bytes; the readable version is over 4800.

What commenters are saying

Commenters praised the write-up and the code-golfing techniques, but many noted the interpreter only handles a tiny, fragile subset-single-letter variables, keywords identified by first character only, no error checking. Some compared it favorably to SectorLISP and SectorC. A camp argued the title is misleading: "Python-like" or "Python subset" would be more accurate. Others discussed whitespace handling complexity and recalled similar techniques in 8-bit BASIC interpreters.