list of entries
working with brainfuck
Both a uniquely named and purposed esoteric programming language, brainfuck is composed of a mere eight commands, making it very difficult to use effectively. This language provides many useful functions despite its difficult syntax.
Because of the simplicity of this language, most brainfuck programs will be much larger in terms of file size that their counterparts in other languages. For this reason, many programmers have found ways to optimize code so that it can have a greater usability with fewest possible commands.
Brainfuck operates using a single array on which array elements can hold values ranging from 0 to 255. Arrays may be either unlimited or of size 255, depending on the implementation. There is a single pointer which is located on any one of the array elements at a time.
The pointer can increment or decrement the value within the current array element, and the pointer can be incremented or decremented to a new position on the array. Both values wrap around between 0 and 255 once the value goes out of this range, depending on the implementation.
This language also comes with a simple while loop that operated based on whether the value of the current array element is zero or not. If the value is zero, then the loop wil be discontinued.
A project on which I am working is a Python program that accepts ASCII character input and creates a brainfuck program which will output those characters when interpreted.
The program creates multiple segments of code for each character, then it finds the smallest segments and combines them into a single program.
This gives the effect of organization. For example, using a code segment length of eight and four code segments per line, the program will output the following code for a program that merely outputs hello:
++++++++ [>++++++ +++++++< -]>.—-. +++++++. .+++.
Because of the way most brainfuck interpreters parse code, comments are usually quite flexible. Comments can be placed throughout brainfuck code as long as no brainfuck commands are used:
++++++++[>++++++++<-]>+. print "A"
Because the specifications for the brainfuck language are not defined well, many brainfuck interpreters do not run code the same way. However, I have created an interpreter which follows the same rules as the majority of interpreters I have seen.
Some concepts have been modelled after pocmo's interpreter, such as the use of the filter function to clean up input and the buildbracemap function, which appears as get_map in my code.
Although many brainfuck programs will rely on ASCII input and output, there is also the possibility to create programs that operate merely with program-specified numbers. Mathematical programs can be created which use predefined outputs and even give output as the number itself and not ASCII characters.
This makes programming brainfuck much less complicated for certain types of programs as long as the interpreter allows output of values.
For example, the following program was created which can multiply a list of numbers and output the product as the value itself and not the necessary ASCII characters. This program, as long as it is interpreted with an interpreter which allows value output, will give the product of these numbers once it completes:
>+++>++++ set the array of factors < [ begin the multiplication loop > [>>[>]+[<]<-] expand the ending item into an array of ones <- decrement the preceding item [>>>[+>]<[<]<<-] increment each item in the array of ones >>> [[>]<-<[<]<+>>] sum the incremented array to form the product sum << [<+>-] move the product sum to the left by one position << ] exit the multiplication loop if no factor remains >. output the final product sum
This program can also be used with the addition of a preceding loop to multiply the same number a specified number of times, which outputs it as a power of the specified exponent:
>+++>++++ set the exponent and base respectively <- decrement the base [ begin the base duplication loop [>] < [>+>+<<-] make two copies of the base and remove the initial base > [[<+>-]>] move the copies to the left by one position << [<] >- decrement the exponent ] exit the base duplication loop if the exponent is zero > [[<+>-]>] move the copies to the left by one position <<< [ begin the multiplication loop > [>>[>]+[<]<-] expand the ending item into an array of ones <- decrement the preceding item [>>>[+>]<[<]<<-] increment each item in the array of ones >>> [[>]<-<[<]<+>>] sum the incremented array to form the product sum << [<+>-] move the product sum to the left by one position << ] exit the multiplication loop if no factor remains >. output the final product sum
While the language has relatively few practical purposes, it has proved a challenge to create some programs with it. Many kinds of programs can be created which manipulate the array in ways other than merely printing strings of characters.