Open In App

BrainFuck Interpreter in Java

Brainfuck consists of only eight simple commands and an instruction pointer. While it is fully Turing-complete, it is not intended for practical use, but to challenge and amuse programmers. 
BrainFuck consists of 8 character commands only which makes its use very challenging even for simple tasks – 

Since BrainFuck consists of only these 8 commands, building an interpreter for BrainFuck is quite simple. In this article, we will build a simple program which takes a BrainFuck code as input and produces the desired output. We will simply accept the BrainFuck code as a String and produce the output by parsing the String and checking every character for its actual functionality. The memory is represented by an array of byte type simulating memory of max 65535 bits from 0 to 65534(65535 is the highest number which can be represented by an unsigned 16-bit binary number). The variable ptr refers to the current index of the memory array.
In this article, we won’t be discussing the details of writing programs in BrainFuck.For more details on writing BrainFuck programs, refer to the following links: 
 



Examples: 

Input : 
Output :  Hello World!

Input : 
Output : GEEKS FOR GEEKS

Java Implementation of the BrainFuck Interpreter-






import java.util.*;
 
class BrainFuck
{
    private static Scanner ob = new Scanner(System.in);
    private static int ptr; // Data pointer
     
    // Max memory limit. It is the highest number which
    // can be represented by an unsigned 16-bit binary
    // number. Many computer programming environments
    // beside brainfuck may have predefined
    // constant values representing 65535.
    private static int length = 65535;
     
    // Array of byte type simulating memory of max
    // 65535 bits from 0 to 65534.
    private static byte memory[] = new byte[length];
     
    // Interpreter function which accepts the code
    // a string parameter
    private static void interpret(String s)
    {
        int c = 0;
         
        // Parsing through each character of the code
        for (int i = 0; i < s.length(); i++)
        {
            // BrainFuck is a tiny language with only
            // eight instructions. In this loop we check 
            // and execute all those eight instructions
             
             
            // > moves the pointer to the right
            if (s.charAt(i) == '>')
            {
                if (ptr == length - 1)//If memory is full
                    ptr = 0;//pointer is returned to zero
                else
                    ptr ++;
            }
             
            // < moves the pointer to the left
            else if (s.charAt(i) == '<')
            {
                if (ptr == 0) // If the pointer reaches zero
 
                    // pointer is returned to rightmost memory
                    // position
                    ptr = length - 1;
                else
                    ptr --;
            }
             
            // + increments the value of the memory
            // cell under the pointer
            else if (s.charAt(i) == '+')
                memory[ptr] ++;
 
            // - decrements the value of the memory cell
            // under the pointer
            else if (s.charAt(i) == '-')
                memory[ptr] --;
 
            // . outputs the character signified by the
            // cell at the pointer
            else if (s.charAt(i) == '.')
                System.out.print((char)(memory[ptr]));
 
            // , inputs a character and store it in the
            // cell at the pointer
            else if (s.charAt(i) == ',')
                memory[ptr] = (byte)(ob.next().charAt(0));
 
            // [ jumps past the matching ] if the cell
            // under the pointer is 0
            else if (s.charAt(i) == '[')
            {
                if (memory[ptr] == 0)
                {
                    i++;
                    while (c > 0 || s.charAt(i) != ']')
                    {
                        if (s.charAt(i) == '[')
                            c++;
                        else if (s.charAt(i) == ']')
                            c--;
                        i ++;
                    }
                }
            }
 
            // ] jumps back to the matching [ if the
            // cell under the pointer is nonzero
            else if (s.charAt(i) == ']')
            {
                if (memory[ptr] != 0)
                {
                    i --;
                    while (c > 0 || s.charAt(i) != '[')
                    {
                        if (s.charAt(i) == ']')
                            c ++;
                        else if (s.charAt(i) == '[')
                            c --;
                        i --;
                    }
                }
            }
        }
    }
 
    // Driver code
    public static void main(String args[])
    {
        System.out.println("Enter the code:");
        String code = ob.nextLine();
        System.out.println("Output:");
        interpret(code);
    }
}

Output 1: 

Enter the code:
--[+++++++>-->+>+>+<<<->---.>--..>+.<<<.+>->>.+++[.<]
Output:
Hello World!

Output 2: 

Enter the code:
++++++++++[>+++++++>++++++++>+++<+++.>++..<+.
Output:
GEEKS FOR GEEKS

 


Article Tags :