Lesson plan

Build a programming language lesson plan

Write Orbit, a small language for a mission control panel, in Python on a Raspberry Pi: tokenizer, recursive descent parser, AST, evaluator, scope, functions and real error messages, ending in a REPL that lights real lamps.

4 chapters, 8 lessons, about 6 hours, on the Raspberry Pi 4 Model B. Every lesson runs in the browser, with nothing to install.

The unit

Objectives and materials

By the end, students can

  • Text into tokens, tokens into answers: the front end of every language.
  • A tree in the middle, and an evaluator that walks it to drive the board.
  • Variables, environments, functions and closures.
  • Errors that point at the mistake, then Orbit 1.0 with a REPL on the Serial Monitor.

Materials for the whole unit

  • Pushbutton
  • Raspberry Pi 4 Model B
  • Traffic light module

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 1 h 20 min

Reading code

Text into tokens, tokens into answers: the front end of every language.

LessonTimeWhat students do
TokensStart Orbit, a language for a mission control panel: a tokenizer that turns text into numbers, names, strings and symbols, one character at a time, the first stage of every compiler and interpreter there is.40 min
  1. Run it: text into tokens
  2. Whole numbers, and decimals
  3. Names
  4. Strings and punctuation
  5. A whole program
Recursive descentTurn tokens into answers with a recursive descent parser: one function per rule of a grammar, precedence that falls out of which function calls which, brackets, unary minus and the remainder operator.40 min
  1. Run it: a parser that adds
  2. Precedence: times before plus
  3. Brackets: the recursion in recursive descent
  4. Unary minus
  5. The remainder operator

Materials

  • Pushbutton
  • Raspberry Pi 4 Model B
  • Traffic light module

Check for understanding

Each student finishes "Recursive descent" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "The remainder operator".

Chapter 2. About 1 h 20 min

Running code

A tree in the middle, and an evaluator that walks it to drive the board.

LessonTimeWhat students do
An ASTSplit understanding from doing: the parser builds an abstract syntax tree out of tuples, and small recursive functions draw it, evaluate it and turn it into reverse Polish notation.40 min
  1. Run it: a tree made of tuples
  2. Draw the tree
  3. Evaluate the tree
  4. Reverse Polish notation
  5. Brackets vanish
EvaluatingOrbit grows statements: a program is a list of statement nodes, and a run loop carries each one out, printing, switching real lamps on the Pi and waiting, until a launch sequence written in your own language counts down on the panel.40 min
  1. Run it: a program is a list of statements
  2. Lamps: the language touches the world
  3. wait
  4. A launch sequence in Orbit
  5. Trace one statement

Materials

  • Pushbutton
  • Raspberry Pi 4 Model B
  • Traffic light module

Check for understanding

Each student finishes "Evaluating" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Trace one statement".

Chapter 3. About 1 h 30 min

Names

Variables, environments, functions and closures.

LessonTimeWhat students do
ScopeVariables, assignment and repeat blocks, then the idea that decides where a name can be seen: environments chained to their parents, so a block gets names of its own and the outside world stays safe.40 min
  1. Run it: an unknown word
  2. let and names
  3. Assignment and repeat blocks
  4. Environments with parents
  5. Shadowing
Functionsdef, calls and return in your own language: functions as values in the environment, return as an exception that unwinds the interpreter, the difference between lexical and dynamic scope proved on one program, and recursion with if.50 min
  1. Run it: def is not a word yet
  2. def and calls
  3. return
  4. Lexical scope: which parent?
  5. if, and recursion
  6. Recursion all the way down

Materials

  • Pushbutton
  • Raspberry Pi 4 Model B
  • Traffic light module

Check for understanding

Each student finishes "Functions" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Recursion all the way down".

Chapter 4. About 1 h 40 min

Ship it

Errors that point at the mistake, then Orbit 1.0 with a REPL on the Serial Monitor.

LessonTimeWhat students do
ErrorsThe difference between a toy and a tool: tokens that know their line and column, one error type for the whole language, a caret under the exact spot, and runtime errors like division by zero and adding text to a number caught before Python sees them.40 min
  1. Run it: an unhelpful error
  2. Line and column
  3. Point at it
  4. Runtime errors
  5. Type errors
Boss: a calculator language on a boardFinish Orbit 1.0: a read-eval-print loop on the Serial Monitor that remembers between lines, reads multi-line blocks, survives errors and Ctrl-C, has built-in math, and flies a launch sequence on the Pi's lamps typed live.60 min
  1. Run it: a REPL
  2. Remember between lines
  3. Blocks over several lines
  4. Survive Ctrl-C
  5. Built-in functions
  6. Boss: fly a mission
  7. Share your language

Materials

  • Pushbutton
  • Raspberry Pi 4 Model B
  • Traffic light module

Check for understanding

Each student finishes "Boss: a calculator language on a board" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Share your language".

Your class

Teach this unit as a class

Start a class from this course and its 8 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.