TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Build python ASTs in 502 lines of python without using modules

2 pointsby asrpabout 8 years ago

1 comment

asrpabout 8 years ago
This release is a bit early but it does run just fine, at least for the tests I did. I thought I&#x27;d make it available today because of recent interest in similar topics [1, 2].<p>I didn&#x27;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&#x27;t needed except for debugging). At least top search engine results all point to using large tools and&#x2F;or needing multiple passes of different kind which makes the subject sound complicated and hard to get anything even working.<p>Prompted by arcfide&#x27;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&#x27;m starting to see why shorter code is easier to change globally. I&#x27;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&#x27;t as useful as I thought.)<p>I wasn&#x27;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).<p>[1] <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13797797" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13797797</a><p>[2] <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13825225" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13825225</a><p>(both are not quite the same, just similar)