def add(self, identifier, is_global=False): """ Add Identifier to Scope. Adds a new identifier to either the current scope of global. Arguments: identifier: An Identifier named tuple object describing the new identifier to add to the table. is_global: Determines whether the identifier should be added to the current scope or the global scope. (Default: False) Raises: ParserNameError if the identifier has been declared at this scope. """ scope = -1 if not is_global else 0 if is_global and len(self) > 2: raise ParserNameError(f'Global name "{identifier.name}" must be defined in program scope') if is_global and (identifier.name in self[0] or (len(self) > 1 and identifier.name in self[1])): raise ParserNameError(f'Name "{identifier.name}" already declared at this scope') if not is_global and identifier.name in self[-1]: raise ParserNameError(f'Name "{identifier.name}" already declared at this scope') self[scope][identifier.name] = identifier
def find(self, name): """Find Identifier in Scope Searches for the given identifier in the current and global scope. Arguments: name: The identifier name for which to search. Returns: An Identifier named tuple containing identifier name, type and size information if found in the current or global scopes. Raises: ParserNameError if the given identifier is not found in any valid scope. """ identifier = None if name in self[-1]: identifier = self[-1][name] elif name in self[0]: identifier = self[0][name] else: raise ParserNameError() return identifier
def add(self, identifier, is_global=False): scope = -1 if not is_global else 0 if is_global and len(self) > 2: raise ParserNameError( 'global name must be defined in program scope') if is_global and (identifier.name in self[0] or (len(self) > 1 and identifier.name in self[1])): raise ParserNameError('name already declared at this scope') if not is_global and identifier.name in self[-1]: raise ParserNameError('name already declared at this scope') self[scope][identifier.name] = identifier return
def find(self, name): if name in self[-1]: identifier = self[-1][name] elif name in self[0]: identifier = self[0][name] else: raise ParserNameError() return identifier