摘要
@Parser.java
@HelloWorld.java
@运行结果
Parser.java
package com.afirez.brainfuck;
import java.io.IOException;
/**
* Project: brainfuck
* Author: afirez
* Date: 2016/7/29 0029.
*/
public class Parser {
private Codes mCodes;
private VM mVM;
public void run() {
mVM.run();
}
Parser() {
this.mCodes = new ReadCodesFromScanner();
mVM = new VM(mCodes.get());
}
Parser(Codes codes) {
this.mCodes = codes;
mVM = new VM(mCodes.get());
}
public interface Codes {
String get();
}
interface CallBack {
void execute(VM vm);
}
private class VM {
public static final String TOKENS = "><+-,.[]";
static final int STACK_SIZE = 1000;
static final int DATAS_SIZE = 30000;
char[] codes;
int codePointer;
int[] stack;
int stackPointer;
char[] datas;
int dataPointer;
CallBack[] callBacks;
private VM(String codes) {
this.codes = codes.toCharArray();
stack = new int[STACK_SIZE];
datas = new char[DATAS_SIZE];
initCallBacks();
}
private void initCallBacks() {
callBacks = new CallBack[128];
this.callBacks['>'] = new Forward();
this.callBacks['<'] = new Backward();
this.callBacks['+'] = new Increment();
this.callBacks['-'] = new Decrement();
this.callBacks['.'] = new Output();
this.callBacks[','] = new Input();
this.callBacks['['] = new WhileEntry();
this.callBacks[']'] = new WhileExit();
}
void run() {
for (; this.codePointer < codes.length ; this.codePointer++) {
this.callBacks[this.codes[this.codePointer]].execute(this);
}
}
}
private class Forward implements CallBack {
@Override
public void execute(VM vm) {
vm.dataPointer++;
vm.dataPointer %= VM.DATAS_SIZE;
}
}
private class Backward implements CallBack {
@Override
public void execute(VM vm) {
vm.dataPointer--;
vm.dataPointer = (vm.dataPointer + VM.DATAS_SIZE) % VM.DATAS_SIZE;
}
}
private class Increment implements CallBack {
@Override
public void execute(VM vm) {
vm.datas[vm.dataPointer]++;
}
}
private class Decrement implements CallBack {
@Override
public void execute(VM vm) {
vm.datas[vm.dataPointer]--;
}
}
private class Input implements CallBack {
@Override
public void execute(VM vm) {
vm.datas[vm.dataPointer] = getChar();
}
}
private class Output implements CallBack {
@Override
public void execute(VM vm) {
System.out.printf("%c",vm.datas[vm.dataPointer]);
}
}
private class WhileEntry implements CallBack {
@Override
public void execute(VM vm) {
if(0 != vm.datas[vm.dataPointer]) {
vm.stack[vm.stackPointer] = vm.codePointer - 1;
vm.stackPointer++;
} else {
int count = 1;
for (vm.codePointer++; 0 != vm.codes[vm.codePointer] && 0 != count; vm.codePointer++) {
if (vm.codes[vm.codePointer] == '[') {
count++;
} else if (vm.codes[vm.codePointer] == ']') {
count--;
}
}
}
}
}
private class WhileExit implements CallBack {
@Override
public void execute(VM vm) {
if (0 != vm.datas[vm.dataPointer]) {
vm.stackPointer--;
vm.codePointer = vm.stack[vm.stackPointer];
}
}
}
private char getChar() {
char temp;
try {
temp = (char) System.in.read();
} catch (IOException e) {
e.printStackTrace();
temp = 0;
}
return temp;
}
}
HelloWorld.java
package com.afirez.brainfuck;
/**
* Project: brainfuck
* Author: afirez
* Date: 2016/7/29 0029.
*/
public class HelloWorld {
public static void main(String[] args) {
new Parser(() -> "++++++++++[>+++++++>++++++++++>+++>+<<<<-]" +
">++.>+.+++++++..+++.>++.<<+++++++++++++++." +
">.+++.------.--------.>+.>.").run();
}
}