finished interpreter
This commit is contained in:
@@ -27,6 +27,7 @@ public class BfInterpreter {
|
||||
}
|
||||
|
||||
public void interpret() {
|
||||
// ++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>++++++++++++++++++...
|
||||
int pointer = 0;
|
||||
for (int i = 0; i < this.code.length; i++) {
|
||||
// System.out.println("char is now " + this.code[i]);
|
||||
@@ -51,33 +52,33 @@ public class BfInterpreter {
|
||||
break;
|
||||
case '[': // als de memory[pointer] 0 is, spring naar de plek na de volgende ] ipv een
|
||||
// vooruit
|
||||
// System.out.println("begin loop cell is " + memory[i]);
|
||||
int endLoopPos = i;
|
||||
// search for next ']'
|
||||
while (code[endLoopPos] != ']') {
|
||||
// keep incrementing until end of loop is found
|
||||
endLoopPos++;
|
||||
}
|
||||
// System.out.println("end loop pos: " + endLoopPos);
|
||||
if (memory[pointer] == 0) {
|
||||
i = endLoopPos + 1;
|
||||
} else {
|
||||
pointer++;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case ']': // als de memory[pointer] niet 0 is, spring terug naar de plek voor de vorige [
|
||||
// ipv een vooruit
|
||||
int beginLoopPos = i;
|
||||
// System.out.println("eind loop");
|
||||
// search for previous '['
|
||||
while (code[beginLoopPos] != '[') {
|
||||
// keep decrementing until begin of loop is found
|
||||
beginLoopPos--;
|
||||
}
|
||||
// System.out.println("begin loop pos: " + beginLoopPos);
|
||||
|
||||
if (memory[pointer] != 0) {
|
||||
i = beginLoopPos + 1;
|
||||
} else {
|
||||
pointer++;
|
||||
}
|
||||
i = beginLoopPos;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
@@ -8,7 +8,7 @@ import java.io.UnsupportedEncodingException;
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String excl = "----[-->+++<]>-.--[->+++<]>..[--->+<]>---.";
|
||||
String excl = "++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>>>-----------.++++++++++++..+++++++++++++++.<<++.>+++++++.>-------------------.+++++++++++++.---.------.+++++++++++++.<<+.";
|
||||
|
||||
try {
|
||||
BfInterpreter interpreter = new BfInterpreter(excl);
|
||||
|
||||
Reference in New Issue
Block a user