We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.

BrainSwitch

From Esolang
Jump to navigation Jump to search

BrainSwitch is a brainfuck derivative in which the meanings of + and - swap whenever the current cell holds a prime number. Every other instruction is identical to brainfuck's. This one change is small to state and unusually disruptive in practice: to know what + will actually do, the programmer must already know, at that exact point in execution, whether the current cell's value is prime. Since every arithmetic instruction can change that fact, this knowledge has to be tracked through the entire execution history of a cell, not just its current state.

Language overview

BrainSwitch runs on the same memory model as Brainfuck: a tape of byte cells (0–255, wrapping on overflow and underflow), unbounded in both directions, all initialized to zero, with a single data pointer.

The commands:

Command Meaning
> Move the data pointer right.
< Move the data pointer left.
+ If the current cell is prime, decrement it; otherwise, increment it.
- If the current cell is prime, increment it; otherwise, decrement it.
. Output the current cell as an ASCII character.
, Input a byte and store it in the current cell.
[ Jump past the matching ] if the current cell is 0.
] Jump back to the matching [ if the current cell is nonzero.

All characters other than ><+-.,[] are comments and are ignored.

The [ and ] brackets are ordinary zero/nonzero tests on the raw cell value — primality has no bearing on them. This turns out to matter a great deal for the language's computational class.


External Resources