finished interpreter

This commit is contained in:
Sem van der Hoeven
2019-12-03 10:46:04 +01:00
parent ff43a38c57
commit 176a329571
2 changed files with 9 additions and 8 deletions

View File

@@ -27,6 +27,7 @@ public class BfInterpreter {
} }
public void interpret() { public void interpret() {
// ++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>++++++++++++++++++...
int pointer = 0; int pointer = 0;
for (int i = 0; i < this.code.length; i++) { for (int i = 0; i < this.code.length; i++) {
// System.out.println("char is now " + this.code[i]); // System.out.println("char is now " + this.code[i]);
@@ -51,33 +52,33 @@ public class BfInterpreter {
break; break;
case '[': // als de memory[pointer] 0 is, spring naar de plek na de volgende ] ipv een case '[': // als de memory[pointer] 0 is, spring naar de plek na de volgende ] ipv een
// vooruit // vooruit
// System.out.println("begin loop cell is " + memory[i]);
int endLoopPos = i; int endLoopPos = i;
// search for next ']' // search for next ']'
while (code[endLoopPos] != ']') { while (code[endLoopPos] != ']') {
// keep incrementing until end of loop is found // keep incrementing until end of loop is found
endLoopPos++; endLoopPos++;
} }
// System.out.println("end loop pos: " + endLoopPos);
if (memory[pointer] == 0) { if (memory[pointer] == 0) {
i = endLoopPos + 1; i = endLoopPos + 1;
} else { }
pointer++;
}
break; break;
case ']': // als de memory[pointer] niet 0 is, spring terug naar de plek voor de vorige [ case ']': // als de memory[pointer] niet 0 is, spring terug naar de plek voor de vorige [
// ipv een vooruit // ipv een vooruit
int beginLoopPos = i; int beginLoopPos = i;
// System.out.println("eind loop");
// search for previous '[' // search for previous '['
while (code[beginLoopPos] != '[') { while (code[beginLoopPos] != '[') {
// keep decrementing until begin of loop is found // keep decrementing until begin of loop is found
beginLoopPos--; beginLoopPos--;
} }
// System.out.println("begin loop pos: " + beginLoopPos);
if (memory[pointer] != 0) { if (memory[pointer] != 0) {
i = beginLoopPos + 1; i = beginLoopPos;
} else { }
pointer++;
}
break; break;
default: default:

View File

@@ -8,7 +8,7 @@ import java.io.UnsupportedEncodingException;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
String excl = "----[-->+++<]>-.--[->+++<]>..[--->+<]>---."; String excl = "++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>>>-----------.++++++++++++..+++++++++++++++.<<++.>+++++++.>-------------------.+++++++++++++.---.------.+++++++++++++.<<+.";
try { try {
BfInterpreter interpreter = new BfInterpreter(excl); BfInterpreter interpreter = new BfInterpreter(excl);