Self-modifying Brainfuck

From Esolang
Jump to navigation Jump to search

Self-modifying Brainfuck, also known as SMBF, is a variant of brainfuck by Simon Howard, which allows a program to modify its own source code. The program code is placed in the data array, and the data pointer starts at the byte immediately after the last instruction in the code.

Language overview

SMBF 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:

Command Description
+ Increment the memory cell under the pointer
- Decrement the memory cell under the pointer
> Move the pointer to the right
< Move the pointer to the left
. Output the character signified by the cell at the pointer
, Input a character and store it in the cell at the pointer
[ Jump past the matching ] if the cell under the pointer is 0
] Jump back to the matching [ if the cell under the pointer is nonzero

All characters other than ><+-.,[] are ignored if executed but may be accessed, modified, or printed as part of the source code on the tape.

Examples

This prints Hello, World! :

<[.<][0!dlroW ,olleH]

This will print 1 only in the Python interpreter (otherwise nothing), because it dynamically creates code on the tape outside of the initial program code:

>>++++++[<<++++++++++>++++++++>-]<--<1

This is a quine. It will print it's own source code:

<[<]>[.>]Quines are easy!

It will print it's own source code:

<[<]0>[.>]

See also

External resources