Source code for pyrser.type_system.symbol

# symbol for type checking
import weakref


[docs]class Symbol: """ Symbol are semantic name used in a language. Could be map to binary symbol but conceptually are more related to language symbol table. """
[docs] def __init__(self, name, parent=None): self.name = name self.set_parent(parent)
[docs] def __getstate__(self): """ For pickle don't handle weakrefs... """ state = self.__dict__.copy() del state['parent'] return state
[docs] def set_parent(self, parent) -> object: if parent is not None: self.parent = weakref.ref(parent) else: self.parent = None return self
[docs] def get_parent(self) -> object: """ Auto deref parent and return the instance. """ if hasattr(self, 'parent') and self.parent is not None: return self.parent() return None
[docs] def get_scope_list(self) -> list: """ Return the list of all contained scope from global to local """ # by default only return scoped name lstparent = [self] p = self.get_parent() while p is not None: lstparent.append(p) p = p.get_parent() return lstparent
[docs] def get_scope_names(self) -> list: """ Return the list of all contained scope from global to local """ # allow global scope to have an None string instance lscope = [] for scope in reversed(self.get_scope_list()): if scope.name is not None: # handle fun/block scope decoration lscope.append(scope.name) return lscope # to overload for new language
[docs] def show_name(self) -> str: """ Return a convenient name for type checking """ return ".".join(self.get_scope_names()) # to overload for new language
[docs] def internal_name(self) -> str: """ Returns the namespace's internal_name. """ unq = "_".join(self.get_scope_names()) return unq
[docs] def __str__(self) -> str: return self.show_name()
[docs] def __repr__(self) -> str: return self.internal_name()