git @ Cat's Eye Technologies SixtyPical / 394fbdd
Refactor to avoid storing LocationRefs in SymEntry. Chris Pressey 2 years ago
1 changed file(s) with 26 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
1111
1212
1313 class SymEntry(object):
14 def __init__(self, ast_node, model):
14 def __init__(self, ast_node, type_):
1515 self.ast_node = ast_node
16 self.model = model
16 self.type_ = type_
1717
1818 def __repr__(self):
19 return "%s(%r, %r)" % (self.__class__.__name__, self.ast_node, self.model)
19 return "%s(%r, %r)" % (self.__class__.__name__, self.ast_node, self.type_)
2020
2121
2222 class ForwardReference(object):
3434 self.typedefs = {} # token -> Type AST
3535 self.consts = {} # token -> Loc
3636
37 for token in ('a', 'x', 'y'):
38 self.symbols[token] = SymEntry(None, LocationRef(TYPE_BYTE, token))
39 for token in ('c', 'z', 'n', 'v'):
40 self.symbols[token] = SymEntry(None, LocationRef(TYPE_BIT, token))
37 for name in ('a', 'x', 'y'):
38 self.symbols[name] = SymEntry(None, TYPE_BYTE)
39 for name in ('c', 'z', 'n', 'v'):
40 self.symbols[name] = SymEntry(None, TYPE_BIT)
4141
4242 def __str__(self):
4343 return "Symbols: {}\nStatics: {}\nTypedefs: {}\nConsts: {}".format(self.symbols, self.statics, self.typedefs, self.consts)
4444
45 def fetch(self, name):
45 def fetch_ref(self, name):
4646 if name in self.statics:
47 return self.statics[name].model
47 return LocationRef(self.statics[name].type_, name)
4848 if name in self.symbols:
49 return self.symbols[name].model
49 return LocationRef(self.symbols[name].type_, name)
5050 return None
5151
5252
5959 self.scanner.syntax_error(msg)
6060
6161 def lookup(self, name):
62 model = self.context.fetch(name)
62 model = self.context.fetch_ref(name)
6363 if model is None:
6464 self.syntax_error('Undefined symbol "{}"'.format(name))
6565 return model
6666
67 def declare(self, name, symentry, static=False):
68 if self.context.fetch(name):
67 def declare(self, name, ast_node, type_, static=False):
68 if self.context.fetch_ref(name):
6969 self.syntax_error('Symbol "%s" already declared' % name)
7070 if static:
71 self.context.statics[name] = symentry
72 else:
73 self.context.symbols[name] = symentry
71 self.context.statics[name] = SymEntry(ast_node, type_)
72 else:
73 self.context.symbols[name] = SymEntry(ast_node, type_)
7474
7575 def clear_statics(self):
7676 self.context.statics = {}
128128 typenames = ['byte', 'word', 'table', 'vector', 'pointer'] # 'routine',
129129 typenames.extend(self.context.typedefs.keys())
130130 while self.scanner.on(*typenames):
131 defn = self.defn()
132 self.declare(defn.name, SymEntry(defn, defn.location))
131 type_, defn = self.defn()
132 self.declare(defn.name, defn, type_)
133133 defns.append(defn)
134134 while self.scanner.consume('define'):
135135 name = self.scanner.token
136136 self.scanner.scan()
137 routine = self.routine(name)
138 self.declare(name, SymEntry(routine, routine.location))
137 type_, routine = self.routine(name)
138 self.declare(name, routine, type_)
139139 routines.append(routine)
140140 self.scanner.check_type('EOF')
141141
190190
191191 location = LocationRef(type_, name)
192192
193 return Defn(self.scanner.line_number, name=name, addr=addr, initial=initial, location=location)
193 return type_, Defn(self.scanner.line_number, name=name, addr=addr, initial=initial, location=location)
194194
195195 def const(self):
196196 if self.scanner.token in ('on', 'off'):
299299
300300 self.clear_statics()
301301 for defn in statics:
302 self.declare(defn.name, SymEntry(defn, defn.location), static=True)
302 self.declare(defn.name, defn, defn.location.type, static=True)
303303 block = self.block()
304304 self.clear_statics()
305305
306306 addr = None
307307 location = LocationRef(type_, name)
308 return Routine(
308 return type_, Routine(
309309 self.scanner.line_number,
310310 name=name, block=block, addr=addr,
311 location=location, statics=statics
311 location=location, statics=statics,
312312 )
313313
314314 def labels(self):
338338 else:
339339 name = self.scanner.token
340340 self.scanner.scan()
341 loc = self.context.fetch(name)
341 loc = self.context.fetch_ref(name)
342342 if loc:
343343 return loc
344344 else:
370370 def statics(self):
371371 defns = []
372372 while self.scanner.consume('static'):
373 defn = self.defn()
373 type_, defn = self.defn()
374374 if defn.initial is None:
375375 self.syntax_error("Static definition {} must have initial value".format(defn))
376376 defns.append(defn)