Reading — step 1 of 5
Read
~1 min readReader & S-Expressions
Parse to AST
After tokenizing, parse the token stream into nested lists (the Lisp AST is just a tree of nested lists).
python
That's it. The recursive descent is trivial because Lisp's grammar is essentially just nested lists.
Example:
(* (+ 1 2) (- 5 3))
parses to
['*', ['+', 1, 2], ['-', 5, 3]]
The AST is just nested Python lists. No fancy AST node classes. This is the homoiconicity advantage — code lives as a data structure your interpreter already understands.
Round-trip: a print function (unparse) walks the tree and outputs (...) text. Read → Parse → Eval → Print → REPL.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…