Skip to main content

Week 6 Studio: DEFINE, call frames, and RETURN/FRETURN/NRETURN

User-defined functions on top of last week's statement executor — call frames, save/restore, and recursion that needs no special case

29 March 2027

Before the session

Bring last week’s statement executor: labeled statements, a program counter, and goto-field dispatch on success/failure. This session’s function calls are built as another kind of statement outcome, not a parallel execution path.

In the session

Sample transcript

Genuine captured output from this course’s own checkpoint-2 reference solution, run against four example programs — including one built to fail on purpose:

$ dune exec bin/main.exe -- examples/loop.sno
1,2,3,4,5,DONE

$ dune exec bin/main.exe -- examples/define.sno
36
49

$ dune exec bin/main.exe -- examples/recursion.sno
120

$ dune exec bin/main.exe -- examples/fail.sno
ARITHMETIC FAILED AS EXPECTED

recursion.sno’s 120 is a factorial computed entirely with DEFINE and the goto field — no loop construct exists in the language at this point, so that single number is the save/restore discipline working correctly several call frames deep. fail.sno is there to prove the negative case: a program whose whole point is to fail a predicate and print the :F()-branch message, not the success path.

Afterwards

Movement II ends here: statements, the goto field, and DEFINEd functions with proper call frames complete everything the language needs for control flow and procedure calls, with no pattern matching anywhere yet. Checkpoint 2, due at the end of this week, asks you to complete a starter that adds exactly this material on top of Checkpoint 1’s working evaluator. Movement III starts next week with the pattern sublanguage — the part of the language this whole movement was built to make room for.

The spec

What you bring to the session. Written so a reader can tell whether it has been met without asking you.

Teaching team