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() {
// ++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>++++++++++++++++++...
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:

View File

@@ -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);