71 lines
2.5 KiB
Python
71 lines
2.5 KiB
Python
import sys
|
|
import getch
|
|
|
|
memory, stack, ptr, i, program = [0 for v in range(300000)], [], 0, 0, sys.argv[1]
|
|
|
|
|
|
while i < len(program):
|
|
cmd = program[i]
|
|
|
|
memory[ptr], ptr = (
|
|
(memory[ptr] - 1) % 256
|
|
) if cmd == "-" else (
|
|
(memory[ptr] + 1) % 256
|
|
) if cmd == "+" else [
|
|
lambda x:
|
|
memory[ptr],
|
|
lambda x:
|
|
ord(getch.getch()),
|
|
print(
|
|
chr(memory[ptr]),
|
|
end="")
|
|
if cmd == "."
|
|
else None
|
|
|
|
][
|
|
1
|
|
if cmd == ","
|
|
else
|
|
0
|
|
](None), (
|
|
(ptr - 1) % len(
|
|
memory
|
|
)
|
|
) if cmd == "<" else (
|
|
(ptr + 1) % len(
|
|
memory
|
|
)
|
|
) if cmd == ">" else (
|
|
ptr
|
|
)
|
|
|
|
if cmd == "]":
|
|
depth = 0
|
|
|
|
while not (program[i] == "[" and depth == 0):
|
|
depth, i = (
|
|
depth + 1
|
|
) if program[i-1] == "]" else (
|
|
depth - 1
|
|
) if program[i-1] == "[" and depth != 0 else (
|
|
depth
|
|
), i-1
|
|
i -= 1
|
|
|
|
if cmd == "[":
|
|
if memory[ptr] == 0:
|
|
depth = 0
|
|
|
|
while not (program[i] == "]" and depth == 0) and i < len(program):
|
|
depth, i = (
|
|
depth + 1
|
|
) if program[i+1] == "[" else (
|
|
depth - 1
|
|
) if program[1+i] == "]" and depth != 0 else (
|
|
depth
|
|
), i+1
|
|
|
|
i += 1
|
|
|
|
|
|
|