Code patternsDataA stack

A stack

Keeps values in a pile: you add to the top and take from the top.

Watch it run

The form “Push a constant” works on 4 cells, named “base”, “item”, “top” and “free”. Press Play, drag the step slider forwards and back, or change constant with the slider and edit the stack, bottom first:

The figure needs JavaScript. The program below is the result for the example values.

The tape itself is the pile, and the pointer is the top. To push, step right and write. To drop, clear and step left.

Nothing has to be searched for, because the program always stands on the newest value. That makes every operation a short flat piece.

Whole programs can be written this way: every piece takes its inputs from the top and leaves its result there.

The code

This is the program the figure ran, written for these values. The pointer starts on “top” (cell 2). It is brainfuck as it stands: copy it, or open it in the playground, where you can change it:

>+++
Open in the playground4 characters

What it needs

Common mistakes
  • Leaving rubbish above the top. The next push adds to it.
  • Dropping below the base of the pile: the pointer walks off the tape.

What it costs

A plain step is one command. An optimised step is one step of an interpreter that folds runs of the same command and flat loops. Choose the values and watch the two bars; What costs what explains how cost is counted:

The figure needs JavaScript. The table below has the measured steps.

Cost as a formula, and measured
Table 1. The cost as a function of the values. Exact formulas are checked on every case they run.
FormPlain stepsOptimised steps
Push a constant1 + the constant1
Drop2 + 2 × top2
Duplicate the top5 + 15 × top3
Add the two top items2 + 5 × top2
Swap the two top items7 + 12 × top + 5 × the one below4
A small program: (2 + 3) squared82129
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
Push a constantconstant = 3; s = 5, 88441
Drops = 5, 884182
Duplicate the tops = 5, 88201253
Add the two top itemss = 5, 887422
Swap the two top itemss = 5, 88241284
A small program: (2 + 3) squaredno values to choose87982129

Other forms

Drop

Clear the top, step left.

[-]<
Open in the playground4 characters
Run this form step by step

The form “Drop” works on 3 cells, named “base”, “item” and “top”. Press Play, drag the step slider forwards and back, or edit the stack, bottom first:

The figure needs JavaScript. The program below is the result for the example values.

Duplicate the top

A copy, with the cell two above as scratch.

[->+>+<<]>>[-<<+>>]<
Open in the playground20 characters
Run this form step by step

The form “Duplicate the top” works on 5 cells, named “base”, “item”, “top”, “free” and “scratch”. Press Play, drag the step slider forwards and back, or edit the stack, bottom first:

The figure needs JavaScript. The program below is the result for the example values.

Add the two top items

Two items become one.

[-<+>]<
Open in the playground7 characters
Run this form step by step

The form “Add the two top items” works on 3 cells, named “base”, “item” and “top”. Press Play, drag the step slider forwards and back, or edit the stack, bottom first:

The figure needs JavaScript. The program below is the result for the example values.

Swap the two top items

Three moves, with the free cell above as scratch.

[->+<]<[->+<]>>[-<<+>>]<
Open in the playground24 characters
Run this form step by step

The form “Swap the two top items” works on 4 cells, named “base”, “item”, “top” and “free”. Press Play, drag the step slider forwards and back, or edit the stack, bottom first:

The figure needs JavaScript. The program below is the result for the example values.

A small program: (2 + 3) squared

Push 2, push 3, add, duplicate, multiply. The pointer starts on cell 1, the first place of the pile.

++>+++[-<+>]<[->+>+<<]>>[-<<+>>]<[-<[->>+>+<<<]>>>[-<<<+>>>]<<]<[-]>>[-<<+>>]<<
Open in the playground79 characters
Run this form step by step

The form “A small program: (2 + 3) squared” works on 5 cells; the named ones are “base”, “result”, “free” and “scratch”. Press Play, drag the step slider forwards and back:

The figure needs JavaScript. The program below is the result for the example values.

Where it is used

Sources

Every program on this page was run on many inputs, in plain and in optimised mode, and compared with JavaScript. The explanations are written for this page.