Brain-Flak

From Esolang
(Redirected from Brainflak)
Jump to navigation Jump to search

Brain-Flak is a stack-based esoteric language designed by Programming Puzzles and Code-Golf user, DJMcMayhem. The name is a cross between "brainfuck", which was a big inspiration for the language, and "flak-overstow", since the language is confusing and stack-based.

Description

The language, like its inspiration, has only 8 valid characters. However unlike brainfuck, all of these characters are brackets. The valid characters are

()[]{}<>

Additionally, every brain-flak program must have these brackets perfectly balanced. So

(((
([)]
<<}}

Are all invalid in brain-flak.

Ignoring all non-brace characters syntactically valid Brain-Flak programs will fall into the following context free grammar:

S ::= "(" S ")" | "[" S "]" | "<" S ">" | "{" S "}" | S S | ""

Tutorial

Brain-Flak has two stacks, known as 'left' and 'right'. The active stack starts at left. If an empty stack is popped, it will return 0. That's it. No other variables. When the program starts, each command line argument is pushed on to the active stack.

Nilads

There are two types of functions: Nilads and Monads. A nilad is a function that takes 0 arguments. Here are all of the nilads:

- () Evaluates to one.
- [] Evaluates to the height of the current stack.
- {} Pop the active stack. Evaluates to the popped value.
- <> Toggle the active stack. Evaluates to zero.

These are added together when they are evaluated. So if we had a '3' on top of the active stack, this snippet:

()(){}

would evaluate to 1 + 1 + active.pop() which would evaluate to 5.

Monads

The monads take one argument, a chunk of Brain-Flak code. Here are all of the monads:

- (n) Push 'n' on the active stack.
- [n] Evaluates to negative 'n'
- {foo} While zero is not on the top of the stack, do foo.
- <foo> Execute foo, but evaluate it as 0.

These functions will also return the value inside of them, so

(()()())

Will push 3 but

((()()()))

Will push 3 twice.

The {} will evaluate to the sum of all runs. So if we had '3' and '4' on the top of the stack:

{{}}

would evaluate as 7.

When the program is done executing, each value left on the active stack is printed, with a newline between. Values on the other stack are ignored.

That's it. That's the whole language.

Comments

Some Brain-Flak implementation allow for comments. Rain-Flak, the original Ruby implementation of Brain-Flak uses line comments initiated by a # and ending with a newline.

code # comment
code

Anything between the # and the newline will be neither parsed nor executed.

BrainHack, a later Haskell implementation also uses # to indicate a comment, however its comments are block comments enclosed in #{...}, and thus will be parsed for validity but not executed.

code #{ comment
more comment } code

Brain-Crack, an even later cross-platform C++ implementation still uses # to indicate a comment, but this time the characters between two #'s are comments and will not be parsed nor executed.

code # comment # code
# comment
more comment # code

BrainFlog and Crane-Flak have no comments.

Sample Code

A larger variety of up to date code samples can be found in the Brain-Flak wiki.

Add two numbers

({}{})

Sum of all inputs

(([]){[{}]{}([])}{})

Fibonacci

<>((()))<>{({}[()])<>({}<>)<>(({})<>({}<>))<>}<>{}{}

Integer division.

This is the shortest known integer division program that works for all non-zero integers at 246 characters.

(()()){({}[()]<([([({})](<()>))](<>())){({}())<>}{}<>{}{}<>(({})){{}{}<>(<(())>)}{}(({})<>)<>{(<{}([{}])>)}{}({}<>)<>({}<><({}<>)>)>)}{}({}(<>))<>([()]{()<(({})){({}[()])<>}{}>}{}<><{}{}>)<>(({}{}[(())])){{}{}(((<{}>)))}{}{}{(<{}<>([{}])><>)}{}<>

Quine

(()()()()()()())(())(()()()()()()())(()()()()()()()())(()()())(()()()())(()())(()()())(()()()())(()()()()()()()())(()()())(()()()())(())(()()()()())(()()()()()())(()())(()()()()()()())(())(()()()()()()())(()()()()()()()())(()()()()())(())(()())(()()()()()())(()()())(())(())(()()()()())(()()()()()()())(()()()()()()()())(()()()()()())(())(()())(()()())(())(())(())(())(())(())(())(())(())(()())(())(()())(())(()())(())(()())(())(()())(()())(()()()()()()())(()()()()()()()())(()())(()()()()()()())(()()()()()()()())(()())(()()()()()()())(()()()()()()()())(()())(()()())(()()()())(()())(()())(())(()())(()())(()())(()()()())(()())(()()())(()()()())(()())(()()()()()()())(())(()()()()()()())(()()()()()()()())(())(()())(()()())(())(()()()()()()())(()()()()()()()())(())(()())(()())(()()()())(()())(()()()()()()())(())(()()()()()()())(()()()()()()()())(())(()())(()()())(())(()()()()()()())(()()()()()()()())(())(())(())(())(()())(())(()())(())(()())(()())(()())(()()()()()()())(()()()()()()()())(()()()()()()())(()()()()()()()())(()())(()()()()()()())(()()()()()()()())(())(()())(()())(()()()())(()())(()()()()()()())(())(()()()()()()())(()()()()()()()())(())(()())(()()())(())(()()()()()()())(()()()()()()()())(())(()())(())(()())(()())(()()()())(()())(()()()()()()())(())(()()()()()()())(()()()()()()()())(())(()())(()()())(())(()()()()()()())(()()()()()()()())(())(())(())(())(()())(())(()())(())(()())(()())(()()()()()()())(()()()()()()()())(())(()())(()())(()()()()()()())(()()()()()()()())(()())(()()()()()()())(()()()()()()()())(())(()())(()())(()()()())(()())(()()()()()()())(())(()()()()()()())(()()()()()()()())(())(()())(()()())(())(()()()()()()())(()()()()()()()())(())(()())(())(()())(()())(()()()())(()())(()()()()()()())(())(()()()()()()())(()()()()()()()())(())(()())(()()())(())(()()()()()()())(()()()()()()()())(())(())(())(())(()())(())(()())(())(()())(())(()())(())(()())(()())(()())(()()()()()()())(()()()()()()()())(()()()()()()())(()()()()()()()())(()())(()()()()()()())(()()()()()()()())(()())(()()()())(()())(()()()()()()())(())(()()())(()()()()()()())(()()()()()()()())(())(()()()()()()())(()()()()()()()())(())(()())(())(()())(()())(()()()())(()())(()()()()()()()())(()()()()()()()())(()()()()()()()())(()()()()()()()())(()()()()()()()())(()()()()()()()())(()()()()()()()())(()()()()()()())(()()()()()()()())(())(()()()()())(()()()()()())(()()())(())(()()()()()()())(()()()()()()()())(()()())(()()()()()()())(())(()()()()()()())(()()()()()()()())(()()())(()()()())(()())(()()())(()()()())(()()()()()()()())(()()())(()()()())(()()()())(()())(()()()())(()())(()()()()()()())(())(()()()()()()())(()()()()()()()())(()()()()())(())(()())(()()()()()())(()()())(())(()()()()()()())(()()()()()()()())(()()())(()()()())(()())(()()())(()()()())(()()()())(()())(()()()()()()()())(()()()()()()())(()()()()()()()())(()()()()()()())(())(()()()()()()())(()()()()()()()())(())(()())(()()())(())(())(())(()()()()()()())(()()()()()()()())(()()()()())(())(()())(()()()()()())(()())(())(()())(()())(()())(()()()())(()())(()()()()()()()())(()()()()()()())(()()()()()()()())(()()())(()()()())(()()()())(()())(()()()()()()()())(()()()()()()())(()()()()()()()())(()()()()()()())(())(()()()()()()())(()()()()()()()())(()()())(()()()())(()())(()()())(()()()())(()()()()()()()())(()()())(()()()()){({}<>)<>}<>([]){({}[()]<(([{}]()<((((((((()()()()()){}){}){})<>))()))>)<>){({}()<({}())>){({}()<({}(((()()())){}{}){}())>){({}()<({}()())>){({}()<({}(((()()()){}()){}){}())>){({}()<({}()())>){({}()<({}(((()()()()())){}{}){})>){(<{}({}()())>)}}}}}}}{}([]<({}<{({}<>)<>}<>>)>){({}[()]<({}<>)<>>)}{}{({}()<((({}[()])()))>)}{}<>>)}{}{({}<>)<>}<>

Interpreters

Compilers

External resources

  • BrainFlak::Symbolic is a Perl module for analysing programs in the fragment of Brain-Flak without {…} (i.e. linear programs with no control flow), and contains a superoptimiser for generating minimal-length Brain-Flak program fragments with specific effects on the stack.