Tape and Stack Languages

Memory Models Across Esoteric Languages

Compare tape, stack, queue and grid storage in esoteric languages, and see what kinds of programs each memory layout makes natural or painful.

On this page
  1. Memory decides the style
  2. Tape
  3. Stack
  4. Queue
  5. Grid
  6. Counters and registers
  7. Comparing them on one task
  8. Universal in theory, different in feel
  9. Mixing models

Memory decides the style

When you strip a language down, the memory model is often the one design choice that remains. It determines which operations are cheap, which are clumsy and what the programs end up looking like. The same algorithm can be a few lines in one model and a puzzle in another. This article compares the four layouts you meet most often in esoteric languages: tape, stack, queue and grid.

The key question for each is how you reach a value. Access rules matter more than raw capacity.

Tape

A tape is a line of cells with a movable pointer. You can read or change the current cell and shift one step left or right.

  • Easy: keeping a fixed number of slots, walking through a sequence, processing items one after another.
  • Awkward: reaching a distant cell, since you must walk there; copying, because the common operations are destructive.

Reading a value far away costs one move per cell, so well-written programs arrange their data close together. The tape also keeps old data around. Nothing disappears unless you clear it, so leftover values can interfere with later work.

Stack

A stack gives access to the top item only, with push and pop as the operations.

  • Easy: nested calculations, temporary values and anything that unwinds in reverse order, such as matching brackets or reversing a string.
  • Awkward: long-lived named variables and access to items below the top.

Reversal is the natural strength: whatever goes in first comes out last. A common beginner exercise is pushing the characters of input and then popping them all, which prints them backwards. Because the data disappears when you pop it, you must duplicate anything you want to use twice. Running out of items is also an error condition on its own, and a program that pops too much has to be handled carefully.

Befunge keeps its numbers on a program stack, while Funge-98 extends this with a whole stack of stacks.

Queue

A queue works like a line at a shop: items join at the back and leave from the front.

  • Easy: processing items in the order they arrived, cyclic behavior, simple rewriting rules.
  • Awkward: anything that needs the most recent item first, or random access.

Queue-based languages are less common but have a strong theoretical presence. Tag systems, for example, read the front of a queue, remove some items and append others at the back, and this is enough to model universal computation under suitable rules.

Grid

A grid places values at coordinates in two or more dimensions. Reading and writing at a position is direct, and movement is by coordinates rather than steps.

  • Easy: random access, storing values at fixed addresses, treating position as meaning. Cellular automata are the clearest example, where the grid is the whole state and each cell is updated from its neighbors.
  • Awkward: sequential data, since the programmer has to invent a layout and a path through it.

In the Funge family, the grid is also where the program lives. Get and put instructions let code read and write cells, which means the same space holds both code and data. This mixing is what makes self-modifying code possible and is how programmers use grid variables to hold values when the stack is not convenient. When the grid wraps at its edges the space is called toroidal, which affects how far a pointer can travel.

Counters and registers

A counter machine, mentioned in a separate article in this cluster, has a handful of integers and nothing else. It is the extreme minimal case, and a good reminder that a memory model is a set of rules about access rather than a physical structure.

Comparing them on one task

Consider reversing a short word. On a stack, you push each letter and pop them all, and you are done. On a tape, you write the letters into consecutive cells, move the pointer to the end, and walk back printing; no extra structure is needed, but the loop is more elaborate. On a queue, reversal is genuinely awkward, because the structure preserves order, so you would need extra machinery. On a grid, you can lay the letters in a row and read them from the other end, but you must choose where to put them.

Now consider adding up a list of numbers. A stack or a counter does this naturally. A tape does it with a running total in a spare cell.

The lesson is that each structure matches certain algorithms, and a language built on one tends to attract those algorithms.

Universal in theory, different in feel

All of these models can be made capable of universal computation in principle, given enough memory. The Turing machine uses a tape; stack-based, queue-based and grid-based languages can all simulate one, and be simulated by one. So the choice is not about what can be computed. It concerns the cost and convenience of doing so, and how much bookkeeping falls on the programmer. That is why comparisons of memory models are really comparisons of programming experience.

Mixing models

Real esoteric languages often combine two. Befunge pairs a stack with a grid. Some tape languages add a stack for return addresses or a second tape. Mixed models tend to be more practical and less pure, and many language designers find the purest forms the most interesting precisely because nothing softens the constraint.

The complete guide to tape and stack languages

Tape and Stack Based Esoteric Languages
Read the guide

Keep exploring

Guides from other topics

All topics →