Top
Best
New

Posted by azhenley 10 hours ago

Making a Python interpreter in 1024 bytes(austinhenley.com)
236 points | 84 comments
stevefan1999 2 minutes ago|
But to be honest, I wonder what is the smallest interpretable and practical Turing Complete VM? I would argue that implementing a brainfuck that we lower Python interpreter to, or even say like an interpreter untyped lambda calculus or SKI combinator would be very useful, especially for the hardware bootstrapping.

I'm talking about things like SectorLisp https://justine.lol/sectorlisp/

jrdres 8 hours ago||
The code makes me smile, because it's nasty. This isn't like C4, a tiny but complete C compiler which does error checking on its subset. Instead, this is worse than Sector C, which takes every shortcut and just plain assumes everything in the source is right.

This "Python" just plain assumes for keywords: Any "f" is a "for [x] in range[y]" (exactly that, no other for's). Any "w" is a "while". Any "i" is an "if". Any "d" is a "def". Any "p" is a "print("

Nasty, nasty.

(Also nasty is that the code snippets in the article has more comments than the github copy of the "readable" version. You need the article to understand what's going on.)

This is a just a bit too simple for a "Tiny Python". If somebody is willing to allow a few more K's of bytes, I'd love to see at least lists & dicts here--Lisp can do them!

tveita 4 hours ago||
As they say in TDD, write a test, then write the simplest code that will make it pass.

Clearly supporting multiple functions starting with 'p' would be overengineering.

bloppe 1 hour ago||
By that standard, this is totally over-engineered. Just hardcode it.
adamddev1 1 hour ago|||
Reminds me of the good 'ol Apple II BASIC. You can name your variables whatever you want, but only the first two letters matter.
userbinator 1 hour ago||
Two letters is luxury, when most BASIC interpreters in those days only recognised 1-letter variables.
kristianp 3 hours ago|||
> Any "w" is a "while"

Meaning that something as simple as "w = 4" would fail? A little too nasty for my liking. Not a choice I would have made, but admire the amount of work done here and the readability of the article. And it's more human-written code than I've done in a number of months!

eru 8 hours ago||
If you are willing to sacrifice performance, you can implement dicts via linear lookup in much less code than a proper hash table.
Someone 1 hour ago|||
Because Python dicts guarantee iteration order is the same as insertion order (https://docs.python.org/3.7/library/stdtypes.html#typesmappi...) Python dicts aren’t just proper hash tables.

Because of that it wouldn’t surprise me much if that sped up some standard benchmarks, for example ones parsing lots of small json objects into dictionaries.

FridgeSeal 7 hours ago|||
It’s Python, you’ve already sacrificed performance, what a little bit more?
eru 6 hours ago||
That's the spirit!

(Slightly less silly: the folks at https://github.com/faster-cpython are doing great work, too.)

tempay 4 hours ago||
Are they still? I thought this project was no longer really active.
teddyh 10 hours ago||
For those who actually need something like this in production, there is Snek: <https://sneklang.org/> “Snek is a tiny embeddable language targeting processors with only a few kB of flash and ram.”
jrdres 9 hours ago||
Yes, but compiling or modifying Snek from source is very challenging. I wish it was one single C file for an example base like Posix, instead of many files for many platforms plus a custom parser in Python (Lola).
eru 8 hours ago||
Or Forth.
marcelo-earth 8 hours ago||
Reading the article, I can't believe I just found out Code Golf is a thing. I've been a programmer for more than a decade.

But yes, amazing project! I like that it's human-made :)

kylecazar 8 hours ago||
The quintessential example is donut.c. I was amazed when I first came across it.

https://www.a1k0n.net/2006/09/15/obfuscated-c-donut.html

eclipticplane 6 hours ago|||
> This was my first attempt at obfuscated C and I feel it's pretty amateurish

I love feelings of inadequacy at 11:07pm on a Sunday.

Here's the judges' remarks and author commentary on the second edition of this from the 2006 results: https://www.ioccc.org/2006/sloane/index.html

HappMacDonald 3 hours ago||||
This one is my favorite:

https://github.com/ioccc-src/winner/blob/master/2025/ncw1/pr...

agumonkey 4 hours ago||||
Always fun to see this pop up years later. (Damn, 20 years now)
bolangi 7 hours ago||||
Thanks, this makes me happy.
Forgeties79 6 hours ago|||
Absolute legend

> it’s worked on every system I’ve tried so far though

Still my favorite part of the whole thing. Classic moment of “who among us hasn’t doesn’t this?” lol

Lerc 5 hours ago|||
Get thee to

https://beta.dwitter.net

Also https://github.com/nanochess

And then if you really want to go large

https://phoboslab.org/log/2021/09/q1k3-making-of

I still have a soft spot for https://www.pouet.net/prod.php?which=1221

rottc0dd 2 hours ago|||
This is my favorite : https://www.cise.ufl.edu/~manuel/obfuscate/pi.c
NooneAtAll3 2 hours ago||
look at https://codegolf.stackexchange.com
userbinator 7 hours ago||
To be precise this is 1024 bytes of C, which compiles to a binary many times larger, and implements a very tiny subset of Python.

loops work by jumping backwards and reparsing the source each iteration

This is how the DOS .bat processing works; not sure if Unix-style shells are the same, as I've never had the need to exploit that "feature".

Another comment here has mentioned C4, but another extremely dense (and slightly larger, since it wasn't actually deliberately(!) "code-golfed") interpreter you may want to look at is the J Incunabulum:

https://www.jsoftware.com/ioj/iojATW.htm

More generally, the array programming culture seems to consider this level of density the norm:

https://news.ycombinator.com/item?id=45800777

shakna 4 hours ago||
Bash lines are buffered, so modifying behind the program position doesn't really work, but you can self-append to the file to keep a script going infinitely.
maya335 3 hours ago||
[flagged]
krttherealest 23 minutes ago||
well written, looks cool ngl
anitil 9 hours ago||
This is really cool! It's so fun to see what you can achieve and what's optional. I have seen the 'single character variable' limitation in some other minilangs before, but using the source itself as the target of function calls and loops is new to me. It does make a lot of sense but I wouldn't have thought of that.
userbinator 7 hours ago|
but using the source itself as the target of function calls and loops is new to me

This was standard practice on interpreters for 8-bit microcomputers; with only a 64K total address space, creating an AST first seems immensely wasteful, so you interpret from the source directly.

I believe shells still do this when you run shell scripts; I know the DOS COMMAND.COM definitely does.

andai 5 hours ago||
Also by the author:

Let's make a teeny tiny compiler

https://news.ycombinator.com/item?id=36102460

Scubabear68 10 hours ago||
I was very disappointed that this is “interpreting” some tiny made up language.

This is not Python, or even within three orders of magnitude of Python.

stingraycharles 8 hours ago||
Yeah the amount of Python code that would work here is probably not a lot more than this specific FizzBuzz example. Lots of shortcuts taken, which I guess is understandable.
SPBS 9 hours ago||
It’s true, the title should have said “Python-like”
happycube 9 hours ago|||
TBF the fizzbuzz code works just fine in CPython.
benatkin 8 hours ago||
Indeed, for some code, CPython and this interpreter produce identical output. I gave it an upboat.
benatkin 8 hours ago|||
I like python subset. However, many don't see it that way.
hmry 7 hours ago|||
You're right, mathematically it's undeniably true. However...

One could imagine an even smaller subset interpreter. It's an interpreter for a subset of Python, consisting only of the programs that print "Hello World". Since it doesn't do any error checking, for all other programs the output is undefined. Implementing it is very simple: Just ignore the input file, and print "Hello World". As a bonus, it's an interpreter for the subset of "Hello World" programs in all other programming languages too!

</tongue-in-cheek>

tyilo 2 hours ago|||
It's not a subset of python.

For instance this program works in this interpreter, but not in python:

    fxx i in rxxxx(10):
        pxxxx(i)
tempodox 9 hours ago|
This seems to be in the same spirit as Justine Tunney's SectorLISP. Very cool.

https://justine.lol/sectorlisp/

forgotpwd16 2 hours ago||
SectorLISP makes an important question in its implementation: how much can strip down Lisp before stops being Lisp. Same is not done for submitted interpreter. So, although SectorLISP goal is to be a Lisp-reduced-to-its-essentials implementation, the Python-1024 goal seems to be imitating Python in most minimal code possible.
gabrielsroka 8 hours ago||
Or sectorC

https://github.com/xorvoid/sectorc

Edit: I wonder if sectorC could compile python1024

More comments...