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.
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.
Reading code
Text into tokens, tokens into answers: the front end of every language.
| Lesson | Time | What 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 |
|
| 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 |
|
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".
Running code
A tree in the middle, and an evaluator that walks it to drive the board.
| Lesson | Time | What 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 |
|
| 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 |
|
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".
Names
Variables, environments, functions and closures.
| Lesson | Time | What 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 |
|
| 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 |
|
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".
Ship it
Errors that point at the mistake, then Orbit 1.0 with a REPL on the Serial Monitor.
| Lesson | Time | What 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 |
|
| 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 |
|
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".
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.