-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8fbbc79
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import sys | ||
import getch | ||
|
||
cells = [0] * 10000 | ||
|
||
f = open(sys.argv[1], 'r') | ||
code = f.read() | ||
|
||
pointer = 0 | ||
code_i = 0 | ||
brackets = {} | ||
|
||
matching_queue = [] | ||
for i, c in enumerate(code): | ||
if c == '[': | ||
matching_queue.append(i) | ||
elif c == ']': | ||
opening_bracket = matching_queue.pop() | ||
brackets[i] = opening_bracket | ||
brackets[opening_bracket] = i | ||
|
||
while code_i < len(code): | ||
c = code[code_i] | ||
if c == '>': | ||
pointer += 1 | ||
elif c == '<': | ||
pointer -= 1 | ||
elif c == '+': | ||
cells[pointer] += 1 | ||
elif c == '-': | ||
cells[pointer] -= 1 | ||
elif c == '.': | ||
print(chr(cells[pointer]), end='') | ||
elif c == ',': | ||
cells[pointer] = ord(getch.getch()) | ||
elif c == '[' and cells[pointer] == 0: | ||
code_i = brackets[code_i] | ||
elif c == ']' and cells[pointer] != 0: | ||
code_i = brackets[code_i] | ||
code_i += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class _Getch: | ||
"""Gets a single character from standard input. Does not echo to the | ||
screen.""" | ||
def __init__(self): | ||
try: | ||
self.impl = _GetchWindows() | ||
except ImportError: | ||
self.impl = _GetchUnix() | ||
|
||
def __call__(self): return self.impl() | ||
|
||
|
||
class _GetchUnix: | ||
def __init__(self): | ||
import tty, sys | ||
|
||
def __call__(self): | ||
import sys, tty, termios | ||
fd = sys.stdin.fileno() | ||
old_settings = termios.tcgetattr(fd) | ||
try: | ||
tty.setraw(sys.stdin.fileno()) | ||
ch = sys.stdin.read(1) | ||
finally: | ||
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | ||
return ch | ||
|
||
|
||
class _GetchWindows: | ||
def __init__(self): | ||
import msvcrt | ||
|
||
def __call__(self): | ||
import msvcrt | ||
return msvcrt.getch() | ||
|
||
|
||
getch = _Getch() |