The course
Build a programming language is an advanced Coding course in eight lessons. Every lesson runs on the same circuit: a Raspberry Pi 4 with a traffic light module (red on GPIO17, amber on GPIO27, green on GPIO22), which is Orbit's panel. Each step is checked against what the Pi prints or which lamp it lights.
- Tokens: a tokenizer that cuts text into numbers, names, strings and symbols, with comments, whitespace and an END token.
- Recursive descent: one method per grammar rule. Precedence, left
associativity, brackets, unary minus and
%fall out of which method calls which. - An AST: the parser builds a tree of tuples, and small recursive functions draw it, evaluate it and print it in reverse Polish notation.
- Evaluating: statements.
print, lamps that switch real GPIO pins,wait, and a launch sequence written in Orbit. - Scope:
let, assignment andrepeatblocks, then environments chained to their parents, so a block's names stay in the block. - Functions:
def, calls,returnas an exception, lexical against dynamic scope shown on one program,ifand recursion up tofib(15). - Errors: tokens that know their line and column, one
OrbitError, a caret under the mistake, and division by zero and type errors caught before Python sees them. - Boss: a calculator language on a board: Orbit 1.0.
The finale
The boss lesson turns the interpreter into a read-eval-print loop on the
Serial Monitor. It keeps one global environment for the session, reads a
block over several lines while its braces are open (with a ...> prompt),
survives errors and Ctrl-C, and has sqrt, round and abs built in. The
last step types a whole mission live:
let t = 3
def launch() {
red on
repeat t {
print "T minus", t
amber on wait 0.5 amber off wait 0.5
t = t - 1
}
red off
green on
print "LIFTOFF at", round(sqrt(398600 / 6771), 2), "km/s"
}
launch()
The panel counts down and goes green, and Orbit works out the space station's orbital speed, 7.67 km/s.
What the Pi's engine does differently
The Pi runs MicroPython 1.29, not CPython. For this course that shows in a few places:
strhasisalphaandisdigitbut noisalnum, so the tokenizer asks for both.input()reads a line typed in the Serial Monitor, and Ctrl-C there raisesKeyboardInterruptin the program, which the REPL catches.time.sleepruns on the simulated clock, sowait 0.5is exactly half a simulated second.- When
main.pyends, the Pi lets go of its pins and the lamps go dark, so the non-REPL lessons wait five seconds at the end.
Everything else, from exceptions as control flow to closures, is plain Python that runs the same on a real Pi.