Java 8 - 354 301 bytes
1. New version (301 bytes)
Golfed as this:
class D{static int i,j;public static void main(String[]z){boolean[]b=new boolean[256];Runnable[]r=new Runnable[26];r[20]=()->b[i]=!b[i];r[9]=()->i++;r[6]=()->i--;r[13]=()->System.out.print(b[i]?1:0);r[1]=()->j+=b[i]?1:0;r[22]=()->j-=b[i]?3:0;for(;;){r[z[j].charAt(3)-99].run();i=i<0|i>255?0:i;j++;}}}
Somewhat ungolfed version (whitespaces and comments added):
class D {
// Declared as static so they may be changed inside the lambdas without the compiler complaining about that.
// i is the bit index and j is the instruction pointer.
static int i, j;
public static void main(String[] z) {
// The array.
boolean[] b = new boolean[256];
// An array of lambdas. The index is the ascii code of 4th letter of the command name subtracted from 99.
// Thanks for CatsAndFluffy for telling about the 4th letter trick.
Runnable[] r = new Runnable[26];
r[20] = () -> b[i] = !b[i]; // chew
r[9] = () -> i++; // swallow
r[6] = () -> i--; // vomit
r[13] = () -> System.out.print(b[i] ? 1 : 0); // complain
r[1] = () -> j += b[i] ? 1 : 0; // feedtodog. It is 1 and 0 instead of 2 and 1 due to the j++ down there.
r[22] = () -> j -= b[i] ? 3 : 0; // playwithfood. It is -3 and 0 instead of -2 and +1 due to the j++ down there.
// This is the interpreter itself. Runs forever (or until an exception is raised).
for (;;) {
// Fetch an instruction and runs it. Throws an exception on unknown or mistyped instructions.
r[z[j].charAt(3) - 99].run();
// Resets the bit index if out of range.
i = i < 0 | i > 255 ? 0 : i;
// Next instruction. No special treatment for branching needed because they already considers this.
j++;
}
}
}
We surely should have testcases. I am not sure if there is some bug hiding out there.
Anyway I run it with that (input is given as command line arguments):
chew complain swallow chew complain swallow chew complain swallow complain
And here is the output:
1110
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at C.main(C.java:1)
The ArrayIndexOutOfBoundsException
is the standard way to finish the program, it is not a bug!
BTW, this answer abuses the fact that the 4th char of each command is different than each other (thanks CatsAndFluffy). This means that whew
is interpreted as chew
and that yellow
is interpreted as swallow
.
2. Old version (354 bytes)
If strict conformance to command names are required (and just looking for the 4th letter in command's name is not enough), then I have this (older) answer with 354 bytes:
import java.util.*;class C{static int i,j;public static void main(String[]z){boolean[]b=new boolean[256];List o=Arrays.asList("chew","swallow","vomit","complain","feedtodog","playwithfood");Runnable[]r={()->b[i]=!b[i],()->i++,()->i--,()->System.out.print(b[i]?1:0),()->j+=b[i]?1:0,()->j-=b[i]?3:0};for(;;){r[o.indexOf(z[j])].run();i=i<0|i>255?0:i;j++;}}}
Somewhat ungolfed version (whitespaces and comments added):
// Needed for java.util.List and java.util.Arrays.
import java.util.*;
class C {
// Declared as static so they may be changed inside the lambdas without the compiler complaining about that.
// i is the bit index and j is the instruction pointer.
static int i, j;
public static void main(String[] z) {
// The array.
boolean[] b = new boolean[256];
// Declared as a list so we can use the indexOf method further down. Screw up the generics.
List o = Arrays.asList("chew", "swallow", "vomit", "complain", "feedtodog", "playwithfood");
// An array of lambdas.
Runnable[] r = {
() -> b[i] = !b[i], // chew
() -> i++, // swallow
() -> i--, // vomit
() -> System.out.print(b[i] ? 1 : 0), // complain
() -> j += b[i] ? 1 : 0, // feedtodog. It is 1 and 0 instead of 2 and 1 due to the j++ down there.
() -> j -= b[i] ? 3 : 0 // playwithfood. It is -3 and 0 instead of -2 and +1 due to the j++ down there.
};
// This is the interpreter itself. Runs forever (or until an exception is raised).
for (;;) {
// Fetch an instruction and runs it. Throws an exception on unknown or mistyped instructions.
r[o.indexOf(z[j])].run();
// Resets the bit index if out of range.
i = i < 0 | i > 255 ? 0 : i;
// Next instruction. No special treatment for branching needed because the branching instructions already considers this.
j++;
}
}
}