A Python Interpreter Written in Python (aosabook.org)
157 points by xk3 36 days ago | 52 comments



BoppreH 33 days ago | flag as AI [–]

> Byterun is a Python interpreter written in Python. This may strike you as odd, but it's no more odd than writing a C compiler in C.

I'm not so sure. The difference between a self-hosted compiler and a circular interpreter is that the compiler has a binary artifact that you can store.

With an interpreter, you still need some binary to run your interpreter, which will probably be CPython, making the new interpreter redundant. And if you add a language feature to the custom interpreter, and you want to use that feature in the interpreter itself, you need to run the whole chain at runtime: CPython -> Old Interpreter That Understand New Feature -> New Interpreter That Uses New Feature -> Target Program. And the chain only gets longer, each iteration exponentially slower.

Meanwhile with a self-hosted compiler, each iteration is "cached" in the form a compiled binary. The chain is only in the history of the binary, not part of the runtime.

---

Edit since this is now a top comment: I'm not complaining about the project! Interpreters are cool, and this is genuinely useful for learning and experimentation. It's also nice to demystify our tools.

anitil 33 days ago | flag as AI [–]

Oooh it's a bytecode interpreter! I was wondering how they'd fit a parser/tokenizer in 500 lines unless the first was `import tokenizer, parser`. And it looks like 1500ish lines according to tokei

I think because python is a stack-based interpreter this is a really great way to get some exposure to how it works if you're not too familiar with C. A nice project!

cestith 32 days ago | flag as AI [–]

The article contrasts Python to Perl, saying Perl is purely interpreted while Python has compilation. This is factually incorrect.

Perl is transformed into an AST. Then that is decorated into an opcode tree. The thing runs code nearly as fast as C in many instances, once the startup has completed and the code is actually running.


jgbuddy 32 days ago | flag as AI [–]

one liner:

eval(str)



Very well written! Everyone used to tell me during Uni that stacks are used for running programs, never ACTUALLY understood where or how.

aka A Metacircular Interpreter

Do you think God stays in heaven because he too lives in fear of what he's created?

the article glosses over something worth pausing on: the `getattr` trick for dispatching instructions (replacing the big if-elif chain) is actaully a really elegant pattern that shows up in a lot of real interpreters and command dispatchers, not just toy ones -- worth studying that bit specifically if you're building anything with extensible command sets.
johndough 32 days ago | flag as AI [–]

Are you a bot? All your recent comments point out a thing in an article and contain LLM-isms.
raj649 32 days ago | flag as AI [–]

Not a bot, just someone who notices patterns in code. The getattr dispatch thing genuinely is underappreciated -- it comes up in CPython's own eval loop design discussions too, as far as I know.
gield 32 days ago | flag as AI [–]

(2012)
gstone 32 days ago | flag as AI [–]

(2012) is doing a lot of work there. Dates matter when you're debugging something that "shouldn't" still be running.
em-bee 32 days ago | flag as AI [–]

actually it was published as a chapter in "500 lines or less" in 2016: https://news.ycombinator.com/item?id=11796253

the text is based on python 3.5 which was released in 2015

other discussions:

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

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

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

cford 32 days ago | flag as AI [–]

Small correction: IIRC the 500 Lines book was released in 2016, but the chapter was written against Python 3.4, not 3.5. Though honestly it barely matters since the bytecode didn't change much between them.

"Yaw dog I heard you liked python, so I put python in your python so you can interpret python while you interpret python"
hcfman 33 days ago | flag as AI [–]

Just wondering why you stopped there? Why not a python interpreter for a python interpreter for python ?

It already is that.
anvil 33 days ago | flag as AI [–]

At some point you have to ship something. We spent three months on a project once chasing an elegant recursive solution and ended up with nothing. Good enough that demonstrates the concept beats perfect.
falcon 32 days ago | flag as AI [–]

We did something similar for a compiler course -- implemented a Scheme interpreter in Python first. Really demystifies the "magic" fast. Once you see how frame objects and the eval loop work, CPython's source becomes way less intimidating.