Java中的BrainFuck解释器

Brainfuck只包含八个简单的命令和一个指令指针。虽然它是完全图灵完成的,但它不是为了实际使用,而是为了挑战和娱乐程序员。 BrainFuck只包含8个字符的命令,这使得它的使用非常具有挑战性,即使是对于简单的任务-

null
  • >命令增加数据指针(指向右边的下一个单元格)。
  • +命令增加(增加一个)数据指针处的字节。
  • –命令使数据指针处的字节递减(减少一个)。
  • 这个命令在数据指针处输出字节。
  • 命令接受一个字节的输入,将其值存储在数据指针的字节中。
  • [–如果数据指针处的字节为零,则不将指令指针向前移动到下一个命令,而是将其向前跳转到匹配]命令之后的命令。
  • ]–如果数据指针处的字节不为零,则不要将指令指针向前移动到下一个命令,而是将其跳回匹配的[command]之后的命令。
  • (或者,]命令可以转换为无条件跳转到相应的[命令,反之亦然;程序的行为相同,但由于不必要的重复搜索,运行速度会更慢。)
  • [和]通常与括号匹配:每个[正好匹配一个],反之亦然,[排在第一位,两者之间不可能有不匹配的[或]。

因为BrainFuck只包含这8个命令,所以为BrainFuck构建一个解释器非常简单。在本文中,我们将构建一个简单的程序,它以BrainFuck代码作为输入,并生成所需的输出。我们将简单地接受BrainFuck代码作为字符串,并通过解析字符串并检查每个字符的实际功能来生成输出。内存由字节型模拟内存数组表示,从0到65534,最大为65535位(65535是可以用无符号16位二进制数表示的最高数字)。变量ptr指的是内存阵列的当前索引。 在本文中,我们将不讨论在BrainFuck中编写程序的细节。有关编写BrainFuck程序的更多详细信息,请参阅以下链接:

例如:

Input : Output :  Hello World!Input : Output : GEEKS FOR GEEKS

BrainFuck解释器的Java实现-

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

本文由 苏米克·拉克什 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享