-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.py
More file actions
143 lines (110 loc) · 2.73 KB
/
Parser.py
File metadata and controls
143 lines (110 loc) · 2.73 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
class Parser:
A_CMD = 0
C_CMD = 1
L_CMD = 2
ALPHABET = ['D', 'M', 'A', '0', '1', '!', '+', '-', '|', '&', '=', ';',
'J', 'E', 'M', 'Q', 'N', 'G', 'L', 'T', 'P']
JUMPABET = ["JEQ", "JMP", "JNE", "JLT", "JGT", "JGE", "JLE"]
def __init__(self, infile):
self.cmds = []
self.index = 0
for line in (open(infile, 'r')):
i = line.find("//")
newstr = ""
if i != -1:
newstr += line[:i]
else:
newstr += line
nowhite = "".join(newstr.split())
if len(nowhite) > 0:
self.cmds.append(nowhite)
def hasMoreCmds(self):
return self.index < len(self.cmds)
def advance(self):
self.index += 1
def reset(self):
self.index = 0
def getCurrent(self):
if not self.hasMoreCmds():
return None
return self.cmds[self.index]
def cmdType(self):
curr = self.getCurrent()
if curr[0] == '@':
return self.A_CMD
elif set([i in self.ALPHABET for i in curr]) == set([True]):
return self.C_CMD
elif (curr[0] == '(') and (curr.find(')') != -1):
return self.L_CMD
else:
raise ParseError("Invalid command type.")
def sym(self):
if self.cmdType() == self.C_CMD:
return None
curr = self.getCurrent()
symbol = ""
if self.cmdType() == self.L_CMD:
symbol += curr[1:curr.find(')')]
else:
symbol += curr[1:]
digits = [chr(i+0x30) for i in range(10)]
lowers = [chr(i+0x61) for i in range(26)]
uppers = [chr(i+0x41) for i in range(26)]
specials = ['.', '$', ':', '_']
if (symbol[0] in digits) and (self.cmdType() == self.L_CMD):
raise ParseError("A label cannot be a number.")
for n in symbol:
if n not in digits\
and n not in lowers\
and n not in uppers\
and n not in specials:
raise ParseError(str("Symbol cannot contain the following character: " + n))
return symbol
def dest(self):
c = self.getCurrent()
if '=' not in c:
return None
s = ""
i = 0
while c[i] != '=':
if c[i] not in self.ALPHABET[:3]:
raise ParseError("Destination field can only consist of D, M, and A.")
if i > 3:
raise ParseError("Destination field can be no more than 3 characters.")
s += c[i]
i += 1
return s
def comp(self):
if self.cmdType() != self.C_CMD:
return None
c = self.getCurrent()
i = c.find('=')
j = c.find(';')
s = ""
if i == -1 and j == -1:
s = c
return s
elif i == -1:
s += "".join([c[n] for n in range(j)])
return s
elif j == -1:
s += c[i+1:]
return s
else:
s = c[i:j]
return s
def jump(self):
c = self.getCurrent()
i = c.find(';')
if i == -1:
return None
s = c[i+1:]
if s not in self.JUMPABET:
print s
raise ParseError("Not a valid jump instruction.")
return s
class ParseError(Exception):
def __init__(self, sstr):
self.val = sstr
def __str__(self):
print ':', self.val