This repository has been archived by the owner on May 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.py
53 lines (40 loc) · 1.45 KB
/
Parser.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
import re
from common_backend import *
label_instructions = ['jal', 'beq', 'bge', 'blt', 'bne']
label_dict = {}
class MyException(Exception):
pass
def findLabels(line, num):
label = re.compile("(\A\w+)\:")
label_matches = label.search(line)
if label_matches!= None:
if label_dict.get(label_matches[0].replace(':', '')) == None:
label_dict[label_matches[0].replace(':', '')] = 4*num
else:
raise MyException("Label '" + label_matches[0].replace(':', '') + "' defined twice")
def parse(line, num):
instruction = re.compile("^(\w)+(\s(\w)+)+")
instruction_matches = instruction.search(line)
comment = re.compile("\#\w*")
comment_matches = comment.search(line)
if comment_matches != None:
lines = line.split("#")
line = lines[0]
elif instruction_matches != None:
lines = instruction_matches[0].replace(',', ' ').split(' ')
if lines[0] in label_instructions:
try:
int(lines[-1])
except:
try:
lines[-1] = int(label_dict[lines[-1]] - 4*num)
except:
raise MyException("Label not recognized on line " + str(num))
line = ' '.join(str(elem) for elem in lines)
return line
def parseDoc(lines):
for i in range(len(lines)):
findLabels(lines[i], i)
for i in range(len(lines)):
lines[i] = parse(lines[i], i)
return lines