Extended Brainfuck

From Esolang
Jump to: navigation, search

Extended Brainfuck is a variant of brainfuck with unknown authorship. It has an extended vocabulary to allow smaller source sizes and improved speed of execution. A correct implementation dynamically lengthens the size of the internal array as needed, the arrays length increasing by half. This is the first release of the Extended Brainfuck.

The goal of extended brainfuck is to make the normally hard to program in brainfuck, fairly easy and be completely impossible to actually compile.

Basic Commands[edit]

Extended Brainfuck has the basic 8 commands.

The eight Basic language commands, each consisting of a single character:

Character Meaning
> Increment the pointer (to point to the next cell to the right).
< Decrement the pointer (to point to the next cell to the left).
+ Increment (increase by one) the byte at the pointer.
- Decrement (decrease by one) the byte at the pointer.
. Output the value of the byte at the pointer.
, Accept one byte of input, storing its value in the byte at the pointer.
[ Jump forward to the command after the corresponding ] if the byte at the pointer is zero.
] Jump back to the command after the corresponding [ if the byte at the pointer is nonzero.

Extended Type I[edit]

An extended set of 9 commands which can be accessed by designating -x on the interpreters commandline.

The nine Extended Type I language commands, each consisting of a single character:

Character Meaning
@ Ends the program, generally used as a separator between code and data.
$1 Overwrites the byte in storage with the byte at the pointer.
!1 Overwrites the byte at the pointer with the byte in storage.
} Performs a single right logical shift of the byte at the pointer.
{ Performs a single left logical shift of the byte at the pointer.
~ Performs a bitwise NOT operation on the byte at the pointer (all 1's and 0's are swapped).
^ Performs a bitwise XOR operation on the byte at the pointer and the byte in storage, storing its result in the byte at the pointer.
& Performs a bitwise AND operation on the byte at the pointer and the byte in storage, storing its result in the byte at the pointer.
| Performs a bitwise OR operation on the byte at the pointer and the byte in storage, storing its result in the byte at the pointer.
  1. Storage — The number in storage can only be changed by using a $ command, operations take place in the cell, not in storage.

Extended Type II[edit]

The second extended set of 8 commands which can be accessed by designating -x2 on the interpreters commandline. This set also adds the code itself to the cells, such as Self-modifying Brainfuck does and allows the code to modify itself. This set automatically inherits the commands from Extended Type I.

In Type II, if a loop brackets partner cannot be found before the @ ignore that bracket.

Data after the @ is added into the data cells as cell initializer data. Otherwise as normal all cells starts with at zero. Appended data can also be added to the program using the -d<filename> commandline switch.

Extended Type II Topography

Storage Source Memory

The storage byte takes up the leftmost cell. Followed by the source code, then directly by the normal memory cells.

The eight Extended Type II language commands, each consisting of a single character:

Character Meaning
?1 Sets the current point of execution to the location of the pointer.
)2 Inserts a new cell before the current pointer, shifting all following cells to the right.
(3 Removes the cell at the current pointer, shifting all following cells to the left.
*4 Multiplies the byte at the pointer with the byte in storage, storing its result in the byte at the pointer.
/4 Divides the byte at the pointer with the byte in storage, storing its result in the byte at the pointer.
=4 Adds the byte at the pointer with the byte in storage, storing its result in the byte at the pointer.
_4 Subtracts the byte at the pointer with the byte in storage, storing its result in the byte at the pointer.
%4 Preforms a Modulo operation on the byte at the pointer and the byte in storage, storing its result in the byte at the pointer.
  1. Goto — The new position will execute immediately after the ?, there is no restrictions on the goto, meaning it is possible to jump into the middle of a loop.
  2. Insertion — After this operation resolves the pointer is pointing at the newly created cell.
  3. Deletion — After this operation resolves the pointer is pointing at the cell that was originally to the right of the deleted cell.
  4. Mathematical Operators — Adding or Multiplying a number only stores the first 8 bits of the result. Dividing by zero causes the interpreter to crash or causes undefined behavior. Subtracting a larger number from a smaller number may produce unpredictable results.

Extended Type III[edit]

This is an extended set to speed the execution and self modifying ability of Extended Brainfuck. This unlike previous versions may cause problems with text within its code body (but ignores all unknown characters). So a comment command has been added. These can be accessed by designating -x3 on the interpreters command line.

The Extended Type III language commands, each consisting of a single character:

Character Meaning
X This command moves the pointer to the cell containing currently executing command. (That is this command)
x1 This command returns the pointer it was pointing to prior to the last call to X, if X has not been called, it resets it to its initial position (cell 0). If x has been called to return from X, it returns it to the cell it would have returned to.
M Marks the current cell as the cell to use as the 'storage' cell defined in extended type I.
m Resets the storage cell to the initial storage cell.
L Locks a cell and prevents it from being changed. Any attempt to change it silently fails. (This includes deleting it)
l Unlocks a previously locked cell and allows it to be edited again. Calling this on a already unlocked cell does nothing.
: Moves the pointer forward or back by the signed number at the current cell. So a cell value of 5, moves the pointer ahead 5 places, where as 251 (signed -5) would move the pointer back 5 places. This is useful for simple variable determining pointer movement.
0 to F Fast Cell Initializer Commands: 0 1 2 3 4 5 6 7 8 9 A B C D E F

Turns values 0 to F to their hex equivalents and initializes the cell to be the value of these multiplied by 16 or 0x10. So F would change the value of the cell to (15*16) 240 or 0xF0, where as 5 changes it to (5*16) 80 or 0x50.

# All following characters between this and the next # are ignored (unless they are in the data area). Comments can be changed to become uncommented due to the nature of self modifying code.
  1. Cell — If the cell was deleted somehow, it returns to the cell immediately to the right of the that was deleted cell. (or if that one was deleted to the right of that, and so on).

Hello World![edit]

Basic[edit]

++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

Type I[edit]

++++{+{{{.$>-}}^>++{{{++$<^.$>[-]++{{+^..$>+++|.>++{{{{.<<<<$>>>>>-}}}^.<<.+++.<.<-.>>>+.>>+++++{.@

Type II[edit]

>++{{$+*.>!++$*+.>!*=--..$>!+++.>++{${*.>++$</$>![<]!-$>=.>>>.+++.<.<-.<<=+++.@

Code with appended initializer data.

[.>]@Hello World!

Type III[edit]

>5--------.7-----------.+++++++..+++.<2.5+++++++.>.+++.------.--------.2+.

Using fast cell initializer commands to compress the code. This example was created using Brainfuck code generation with a genetic algorithm.