Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python.announce > #3551

[ANN] pyastinterp - Tree-walking interpreter for Python (+ "advent of code" idea)

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Paul Sokolovsky <pmiscml@gmail.com>
Newsgroups comp.lang.python.announce
Subject [ANN] pyastinterp - Tree-walking interpreter for Python (+ "advent of code" idea)
Date Wed, 23 Dec 2020 19:01:19 +0300
Lines 118
Approved python-announce-list@python.org
Message-ID <20201223190119.70f5ac9b@zenbook> (permalink)
Reply-To python-list@python.org
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de qKhoswuf7el5a3zyza3bcgUIy8eft/3I5PObvTS1xkBQ==
Authentication-Results mail.python.org; dkim=pass reason="2048-bit key; unprotected key" header.d=gmail.com header.i=@gmail.com header.b=vXuLY2KO; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'url-ip:140.82.121.4/32': 0.03; 'url-ip:140.82.121/24': 0.03; 'url-ip:140.82/16': 0.03; 'project,': 0.05; 'usage': 0.05; 'e.g.': 0.07; 'explicitly': 0.07; 'is.': 0.07; 'language,': 0.07; 'minimal': 0.07; 'modules': 0.07; 'underlying': 0.07; 'python.': 0.07; '===========': 0.09; 'ast': 0.09; 'holiday': 0.09; 'python-ideas': 0.09; "shouldn't": 0.09; 'skip:` 10': 0.09; 'to:addr:python-announce-list': 0.09; 'unlike': 0.09; 'subject:Python': 0.11; 'url-ip:140/8': 0.12; 'looks': 0.13; 'import': 0.15; 'possible,': 0.15; 'url:github': 0.15; 'python': 0.15; 'boilerplate': 0.16; 'confusion.': 0.16; 'consequence': 0.16; 'ecosystem.': 0.16; 'expense': 0.16; 'hackers': 0.16; 'holidays': 0.16; 'interpreter': 0.16; 'levels,': 0.16; 'python".': 0.16; 'python3': 0.16; 'readme': 0.16; 'relatively': 0.16; 'reusable': 0.16; 'runtime': 0.16; 'semantics': 0.16; 'semantics,': 0.16; 'somewhat': 0.16; 'subject:ANN': 0.16; 'subject:code': 0.16; 'subject:interpreter': 0.16; 'subset': 0.16; 'syntactic': 0.16; 'syntax,': 0.16; 'syntax.': 0.16; 'tree,': 0.16; 'tries': 0.16; 'url:cpython': 0.16; 'url:test': 0.16; 'useless': 0.16; 'x-mailer:claws mail 3.16.0 (gtk+ 2.24.32; x86_64 -pc-linux-gnu)': 0.16; 'values': 0.16; 'implement': 0.18; 'supported': 0.18; 'subject:] ': 0.20; 'maybe': 0.20; 'requires': 0.21; 'project.': 0.22; 'programming': 0.23; 'exception': 0.23; 'actual': 0.24; 'command': 0.24; 'goal': 0.24; 'code': 0.25; 'run': 0.25; 'written': 0.26; 'concerned': 0.27; 'it,': 0.29; "we'll": 0.29; 'mailing': 0.29; 'done': 0.30; '(and': 0.30; 'subject:[': 0.30; 'but': 0.32; 'there': 0.32; 'takes': 0.32; 'subject:for': 0.33; 'script': 0.33; 'available.': 0.33; 'concern': 0.33; 'unexpected': 0.33; 'year': 0.33; 'unless': 0.33; 'received:google.com': 0.34; 'particular': 0.34; 'pretty': 0.34; 'means': 0.35; 'from:addr:gmail.com': 0.35; 'same': 0.36; 'those': 0.36; 'running': 0.36; 'level.': 0.36; 'cases': 0.37; 'way': 0.38; 'something': 0.38; 'whose': 0.62; 'personal': 0.63; 'best': 0.63; 'clear': 0.63; 'pass': 0.65; 'during': 0.65; 'ago.': 0.66; 'well': 0.66; 'full': 0.67; 'less': 0.67; 'decided': 0.68; 'requirements': 0.68; 'choice': 0.68; 'received:212': 0.68; 'complete': 0.69; 'exactly': 0.70; 'fixing': 0.70; 'interpreted': 0.70; 'mit': 0.70; 'skip:o 20': 0.70; 'above,': 0.71; 'causing': 0.71; 'deal': 0.74; 'known': 0.76; 'released': 0.76; 'operations': 0.76; 'challenges': 0.77; 'easy': 0.77; 'quick': 0.79; '(c)': 0.82; 'paul': 0.82; 'url:lib': 0.82; '(such': 0.84; 'besides,': 0.84; 'catching': 0.84; 'debugging,': 0.84; 'generators,': 0.84; 'interpreters': 0.84; 'itself.': 0.84; 'license.': 0.84; 'minimalist': 0.84; 'opposite': 0.84; 'delivered': 0.85; 'avoiding': 0.91; 'stopping': 0.91; 'spiritual': 0.93; 'goals': 0.96
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mime-version :content-transfer-encoding; bh=E05vnai4nhBYNPa0uf0pRqDhKTQihiHmAzMztg8bnwg=; b=vXuLY2KOwVLUUNlduYZrKcR6m6Dz/aBkIHsrdBD07ob4Wuvp1mlLBq4n94I9rHhPr2 R7W00QjNgbXCIZRoBVm1XtHZ8o7vNa9j5uIkN84sZ7B5J6AfMsYnzw6FL/jFFapVciTc ZByqU2Jx3PbLfqzwQl84b2zaQqPgpYzADuEBnlA0YtjD0qHME1Oaxu/Pnd2c2MSDJopX oMU/KtTQLW1dBW+MKF89YcI3NIbs1cFzPsUMuviPsz5+PfqtIHophLIMKDRw6bvyKZ5Q 12qeUbxp5nMXDJSZUDjrGpC+YrNBUzDzPlvOME2YSQ+4XoOWTwR0CcCOD1v5Jt2HHyFZ 8knw==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-transfer-encoding; bh=E05vnai4nhBYNPa0uf0pRqDhKTQihiHmAzMztg8bnwg=; b=J7OPUx2NMgaXNfIK5L2e28BU4+jEuvoNhopJRW2X2Idv/eXj8BAZINbYk9EZBv3H5n OW470yWeJnLUIdUkM4rYUENgWAgjjD+81auCRgmpH7smou9jJ476bFa3DxrWHuStirbB nrQHtoOTsLW2udI780cY3VgEoFk2YgRJCWjqlsNmXOG8DmS+w0ie/Kzy8HdXkA164CxX JajjdFZgHhQGFgZL3b2zGDbABoA70VyQVrauCFUyqWz0ziUXAVwXI3hVZgmZBr2hEK9W cQ2Q33SeDKZ2SjSG4VcIXU4lYGQCiso0GYL4sMMwv7cwxiCWAC+/ZH6J1lzUa5q8kNqk tqBQ==
X-Gm-Message-State AOAM5322cK5LItk+Za8vzDeInMd28nABFV0ztJN713Y4bxr34j5LPzn+ nYhDpp7Iuv/a6ZgdV90AlPkH6X2y9Os=
X-Google-Smtp-Source ABdhPJwcKx6unba2WlSEjpPQ95beX7vyNXtH4twLoI/3md3hLX3LyNPZmoL/OY/BzekKBFJm42jXNA==
X-Received by 2002:a17:906:c45a:: with SMTP id ck26mr24808253ejb.200.1608739282808; Wed, 23 Dec 2020 08:01:22 -0800 (PST)
X-Mailer Claws Mail 3.16.0 (GTK+ 2.24.32; x86_64-pc-linux-gnu)
X-MailFrom pmiscml@gmail.com
X-Mailman-Rule-Hits emergency
X-Mailman-Rule-Misses dmarc-mitigation; no-senders; approved
Message-ID-Hash M24A7UXWN3XW7QDIR77KEEHK7XYVWL2Z
X-Message-ID-Hash M24A7UXWN3XW7QDIR77KEEHK7XYVWL2Z
X-Mailman-Approved-At Wed, 23 Dec 2020 11:31:40 -0500
X-Mailman-Version 3.3.3b1
Precedence list
List-Id Announcement-only list for the Python programming language <python-announce-list.python.org>
Archived-At <https://mail.python.org/archives/list/python-announce-list@python.org/message/M24A7UXWN3XW7QDIR77KEEHK7XYVWL2Z/>
List-Archive <https://mail.python.org/archives/list/python-announce-list@python.org/>
List-Help <mailto:python-announce-list-request@python.org?subject=help>
List-Post <mailto:python-announce-list@python.org>
List-Subscribe <mailto:python-announce-list-join@python.org>
List-Unsubscribe <mailto:python-announce-list-leave@python.org>
Xref csiph.com comp.lang.python.announce:3551

Show key headers only | View raw


Hello,

pyastinterp is a tree-walking (aka meta-circular) interpreter for
Python. It was the most complete tree-walking interpreter as of a year
ago. As it was written as a personal "advent of code" project during
the winter holidays of 2019/2020.

I remembered about it this holiday season, and decided to post about
it, both as a holiday eyecandy, and as a challenge to other Python
hackers (such winter holidays challenges are known as "advent of
code" (of course, unless you believe there's only one of them)): can you
write a more complete implementation? Or maybe you did during this
year? Please post about it somewhere (perhaps, python-ideas mailing
list?).

https://github.com/pfalcon/pyastinterp


README with more details follows:

pyastinterp
===========

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:

1. Be as complete as possible (and see how much effort that takes and
   if there're any limits).
2. Yet be as minimal as possible, avoiding useless boilerplate and
   reimplementing what may be already available.

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](https://github.com/pfalcon/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:

1. Is concerned with syntax, not semantics (largely).
2. That means that it tries to interpret Python AST tree, but
   relies on the underlying Python runtime to implement the actual
   operations (semantics). In some cases `pyastinterp` actually
   has to deal with runtime semantics, but the point is that it
   should be done only when there's no other choice (i.e. rarely).
3. This in particular means that `pyastinterp` itself requires a
   (pretty) full Python implementation to run, it's not the case
   that it's written in "some subset of Python". (Contrast that
   with e.g. PyPy's RPython, which is a Python subset language,
   in which interpreters are written).
4. Another consequence is that there's no semantic separation
   between "metalevel" (level of the interpreter) and target
   application level. This is again unlike PyPy, where 2 are explicitly
   separated. Lack of separation allows to save on "marshalling"
   values between the 2 levels, but also make it possible to have
   "leaks" between levels, e.g. unexpected and unhandled exception
   in the interpreter to be delivered to the application, causing
   havoc and confusion. Pyastinterp's response to this concern
   is a pledge that there shouldn't be such unhandled interpreter-level
   exceptions. Of course, catching all such cases may take quite
   a lot of testing and debugging, and fixing them may affect code
   minimality and clarity. We'll see how it goes.
5. However, due to choices described above, implementation of many
   syntactic constructs is very clear, concise, and isomorphic
   between the target and meta levels. See e.g. how "if" statement
   is implemented: the implementation looks almost exactly how the
   usage of the "if" statement is.


Supported Python subset
-----------------------

Pyastinterp 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.


Running
-------

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.


Credits and licensing
---------------------

Pyastinterp is (c) [Paul Sokolovsky](https://github.com/pfalcon) and
is released under the MIT license.



-- 
Best regards,
 Paul                          mailto:pmiscml@gmail.com

Back to comp.lang.python.announce | Previous | Next | Find similar | Unroll thread


Thread

[ANN] pyastinterp - Tree-walking interpreter for Python (+ "advent of code" idea) Paul Sokolovsky <pmiscml@gmail.com> - 2020-12-23 19:01 +0300

csiph-web