Norvig's Lispy: a working Scheme interpreter in ~100 lines of Python
Peter Norvig’s classic tutorial walks through building a functioning interpreter for a subset of Scheme—nicknamed Lispy—in roughly 100 lines of Python. The pitch, borrowed from Steve Yegge, is that understanding interpreters and compilers is foundational to understanding how computers actually work. Norvig leans on Scheme precisely because its syntax is so spare: everything is an expression, there’s no statement/expression split, and operators like + and > are just ordinary symbols. That uniformity makes the parsing and evaluation logic remarkably compact.
The interpreter splits into two stages. Parsing tokenizes the source (a clever trick pads parentheses with spaces and splits on whitespace) and recursively assembles the tokens into a nested Python list that serves as the abstract syntax tree, with numbers coerced to int or float and everything else treated as a symbol. Evaluation is a single recursive eval function dispatching on a handful of cases: symbol lookups, numeric constants, the if and define special forms, and the general procedure-call case. An environment—just a Python dict seeded with math functions and operators from the operator module—maps names to values.
The payoff is a real read-eval-print loop where you can define variables, compute a circle’s area, and call built-ins like expt and list. The article then extends toward fuller Scheme by adding lambda, which builds procedures with their own local environments. It remains a touchstone for anyone learning language implementation because it strips the problem down to its essentials without sacrificing a working result.
Read the full article
Continue reading at Hacker News →This is an AI-generated summary. Read the original for the full story.