-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
485 lines (398 loc) · 17.1 KB
/
main.py
File metadata and controls
485 lines (398 loc) · 17.1 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#################### imports ##################
import sys
import re
############### global variables ###########
global last_register #variable holding last used register
global a #register A
global b #regisrer B
global vars #array with variables
global index_name #dictionary returning name of variable of given index
global name_index #dictionary returning index in vars of given variable's name
global vars_count #number of variables in vars
vars = [] #initialisation of global variables
index_name = {}
name_index = {}
vars_count = 0
last_register = None
a = None
b = None
############## classes #############
class pointer: #pointer class used to access and iterate over arrays
def __init__(self, address, offset):
if not address in name_index:
abort("unknown address: " + address)
self.address = address
self.offset = offset
############## functions' declarations #############
def abort(message):
print("!!!!=======ERROR=======!!!!")
print(message)
print("execution aborted, no further instructions were performed")
print("!!!!=======ERROR=======!!!!")
sys.exit(1)
def abort_compilation(message,line):
print("!!!!=======ERROR=======!!!!")
print("execution aborted at line:\n" + line)
print(message)
print("current state of variables")
print_vars()
print("execution aborted, no further instructions were performed")
print("!!!!=======ERROR=======!!!!")
sys.exit(1)
def uncomment(string): #gets rid of a comment in line
if ";" in string:
return string[:string.find(";")]
else:
return string
def print_vars(): #prints variables
for x in range(len(vars)):
if (type(vars[x]) is pointer):
print(index_name[x] + ": " + vars[x].address + ((" + " + str(vars[x].offset)) if vars[x].offset else ""))
else:
print(index_name[x] + ": " + str(vars[x]))
def segment_declaration(line): #checks if line contains segment declaration.
#Returns True if segment is present and False otherwise
#doesn't include .END !
segments = [".UNIT",".DATA",".CODE"]
for x in segments:
if x in line:
return True
return False
def get_segment(line): #returns string with segment name extracted from line
#doesn't include .END!
index = line.find(".")
return line[index:index+5]
def load(register,value): #loads value to given register
global a
global b
global last_register
if (register=="@A" or register=="@a"):
a=value
last_register = a
elif (register=="@B" or register=="@b"):
b=value
last_register = b
else:
abort("invalid register provided: " + register)
def check_ptrs(a,b): #checks if a or b is pointer
if (type(a) is pointer or type(b) is pointer):
return True
return False
def check_both_ptrs(a,b): #checks if a and b are pointers
if (type(a) is pointer and type(b) is pointer):
return True
return False
def add_values(register,value): #returns new offset which is sum of register and value
if (type(a) is pointer and type(b) is pointer):
abort("cannot perform operation on two pointers")
if (type(value) is pointer and not type(register) is pointer):
return value.offset + register
if (not type(value) is pointer and type(register) is pointer):
return register.offset + value
def add(register,value): #adds value to register
global a
global b
global last_register
if (register=="@A" or register=="@a"): #register A
if (a==None): #check if A is not empty
abort("Nothing was loaded to @A. Cannot perform addition")
if (check_ptrs(a,value)): #check if operation is performed on pointers
if (type(a) is pointer):
a.offset = add_values(a,value)
else:
value.offset = add_values(a,value)
a = value
else: #default case
a+=value
last_register = a
elif (register=="@B" or register=="@b"): #register B (same as A)
if (b==None):
abort("Nothing was loaded to @B. Cannot perform addition")
if (check_ptrs(b,value)):
if (type(b) is tuple):
b.offset = add_values(b,value)
else:
value.offset = add_values(b,value)
b = value
else:
b+=value
last_register = b
else:
abort("invalid register provided: " + register)
def sub(register,value): #subtracts value from register
global a
global b
global last_register
if (register=="@A" or register=="@a"): #register A
if (a==None): #check if register is empty
abort("Nothing was loaded to @A. Cannot perform sub")
if (check_ptrs(a,value)): #pointers case
if (check_both_ptrs(a,value)):
a = a.offset - value.offset
else:
a.offset = a.offset - value
return
a-=value #int case
last_register = a
elif (register=="@B" or register=="@b"): #register B (same as A)
if (b==None):
abort("Nothing was loaded to @B. Cannot perform sub")
if (check_ptrs(b,value)):
if (check_both_ptrs(b,value)):
b = b.offset - value.offset
else:
b.offset = b.offset - value
return
b-=value
last_register = b
else:
abort("invalid register provided: " + register)
def mult(register,value): #multiplies register by value
global a
global b
global last_register
if (register=="@A" or register=="@a"): #register A
if check_ptrs(a,value): #make sure there are no ptrs
abort("cannot perform multiplication on pointers!")
if (a==None): #check if register is empty
abort("Nothing was loaded to @A. Cannot perform mult")
a*=value #perform multiplication
last_register = a
elif (register=="@B" or register=="@b"): #register B (same as A)
if check_ptrs(b,value):
abort("cannot perform multiplication on pointers!")
if (b==None):
abort("Nothing was loaded to @B. Cannot perform mult")
b*=value
last_register = b
else:
abort("invalid register provided: " + register)
def div(register,value): #divides register by value
global a
global b
global last_register
if (value==0): #check if value != 0
abort("cannot perform division by 0")
if (register=="@A" or register=="@a"): #register A
if check_ptrs(a,value): #make sure there are no pointers
abort("cannot perform division on pointers!")
if (a==None): #make sure A is not empty
abort("Nothing was loaded to @A. Cannot perform div")
a/=value #perform division
last_register = a
elif (register=="@B" or register=="@b"): #register B (same as A)
if check_ptrs(b,value):
abort("cannot perform division on pointers!")
if (b==None):
abort("Nothing was loaded to @B. Cannot perform div")
b/=value
last_register = b
else:
abort("invalid register provided: " + register)
def store(register,address): #saves value from given register to address
global a
global b
global last_register
global vars
if (register=="@A" or register=="@a"): #register A
if (a==None): #check if A is empty
abort("Nothing was loaded to @A. Cannot perform store")
if (type(a) is list): #case 1: save pointer to array
if not address in name_index:
abort("Unknown variable: " + address)
vars[name_index[address]] = pointer(index_name[vars.index(a)],0)
last_register = a
return
if ("(" in address): #case 2: save integer in array
if not address[1:-1] in name_index:
abort("Unknown variable: " + address)
ptr = vars[name_index[address[1:-1]]]
vars[name_index[ptr.address]][ptr.offset] = a
last_register = a
return
if not address in name_index: #case 3: save integer in normal address
abort("Unknown variable: " + address)
vars[name_index[address]] = a
last_register = a
elif (register=="@B" or register=="@b"): #register B (same as A)
if (b==None):
abort("Nothing was loaded to @B. Cannot perform store")
if (type(b) is list):
if not address in name_index:
abort("Unknown variable: " + address)
vars[name_index[address]] = pointer(index_name[vars.index(b)],0)
last_register = b
return
if ("(" in address):
if not address[1:-1] in name_index:
abort("Unknown variable: " + address)
ptr = vars[name_index[address[1:-1]]]
vars[name_index[ptr.address]][ptr.offset] = b
last_register = b
return
if not address in name_index:
abort("Unknown variable: " + address)
vars[name_index[address]] = b
last_register = b
else:
abort("invalid register provided: " + register)
def get_value(string): #extracts value from instruction
if re.match("[-+]?\d+$",string): #case 1: a number
return int(string)
if string.count(")")==2: #case 2: element from array
res = string.replace(")", "")
res = res.replace("(","")
if not res in name_index:
abort("Unknown variable " + string)
return vars[name_index[vars[name_index[res]].address]][vars[name_index[res]].offset]
if string.count(")")==1: #case 3: normal variable
if not (string[string.find("(")+1:string.find(")")]) in name_index:
abort("Unknown variable " + string)
return vars[name_index[(string[string.find("(")+1:string.find(")")])]]
if not string in name_index: #case 4: array (pointer to array)
abort("Unknown variable " + string)
return vars[name_index[string]]
def unhash(string): #converts # to normal input (used in array initialisaion)
res = ""
input = string.split(",")
for cell in input:
if '#' in cell:
value = cell[cell.find("#")+1:]
repeat = int(cell[:cell.find("#")])
for iterator in range(repeat):
res+=value
if (iterator+1!=repeat):
res+=","
else:
res+=cell
res+=','
res =res[:-1]
return res
def initialise_array(string,name): #initialises array
global vars_count
global index_name
global name_index
global vars
array = []
for num in string.split(","):
array.append(int(num))
vars.append(array)
index_name[vars_count]=name
name_index[name]=vars_count
vars_count+=1
############## main #############
def main():
global vars_count
global index_name
global name_index
global vars
file = open("main.txt", "r") #open file
raw_input = file.readlines()
lab_dict = {} #dictionary with number of lines corresponding to each goto label
input = [] #lines from .CODE segments
segment = None #current segment
line_count = 0 #number of lines of .CODE segments
for line in raw_input:
current_line = line #create copy
current_line = uncomment(current_line) #get rid of unwanted characters
current_line = current_line.replace(" ","")
current_line = current_line.replace("\t","")
current_line = current_line.replace("\n","")
if (segment_declaration(current_line)): #check if line contains new segment
segment = get_segment(current_line)
continue
if (segment == ".DATA"): #DATA segment case
if (current_line.count(",")==1): #creating new variable
index = current_line.find(":")
if (not re.match("^[a-zA-Z_]\\w*$",current_line[:index])): #check if variable name is correct
abort_compilation("invalid variable name: "+ current_line[:index], current_line)
if(re.match("[-+]?\d+$",current_line.split(",")[1])): #case 1: simple number
index_name[vars_count]=current_line[:index] #index -> name
name_index[current_line[:index]]=vars_count #name -> index
vars.append(int(current_line.split(",")[1]))
vars_count+=1
else: #case 2: pointer
vars.append(pointer(current_line.split(",")[1],0))
index_name[vars_count]=current_line[:index] #index -> name
name_index[current_line[:index]]=vars_count #name -> index
vars_count+=1
if (current_line.count(",")>1): #creating new array
initialise_array(unhash(current_line[current_line.find(".WORD")+6:]),current_line[:current_line.find(":")])
if (segment == ".CODE"): #CODE segment
if ((":" in current_line) and (not ".WORD" in current_line)): #create dictionary with labels
label = current_line.split(":")[0]
lab_dict[label] = line_count
current_line = current_line.replace(label + ":","")
line_count+=1
input.append(current_line) #prepare code to be executes
raw_input.clear() #clean up
file.close()
print("exec start: ") #print initial state of variables
print_vars()
x = 0 #variable with information which line are we in
while(True): #exec start
line = input[x] #load line
x += 1 #prepare move to next line
#print(repr(line))
if (len(line)==0): #check if line is empty
continue
split = line.split(",")
if split[0]=="null": #instructions
continue
elif split[0]=="load":
load(split[1],get_value(split[2]))
elif split[0]=="add":
add(split[1],get_value(split[2]))
elif split[0]=="sub":
sub(split[1],get_value(split[2]))
elif split[0]=="div":
div(split[1],get_value(split[2]))
elif split[0]=="mult":
mult(split[1],get_value(split[2]))
elif split[0]=="store":
store(split[1],split[2])
#print_vars()
elif split[0]=="jump":
if not split[1] in lab_dict:
abort_compilation("label: " + split[1] + "not found in code", line)
x=lab_dict[split[1]]
elif split[0]=="jzero":
if not split[1] in lab_dict:
abort_compilation("label: " + split[1] + "not found in code", line)
if last_register==0:
x=lab_dict[split[1]]
else:
continue
elif split[0]=="jnzero":
if not split[1] in lab_dict:
abort_compilation("label: " + split[1] + "not found in code", line)
if last_register!=0:
x=lab_dict[split[1]]
else:
continue
elif split[0]=="jpos":
if not split[1] in lab_dict:
abort_compilation("label: " + split[1] + "not found in code", line)
if last_register>0:
x=lab_dict[split[1]]
else:
continue
elif split[0]=="jneg":
if not split[1] in lab_dict:
abort_compilation("label: " + split[1] + " not found in code", line)
if last_register<0:
x=lab_dict[split[1]]
else:
continue
elif split[0]=="halt":
break
else:
abort_compilation("Inavlid instruction: " + split[0],line)
if (".END" in line):
break
print("final state:") #print final state of variables
print_vars()
print("press enter to quit")
sys.stdin.read(1) #enter to close
if __name__=='__main__':
main()