/
brainfuck.py
180 lines (133 loc) · 4.18 KB
/
brainfuck.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# -*- coding: utf-8 -*-
import sys
class Brainfuck(object):
actions = []
action_pos = 0
pointer = []
position = 0
def __init__(self):
self.actions = ''
self.action_pos = 0
self.position = 0
self.pointer = [0]
def readScript(self, strings):
self.actions += strings
return self
def exe(self):
while len(self.actions) > self.action_pos:
act = self.actions[self.action_pos]
if '>' == act:
self.incrementPosition()
elif '<' == act:
self.decrementPosition()
elif '+' == act:
self.incrementPointer()
elif '-' == act:
self.decrementPointer()
elif '.' == act:
self.printPointer()
elif ',' == act:
self.setPointer()
elif '[' == act:
self.jumpToClose()
elif ']' == act:
self.jumpToOpen()
elif ' ' == act:
pass
elif '\n' == act:
by pass
else:
raise SyntaxError(self.action_pos)
self.action_pos += 1
def jumpToClose(self):
if self.pointer[self.position] == 0:
open_counter = 0
while True:
self.action_pos += 1
if self.actions[self.action_pos] == '[':
open_counter += 1
elif self.actions[self.action_pos] == ']':
if open_counter == 0:
break
open_counter -= 1
def jumpToOpen(self):
if self.pointer[self.position] != 0:
close_counter = 0
while True:
self.action_pos -= 1
if self.actions[self.action_pos] == ']':
close_counter += 1
elif self.actions[self.action_pos] == '[':
if close_counter == 0:
break
close_counter -= 1
def incrementPosition(self):
self.position += 1
if len(self.pointer) <= self.position:
self.pointer.append(0)
def decrementPosition(self):
self.position -= 1
def incrementPointer(self):
self.pointer[self.position] +=1
def decrementPointer(self):
self.pointer[self.position] -=1
def printPointer(self):
num = self.pointer[self.position]
sys.stdout.write(unichr(num))
def setPointer(self):
c = self.getchar()
if len(self.pointer) <= self.position or len(self.pointer) == 0:
self.pointer.append(c)
return
self.pointer[self.position] = self.getchar()
def printPosition(self):
print self.position
def getchar(self):
c = sys.stdin.read(1)
return c
if __name__ == '__main__':
'''test1 hogeと出力されます'''
b = "++++++++++++++++++++++++++++++++"
b = b + b + b
b += "++++++++."
b += "+++++++."
b += "--------."
b += "--."
program1 = b
'''test2 hogeと出力されます'''
b = "++++++++++[>++++++++++<-]>"
b += "++++.+++++++.--------.--."
program2 = b
'''test3 足し算をして3が表示されます'''
b = "+>++><<"
b += ">[-<+>]<"
b += "++++++++++++++++++++++++++++++++++++++++++++++++."
program3 = b
'''test4 足し算をして3が表示されます'''
b = "+>++><<"
b += "[->>>+<<<]"
b += ">>>[-<+<<+>>>]<<"
b += "[->>+<<]"
b += ">>[-<+<+>>]<"
b += "++++++++++++++++++++++++++++++++++++++++++++++++."
program4 = b
'''test5 かけ算を行っています 4 * 2を行い8が出力されます'''
b = "++++>++><<"
b += "[-"
b += " >[->>+<<]"
b += " >>[-<+<+>>]"
b += " <<<"
b += "]>>"
b += "++++++++++++++++++++++++++++++++++++++++++++++++."
program5 = b
fuck = Brainfuck()
fuck.readScript(program1).exe()
print ""
fuck.readScript(program2).exe()
print ""
fuck.readScript(program3).exe()
print ""
fuck.readScript(program4).exe()
print ""
fuck.readScript(program5).exe()
print ""