BF2 is an extension of the Brainfuck programming language that uses the conventional brainfuck commands but enhances it by adding additional commands as well as being compiled into javascript. It retains the core Brainfuck functionality while adding flexibility for memory management and other features. Live Link
Brainfuck is an esoteric programming language created in 1993 by Urban Müller. It operates on a simple model consisting of an array of memory cells (or "tape") and a pointer that can move left or right across the cells. The language has only eight commands, making it minimalistic yet Turing complete. While Brainfuck is known for its challenging syntax and difficulty in writing complex programs, it serves as an interesting exercise in low-level programming concepts.
BF2 is a remake of the original Brainfuck concept, implemented in JavaScript with enhancements for better usability and flexibility. This interpreter aims to maintain the core simplicity and challenge of Brainfuck while introducing features that improve memory management and expand the language's capabilities. With BF2, users can explore the intriguing world of Brainfuck programming in a more accessible and modern environment.
Due to the compiler being built in javascript, an easy to implement easy of life feature has been inplemented which removes whitespaces and new/next lines. This allows to make easier to read brainfuck code which can be written out spread over multiple lines and indentation. Comments are supported aswell as any character other than the reserved characters are removed/ignored.
Tape terminal is a string that will be returned after the brainfuck code is interpreted. This string is formatted in a terminal style to provide feedback to the user showing exactly what changes are happening in the tape after a specific action and what is being printed during that action. This tape terminal is specially useful when writing code as it provides insight into the memory updation and allocation. The run function takes a boolean to determine whether the tape will be returned as it is a performance intensive action. Default is false meaning tape will not be recorded and performance will be optimal.
In BF2, tape length can be specified using the %
symbol. The Brainfuck code between the first two %
symbols is executed to determine the tape length.
%Brainfuck-code-for-tape-length%
- Example:
%+++++%
will set the tape length to 5. - If no tape length is provided, the tape defaults to length 10, and a warning is thrown.
BF2 standard Brainfuck commands and new commands:
Command | Description |
---|---|
> |
Move the pointer to the right. |
< |
Move the pointer to the left. |
+ |
Increment the memory cell at the pointer. |
- |
Decrement the memory cell at the pointer. |
. |
Output the ASCII value at the memory cell. |
, |
Input a character and store its ASCII value in the memory cell. |
[ |
Jump past the matching ] if the cell at the pointer is 0. |
] |
Jump back to the matching [ if the cell at the pointer is not 0. |
% |
Defines starting and ending of a segment of the code |
This Brainfuck code prints "Hello, World!" to the output.
%+++++++% ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
This code takes a single character input and prints "Hello, {input}".
++ c0 = 2
> +++++ c1 = 5
[< + > -] ++++ ++++ [< +++ +++ > -] < .
This code defines the tape length to 10 using the % symbol.
%++++++++++% >+.
This code prints the numbers from 1 to 10, each followed by a newline.
+++++++++++++++++++++++++++++++++++++++++++++++++>
++++++++++>
+++++++++[<<.+>.>-]
+++++++++[<<->>-]
<<.-.>.
The BF2 interpreter includes error handling for the following cases:
- Mismatched Brackets: If
[
and]
are mismatched, an error will be thrown. - Pointer Out of Bounds: If the pointer moves beyond the tape length, an error will occur.
- Missing/Invalid Tape Length: If no tape length is defined or if the definition is invalid, the tape defaults to length 1, and a warning is displayed.
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeature
). - Make your changes and commit them (
git commit -m 'Add new feature'
). - Push your branch (
git push origin feature/YourFeature
). - Open a Pull Request.