Semantic Brain

From Esolang
Jump to navigation Jump to search
Semantic Brain
Paradigm(s) imperative
Designed by Leonora Tindall
Appeared in 2015
Memory system tape-based, stack-based
Dimensions one-dimensional
Computational class Turing complete
Major implementations Rust library (in progress)
Influenced by Brainfuck
File extension(s) .sbrain


Semantic Brain, or SBrain, is a brainfuck-like language created by Leonora Tindall. It is based on brainfuck, but adds a stack, a general-purpose register, and bitwise operation instructions. It was created in as part of a genetic programming project, and its implementation is still a work in progress.

Language overview

Semantic Brain, or SBrain, extends brainfuck with a stack, a general-purpose register, and additional commands. It is an attempt to make brainfuck more amenable to genetic programming. As it implements classical brainfuck with extensions, rather than modifying it, SBrain is necessarily Turing complete. While it does add useful features like data tape initialization and single instruction arithmetic, SBrain is still firmly a Turing tarpit.

Specification

Data Structures

SBrain requires:


  • a read/write tape datastructure ("data tape") which is addressable up to, at minimum, 65,536 (0x0 - 0xFFFF) 32-bit cells. Not all of these must be active in memory; however, SBrain programs may assume that they are all addressable. They must be initially set to zero unless set with an initialization instruction.
  • a read/write stack (FILO) datastructure ("data stack") which must support, at minimum, 256 values. Not all of these must be active in memory; however, SBrain programs may assume that they are addressable. They must be initially set to zero.
  • a read-only tape datastructure which contains the executable code. This code is represented as a list of unsigned integers of, at minimum, six bits in width.
  • a read-only nonreversable tape containing the program's input (note: as this tape is nonreversable and nonwriteable, a function like C's getch() works fine.)
  • a write-only nonreversable tape containing the program's output (note: as this tape is nonreversable and nonreadable, a function like C's putch() works fine.)
  • a read/write register (data_p) of enough bits to store a position on the data tape
  • a read/write register (inst_p) of enough bits to store a position on the instruction tape
  • a read/write register (jump_p) of enough bits to store a position on the instruction tape
  • a read/write register (auxi_r) of the same size as a cell on the data tape

Commands and Source Code

SBrain source code consists of text characters. Executable code consists of unsigned integers of four bits. A transliterator converts the source code to executable code by a one-to-one mapping, with two exceptions. The first is noted in the entry for instruction 15 (@), which is a metacharacter in certain circumstances. The second is the comment character, #. All data between # characters is ignored by the transliterator.


The first eight instructions are the standard brainf--- instructions. Any brainf--- program is a valid SBrain program and should behave in the same way as in a standard, semantically equivalent brainf--- interpreter, as long as comments are correctly escaped.


Decimal Code Semantics
0 < Decrement data_p
1 > Increment data_p
2 - Subtract one from the cell pointed at by data_p
3 + Add one to the cell pointed at by data_p
4 [ If the cell pointed at by data_p is zero, jump forwards until inst_p points at the corresponding 5 (]). If there is no corresponding 5, NOP.
5 ] If the cell pointed at by data_p is non-zero, jump backwards until inst_p points at the corresponding 4. If there is no corresponding instruction, NOP.
6 . Place the value in the cell pointed at by data_p on the output tape
7 , Place the next value from the input tape in the cell pointed at by data_p
8 { Push the value from the cell pointed at by data_p onto the stack
9 } Pop the next value from the stack into the cell pointed at by data_p
10 ( Set auxi_r to the value of the cell pointed at by data_p
11 ) Set the cell pointed at by data_p to the value in auxi_r
12 z Set the value in auxi_r to 0
13 ! Perform a bitwise NOT on the value in auxi_r.
14 s Perform a bitwise AND on the value in auxi_r and the cell pointed at data_p, placing the value in auxi_r.
15 @ End the program. The exit code is the value in auxi_r. If repeated twice (@@) in the source code, the transliterator will consider all further source code to be data and will use it to initialize the data tape.

Further Rules

No read operation shall ever disrupt a cell on the data tape.

Reading an EOF always produces a 0.

Non-command characters in the instruction section of source code must be ignored.

Examples

Any brainfuck program will run and behave in the same way in SBrain; however, many simple programs are much, much shorter in SBrain. For example, Hello, World! is simply:

[.>]@@Hello, World!