Symbolic Brainfuck

From Esolang
Jump to navigation Jump to search

Symbolic Brainfuck

Symbolic Brainfuck is a brainfuck derivative that adds "symbolic references" to 8 named registers, as well as adding "reference" and "dereference" operators that allow cells to store pointer values. Since the references are symbolic, all Symbolic Brainfuck operands are PC characters (all of which are also available in Unicode) that can not be immediately typed on a standard US keyboard.

Language overview

Symbolic Brainfuck operates on an array of memory cells, also referred to as the tape, each initially set to zero. There is a pointer, initially pointing to the first memory cell. The commands are:

Brainfuck Command Symbolic Command Description Codepage value
> Move the pointer to the right 26
< Move the pointer to the left 27
+ Increment the memory cell under the pointer 30
- Decrement the memory cell under the pointer 31
. ¡ Output the character signified by the cell at the pointer 173
, ¿ Input a character and store it in the cell at the pointer 168
[ Jump past the matching ] if the cell under the pointer is 0 243
] Jump back to the matching [ if the cell under the pointer is nonzero 242

The following commands are not directly related to brainfuck commands.

Symbolic Command Description Codepage value
² Double the value under the pointer 253
½ Halve the value under the pointer. 171
Set the value under the pointer to the value of the pointer. 23
Set the value of the pointer to the value under the pointer. 127
α Swap value under the pointer with register α. 224
ß Swap value under the pointer with register ß. 225
π Swap value under the pointer with register π. 227
σ Swap value under the pointer with register σ. 229
µ Swap value under the pointer with register µ. 230
δ Swap value under the pointer with register δ. 235
φ Swap value under the pointer with register φ. 237
ε Swap value under the pointer with register ε. 238

"Codepage value" means the corresponding PC character code.

Hello World

The following code represents a hello world program that uses a variety of the features available in Symbolic Brainfuck. Note all non-symbolic-brainfuck characters are ignored by the compiler and represent comments.


▲▲²²²²²▲        α  
▲▲²²²▼▼▼▼²²²▲   ß  
▲▲²²²²          π 

▲▲²²²²²²        µ 
▲▲²²²²²²²       δ 

µ → δ ⌂             

	α               
	≤
		→ ▲ ←      
		← ⌂⌂        
		▲ →→→→→→ ▲   
		←←←←←←← ⌂→⌂  
		▼        
	≥
	→ α ←          

	ß
	≤
		→ ▲ ←
		← ⌂⌂
		→▲→▲→▲→▲→→→▲→▲→▲→▲
 		→→ ⌂→⌂
		▼
	≥
	→ ß ←

	π
	≤
		→ ▲ ←
		← ⌂⌂
		→→→→→▲→→→→→→▲
		→ ⌂→⌂
		▼
	≥
	→ π

⌂⌂

Print output
▲▲▲▲▲▲▲¡→           H
▲▲▲▲¡→              e
▲▲▲▲▲▲▲▲▲▲▲¡→       l
▲▲▲▲▲▲▲▲▲▲▲¡→       l
▲▲▲▲▲▲▲▲▲▲▲▲▲▲¡→    o
¡→                   
▲▲▲▲▲▲▲▲▲▲▲
▲▲▲▲▲▲▲▲▲▲▲¡→       W
▲▲▲▲▲▲▲▲▲▲▲▲▲▲¡→    o
▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲¡→ r
▲▲▲▲▲▲▲▲▲▲▲¡→       l
▲▲▲¡→               d
▲¡                  !

Patterns

The example here uses several patterns available to SBF programmers.

▲▲²²²²²▲        α  
▲▲²²²▼▼▼▼²²²▲   ß  
▲▲²²²²          π 

▲▲²²²²²²        µ 
▲▲²²²²²²²       δ 

This section initializes a number of constants: 65, 97, 32, 128, and 256. 65 corresponds with the ascii character 'A', 97 with 'a', and 32 with ' '. The values 128 and 256 will be used as memory addresses in a jump table at the beginning of the tape. 128 will be the output data location and 256 represents a 'working address' that will be for storing loop counters while copying data.

µ → δ ⌂   

This line instantiates the jump table, with 128 in cell [0] and 256 in cell [1]. The ⌂ command then immediately jumps to cell 256.

α               
≤
  → ▲ ←      
  ← ⌂⌂        
  ▲ →→→→→→ ▲   
  ←←←←←←← ⌂→⌂  
  ▼        
≥
→ α ←       

This loop demonstrates how to copy values out of registers. First it swaps the value at the the working address (0) with register α (65). Since this sets the register to 0 and the value in cell [256] will be used as a loop counter the data will be lost if we do not replicate it during our loop and then reassign it to the register. Even though the example program does not later use the register value copying and restoring register values is considered best practice.

For every cycle through the loop the counter value at [256] is decremented and [257] is incremented. This guarantees that [257] will match the original contents of α after the loop. After incrementing [257] the program jumps to [0] and then immediately jumps to the value stored in [0], which is [128]. This demonstrates the use of a jump table. The 1st and 6th cells in the output location are incremented for every decremented of the loop counter and then the program jumps to [0], [1], [256] to complete the loop.

▲▲▲▲▲▲▲¡→
▲▲▲▲¡→
▲▲▲▲▲▲▲▲▲▲▲¡→
.
.
.

After copying the correct values to the output location, printing a string is as simple as incrementing by each desired letters offset from the beginning of the alphabet and then writing the result to the screen.

Runtime and compilation

Memory

While the tape is considered infinite, real world applications of Symbolic Brainfuck should implement a tape of 160,000 32 bit integers. A tape of this size can be considered pseudo-infinite, after all, 640k ought to be enough for anybody.

Notable implementations

.Net compiler in C#, Partially implemented

Computational Class

Since it is a super set of brainfuck, Symbolic Brainfuck is Turing Complete. It is also a super set of the I/D machine.