git @ Cat's Eye Technologies ZOWIE / f316734
Reference class hierarchy. Begin to use Scanner for scanning. Chris Pressey 7 years ago
1 changed file(s) with 44 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
189189 self.storage_register[key] = value
190190
191191
192 class Reference(object): # abstract
193 pass
194
195
196 class ImmediateReference(Reference):
197 def __init__(self, number):
198 self.number = number
199
200
201 class DirectRegisterReference(Reference):
202 def __init__(self, number):
203 self.number = number
204
205
206 class IndirectRegisterReference(Reference):
207 def __init__(self, regref):
208 self.regref = regref
209
210
192211 class Scanner(object):
193212 def __init__(self, line):
194213 self.line = line
214
215 def expect(self, s):
216 if not self.line or not self.line.startswith(s):
217 raise SyntaxError("Expected '%s'" % s)
218 self.line = self.line[len(s):].lstrip()
195219
196220 def scan_integer(self):
197221 number = 0
198222 if not self.line or not self.line[0].isdigit():
199223 raise SyntaxError('Expected integer')
200224 while self.line and self.line[0].isdigit():
201 number = number * 10 + (ord(line[0]) - ord('0'))
225 number = number * 10 + (ord(self.line[0]) - ord('0'))
202226 self.line = self.line[1:]
203227 return number
204228
205 def scan_register(self):
206 if not self.line or self.line[0] != 'R':
207 raise SyntaxError('Expected R')
208 self.line = self.line[1:]
209 if not self.line:
210 raise SyntaxError('Expected register designator')
211 if self.line[0] == '[':
212 pass # ...
213 else:
214 regnum = self.scan_integer()
215 return ('direct', regnum)
229 def scan_reference(self):
230 if self.line[0] == 'R':
231 self.line = self.line[1:]
232 if self.line[0] == '[':
233 self.line = self.line[1:]
234 inner = self.scan_register()
235 self.expect(']')
236 return IndirectRegisterReference(inner)
237 else:
238 regnum = self.scan_integer()
239 return DirectRegisterReference(regnum)
240 else:
241 imm = self.scan_integer()
242 return ImmediateReference(imm)
216243
217244
218245 class Instruction(object):
228255 if not line or line[0] == ';':
229256 return False
230257
231 if len(line) < 3 or not line.startswith('MOV'):
232 raise SyntaxError("Could not parse line '%s'" % line)
233
234 line = line[3:].strip()
258 scanner = Scanner(line)
259 scanner.expect('MOV')
260 line = scanner.line
261
262 # lhs = scanner.scan_reference()
235263
236264 m = re.match(r'^R(\d+)\s*,\s*(\d+)\s*(\;.*)?$', line)
237265 if m is not None: