Brain-Flak
#Design
Design
#Overview
Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack ExchangeStack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Explore Stack InternalBrain-Flak is a language designed by me, DJMcMayhem, and written as a collaboration between me and two other users, @Wheat Wizard@Wheat Wizard, and @1000000000@1000000000.
Shortly after I wrote Are the brackets fully matched?Are the brackets fully matched?, it made me wonder how much information you can store with only matched brackets. One thing that stood out to me, was that even though you only have 4 "atoms" of sorts:
Brain-Flak is a language designed by me, DJMcMayhem, and written as a collaboration between me and two other users, @Wheat Wizard, and @1000000000.
Shortly after I wrote Are the brackets fully matched?, it made me wonder how much information you can store with only matched brackets. One thing that stood out to me, was that even though you only have 4 "atoms" of sorts:
Brain-Flak is a language designed by me, DJMcMayhem, and written as a collaboration between me and two other users, @Wheat Wizard, and @1000000000.
Shortly after I wrote Are the brackets fully matched?, it made me wonder how much information you can store with only matched brackets. One thing that stood out to me, was that even though you only have 4 "atoms" of sorts:
Brain-Flak is a language designed by me, DJMcMayhem, and written as a collaboration between me and two other users, @Wheat Wizard, and @1000000000.
When your Stack Overflows, you forget whatever you were talking about. If this happens very badly, to the point where you can no longer speak straight, we call this a "Flak-Overstow". This language is stack based, designed to hurt your brain, and very similar to Brainf*ck, so it seemed appropriate to name it "Brain-Flak".
An online interpreter can be found at brain-flak.tryitonline.net
#Design
Shortly after I wrote Are the brackets fully matched?, it made me wonder how much information you can store with only matched brackets. One thing that stood out to me, was that even though you only have 4 "atoms" of sorts:
(){}[]<> you really have 8 units of information to convey, since each of these bracket types can be empty, or have other brackets in between, which are fundamentally different pieces of information. So, I decided to write a language that only allowed for matched brackets, and where empty brackets convey something different than brackets with other brackets inside of them.
One other thing that influenced the design, was annoyance at brainf*cks inability to easily process decimal numbers. Taking IO in ASCII is very obnoxious, so Brain-Flak handles all inputs and outputs in decimal by default. In August 2016, I added an "ASCII mode", where IO functions identical to brainf*ck.
#Overview
(Copied from the github README)
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.
The only valid characters in a Brain-Flak program are ()[]{}<>, and they must always be balanced. 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 concatenated 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.
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.