-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.py
96 lines (85 loc) · 2.57 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
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
from Language import Language
from Errors import Error
class Parser():
"""docstring for Parser"""
def __init__(self, Program, ASM_Language):
self.ASM = ASM_Language
print("Parsing....")
self.Program = Program.upper()
self.commentSymboles = {"/", ";"}
self.labelSymbol = ":"
self.ProgramLines = self.parseLines()
print("Remove comments...")
self.uncommentedProgram = self.RemoveComments()
print("Remove labels...")
self.unLabeledProgram, self.labels = self.getLabels()
self.programCommands = self.parse()
print("Assembling...")
self.FinalCode = self.Compile()
print("Assembled !")
def parseLines(self):
return self.Program.split("\n")
def RemoveComments(self):
ProgramWithoutComments = []
for n, line in enumerate(self.ProgramLines):
commIdx = None
for comSymb in self.commentSymboles:
if comSymb in line :
idxComSymb = line.index(comSymb)
if commIdx is None or commIdx > idxComSymb:
commIdx = idxComSymb
if commIdx is None :
if line.strip() :
ProgramWithoutComments.append(line.strip())
else :
new_line = line[:commIdx].strip()
if new_line :
ProgramWithoutComments.append(new_line)
return ProgramWithoutComments
def getLabels(self):
programWithoutLabels = []
Labels = {}
line_number = 0
for line in self.uncommentedProgram :
line_rest = None
if self.labelSymbol in line :
label, line_rest = line.split(self.labelSymbol, 1)
Labels[label] = line_number
if line_rest :
programWithoutLabels.append(line_rest)
line_number += 1
else :
programWithoutLabels.append(line)
line_number += 1
return programWithoutLabels, Labels
def parse(self):
parsedProgram = []
for n, line in enumerate(self.unLabeledProgram):
command = []
if " " in line :
instr, paramstr = line.split(" ", 1)
paramstr = paramstr.replace(" ", "")
else :
instr = line.strip()
paramstr = ""
# for label in self.labels.keys() :
# if label in paramstr :
# paramstr = paramstr.replace(label, str(self.labels[label]) )
if "," in paramstr :
params = paramstr.split(",")
elif paramstr :
params = [paramstr.strip()]
else :
params = []
command.append(instr)
command.append(params)
parsedProgram.append(command)
return parsedProgram
def Compile(self):
# print(self.labels)
correct, corrected, line = self.ASM.isValidCode(self.programCommands, self.labels)
if not correct :
error = Error("Invalid Instruction", corrected,line,str(self.programCommands[line]), "test.asm")
error.RAISE()
else :
return self.ASM.Assemble(corrected)