A Python Interpreter Written in Python (aosabook.org)
64 points by xk3 4 days ago
BoppreH 22 minutes ago
> 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 self-hosted a 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.
vachanmn123 27 minutes ago
Very well written! Everyone used to tell me during Uni that stacks are used for running programs, never ACTUALLY understood where or how.
anitil 4 hours ago
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!
blueybingo an hour ago
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.
tekknolagi 4 hours ago
bjoli 4 hours ago
And, in some ways, PyPy. I still think it is the sanest way to implement Python.
It makes me sad that I have to write C to make any meaningful changes to Python. Same goes for ruby. Rubinius was such a nice project.
Hacking on schemes and lisps made me realize how much more fun it is when the language is implemented in the language itself. It also makes sure you have the right abstractions for solving a bunch of real problems.
actionfromafar an hour ago
Well, one could rewrite Python (perhaps piece by piece?) in Shedskin.
Shedskin is very nearly Python compatible, one could say it is an implementation of Python.
anitil 4 hours ago
> And, in some ways, PyPy
What do you mean by that? I'm not familiar with PyPy
nxpnsv 4 hours ago
woadwarrior01 2 hours ago
aka A Metacircular Interpreter
andltsemi3 an hour ago
"Yaw dog I heard you liked python, so I put python in your python so you can interpret python while you interpret python"
hcfman 3 hours ago
Just wondering why you stopped there? Why not a python interpreter for a python interpreter for python ?
dnnddidiej 2 hours ago
It already is that.