Smoothbrain

From Esolang
Jump to navigation Jump to search
Smoothbrain
Designed by User:Ashli Katt
Appeared in 2025
Computational class Turing Complete
Reference implementation Unimplemented

Smoothbrain is a brainfuck derivative that restricts what is considered a compliant interpreter/compiler. It was made to dictate an exact brainfuck specification for a personal project of mine. Trying to make compliant code more portable is also a secondary goal.

Memory Model

Smoothbrain's memory consists of a tape and a tape head. The tape is a right-unbounded array of "cells" containing integers in the range 0 to 255 inclusive. The tape head is a value that can represent any single cell in the tape, and is initialized to the leftmost tape cell ("index 0").

Program

Any UTF-8 encoded, finite string of text that has matching square brackets ([]) SHOULD be a valid, executable Smoothbrain program. "Matching square brackets" means the source code has an equal number of [ and ] characters, and that all [ characters uniquely "pair" with a ] character such that no unpaired brackets are between them.

Every character in a program's source code is an instruction. When executing a Smoothbrain program, instructions are evaluated linearly, in the order they appear in the source code. However, certain instructions ([]) can cause execution to "jump" backwards or forwards, skipping or repeating instructions. Program execution terminates if the final instruction is executed and it does not perform a jump; Program execution also terminates if an error occurs (see below).

Recognized Instructions
Character Effect on Program State
+ Increment the integer in the tape cell currently pointed to by the tape head. If the cell's value was 255, then incrementing it results in 0.
- Decrement the integer in the tape cell currently pointed to by the tape head. If the cell's value was 0, then decrementing it results in 255.
< Position the tape head one cell closer to the tape edge, relative to the cell it currently points to. If this instruction is executed when the tape head is already at the leftmost cell ("index 0"), then the program should immediately halt with exit code 1.
> Position the tape head one cell further away from the tape edge, relative to the cell it currently points to.
[ If the integer in the tape cell currently pointed to by the tape head is 0, then jump program execution to this instruction's paired ].
] If the integer in the tape cell currently pointed to by the tape head is NOT 0, then jump program execution to this instruction's paired [.
. Take the integer in the tape cell currently pointed to by the tape head and write it to STDOUT.
, Read a byte from STDIN and set the integer in the tape cell currently pointed to by the tape head to it. If no byte can be read (i.e. EOF) then the tape MUST NOT be modified.
Every other instruction MUST do nothing.

IO

Smoothbrain input and output (using .,) inherently relies on STDIN and STDOUT. It is impossible to write a compliant interpreter or compiler for a target that does not support or otherwise cannot emulate standard streams.

Data read from STDIN and written to STDOUT are performed on a byte-by-byte basis and are treated as raw data. However, if a Smoothbrain program decides to treat input or output as text, then it should be treated as UTF-8. Well-formed Smoothbrain programs that decide to treat input as text should be able to handle any data they receive, even potentially malformed UTF-8. Compilers and interpreters SHOULD NOT pre-process STDIN or process STDOUT— an exception is made for newlines. Compilers and interpreters MAY have the ability to convert singular LINE FEED characters into the target platform's preferred newline style when writing to STDOUT; If this feature is present, it MUST be optional so as to not interfere with programs that output raw data.

Error States

The following are cases where a Smoothbrain program MUST halt with an error code.

  1. Instruction < is executed while the tape head is at the leftmost cell ("index 0"). Exit code 1.
  2. Memory cannot be (re)allocated for the tape because the OS denies the request or otherwise no memory is available. Exit code 2.

Implementation Concerns

Smoothbrain is extremely similar to brainfuck, and thus retains similar optimizations as it, such as [-] becoming a "SET CELL TO 0" internal-instruction. However, it should be noted that it is well-defined in Smoothbrain that going past the left edge of the tape is an error state, and MUST crash the program. Thus, while the command sequence >< can be optimized out of a program, the sequence <> will error if the tape head was at the leftmost cell when executed, thus optimizing it away results in an invalid implementation if the compiler/interpreter can't prove that a crash cannot happen.

Turing Completeness

Smoothbrain is trivially Turing Complete by way of being equivalent to Brainfuck aside from IO, program form, and error-state specifics. (All of which are outside of the scope of brainfuck's turing-completeness)