-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyfuck.py
executable file
·100 lines (83 loc) · 1.64 KB
/
pyfuck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3
import sys
ptr = 0
cells = {}
stack = []
i = 0
f = open(sys.argv[1])
content = f.read()
def readcell():
global ptr, cells
cell = 0
if ptr in cells:
cell = cells[ptr]
return cell
def right():
global ptr
ptr = ptr + 1
def left():
global ptr
ptr = ptr - 1
def increment():
global ptr, cells
if ptr in cells:
cells[ptr] = cells[ptr] + 1
else:
cells[ptr] = 1
def decrement():
global ptr, cells
if ptr in cells:
cells[ptr] = cells[ptr] - 1
else:
cells[ptr] = -1
def putchar():
global ptr, cells
char = readcell()
print(chr(char),end='')
def readchar():
global ptr, cells
char = sys.stdin.read(1)
cells[ptr] = ord(char)
def startloop():
global ptr, cells, i
cell = readcell()
if cell == 0:
opened = 1
while opened > 0 and i < len(content):
c = content[i]
i = i + 1
if c == '[':
opened = opened + 1
elif c == ']':
opened = opened - 1
else:
stack.append(i)
i = i + 1
def endloop():
global ptr, cells, i
cell = readcell()
if cell != 0:
# move to start of loop
i = stack.pop()
else:
i = i + 1
cmds = {
'>': right,
'<': left,
'+': increment,
'-': decrement,
'.': putchar,
',': readchar
}
while i < len(content):
c = content[i]
if c in cmds:
cmds[c]()
i = i + 1
elif c == '[':
startloop()
elif c == ']':
endloop()
else:
i = i + 1
print()