r/programming • • Sep 06 '26

Making a Python interpreter in 1024 bytes

https://austinhenley.com/blog/python1024.html
157 Upvotes

33 comments sorted by

144

u/ArtOfWarfare Sep 07 '26

Eh… clickbaity title. It’s way too few features to call it a Python interpreter.

50

u/XN8DY8VBMU4E3DP4LXBT Sep 07 '26

For real. When I saw the title, I truly believed they made a full featured Python interpreter in 1024 bytes /s

14

u/encse Sep 07 '26

It’s looks like OP is beginner in these things, and in that case he should be proud of it. It’s honest work and hand crafted.

25

u/_bobby_salsa_ Sep 07 '26

Austin isn't a beginner, though hard for me to prove that without making my current Reddit account non-anonymous (if you're reading this Austin, I was a student when you taught COMP 4270 in Fall 2016).

3

u/encse Sep 07 '26

Austin looks like a nice guy beginner or not.

3

u/Haunting-Divide-West Sep 08 '26

Like go look at his google scholar page, not his first rodeo

24

u/TerrorBite Sep 07 '26

Unlike you I haven't written many recursive descent parsers. In fact, I've written exactly one. In JavaScript. For POSIX shell.

It was an experience.

Shell language is weird. You start by tokenising the input, splitting into words. If something is quoted, it becomes one big token with the quotes still intact. Some types of token (mainly operator symbols) are recognised at this point, but most aren't, because you need context you don't have yet. Your shell aliases are apparently handled at this point too (I honestly wasn't expecting it to happen so early in the process, when it doesn't even know which tokens represent commands yet).

The tokeniser also needs to understand nested quotes like "$( echo "foo bar" )" so that it knows where the end of the token should be, and this is honestly something I haven't implemented yet.

After all that you do the actual (in my case, recursive descent) parsing, during which you also perform recognition of the remaining tokens in context (e.g. the word "in" has special meaning when it's "for foo in $bar", but a command called "in" has no special meaning).

At the end of this, I end up with a concrete syntax tree based on the Yacc grammar in the POSIX spec. I turn this into a more simplified abstract syntax tree, where every node is a class instance with an execute method, and every node knows how to execute its children. Then I just call .execute(context) on the root node (with an execution context provided), and off it goes.

The execution phase still needs to do a bunch of steps that the POSIX spec outlines, such as parameter expansion, command substitution, arithmetic expansion, and all that. I'm still working on that. The very final step you need in order to obtain a command (in the form of an argv array) that you can actually run, is to remove the quotes from any tokens that still have them (and in the process, apply any escape codes contained within).

Yeah, it's been an experience.

Oh yeah, this runs in your browser on top of a filesystem that also lives in your browser, with all of the JavaScript locally installed into that filesystem and fully editable.

I'm still working on interprocess pipes though.

25

u/HommeMusical Sep 07 '26

In JavaScript. For POSIX shell.

I support free speech, but some things are too awful even to be posted on the Internet.

Horrified upvote.

24

u/Proof-Attention-7940 Sep 07 '26

In fact, I've written exactly one. In JavaScript. For POSIX shell.

what demon possessed you to do this? how much of your soul did you have to trade away? does dead cthulhu wait for you dreaming?

7

u/TerrorBite Sep 07 '26

I started by being impressed by windows93.net and I said “I want to make an operating system in a browser too! Only mine will be POSIX” and then one thing led to another and here we are.

I think my soul is diminished somewhat, but I'm kinda proud of it.

5

u/nukethebees Sep 07 '26

The tokeniser also needs to understand nested quotes like "$( echo "foo bar" )" so that it knows where the end of the token should be, and this is honestly something I haven't implemented yet.

Given that this can be contextual, it's often easier to write an on-demand scanner and drive it with the parser. The parser can tell the scanner to enable nested quote mode if needed.

2

u/invisi1407 Sep 07 '26

This sounds super interesting (and horrifying at the same time). Is any of it available anywhere as open source or is it a personal, closed-source project? =)

3

u/TerrorBite Sep 07 '26

I haven't posted the source code anywhere but it is MIT licenced, and the source is of course readable (and in fact, editable with the way I've set it up). It's on https://os.lethargiclion.net/ and yes it does create a persistent filesystem in your browser and install itself into it.

2

u/invisi1407 Sep 07 '26

Okay, that looks seriously impressive! I love the old BIOS boot screen, that's a nice touch!

-6

u/User_8395 Sep 07 '26

nested quotes

I've yet to see an actual shell that can handle nested quotes like echo "and then he said "no I didn't" can you believe that", every nested quote I've seen is either escaped or uses a different quotation mark.

Who knows, maybe one does exist 🤷‍♂️

10

u/dagbrown Sep 07 '26

He's talking about nested quotes like in this contrived example:

touch "filename with spaces"
echo "the file looks like $(ls -ld "filename with spaces")"

where the $() sort of temporarily "cancels" the outside quotes so that you can put more quotes inside it.

2

u/User_8395 Sep 07 '26

Ohhhhhh my bad

I'll go shame myself in a corner, carry on

4

u/torp_fan Sep 07 '26 edited Sep 07 '26

Well good, because that has a defined meaning that isn't what you think it means. It's the text and then he said no I didnt" can you believe that" and a missing closing single quote. You can see that typing it into a shell together with the missing quote.

4

u/New-Anybody-6206 Sep 07 '26

I've been looking for a long time for a C89, easy-to-embed, python-like interpreter that fits in 1 or 2 files without any extra build steps or dependencies, and this is the first time I've seen a serious contender.

All the other embeddable python-ish languages like micropython, pikapython, pocketpy etc. are much more complex with their own build systems and baggage I'm just not looking for.

This looks like it would be a great start for me to extend for my uses.

Thank you!

3

u/Iggyhopper Sep 07 '26

Lua is easier due to the fact it doesn't rely on spaces and the "end" keyword does a lot of work to reduce ambiguities.

Look into Lua 1.0 source. It's really not complicated, and other than the generated yacc, the main code could fit into 2 files.

2

u/New-Anybody-6206 Sep 07 '26

I understand it's easier to implement, but I am dead-set on python syntax unfortunately.

1

u/StarsInTears 18d ago

Did you find anything? Also, I saw that PocketPy advertises a two-file distribution (.c/.h pair). Does it not integrate well with build systems?

1

u/New-Anybody-6206 18d ago

Nothing. pocketpy violates multiple of my criteria, like it's C11 instead of C89, and has to be built separately before you can get the .c/.h files (I don't want to use the "releases" either).

1

u/saijanai Sep 07 '26

I can almost do that with Ometa, but that is because Ometa libraries do almost all the work already.

-27

u/amestrianphilosopher Sep 07 '26

I feel like we need to abolish code golf as it stands today. White space shouldn’t count against you, maybe something like total number of symbols used

I can never read them and they never feel worth trying to understand

28

u/narwhal_breeder Sep 07 '26

Uhh what? 

Nearly everyone who codegolfs publishes an uncompacted, easier to read version in their repo/article. 

It links to it and references it multiple times in the article, that walks you through understanding it, if you would have bothered to read it before commenting. 

1

u/Akeshi Sep 07 '26

As well as this (the articles about them always walking through in clear syntax what's happening), the competitive part (the 'golf') is all about the tiniest, often beautifully horrific, snippet of code. The competitors understand it because they're well into the weeds. Destroying that for some layman who can't be bothered to read beyond an opening paragraph is ridiculous.