repl.it
@mat1/

bf interpriter?

Python

imma try but probably fail

fork
loading
Files
  • main.py
  • brainfk.bf
main.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
#this is a brainfuck interpripriter thingy with extra command support

#puts brainfk.txt into brainfk string
with open('brainfk.bf', 'r') as file:
    brainfk = file.read().replace('\n', '')
#creates "infinite" loop array
loop = []
loopInit = 0
loopCounter = 0
inputtempstore = ''
bracketPairs = []
bracketCounter = 0
#sets up 1000 sections of loop can be changed
def init(loop, loopInit): 
	for i in range(1000):
		loop.append(loopInit)
	#print(loop)
def findBracketPairs(brainfk, bracketPairs,bracketCounter):
	bracketStorage = 0
	closingFirstLoc = 0
	closingFirstStart = 0
	loop2Start = 0
	loop1Start = 0
	for i in range(len(brainfk)):
		if loop1Start == 0:
			loop1Start = 69
			bracketCounter += 1
		if bracketCounter == 0:
			bracketPairs[bracketStorage] = string(bracketPairs[bracketStorage]) + string(i)
		if loop1Start == 69:
			bracketCounter = 0
			if brainfk[i] == '[':
				loop1Start = 420
			else:
				loop1Start = 0
		if '[' == brainfk[i]:
			bracketCounter += 1
			bracketPairs.append(i)
		if ']' == brainfk[i]:
			bracketCounter = bracketCounter - 1
			if closingFirstStart == 0:
				closingFirstLoc = i
				closingFirstStart = 69
	for i in range(bracketCounter):
		if loop2Start == 0:
			if i == closingFirstLoc:
				loop2Start = 1
		else:
			if brainfk[i] == ']':
				bracketStorage += 1
				bracketPairs[bracketStorage] = string(bracketPairs[bracketStorage]) + string(i)
			

#actually does the commands
def brainFK(brainfk, loop, loopCounter):
	for i in range(len(brainfk)):
		if 	loopCounter < 0:
			loopCounter = 1000
		elif loopCounter > 1000:
			loopCounter = 0
		if '+' == brainfk[i]:
			loop[loopCounter] = loop[loopCounter] + 1
		elif '-' == brainfk[i]:
			loop[loopCounter] = loop[loopCounter] - 1
		elif '<' == brainfk[i]:
			loopCounter = loopCounter - 1
		elif '>' ==	brainfk[i]:
			loopCounter = loopCounter + 1
		elif '.' == brainfk[i]:
			print(chr(loop[loopCounter]))
		elif ',' == brainfk[i]:
			inputtempstore = input()
			loop[loopCounter] == ord(inputtempstore)
			inputtempstore = ''
#runs functions
init(loop, loopInit)
findBracketPairs(brainfk, bracketPairs,bracketCounter)
print(bracketPairs)
brainFK(brainfk, loop, loopCounter)