User:Elronnd/brainfcuk

From Esolang
Jump to navigation Jump to search

This is brainfcuk (speling intentional). It is a brainfuck derivative/extension. Below follows the official specification.

As with other extensions, brainfuck code should be forwards-compatible with brainfcuk, except for the fact that certain characters may be ignored/considered comments by standard brainfuck, but be considered commands in brainfcuk.


It inherits the following eight basic commands from brainfuck but aside from that goes in a different direction from most brainfuck extensions, as far as I know:

Command Description
> Move the pointer to the right
< Move the pointer to the left
+ Increment the memory cell under the pointer
- Decrement the memory cell under the pointer
. Print the character signified by the cell at the pointer to stdout
, 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

In addition to those commands, the following commands are also available:

Command Description
{ Place n cells to the right of the current cell into the string cell, where n is the number stored in the current cell. (More on this later).
} Place n cells to the left of the current cell into the string cell, where n is the number stored in the current cell.
( Begin function declaration in current cell.
) End function declaration in current cell.
" Open the file specified by the string cell for writing. If it is empty, open stdout.
_ Open the file specified by the string cell for reading. If it is empty, open stdin.
/ Write the values contained in the string cell to the file currently open for writing
\ Read the values contained in the file currently open for reading into the string cell.
+ Close the currently open file, if it exists.
$ Place the contents of the environment variable specified by the string cell in the string cell.
: Run the functions in the current cell
' Print out the contents of the current cell to stderr.


Overview

File handling

There is now a new thing in addition to the tape, the string, and each cell on the tape now holds two values (or there are two tapes, whichever way you want to look at it). The string is separate from the the tape and is used for file operations and environment variable handling. Here is an example of its usage:

Let us assume that some section of the tape looks like this:

            "lsdkf"

[000] [009] [014] [008] [102] [105] [108] [101] [46] [116] [120] [116] [032] [014]
                    ^

Where ^ represents the current cell and the value enclosed by quotes represents the current string cell. 102, 105, 108, 101, 46, 116, 120, 116, collectively are the ASCII values that together represent file.txt

Now, given this configuration, let us put { into the parser. It will take 8 values from the right and put them into the string value. The program state is now this:

            "file.txt"

[000] [009] [014] [008] [102] [105] [108] [101] [46] [116] [120] [116] [032] [014]
                    ^

Now let's open the file by giving " to the interpreter. This will open file.txt for writing. Since file.txt does not yet exist (assume), the [008] will be replaced by [002]. We can now use the hello world program and then close the file (+). file.txt will have been created and "Hello, world!\n" will have been placed in it.

Each cell on the tape also optionally holds a "function", although it is more akin to a procedure in that it takes no arguments and returns nothing. The function in the current cell can be written to by enclosing any other set of brainfcuk instructing within ( ). This code will not be executed immediately, but only when called via the : instruction. This allows for a crude form of self-modifying code whereby which function is placed in the current cell is conditional. Note that the content of a function need not be a valid brainfcuk program in and of itself -- only a segment of one. For example, (])[-: is valid code, and equivalent to [-].