logoalt Hacker News

Making a Python interpreter in 1024 bytes

236 pointsby azhenleyyesterday at 11:14 PM84 commentsview on HN

Comments

jrdrestoday at 1:09 AM

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!

show 1 reply
stevefan1999today at 9:59 AM

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/

teddyhyesterday at 11:43 PM

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.

show 2 replies
marcelo-earthtoday at 1:58 AM

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 :)

show 2 replies
userbinatortoday at 2:41 AM

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

anitiltoday at 12:39 AM

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.

show 1 reply
andaitoday at 4:16 AM

Also by the author:

Let's make a teeny tiny compiler

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

krttherealesttoday at 9:39 AM

well written, looks cool ngl

Scubabear68yesterday at 11:44 PM

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.

show 2 replies
tempodoxtoday at 12:08 AM

This seems to be in the same spirit as Justine Tunney's SectorLISP. Very cool.

https://justine.lol/sectorlisp/

show 1 reply
hankbondyesterday at 11:44 PM

Good use of free will and well-written. Very nice walkthrough austin!

peter_d_shermantoday at 3:27 AM

The condensed version is impressive to be sure, but I'm an even bigger fan of the readable version:

https://github.com/AZHenley/python1024/blob/main/python1024_...

Well done!

galkktoday at 6:34 AM

I hate when they measure the size of source code instead of the size of a binary.

I appreciate .kkrieger much more than this monstrosity

TZubiriyesterday at 11:21 PM

A lot of criticism of python often mentions the whitespace as lexical scope tokens, and that criticism is usually posited by users of the language.

As implementer of an interpreter, did you feel that whitespace for lexical scoping made the job of writing the lexer significantly more complex?

show 3 replies
ni5argayesterday at 11:33 PM

the blog post is pretty well-written! loved how he wrote about the the code-golfing part.

yuxinkingtoday at 5:21 AM

[dead]

sriniwasxtoday at 6:16 AM

[dead]

luciana1utoday at 2:18 AM

[dead]

einpoklumtoday at 7:25 AM

tl;dr:

1. Choose a small fragment of the language

2. Adapt an existing interpreter

3. Use a tool to shorten the code, 'minify'

ssfdgtoday at 2:24 AM

I don't understand the point of this. If they wanted to make a Python interpreter, why didn't they just ask an AI to do it?

show 2 replies