Python AST interpreter (aka tree-walking interpreter) in Python. Aka meta-circular interpreter.
This is an AST interpreter (also known as tree-walking interpreter) for
the Python programming language, written in Python itself. It is thus
a meta-circular interpreter. While Python implementations of a Python VM
(i.e. bytecode interpreters) are relatively well known (starting with
PyPy), AST interpreters are much less common beyond those which implement
a very limited subset of the syntax.
Pyastinterp strives to fulfil 2 goals:
First goal is pretty obvious: ideally, we’d like Pyastinterp to be able
to interpret (run) any application a native Python interpreter can.
Second goal is rooted in many reasons. To start with, Pyastinterp is
a spiritual part of the Pycopy
project, whose goal is the development of minimalist Python-compatible
ecosystem. Besides, “bloated” meta-circular interpreters are well out
there (the same PyPy) - there’s lack on the opposite side of spectrum.
Pyastinterp is also intended to be a “teaching” and “for-research”
project. Surely, it’s much easier to learn and reuse/repurpose a small,
rather than large, project. These last requirements however also put
bounds on “minimality”: pyastinterp
tries to have a clear focus
(AST interpretation) and avoid unneeded boilerplate, but not be
obfuscated, and actually tries to be clear and easily hackable (provide
some reusable infrastructure), even at the expense of somewhat less
optimality/performance.
To achieve the goals of minimality, pyastinterp
does following:
pyastinterp
actuallypyastinterp
itself requires aPyastinterp should implement almost all Python3.5 features, stopping
at generators, which are not that easy to implement in a tree-walking
interpreter. It also does not include import hook, so the imported
modules are interpreted by the underlying Python VM, not by
Pyastinterp.
Quick way to run something interesting:
wget https://raw.githubusercontent.com/python/cpython/3.6/Lib/test/pystone.py
python3 -m astinterp pystone.my 10000
You can pass any other script on the command line and see how it goes.
Pyastinterp is (c) Paul Sokolovsky and
is released under the MIT license.