SMALLTALK LAB
Build a tiny tape-language interpreter in Squeak
Move a head across a tape, match loop brackets, and stop runaway programs with a step budget.
Before you begin
Open a Workspace in native Squeak and use Print it on each complete checkpoint.
These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.
Expected values are Smalltalk expressions. The printer may display equivalent Smalltalk literals with different spaces or delimiters.
01 - Start with a blank sparse tape
The dictionary stores written cells; missing addresses read as zero. The head is a separate integer address.
| cells |
cells := Dictionary new.
cells at: 0 put: 3.
{cells at: -1 ifAbsent: [0]. cells at: 0 ifAbsent: [0]}Expected: #(0 3)

Try it: Write 9 at -1; the first result becomes 9.
02 - Match nested brackets before running
Hold opening positions on a stack. Each closing bracket takes the most recent opening, so the inner brackets are paired first. The full machine stores both directions and rejects unmatched brackets.
| code opens pairs |
code := '[[]]'. opens := OrderedCollection new. pairs := Dictionary new.
1 to: code size do: [:i | | op j |
op := code at: i.
op = $[ ifTrue: [opens addLast: i].
op = $] ifTrue: [j := opens removeLast. pairs at: j put: i]].
{pairs at: 1. pairs at: 2}Expected: #(4 3)

Try it: Trace the stack before the first closing bracket: it holds 1 and 2.
03 - Read, update, and emit a value
The result is {output, finished?, steps, final head}. Move to address -1, decrement the zero cell to -1, then emit that number. The instruction pointer advances after each operation.
| run |
run := [:code :limit |
| pairs opens cells output pc head steps op value |
limit < 0 ifTrue: [Error signal: 'Negative step budget'].
pairs := Dictionary new. opens := OrderedCollection new.
1 to: code size do: [:i |
op := code at: i.
('><+-.[]' includes: op) ifFalse: [Error signal: 'Unknown instruction'].
op = $[ ifTrue: [opens addLast: i].
op = $] ifTrue: [
opens isEmpty ifTrue: [Error signal: 'Unmatched closing bracket'].
value := opens removeLast.
pairs at: i put: value; at: value put: i]].
opens isEmpty ifFalse: [Error signal: 'Unmatched opening bracket'].
cells := Dictionary new. output := OrderedCollection new.
pc := 1. head := 0. steps := 0.
[pc <= code size and: [steps < limit]] whileTrue: [
op := code at: pc. value := cells at: head ifAbsent: [0].
op = $> ifTrue: [head := head + 1].
op = $< ifTrue: [head := head - 1].
op = $+ ifTrue: [cells at: head put: value + 1].
op = $- ifTrue: [cells at: head put: value - 1].
op = $. ifTrue: [output add: value].
(op = $[ and: [value = 0]) ifTrue: [pc := pairs at: pc].
(op = $] and: [value ~= 0]) ifTrue: [pc := pairs at: pc].
pc := pc + 1. steps := steps + 1].
{output asArray. pc > code size. steps. head}].
run value: '<-.' value: 3Expected: {#(-1). true. 3. -1}

Try it: Replace minus with plus: the output changes to 1.
04 - Skip a loop when the cell is zero
The opening bracket jumps to the matching closing bracket, then the common advance lands on the output operation. The plus is not executed.
| run |
run := [:code :limit |
| pairs opens cells output pc head steps op value |
limit < 0 ifTrue: [Error signal: 'Negative step budget'].
pairs := Dictionary new. opens := OrderedCollection new.
1 to: code size do: [:i |
op := code at: i.
('><+-.[]' includes: op) ifFalse: [Error signal: 'Unknown instruction'].
op = $[ ifTrue: [opens addLast: i].
op = $] ifTrue: [
opens isEmpty ifTrue: [Error signal: 'Unmatched closing bracket'].
value := opens removeLast.
pairs at: i put: value; at: value put: i]].
opens isEmpty ifFalse: [Error signal: 'Unmatched opening bracket'].
cells := Dictionary new. output := OrderedCollection new.
pc := 1. head := 0. steps := 0.
[pc <= code size and: [steps < limit]] whileTrue: [
op := code at: pc. value := cells at: head ifAbsent: [0].
op = $> ifTrue: [head := head + 1].
op = $< ifTrue: [head := head - 1].
op = $+ ifTrue: [cells at: head put: value + 1].
op = $- ifTrue: [cells at: head put: value - 1].
op = $. ifTrue: [output add: value].
(op = $[ and: [value = 0]) ifTrue: [pc := pairs at: pc].
(op = $] and: [value ~= 0]) ifTrue: [pc := pairs at: pc].
pc := pc + 1. steps := steps + 1].
{output asArray. pc > code size. steps. head}].
run value: '[+].' value: 2Expected: {#(0). true. 2. 0}

Try it: Count only instructions actually dispatched: opening bracket and output.
05 - Transfer six increments to the next cell
Start with three in cell 0. Each loop adds two to cell 1 and subtracts one from cell 0; closing brackets repeat while the current cell is nonzero. When the loop finishes, move right and emit six.
| run |
run := [:code :limit |
| pairs opens cells output pc head steps op value |
limit < 0 ifTrue: [Error signal: 'Negative step budget'].
pairs := Dictionary new. opens := OrderedCollection new.
1 to: code size do: [:i |
op := code at: i.
('><+-.[]' includes: op) ifFalse: [Error signal: 'Unknown instruction'].
op = $[ ifTrue: [opens addLast: i].
op = $] ifTrue: [
opens isEmpty ifTrue: [Error signal: 'Unmatched closing bracket'].
value := opens removeLast.
pairs at: i put: value; at: value put: i]].
opens isEmpty ifFalse: [Error signal: 'Unmatched opening bracket'].
cells := Dictionary new. output := OrderedCollection new.
pc := 1. head := 0. steps := 0.
[pc <= code size and: [steps < limit]] whileTrue: [
op := code at: pc. value := cells at: head ifAbsent: [0].
op = $> ifTrue: [head := head + 1].
op = $< ifTrue: [head := head - 1].
op = $+ ifTrue: [cells at: head put: value + 1].
op = $- ifTrue: [cells at: head put: value - 1].
op = $. ifTrue: [output add: value].
(op = $[ and: [value = 0]) ifTrue: [pc := pairs at: pc].
(op = $] and: [value ~= 0]) ifTrue: [pc := pairs at: pc].
pc := pc + 1. steps := steps + 1].
{output asArray. pc > code size. steps. head}].
run value: '+++[>++<-]>.' value: 40Expected: {#(6). true. 24. 1}

Try it: At the end of each loop pass, the head is back at cell 0.
06 - Stop a loop without pretending it finished
After plus, the empty loop never changes its deciding cell. The budget ends this run with no output and finished? false.
| run |
run := [:code :limit |
| pairs opens cells output pc head steps op value |
limit < 0 ifTrue: [Error signal: 'Negative step budget'].
pairs := Dictionary new. opens := OrderedCollection new.
1 to: code size do: [:i |
op := code at: i.
('><+-.[]' includes: op) ifFalse: [Error signal: 'Unknown instruction'].
op = $[ ifTrue: [opens addLast: i].
op = $] ifTrue: [
opens isEmpty ifTrue: [Error signal: 'Unmatched closing bracket'].
value := opens removeLast.
pairs at: i put: value; at: value put: i]].
opens isEmpty ifFalse: [Error signal: 'Unmatched opening bracket'].
cells := Dictionary new. output := OrderedCollection new.
pc := 1. head := 0. steps := 0.
[pc <= code size and: [steps < limit]] whileTrue: [
op := code at: pc. value := cells at: head ifAbsent: [0].
op = $> ifTrue: [head := head + 1].
op = $< ifTrue: [head := head - 1].
op = $+ ifTrue: [cells at: head put: value + 1].
op = $- ifTrue: [cells at: head put: value - 1].
op = $. ifTrue: [output add: value].
(op = $[ and: [value = 0]) ifTrue: [pc := pairs at: pc].
(op = $] and: [value ~= 0]) ifTrue: [pc := pairs at: pc].
pc := pc + 1. steps := steps + 1].
{output asArray. pc > code size. steps. head}].
run value: '+[]' value: 12Expected: {#(). false. 12. 0}

Try it: Try a zero budget with an empty program: finished? is true because no instruction is waiting.
Replay the checkpoints

Reason about correctness and limits
This original teaching language is inspired by Brainfuck but omits input, produces numeric output, uses signed integer cells, and allows negative tape addresses. It is not a standard Brainfuck interpreter.
The step budget limits interpretation, not the preliminary scan of the source. Use nonnegative integer budgets and small inputs. A finite run budget is not a claim about universality.
Three practice challenges
Emit three
What short program emits 3?
Show solution
+++.
Move a count
How can you move a count of two to the next cell and emit it?
Show solution
++[>+<-]>. removes one from cell 0 and adds one to cell 1 per iteration.
Spot the loop condition
Why does +[] hit the step limit?
Show solution
The deciding cell is 1 and neither bracket changes it, so closing always takes the same jump.
Reference
Urban Müller’s 1993 Brainfuck archive (Aminet mirror listing)
Continue learning
Smalltalk Interpreter | First Dictionary
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.