Skip to content

Commit

Permalink
Initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
unparalloser committed Aug 29, 2021
0 parents commit 8fbbc79
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
40 changes: 40 additions & 0 deletions brainfuck.py
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
38 changes: 38 additions & 0 deletions getch.py
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()

0 comments on commit 8fbbc79

Please sign in to comment.