Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 14 · From Scratch · Systems from scratch · about 17 min to read

Play it as a journeyTutor comes with a plan

An interpreter of your own

RESP The Wire Protocol
RESP The Wire Protocol · Open board

: the Redis build turns a stream of raw bytes into a command the server understands. This lesson does the same job for program text: it turns characters into a shape the computer can act on.

You type 2 + 3 * 4 and a program must answer 14. It must not answer 20. Nobody told that program that multiplication goes first. Somebody wrote code that works it out from the characters. This is the problem: text is only a row of characters, and a row of characters does not run.

The history (optional)

John McCarthy described Lisp in a paper in Communications of the ACM, April 1960. The paper defines a function called eval that takes an expression and works out its value. McCarthy wrote that S. R. Russell noticed eval could serve as an interpreter for Lisp, and promptly hand coded it. Steve Russell did this for the IBM 704. Before that, work on the language had focused on compiling it. Nothing later in this lesson depends on this story.

By the end of this lesson you will be able to write a small language of your own, with variables, loops and functions, in about 150 lines of Python, and run real programs in it.

Before you start. Redis 02: RESP, The Wire Protocol showed how a server reads raw bytes and finds the command inside them: the same job, smaller. How Code Becomes Execution (compilers, stack & heap) said a compiler turns your words into chip instructions, and that some languages instead use a program that reads the words and acts on them directly. That second kind is what you build here. You need Python 3 to run the code. Reading it needs nothing.

Lesson 111 of 117 in From Scratch

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • Three jobs, in a fixed order
  • The parser: a tree that holds the order
  • The interpreter: walk the tree
  • The environment: where names live
  • Errors are part of the job
  • The grammar
  • The code
  • Run it
  • Break it
  • Trade-offs and real systems

Terms it explains: token, lexer, parser, tree, precedence, tree-walking interpreter, environment, recursive descent, bytecode, Tree-walking is still common.

Review From Scratch