The Trainf*ck project is an attempt to extend the capabilities of the esoteric Brainf*ck language to include viable outside interaction, including file operations and TCP networking. In this respect, the goals of the project are similar to those of Brainf*ck++; Trainf*ck differs in the implementation of these goals.
As is documented elsewhere, Brainf*ck has eight simple operations, which may act on a data buffer of a given size. Trainf*ck extends the list of operations to eighteen, while leaving the other aspects as is. The new instructions are as follows (P refers to the current address in the data space).
Instructions
- File I/O
-
#
- Open or close a file. Takes a null-terminated filename string starting at P. If called again, closes the currently open file; no parameter required for close.;
- Read a byte from file. Saves the byte to P.:
- Write a byte to file. Fetches the byte from P, and writes.(
- Rewind one byte. Modifies P: 0 if start of file was reached, 1 otherwise.)
- Move forward a byte. Modifies P: 0 if end of file was reached, 1 otherwise.
- Networking
-
%
- Connect to an address/port. Takes two parameters: big-endian IPv4 address starting at P, and big-endian TCP port number starting at P+4. If called again, closes currently open socket; no parameters required.$
- Listen on an address/port. Takes address and port as detailed for %. If called again, closes currently open socket; no parameters required.@
- Accepts an incoming connection. If called again, closes the currently accepted connection.`
- Receive a byte from the network stream. Saves the byte to P, or zero if connection was closed.'
- Send a byte. Fetches the byte from P, and sends.
Examples
The canonical "Hello World" example as written in Brainf*ck will run under Trainf*ck exactly as it does under Brainf*ck. An example implementation follows, which prints out "Hello World!".
Trainf*ck sample: Hello World
>+++++++++[<++++++++>-]<.> +++++++[<++++>-]<+.+++++++..+++.>>>++++++++[<++++>-]<. >>>++++++++++[<+++++++++>-]<---.<<<<.+++.------.--------.>>+.
A simple example demonstrating Trainf*ck's file handling capabilities is that of finding the size of a file. The following example calculates the size of the file "abcd", and outputs it as a character code value. The caveat with this simple example is that it will wrap the counted value after 255.
Trainf*ck sample: File size
>++++++++++++++++[<++++++>-]< Generate backtick in 0 [>+>+>+>+<<<<-] Copy 4 times >+>++>+++>++++ Generate "abcd" <<<#<+ Open file and init counter >[<+:>)] Count up file size <.# Output file size and close
Networking is where Trainf*ck's capabilities shine: the language provides the ability to perform networking tasks easily and efficiently. The following example is an implementation of an 'echo' daemon, which listens on port 20480 and repeats any text sent to it.
Trainf*ck sample: Echo daemon
>>> Address 0x00000000 >>++++++++[<++++++++++>-] Port 0x5000 <<<<<$ Listen +[ Continual loop @ Accept a connection [`'] Loop echoing @ Close connection +] Continue endless loop
Implementation
The Trainf*ck interpreter is written in C++, and has been tested working on Linux. The code has been designed with portability in mind, and as such there should be no major issues with execution on any system.
The interpreter is currently incomplete, with the functionality of keyboard input being missing. It is anticipated that this will be added at a later date.
You can obtain the interpreter and the sample files detailed above from the following address.