A 1024-byte C program that runs Python-flavored code, loops and all
As a weekend exercise, Austin Henley set out to write a Python interpreter in just 1024 bytes of dependency-free C — no macros, no library tricks. The goal was never full CPython compatibility but rather a subset that still ‘looks like Python’: def, colons, indentation-based blocks, and parenthesis-free conditionals, enough to run a recognizable FizzBuzz. An initial 512-byte target proved unrealistic, so he relaxed the limit and adopted a build-it-then-shrink-it approach.
The design skips everything CPython does — no tokenizer, no AST, no bytecode. Instead it’s a recursive-descent parser that executes as it reads, keeping nearly all state in a few globals: a fixed buffer holding the whitespace-stripped source, a 256-entry symbol table, and a cursor. Variable names are limited to single lowercase letters so lookups index the table directly, and there’s no error handling at all — the interpreter trusts the input to be correct. Loops and functions work by saving a source position and physically rewinding the cursor to re-parse the same text each iteration or call, leaning on C’s own call stack for recursion.
Getting from a 4,800-byte readable version down to 1,024 bytes came from aggressive code golf: single-letter identifiers, implicit int types and return values from C89, comma and ternary operators, ASCII literals, bitwise instead of logical operators, and function parameters repurposed as stack-preserved temporaries. Some tricks came from an old Stack Overflow guide on golfing C. The final interpreter supports integer variables and arithmetic with precedence, one comparison per expression, if/else, while and for-range loops (with else blocks), truthiness, and even recursive user-defined functions — a compact demonstration of how much language machinery you can express with almost no intermediate representation.
Read the full article
Continue reading at Hacker News →This is an AI-generated summary. Read the original for the full story.