Java BrainFuck解释器

Java BrainFuck解释器

Brainfuck只由八个简单的命令和一个指令指针组成。虽然它是完全图灵完备的,但它并不是为了实际使用,而是为了挑战和娱乐程序员。

BrainFuck只由8个字符命令组成,这使得它的使用即使对于简单的任务也非常具有挑战性 –

  • >命令增加数据指针(指向右边的下一个单元)。
  • <命令减少数据指针(指向左边的下一个单元格)。
  • +命令增加(增加1)数据指针的字节。
  • 命令”-“使数据指针上的字节减少(减少一个)。
  • .命令输出数据指针上的字节。
  • 命令接受一个字节的输入,将其值存储在数据指针的字节中。
  • [- 如果数据指针上的字节为0,那么不要将指令指针向前移动到下一条指令,而是将其向前跳到匹配的]指令之后。
  • ]- 如果数据指针上的字节为非零,那么与其将指令指针向前移动到下一条命令,不如跳回匹配的[ 命令后的命令。
  • (另外,]命令可以被翻译成无条件跳转到相应的[]命令,反之亦然;程序的行为是一样的,但由于不必要的重复搜索,运行速度会更慢。)
  • [和]的匹配通常与括号一样:每个[正好匹配一个],反之亦然,[在前,两者之间不能有未匹配的[或]。

由于BrainFuck只由这8个命令组成,为BrainFuck建立一个解释器是非常简单的。在这篇文章中,我们将建立一个简单的程序,将BrainFuck代码作为输入并产生所需的输出:我们将简单地接受BrainFuck代码作为一个字符串,并通过解析该字符串和检查每个字符的实际功能来产生输出:内存由一个字节类型的数组表示,模拟从0到65534的最大65535位的内存(65535是可以用无符号16位二进制数表示的最高数字)。变量ptr指的是内存数组的当前索引。
在这篇文章中,我们不会讨论在BrainFuck中编写程序的细节。

例子

Input : 
Output :  Hello World!

Input : 
Output : GEEKS FOR GEEKS

BrainFuck解释器的Java实现-

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);
    }
}

输出1

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

输出2

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程