September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -4,18 +4,17 @@ class BrainfuckProgram {
def instructionPointer = 0, dataPointer = 0
def execute() {
while (instructionPointer < program.size()) {
while (instructionPointer < program.size())
switch(program[instructionPointer++]) {
case '>': dataPointer++; break;
case '<': dataPointer--; break;
case '+': memory[dataPointer] = memoryValue + 1; break;
case '-': memory[dataPointer] = memoryValue - 1; break;
case ',': memory[dataPointer] = System.in.read(); break;
case '.': print((char)memoryValue); break;
case '[': handleLoopStart(); break;
case ']': handleLoopEnd(); break;
case '+': memory[dataPointer] = memoryValue + 1; break
case '-': memory[dataPointer] = memoryValue - 1; break
case ',': memory[dataPointer] = System.in.read(); break
case '.': print String.valueOf(Character.toChars(memoryValue)); break
case '[': handleLoopStart(); break
case ']': handleLoopEnd(); break
}
}
}
private getMemoryValue() { memory[dataPointer] ?: 0 }
@ -23,13 +22,12 @@ class BrainfuckProgram {
private handleLoopStart() {
if (memoryValue) return
int depth = 1;
while (instructionPointer < program.size()) {
int depth = 1
while (instructionPointer < program.size())
switch(program[instructionPointer++]) {
case '[': depth++; break;
case ']': if (!(--depth)) return;
case '[': depth++; break
case ']': if (!(--depth)) return
}
}
throw new IllegalStateException('Could not find matching end bracket')
}
@ -37,8 +35,8 @@ class BrainfuckProgram {
int depth = 0
while (instructionPointer >= 0) {
switch(program[--instructionPointer]) {
case ']': depth++; break;
case '[': if (!(--depth)) return; break;
case ']': depth++; break
case '[': if (!(--depth)) return; break
}
}
throw new IllegalStateException('Could not find matching start bracket')