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-like specification for a personal project of mine, as well as stamp out possible portability concerns.

Memory Model

Smoothbrain's memory, much like brainfuck, 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

A Smoothbrain program is defined as a sequence of 0 or more of the following bytes: 43 44 45 46 60 62 91 93, these are called "instructions". Other bytes may be prepended, appended, or interspersed between these, but MUST NOT affect program execution or semantics. This essentially means that Smoothbrain programs are written in ASCII, or a backwards-compatible format like UTF-8, but compilers and interpreters MUST NOT validate or otherwise check their programs for a specific string encoding. Just handle those 8 bytes and ignore everything else.

The bytes 91 and 93 (literally [ and ] in ASCII) MUST form "pairs" in the source code. If one of these bytes cannot be assigned a pair, the program is invalid and MUST be rejected. Each pair starts with 91 and ends with 93. A given 91 is considered to pair with a 93 if all 91s and 93s between them already have pairs, OR if there exist no 91s or 93s between them. In other words, ASCII brackets pair up exactly as you'd expect, and it's invalid to have mismatched brackets.

Smoothbrain instruction bytes should be executed in-order starting from the first instruction. The program terminates if an error occurs, or after the final instruction has been executed. (Unless the final instruction was a 93 and caused the program to jump backwards; see below.) The following table lists the eight recognized instructions and what they do when executed.

Recognized Instructions
Byte Byte in ASCII Effect on Program State
43 + 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.
44 , 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 a compliant interpreter/compiler MUST NOT update any tape cell.
45 - 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.
46 . Take the integer in the tape cell currently pointed to by the tape head and write it to STDOUT.
60 < Point the tape head one cell towards 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.
62 > Point the tape head one cell further away from the tape edge relative to the cell it currently points to.
91 [ 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 93.
93 ] 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 91.

IO

Smoothbrain Input & Output (using instruction bytes 44 and 46) 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 from/to STDIN and STDOUT are to be treated as either byte-by-byte raw data, or as UTF-8 encoded strings. This will depend on the specific program and thus compiler/interpreter configuration.

A compiler SHOULD provide facilities to convert (or otherwise validate) input text into UTF-8, and to convert (and validate) output from UTF-8 into a target platform's native encoding at runtime. This is important to allow Smoothbrain programs to work without modification on platforms that don't use UTF-8.

If the above STDIN/STDOUT automatic-processing is done, a compiler MUST halt with exit code 3 if output data is malformed UTF-8, or input data is malformed / is otherwise impossible to encode as UTF-8. When such text processing is done, any LINE FEED (U+000A) characters the program outputs SHOULD be converted into the target platform's preferred newline character (CARRIAGE RETURNs or other newline-adjacent characters in output MUST be ignored and treated like any other character). Any platform-specific newline character sequences (such as Windows' CRLF) MUST be flattened into a single LINE FEED (U+000A).

Error States

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

  1. Instruction 60 (<) 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.
  3. The program reads or writes malformed text data, only triggered by compiler/interpreter flags for cross-platform compatability. Exit code 3.

Implementation Concerns

Smoothbrain is extremely similar to brainfuck, and thus retains similar optimizations as it, such as [-] becoming a "SET CELL TO 0" 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 halt 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)