class Scope(object): __shared_state = {} __current_function = None __functions = {} def __init__(self): self.__dict__ = self.__shared_state def set_function(self, name): self.__current_function = name def new(self, name, symbols = []): if getattr(self.__functions, name, None): raise ScopeError("multiple definitions of %s" % name) self.__functions[name] = ([], []) self.set_function(name) def add(self, symbols, i): if type(symbols) != type([]): symbols = [symbols] for symbol in symbols: if symbol not in self.__functions[self.__current_function][i]: self.__functions[self.__current_function][i].append(symbol) def add_params(self, symbols): self.add(symbols, 0) def add_locals(self, symbols): self.add(symbols, 1) def contains(self, name): return name in reduce(lambda x,y: x+y, self.__functions[self.__current_function]) def get_variable_offset(self, name): try: return self.__functions[self.__current_function][0].index(name) + 2 except: return -self.__functions[self.__current_function][1].index(name) - 1 def get_variables(self): return self.__functions[self.__current_function] def __str__(self): return "" % str(self.__functions)