esolang

Brian & Chuck

Two brainfuck-like programs, where each one's source code is the other one's memory.

Martin Ender, 2015 · Turing complete: yes · ●●● hard · esolangs.org

Tested with 4 known programs · Hello World on Try It Online: identical · Test method

Run it

Expected: Hello, World!

The idea

Martin Ender made Brian & Chuck in 2015. It has two programs, Brian and Chuck, and only one runs at a time. Brian's tape is Chuck's source code, and Chuck's tape is Brian's. Even the pointers are shared. Brian's tape head is Chuck's instruction pointer, and the other way round. Whatever one program writes, the other will later run.

Read the story

The work is split on purpose. Only Brian can read input, and only Chuck can print. Neither has a real loop. ? hands control to the other program when the cell is not 0, and the two keep passing it back and forth. Ender wanted each half to be weak on its own. Luis CR later proved the pair Turing complete.

The machine

Two tapes, infinite to the right, holding unbounded integers. The first line of the file is Brian and the rest is Chuck; _ stands for a 0 cell. Brian starts.

CommandDoes
+ -Add or subtract 1 at the head
< >Move the head (not past the left end)
{Move left until the cell is 0 or the left end
}Move right until the cell is 0
,Brian only: read a byte (−1 at the end)
.Chuck only: print the cell modulo 256
?If the cell is not 0, the other program takes over, one cell after where it stopped

The run ends when the program that is running reaches the end of its source.

Example

Brian's first command ?
Expected: AC

Brian's first command ? looks at his cell, which is the ! at the start of Chuck's code. It is not 0, so Chuck takes over from the cell after it. Chuck's > moves his head, which is Brian's instruction pointer, onto the text in Brian's source, and . prints it.

Patterns it teaches

  • Code as data: each program's memory is literally the other's code.
  • Self-modifying code: loops are built by editing the partner's code so that a later ? fires or not.
  • Turing completeness: neither half is complete alone; together they can run a Turing machine.

Further

Brian & Chuck builds on Brainfuck.

Self-modifying

Brainfuck derivatives

Write your own in Run Convert a program from Brian & Chuck

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: brian-chuck.

Command line

shib run brian-chuck -e '?ABC
!>.>>.'
shib manual brian-chuck

Source

Module
langs/tio/ports.js
Interpreter
langs/tio/brian-chuck.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org