1
0
mirror of https://github.com/catseye/SixtyPical.git synced 2024-11-25 07:32:16 +00:00

Begin refactoring how the ParsingContext is used by the Parser.

This commit is contained in:
Chris Pressey 2018-09-07 12:24:07 +01:00
parent e7674c44ce
commit c73590f88c

View File

@ -33,7 +33,7 @@ class ParsingContext(object):
def __str__(self): def __str__(self):
return "Symbols: {}\nStatics: {}\nTypedefs: {}\nConsts: {}".format(self.symbols, self.statics, self.typedefs, self.consts) return "Symbols: {}\nStatics: {}\nTypedefs: {}\nConsts: {}".format(self.symbols, self.statics, self.typedefs, self.consts)
def lookup(self, name): def fetch(self, name):
if name in self.statics: if name in self.statics:
return self.statics[name].model return self.statics[name].model
if name in self.symbols: if name in self.symbols:
@ -51,11 +51,16 @@ class Parser(object):
self.scanner.syntax_error(msg) self.scanner.syntax_error(msg)
def lookup(self, name): def lookup(self, name):
model = self.context.lookup(name) model = self.context.fetch(name)
if model is None: if model is None:
self.syntax_error('Undefined symbol "{}"'.format(name)) self.syntax_error('Undefined symbol "{}"'.format(name))
return model return model
def declare(self, name, symentry):
if self.context.fetch(name):
self.syntax_error('Symbol "%s" already declared' % name)
self.context.symbols[name] = symentry
# --- grammar productions # --- grammar productions
def program(self): def program(self):
@ -70,10 +75,7 @@ class Parser(object):
typenames.extend(self.context.typedefs.keys()) typenames.extend(self.context.typedefs.keys())
while self.scanner.on(*typenames): while self.scanner.on(*typenames):
defn = self.defn() defn = self.defn()
name = defn.name self.declare(defn.name, SymEntry(defn, defn.location))
if self.context.lookup(name):
self.syntax_error('Symbol "%s" already declared' % name)
self.context.symbols[name] = SymEntry(defn, defn.location)
defns.append(defn) defns.append(defn)
while self.scanner.on('define', 'routine'): while self.scanner.on('define', 'routine'):
if self.scanner.consume('define'): if self.scanner.consume('define'):
@ -83,9 +85,7 @@ class Parser(object):
else: else:
routine = self.legacy_routine() routine = self.legacy_routine()
name = routine.name name = routine.name
if self.context.lookup(name): self.declare(name, SymEntry(routine, routine.location))
self.syntax_error('Symbol "%s" already declared' % name)
self.context.symbols[name] = SymEntry(routine, routine.location)
routines.append(routine) routines.append(routine)
self.scanner.check_type('EOF') self.scanner.check_type('EOF')
@ -302,7 +302,7 @@ class Parser(object):
c = {} c = {}
for defn in statics: for defn in statics:
name = defn.name name = defn.name
if self.context.lookup(name): if self.context.fetch(name):
self.syntax_error('Symbol "%s" already declared' % name) self.syntax_error('Symbol "%s" already declared' % name)
c[name] = SymEntry(defn, defn.location) c[name] = SymEntry(defn, defn.location)
return c return c
@ -334,7 +334,7 @@ class Parser(object):
elif forward: elif forward:
name = self.scanner.token name = self.scanner.token
self.scanner.scan() self.scanner.scan()
loc = self.context.lookup(name) loc = self.context.fetch(name)
if loc is not None: if loc is not None:
return loc return loc
else: else: