Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
46
Task/Execute-Brain----/Groovy/execute-brain-----1.groovy
Normal file
46
Task/Execute-Brain----/Groovy/execute-brain-----1.groovy
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
class BrainfuckProgram {
|
||||
|
||||
def program = '', memory = [:]
|
||||
def instructionPointer = 0, dataPointer = 0
|
||||
|
||||
def execute() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private getMemoryValue() { memory[dataPointer] ?: 0 }
|
||||
|
||||
private handleLoopStart() {
|
||||
if (memoryValue) return
|
||||
|
||||
int depth = 1;
|
||||
while (instructionPointer < program.size()) {
|
||||
switch(program[instructionPointer++]) {
|
||||
case '[': depth++; break;
|
||||
case ']': if (!(--depth)) return;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException('Could not find matching end bracket')
|
||||
}
|
||||
|
||||
private handleLoopEnd() {
|
||||
int depth = 0
|
||||
while (instructionPointer >= 0) {
|
||||
switch(program[--instructionPointer]) {
|
||||
case ']': depth++; break;
|
||||
case '[': if (!(--depth)) return; break;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException('Could not find matching start bracket')
|
||||
}
|
||||
}
|
||||
1
Task/Execute-Brain----/Groovy/execute-brain-----2.groovy
Normal file
1
Task/Execute-Brain----/Groovy/execute-brain-----2.groovy
Normal file
|
|
@ -0,0 +1 @@
|
|||
new BrainfuckProgram(program: '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.').execute()
|
||||
|
|
@ -1,22 +1,21 @@
|
|||
bf[program_, input_] :=
|
||||
Module[{p = Characters[program], pp = 0, m, mp = 0, bc = 0,
|
||||
instr = StringToStream[input],
|
||||
outstr = OpenWrite[BinaryFormat -> True]},
|
||||
m[_] = 0;
|
||||
While[pp < Length@p,
|
||||
pp++;
|
||||
Switch[p[[pp]],
|
||||
">", mp++,
|
||||
"<", mp--,
|
||||
"+", m[mp]++,
|
||||
"-", m[mp]--,
|
||||
".", BinaryWrite[outstr, m[mp]],
|
||||
",", m[mp] = BinaryRead[instr],
|
||||
"[", If[m[mp] == 0,
|
||||
bc = 1;
|
||||
While[bc > 0, pp++; Switch[p[[pp]], "[", bc++, "]", bc--]]],
|
||||
"]", If[m[mp] != 0,
|
||||
bc = -1;
|
||||
While[bc < 0, pp--; Switch[p[[pp]], "[", bc++, "]", bc--]]]]];
|
||||
Close[instr]; FilePrint[Close[outstr]]];
|
||||
instr = StringToStream[input],
|
||||
m[_] = 0;
|
||||
While[pp < Length@p,
|
||||
pp++;
|
||||
Switch[p[[pp]],
|
||||
">", mp++,
|
||||
"<", mp--,
|
||||
"+", m[mp]++,
|
||||
"-", m[mp]--,
|
||||
".", BinaryWrite[OutputStream["stdout", 1], m[mp]],
|
||||
",", m[mp] = BinaryRead[instr],
|
||||
"[", If[m[mp] == 0,
|
||||
bc = 1;
|
||||
While[bc > 0, pp++; Switch[p[[pp]], "[", bc++, "]", bc--]]],
|
||||
"]", If[m[mp] != 0,
|
||||
bc = -1;
|
||||
While[bc < 0, pp--; Switch[p[[pp]], "[", bc++, "]", bc--]]]]];
|
||||
Close[instr];];
|
||||
bf[program_] := bf[program, ""]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue