esolang

pbrain

Brainfuck with procedures, where the name of a procedure is a number in a cell.

Paul M. Parks, 2004 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 3 known programs · Hello World on Try It Online: identical except the line end · Test method

Run it

Expected: AAA

The idea

Paul M. Parks made pbrain in 2004. It adds three commands to brainfuck. ( and ) wrap a procedure, and : calls one. A procedure has no name in the source. It gets the number that is in the current cell when its ( runs, and : calls whichever procedure matches the current cell.

Read the story

The specification points out the catch. Since the cell can hold anything by then, you often cannot tell which procedure a : calls without running the program. That is bad for readers and good for obfuscation.

The machine

A tape of byte cells that wrap at 256, growing both ways, a pointer, up to 256 procedures and a stack of return points.

CommandDoes
+ - < > . , [ ]As in brainfuck
(Store the code up to the matching ) as procedure cell, and skip it
:Call procedure cell
)Return from the procedure

Calling a number with no procedure stops the run. Reading past the end of the input gives 0, and every other character is a comment.

Example

+(+>) is the procedure 1.

+(+>) is the procedure 1. It adds one to the cell and steps right. The cell still holds 1, so : calls it at once. +: twice more sets the next cell to 1 and calls it again. Three cells now hold 2, and adding 63 to each prints AAA.

Patterns it teaches

  • Code as data: the procedure called is picked by a value on the tape, computed at run time.
  • Turing tarpit: procedures add structure, not power.

Further

pbrain builds on Brainfuck.

Brainfuck derivatives

Cell-based

Write your own in Run Convert a program from pbrain

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: pbrain.

Command line

shib run pbrain -e '+([-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]):+:'
shib manual pbrain

Source

Module
langs/tio/ports.js
Interpreter
langs/tio/pbrain.js

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

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