-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompileAssembly.py
More file actions
287 lines (194 loc) · 8.44 KB
/
CompileAssembly.py
File metadata and controls
287 lines (194 loc) · 8.44 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"""File compiles 5 bit assembly to 5 bit machine code
Author: Drake Setera
Date: 6/20/2025
Version: 3.1.0
"""
import re
class CompiledAssembly:
"""Class converts assembly of inputted file to a new file containing 5 bit machine code
"""
def __init__(self, file_name: str = '', base: str = '5b', display_error: bool = True):
self.__valid_input = re.compile(r'^[01]{5}$')
self.assembly_to_binary_convert = {'USER':'10010','RCV':'10011','SEND':'10100','ADD':'10101','NOT':'10110','OR':'10111','XOR':'11000','>>':'11001','SET':'11010','GET':'11011','IF':'11100','POINT':'11101','DISP':'11110','WAIT':'11111'}
self.display_error = display_error
if len(file_name) > 0:
if file_name.endswith('.5ba'):
self.convert_code(file_name, base)
else:
if self.display_error:
print('Wrong File Type')
raise TypeError
def convert_text_code(self, code: str) -> str:
"""Converts assembly code in string form into binary code in string form
Args:
code (str): String assembly code
Returns:
str: String binary code
"""
assembly = self.get_assembly('', code)
binary, errors, error_amount = self.assembly_to_binary(assembly)
return binary
def convert_code(self, file_name: str, base: str):
"""Creates and inputs machine code based on inputted assembly
Args:
file_name (str): Name of assembly file (ends in .5ba)
"""
assembly = self.get_assembly(file_name)
binary, errors, error_amount = self.assembly_to_binary(assembly)
if self.display_error:
print(errors)
if error_amount == 0:
try:
if base == '5b':
file_name = file_name.removesuffix('a')
file = open(f"Code/Binary/{file_name}", 'w')
file.write(binary)
file.close()
elif base == 'b5':
file_name = file_name.removesuffix('5ba') + 'b5'
file = open(f"Code/Base5/{file_name}", 'w')
base5 = self.binary_to_base5(binary)
file.write(base5)
file.close()
except:
if self.display_error:
print("Error creating binary file")
def get_assembly(self, file_name: str, text_code: str = '') -> list[str]:
"""Gets the assembly instructions from assembly file
Args:
file_name (str): Name of assembly file (ends in .5ba)
Returns:
list[str]: List of assembly instructions
"""
try:
instructions = ''
if len(text_code) == 0:
file = open(f"Code/Assembly/{file_name}")
instructions = file.read().split(";")
instructions = instructions[:-1]
else:
instructions = text_code.split(";")
instructions = instructions[:-1]
for i in range(len(instructions)):
instructions[i] = instructions[i].replace(' ','').replace('\n','').upper()
file.close()
except:
if self.display_error:
print("Couldn't find file")
return instructions
def assembly_to_binary(self, assembly: list[str]) -> list[str, str, str]:
"""Converts assembly instructions to binary machine code
Args:
assembly (list[str]): List of assembly instructions
Returns:
list[str, str, str]: machine code, error message (if any), number of errors ran into
"""
binary = ''
error_message = ''
error_amount = 0
try:
for i in range(len(assembly)):
try:
binary += self.command_to_binary(assembly[i], i)
except InvalidCommandError as ex:
error_message += str(ex)
error_amount += 1
except InvalidValueError as ex:
error_message += str(ex)
error_amount += 1
except MaxInstructionError as ex:
error_message += str(ex)
error_amount += 1
error_message = f"\nThe program ran into {error_amount} errors while compiling\n" + error_message
return binary, error_message, error_amount
def command_to_binary(self, command: str, line_num: int) -> str:
"""Attempts to convert individual assembly instruction to binary
Args:
command (str): assembly instruction
line_num (int): current command line number
Raises:
MaxInstructionError: Raised if line_num is greater than computer can handle
InvalidCommandError: Raised if register doesn't exist
InvalidValueError: Raised if inserted value is outside of 5 bit range
InvalidValueError: Raised if insert raised value error
InvalidCommandError: Raised if command doesn't exist
Returns:
str: binary of inputted instruction
"""
if line_num >= 32768:
raise MaxInstructionError(command, line_num)
if command.startswith('#'):
return ''
if command == 'BLANK':
return '00000'
if command.startswith('REG'):
try:
value = ord(command.removeprefix('REG')) - 64
except:
raise InvalidCommandError(command, line_num)
return bin(value).removeprefix('0b').zfill(5)
if command.startswith('INSERT'):
value = ''
try:
if self.__valid_input.match(command.removeprefix('INSERT')):
value = command.removeprefix('INSERT')
else:
value = int(command.removeprefix('INSERT'))
if value < 0 or 31 < value:
raise InvalidValueError(command, value, line_num)
else:
value = bin(value).removeprefix('0b').zfill(5)
except ValueError:
raise InvalidValueError(command, command.removeprefix('INSERT'), line_num)
return '10001' + value
try:
return self.assembly_to_binary_convert[command]
except:
raise InvalidCommandError(command, line_num)
def binary_to_base5(self, binary: str) -> str:
"""Converts binary code to base 5
Args:
binary (str): Binary instructions
Returns:
str: Base 5 instructions
"""
output = ''
for i in range(0, len(binary), 5):
output += self.convert_binary(binary[i:i+5])
return output
def convert_binary(self, binary: str) -> str:
"""Converts binary instruction into base 5
Args:
binary (str): 5 bit instruction
Returns:
str: base 5 equivalent
"""
val = int(binary,2)
if val < 10:
return str(val)
else:
return chr(val + 55)
class InvalidCommandError(Exception):
"""Error raised when command doesn't exist
"""
def __init__(self, command, line_num):
super().__init__(command, line_num)
self.message = f"\n***Invalid Command Error***\nCommand Number: {line_num}\nCommand: {command}\n"
def __str__(self):
return self.message
class InvalidValueError(Exception):
"""Error raised when inserted value is invalid
"""
def __init__(self, command, value, line_num):
super().__init__(command, value, line_num)
self.message = f"\n***Invalid Value Error***\nCommand Number: {line_num}\nValue: {value} in command: {command}\n"
def __str__(self):
return self.message
class MaxInstructionError(Exception):
"""Error raised when max instruction number is hit
"""
def __init__(self, command, line_num):
super().__init__(command, line_num)
self.message = f"\n***Max Instructions Error***\nCommand Number: {line_num}\nCommand: {command}\n"
def __str__(self):
return self.message