esolang

Brain-Flak

A language made only of balanced brackets. Every bracket pair is an expression with a value, and the only number you can write is 1.

DJMcMayhem, 2016 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 2 examples and 5 known programs · Hello World on Try It Online: identical · Test method

Run it

Example

Expected output

7

Output


The idea

Brain-Flak came out of the code-golf community (Programming Puzzles & Code Golf). The rule is simple and severe. The program may contain only (), [], {} and <>, and they must balance.

Read the story

The trick that makes it work is that every piece of code evaluates to a number, and nesting adds those numbers up. () is 1. ()()() is 3. Wrap something in ( ) and its value is pushed. So you build numbers by counting, and you build programs by nesting.

There are two stacks, and <> switches between them. That is enough for a full, if painful, universal language.

The machine

Two stacks of integers, left (active at the start) and right. Popping an empty stack gives 0.

Nilads (empty pairs) are values:

CodeValueSide effect
()1
[]height of the active stack
{}the popped valuepops the active stack
<>0switches the active stack

Monads (pairs with code inside) take the inner value:

CodeValueSide effect
(x)xpushes x
[x]−x
{x}sum of all runs of xruns x while the top of the active stack is not 0
<x>0runs x and throws its value away

Command-line numbers start on the left stack. At the end, the active stack is printed, top first.

Example

Add two numbers: pop both, sum the values, push the sum.

Add two numbers: pop both, sum the values, push the sum.

({}{})            with 2 and 3 on the stack: prints 5

Count down. (({})[()]) pops n, pushes it back (the inner ({})), then pushes n + (−1). The loop { … } repeats until the top is 0:

{(({})[()])}      with 3 on the stack: leaves 3 2 1 0
Expected: prints: 5
Expected: prints 0, 1, 2, 3 (top first)

Printing text needs character codes built from ()s. The number 72 (H) is a lot of brackets, which is why Brain-Flak golfers have whole tools for finding the shortest way to write each number.

Checked examples

Example

Expected output

6

Output


Quirks

  • A run stops after 3,000,000 steps.

Try this

  1. Push 5 and 7, then print their difference, 7 − 5.
  2. Use <> to move the top of the left stack to the right stack, and leave the right stack active.
  3. Hard: multiply two numbers. (Hint: loop over one, adding the other to a running total kept on the other stack.)

Patterns it teaches

  • Stack machines: two stacks, which together act like a tape.
  • Turing tarpit: eight characters, like Brainfuck, but structured as expressions instead of instructions.
  • Code golf: born on a golf site, and golfed hard ever since.
  • Every text runs: the opposite. Any unbalanced bracket and the program is rejected, so most random strings are not programs.

Further

Stack-based

Write your own in Run Convert a program from Brain-Flak

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run brainflak -e '({}{})' --input ''
shib manual brainflak

Source

Module
langs/classic.js

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

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