In the Rust programming language, when a panic occurs, the program cleans up, prints an error message about the panic and then aborts (ends) the program.
Brainfuck has no sophisticated jump instruction, but these kinds of aborts are still necessary for writing useful programs.
To resolve this, I am proposing a new way to communicate panics using brainfuck instructions that is completely backwards compatible with current brainfuck. This is not a new instruction or anything that would require updating other brainfuck interpreters. If another interpreter does not implement this functionality, it can gracefully fallback to its default behaviour and everything will still function mostly as expected.
Aside: I should note that since brainfuck is turing complete, it is possible to implement this in brainfuck already. One naive implementation could be to simply wrap the remaining program in an if else
statement (implemented in brainfuck of course). That way, if whatever the condition is fails, the rest of the program would not run at all. The downside to this is that it severely complicates the generated brainfuck. Imagine being inside several nested brainfuck loops and then having to somehow navigate your way out to the end of the program. It would be extremely messy and nearly impossible to do so. Every single thing from that point in the program onward would need to be wrapped in conditionals.
Proposal
If a brainfuck program needs to abort, it should enter an infinite loop by using the instructions:
This is an infinite loop. From a syntax point of view, an infinite loop is defined as a brainfuck [
instruction, followed by any number of non-instruction characters, concluded by a ]
instruction. Without any further instructions inside a loop, if the loop is entered, it is guaranteed to run forever. The runtime semantics of this are a bit more complicated and are described below.
Several important points about this:
- The word abort was used instead of panic. Panicking is actually only analogous to aborting. When panicking you need to clean up and output a message. Those things can already be done in brainfuck as it is. The only necessary addition is the last part of panicking, quitting the program and thus ending its execution.
- Normal loop semantics apply. If the cell prior to the first jump instruction is zero, the program will continue onward without aborting. When integrating this into your own brainfuck code, if you have a place where you always want to abort, you can sometimes get away with using
+[]
as a replacement for the code above. The reason this is not specified that way above is because there is no guarantee that +
will not cause a overflow and reset the current cell back to zero. It is up to the programmer to ensure that the current cell is non-zero before the []
instructions if they wish for the abort to occur.
- This is important because it makes it possible to conditionally abort when necessary but otherwise move on in the program.
Error messages and clean up
Printing out an error message and doing whatever clean up is necessary is already possible in brainfuck. Those things are left up to the programmer to do on their own.
Recommendation: once it is decided that the panic will occur, use some other cells to print the error message, then return back to the cell that was used to decide to panic and place the []
instructions.
Exit Status Codes
On Unix-based operating systems and not-so-much Windows, it is possible to return an "exit code" from a program to indicate whether the program succeeded or failed. This is represented as an integer. The convention is to return 0
when the program succeeds, and a non-zero value when the program fails. The non-zero value is not specific and can be anything the program wants.
To facilitate this, an additional extension to this extension is proposed.
In addition to being able to abort with this syntax:
It will also be possible to abort with the following syntax:
or:
In this case, and only in this case, the inner number will be interpreted as the exit code.
The following cases and their variations will not be accepted for this.
- Number in the wrong position:
- Other valid brainfuck instructions:
These will be interpreted using the standard brainfuck interpretation and not used for the exit code.
If no exit code is specified, the default exit code will be 0. So just using []
results in the program exiting with a code of 0
.
Backwards Compatible Behaviour
As stated above, this code will work in a reasonable way on brainfuck interpreters that do not support abort semantics. If a user prints a message before the abort, the message will be displayed and then the program will loop infinitely until the user decides to abort it manually.
For exit codes, those characters are ignored by default anyway, so adding them causes no issues with existing implementations.
Brainfuck Specification Updates
The actual content of the updates to the specification can largely be taken from the text above.
Implementation