Semantic Brain

From Esolang
Jump to: navigation, search
Semantic Brain
Paradigm(s) imperative
Designed by Leo 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 Leo Tindall. It is based on brainfuck, but adds a stack, a general-purpose register, and additional arithmetic 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/write stack (FILO) datastructure ("jump stack") which must support, at minimum, 256 values large enough to store an address on the data tape.
  • 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 six 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 31 (@), which is a metacharacter in certain circumstances. The second is the comment character, #. All data between # characters, including those 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 [ Set jump_p to the current position, push jump_p to the jump stack, and, if the cell pointed at by data_p is zero, cease evaluating instructions until inst_p points at the corresponding 5 (]).
5 ] Pop an address from the jump stack into jump_p. Set inst_p to jump_p if the cell pointed at by data_p is nonzero.
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 bitshift to the left on the value in auxi_r. Bits shifted off the left are lost, and bits shifted in from the right are always zero.
15 S Perform a bitshift to the right on the value in auxi_r. Bits shifted off the right are lost, and bits shifted in from the left are always zero.
31 @ 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.

The following instructions are separated because they all follow similar rules. Each one performs an operation on the value at the cell pointed to by data_p (a) and the value in auxi_r(b), in that order if the operation is not commutative, storing it in the cell pointed at by data_p. The creation of a value in a cell greater than the maximum value able to be held by that cell shall result in a wraparound (e.g. 0xFFFFFFFF + 0b11 = 0b11)

Decimal Code Semantics
16 | a OR b (bitwise)
17 & a AND b (bitwise)
18 * a XOR b (bitwise)
19 ^ a NOR b (bitwise)
20 $ a NAND b (bitwise)
21 a ADD a and b
22 d DIFFERENCE of a and b
23 q QUOTIENT of a and b (a divided by b)
24 m a MODULO b
25 p PRODUCT of a and b (a multiplied by b)

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!

The simple subtraction program, assuming binary (not ASCII) input, is:

   ,>,(<d.@ # Take input, next cell, take input, put that in the register, go back to the other cell, take the difference, output, exit. Technically the exit command is optional. #

A cumulative addition program:

   +[,}] #Take input until we get a 0, putting it on the stack#
    {(>{ #Pop two numbers off the stack.#
    [a(>{]).*(@ #Add, pop into new cell, then loop.#
   # Note this while this section IS after the end of the program, no @@ has been encountered outside of a comment, so there is no data on the tape when the 
   program starts. That would be bad.#
   # This program has a bug where it outputs the final answer twice. This is because I'm lazy and it's just an example. #