added loops
This commit is contained in:
@@ -14,7 +14,7 @@ public class BfInterpreter {
|
|||||||
private byte[] memory;
|
private byte[] memory;
|
||||||
private Scanner sc;
|
private Scanner sc;
|
||||||
|
|
||||||
public final int MAX_SIZE = 65535;
|
public final int MAX_SIZE = 65535; // predefined max size for the array
|
||||||
|
|
||||||
public BfInterpreter(String code) throws Exception {
|
public BfInterpreter(String code) throws Exception {
|
||||||
if (code == null) {
|
if (code == null) {
|
||||||
@@ -29,6 +29,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]);
|
||||||
switch (this.code[i]) {
|
switch (this.code[i]) {
|
||||||
case '+':
|
case '+':
|
||||||
memory[pointer]++;
|
memory[pointer]++;
|
||||||
@@ -48,11 +49,35 @@ public class BfInterpreter {
|
|||||||
case ',':
|
case ',':
|
||||||
memory[pointer] = sc.next().trim().getBytes()[0];
|
memory[pointer] = sc.next().trim().getBytes()[0];
|
||||||
break;
|
break;
|
||||||
case '[': // als de memory[pointer] 0 is, spring naar de plek na de volgende ] ipv een vooruit
|
case '[': // als de memory[pointer] 0 is, spring naar de plek na de volgende ] ipv een
|
||||||
|
// vooruit
|
||||||
|
int endLoopPos = i;
|
||||||
|
// search for next ']'
|
||||||
|
while (code[endLoopPos] != ']') {
|
||||||
|
// keep incrementing until end of loop is found
|
||||||
|
endLoopPos++;
|
||||||
|
}
|
||||||
|
if (memory[pointer] == 0) {
|
||||||
|
i = endLoopPos + 1;
|
||||||
|
} else {
|
||||||
|
pointer++;
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case ']': // als de memory[pointer] niet 0 is, spring terug naar de plek voor de vorige [ ipv een vooruit
|
case ']': // als de memory[pointer] niet 0 is, spring terug naar de plek voor de vorige [
|
||||||
|
// ipv een vooruit
|
||||||
|
int beginLoopPos = i;
|
||||||
|
// search for previous '['
|
||||||
|
while (code[beginLoopPos] != '[') {
|
||||||
|
// keep decrementing until begin of loop is found
|
||||||
|
beginLoopPos--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (memory[pointer] != 0) {
|
||||||
|
i = beginLoopPos + 1;
|
||||||
|
} else {
|
||||||
|
pointer++;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user