-
Notifications
You must be signed in to change notification settings - Fork 0
/
brainsnake_dev.py
executable file
·154 lines (147 loc) · 3.74 KB
/
brainsnake_dev.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#don't use this one wam
import sys
class Operation:
def __init__(self, o, c):
self.opcodes = o
self.counts = c
def compress(brainfuck):
operList1 = []
operList2 = []
num = 0
i = 0
length = len(brainfuck)
print(len(brainfuck))
while(True):
count = 0
char = brainfuck[num]
if(char == ">"):
for x in range(num, len(brainfuck)):
if(brainfuck[x] == ">"):
count += 1
num += 1
if(brainfuck[x] == "<"):
count -= 1
num += 1
if((brainfuck[x] == "<" or brainfuck[x] == ">") == False):
break
elif(char == "<"):
for x in range(num, len(brainfuck)):
if(brainfuck[x] == "<"):
count += 1
num += 1
if(brainfuck[x] == ">"):
count -= 1
num += 1
if((brainfuck[x] == "<" or brainfuck[x] == ">") == False):
break
elif(char == "+"):
for x in range(num, length):
if(brainfuck[x] == "+"):
count += 1
num += 1
if(brainfuck[x] == "-"):
count -= 1
num += 1
if((brainfuck[x] == "+" or brainfuck[x] == "-") == False):
break
elif(char == "-"):
for x in range(num, length):
if(brainfuck[x] == "+"):
count -= 1
num += 1
if(brainfuck[x] == "-"):
count += 1
num += 1
if((brainfuck[x] == "+" or brainfuck[x] == "-") == False):
break
elif(char == "."):
count += 1
num += 1
elif(char == ","):
count += 1
num += 1
elif(char == "["):
count += 1
num += 1
elif(char == "]"):
count += 1
num += 1
else:
num += 1
#raw_input()
operList1.append(char)
operList2.append(count)
if(num == length):
return operList1, operList2
#list of operations
#operation = opcodes[programcounter] and counts[programcounter]
#array of operations
#operation = array, opcodes[programcounter] and counts[programcounter]
#[[opcodes[programcounter], counts[programcounter]],[opcodes[programcounter], counts[programcounter]]]
def execute(opcodes, counts):
programcounter = 0
pointer = 0
cells = [0] * 32768
length = len(opcodes)
char = ""
loopcount = 0
loopdict = {
}
dictloop = {
}
while(True):
char = opcodes[programcounter]
counts[programcounter] = counts[programcounter]
if(char == '['):
c = char
p = programcounter
i = 0
while(True):
c = opcodes[p]
if(c == '['):
i += 1
if(c == ']'):
i -= 1
if(i == 0):
loopdict[p] = programcounter
dictloop[programcounter] = p
break
p += 1
programcounter += 1
if(programcounter == length - 1):
print("Got pairs")
break
programcounter = 0
while(True):
if(opcodes[programcounter] == ">"):
pointer += counts[programcounter]
elif(opcodes[programcounter] == "<"):
pointer -= counts[programcounter]
elif(opcodes[programcounter] == '+'):
if(cells[pointer] + counts[programcounter] > 255):
cells[pointer] = 0 + (counts[programcounter] - 1)
else:
cells[pointer] += counts[programcounter]
elif(opcodes[programcounter] == '-'):
if(cells[pointer] - counts[programcounter] < 0):
cells[pointer] = 255 - (counts[programcounter] - 1)
else:
cells[pointer] -= counts[programcounter]
elif(opcodes[programcounter] == '.'):
sys.stdout.write(chr(cells[pointer]))
sys.stdout.flush()
elif(opcodes[programcounter] == '[' and cells[pointer] == 0):
programcounter = dictloop[programcounter]
elif(opcodes[programcounter] == ']' and cells[pointer] != 0):
programcounter = loopdict[programcounter]
if(programcounter == length - 1):
break
programcounter += 1
def main():
f = open(sys.argv[1])
contents = f.read()
opcodes, counts = compress(contents)
print("Finished compression")
execute(opcodes, counts)
#o = interpret(contents)
main()