
Show HN: Build python ASTs in 502 lines of python without using modules - asrp
https://github.com/asrp/pymetaterp
======
asrp
This release is a bit early but it does run just fine, at least for the tests
I did. I thought I'd make it available today because of recent interest in
similar topics [1, 2].

I didn't think it was possible to get the whole thing into 500 lines (well,
502 lines but `Node.__repr__`, commented code and a number of other knick
knack aren't needed except for debugging). At least top search engine results
all point to using large tools and/or needing multiple passes of different
kind which makes the subject sound complicated and hard to get anything even
working.

Prompted by arcfide's project [1], I rewrote this one and the main part
(boot.py) went down from ~600 to 100 lines. Of course, the features available
are not the same but I'm starting to see why shorter code is easier to change
globally. I'm much more willing to refactor (by hand) anything that only
happens at most 6 times in the code! (And also maybe those other features
aren't as useful as I thought.)

I wasn't sure how to count lines for this one. The generated tree contains no
extra information (it can be regenerated from itself and the bootstrap
grammar).

[1]
[https://news.ycombinator.com/item?id=13797797](https://news.ycombinator.com/item?id=13797797)

[2]
[https://news.ycombinator.com/item?id=13825225](https://news.ycombinator.com/item?id=13825225)

(both are not quite the same, just similar)

