-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfpy.py
149 lines (128 loc) · 3.47 KB
/
bfpy.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
import termcolor
import colorama
import os
import platform
from PIL import Image
from sys import argv
colorama.init() # windows compatibility for cprint
# code functions
def addToCurrentCell():
global register, cellPointer
register[cellPointer] += 1
def subFromCurrentCell():
global register, cellPointer
register[cellPointer] -= 1
def goToNextCell():
global register, cellPointer
try:
cellPointer += 1
register[cellPointer]
except:
register.append(0)
def goToPreviousCell():
global register, cellPointer
if cellPointer == 0: return
cellPointer -= 1
def startLoop():
global char, cellPointer, loopStart, inLoop
inLoop = True
loopStart = char
def endLoop():
global register, cellPointer, loopStart, inLoop, line, char
if inLoop:
if register[cellPointer] == 0:
inLoop = False
char += 1
return
else:
char = loopStart
return
def printCurrentCell():
global register, cellPointer
print(chr(register[cellPointer]), end='')
def inputIntoCurrentCell():
global register, cellPointer
thing = input()
if type(thing) == int:
register[cellPointer] = thing
elif type(thing) == str:
register[cellPointer] = ord(thing[0])
# brainfuck vars
register = [0]
cellPointer = 0
loopStart = 0
inLoop = False
# brainloller vars
running = True
x = 0
y = 0
direction = 2
# other vars
line = 0
char = 0
if len(argv) == 1 or not argv[1] in ["lol", "run"]:
termcolor.cprint("Please provide a valid mode! Usage: bfpy (run|lol) (file)", 'red')
elif argv[1] == "run":
# open and read file
file = open(argv[2], 'r')
code = file.readlines()
# loop through code
for line in code:
while char < len(line):
# run code
if line[char] == "+":
addToCurrentCell()
elif line[char] == "-":
subFromCurrentCell()
elif line[char] == ">":
goToNextCell()
elif line[char] == "<":
goToPreviousCell()
elif line[char] == "[":
startLoop()
elif line[char] == "]":
endLoop()
elif line[char] == ".":
printCurrentCell()
elif line[char] == ",":
inputIntoCurrentCell()
char += 1
elif argv[1] == "lol":
img = Image.open(argv[2])
pix = img.load()
width, height = img.size
x = 0
y = 0
while running:
rgb = (pix[x, y][0], pix[x, y][1], pix[x, y][2])
if "--raw" in argv: print(rgb)
if rgb == (0,255,0):
addToCurrentCell()
elif rgb == (0,128,0):
subFromCurrentCell()
elif rgb == (255,0,0):
goToNextCell()
elif rgb == (128,0,0):
goToPreviousCell()
elif rgb == (255,255,0):
startLoop()
elif rgb == (128,128,0):
endLoop()
elif rgb == (0,0,255):
printCurrentCell()
elif rgb == (0,0,128):
inputIntoCurrentCell()
elif rgb == (0,255,255):
direction += 1
elif rgb == (0,128,128):
direction -= 1
if direction == 5: direction = 1
if direction == 0: direction = 4
if direction == 1: y -= 1
if direction == 2: x += 1
if direction == 3: y += 1
if direction == 4: x -= 1
try:
pix[x, y]
except:
exit(0)