-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_brainfuck.py
executable file
·230 lines (206 loc) · 5.77 KB
/
my_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python
# -*- coding: UTF8 -*-
import sys
import re
"""
Date : 18/06/2021
Auteur : Mathias BALLOT
Project : brainfuck interpreter
Licence : MIT
"""
class Node:
"""
Linked List in python
"""
def __init__(self, value):
"""
construct list
"""
self.value = value
self.next = None
class Stack:
"""
Create object Stack
"""
def __init__(self):
"""
init the stack
"""
self.head = Node("head")
self.size = 0
def __str__(self):
"""
String represente the Stack when we print
"""
current = self.head.next
recup_value = ""
while current:
recup_value += str(current.value) + '->'
current = current.next
return recup_value[:-2]
def getSize(self):
"""
Get the current size of the stack
"""
return self.size
def isEmpty(self):
"""
Check if the stack is empty
"""
return self.size == 0
def push(self, value):
"""
Push the value into the stack
"""
node = Node(value)
node.next = self.head.next
self.head.next = node
self.size += 1
def pop(self):
"""
Remove the value from the stack and return
"""
if self.isEmpty():
raise Exception("Popping from an empty stack")
remove = self.head.next
self.head.next = self.head.next.next
self.size -= 1
return remove.value
class My_Brainfuck:
"""
create object brainfuck
"""
def __init__(self):
"""
construct the the object
"""
self.pc = 0
self.ptr = 0
self.stack_loop = Stack()
self.code_progr = []
self.mem = [0] * 90000
self.actions = {
'<' : (self._opp_ptr,(-1)),
'>' : (self._opp_ptr,(1)),
'+' : (self._incr_val,(1)),
'-' : (self._incr_val,(-1)),
'.' : (self._put_char,(0)),
',' : (self._get_char,(0)),
'[' : (self._enter_loop,(0)),
']' : (self._exit_loop,(0))
}
def _enter_loop(self, nothing):
"""
enter in the loop
"""
self.stack_loop.push(self.pc)
def _jmp(self):
"""
jmp to this instruction
"""
label = self.stack_loop.pop()
self.pc = label - 1 # intruction avant comme sa il fait + 1 pour ceux retourver à l'instruction '['
def _exit_loop(self, nothing):
"""
exit in the loop
"""
if self.mem[self.ptr] == 0:
self.stack_loop.pop()
else:
self._jmp()
def parse_string(self, string):
"""
parse the string programme
"""
for i in range(len(string)):
self.code_progr.append(string[i])
def _opp_ptr(self, i):
"""
pointer operation
"""
self.ptr += i
def _incr_val(self, i):
"""
increment the value where is the pointer
"""
self.mem[self.ptr] += i
def _put_char(self, nothing):
"""
print the ASCII character
"""
print(chr(self.mem[self.ptr]), sep='', end='')
def _get_char(self, nothing):
"""
entry of a byte in the array where the pointer is positioned (ASCII value).
"""
self.mem[self.ptr] = ord(sys.stdin.read(1))
def execute_progr(self):
"""
run program code
"""
while self.pc < len(self.code_progr):
instruction = self.code_progr[self.pc]
action, argv = self.actions[instruction] # passer un dictionnaire en arguments de functions
action(argv)
self.pc += 1
def help_programme():
"""
documentation to assist the user
"""
print("USAGE")
print("\t./my_brainfuck.py programme.bf")
print("DESCRIPTION")
print("\tTo be able to run programs written in brainfuck with the \".bf\" extension")
print("\tThe interpreter also handles comments")
print("\tcharacter accept : \"><.,[]\\t\\n#\" + space")
sys.exit(0)
def buffer_file(filename):
"""
vérify and parse the file programme
"""
split_name = filename.split(".bf")
if split_name[len(split_name) - 1] not in ".bf":
print("Error : The file is not a brainfuck program", file=sys.stderr)
sys.exit(84)
buffer = ""
try:
with open(filename, "r") as filin:
for ligne in filin:
buffer += ligne
except FileNotFoundError as e:
print("Error : File does not exist !", file=sys.stderr)
sys.exit(84)
except IOError as e:
print("Error : file does not exist !", file=sys.stderr)
sys.exit(84)
return buffer
def delate_comment(buffer):
"""
delete comments from the program
"""
new_buffer = re.sub(r"#[^\n]*", "", buffer)
return (new_buffer)
def check_invalid_character(buffer):
"""
check the invalid charact
"""
syntaxes_brainfuck = '-+><.,[] \n\t'
for letter in buffer:
if not letter in syntaxes_brainfuck:
print("Error : The character is not valid !", file=sys.stderr)
sys.exit(84)
## PRINCIPALE PROGRAMME
if __name__ == "__main__":
if (len(sys.argv) != 2):
print("Error : too many or too few arguments. -h to watch the doc", file=sys.stderr)
sys.exit(84)
if (len(sys.argv) == 2 and sys.argv[1] in '-h'):
help_programme()
if (len(sys.argv) == 2):
buffer = buffer_file(sys.argv[1])
buffer = delate_comment(buffer)
check_invalid_character(buffer)
buffer = re.sub(r"[\s]+", "", buffer) # remove white characters
bf = My_Brainfuck()
bf.parse_string(buffer)
bf.execute_progr()