Brainfunk 语言实现

2016/01/01 Java

Brainfunk 是一个很简单的语言,如下是使用 Java 实现的解释器。

代码

import java.util.Scanner;
public class Run {

    char[] code = new char[1000];
    int codeP = 0;

    int[] stack = new int[1000];
    int stackP = 0;

    char[] data = new char[1000];
    int dataP = 0;

    public void setup(String code) {
        char[] chars = code.toCharArray();
        System.arraycopy(chars, 0, code, 0, chars.length);
    }
    public void run() {
        while (code[codeP] != 0) {
            switch (code[codeP]) {
                case '+' :
                    data[dataP]++;
                    break;
                case '-' :
                    data[dataP]--;
                    break;
                case '>' :
                    dataP = (dataP + 1) % 1000;
                    break;
                case '<' :
                    dataP = (dataP - 1) % 1000;
                    break;
                case '.' :
                    System.out.print(data[dataP]);
                    break;
                case ',' :
                    Scanner scanner = new Scanner(System.in);
                    String next = scanner.next();
                    data[dataP] =  next.toCharArray()[0];
                    break;
                case '[' :
                    if(data[dataP] != 0) {
                        stack[stackP] = codeP - 1;
                        stackP++;
                    } else {
                        int c = 1;
                        for(codeP++;code[codeP] != 0 && c != 0;codeP++) {
                            if(code[codeP] == '[') {
                                c++;
                            } else if(code[codeP] == ']') {
                                c--;
                            }
                        }
                    }
                    break;
                case ']' :
                    if(data[dataP] != 0) {
                        stackP--;
                        codeP = stack[stackP];
                    }
                    break;
                default:break;
            }
            codeP++;
        }
    }


    public static void main(String[] args) {
        Run run = new Run();
        //run.setup("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.");
        run.setup(",[.,]");
        run.run();
    }
}

Search

    Table of Contents