From f720e2d49c6292ccb3de8c9668c99a5b799038b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedikt=20B=C3=B6hm?= Date: Mon, 25 May 2009 18:45:16 +0200 Subject: add scope, and symbol class, obsoletes SymbolTable --- src/front/scope.py | 20 ++++++++++++++++++++ src/front/symbol.py | 34 ++++++++++++++++++++++++++++++++++ src/front/symbols.py | 30 ------------------------------ 3 files changed, 54 insertions(+), 30 deletions(-) create mode 100644 src/front/scope.py create mode 100644 src/front/symbol.py delete mode 100644 src/front/symbols.py diff --git a/src/front/scope.py b/src/front/scope.py new file mode 100644 index 0000000..2cc3018 --- /dev/null +++ b/src/front/scope.py @@ -0,0 +1,20 @@ +class Scope(object, DictMixin): + def __init__(self, parent): + self.parent = parent + self.__symbols = {} + + def __getitem__(self, key): + try: + return self.__symbols[key] + except KeyError: + return self.parent[key] + + def __setitem__(self, key, item): + self.__symbols[key] = item + + def keys(self): + keys = self.__symbols.keys() + for key in self.parent.keys(): + if key not in keys: + keys.append(key) + return keys diff --git a/src/front/symbol.py b/src/front/symbol.py new file mode 100644 index 0000000..879403f --- /dev/null +++ b/src/front/symbol.py @@ -0,0 +1,34 @@ +class SymbolClass(object): + def __init__(self, name): + self.name = name + + def __str__(self): + return self.name + + def __repr__(self): + return "" % self + +SymbolClass.CONST = SymbolClass("CONST") +SymbolClass.VAR = SymbolClass("VAR") +SymbolClass.FUNC = SymbolClass("FUNC") + +class SymbolType(object): + def __init__(self, name): + self.name = name + + def __str__(self): + return self.name + + def __repr__(self): + return "" % self + +SymbolType.INT = SymbolType("INT") +SymbolType.BOOL = SymbolType("BOOL") + +class Symbol(object): + __slots__ = ['cls', 'type', 'value'] + + def __init__(self, value, type = SymbolType.INT, cls = SymbolClass.VAR): + self.value = value + self.type = type + self.cls = cls diff --git a/src/front/symbols.py b/src/front/symbols.py deleted file mode 100644 index 0f98d83..0000000 --- a/src/front/symbols.py +++ /dev/null @@ -1,30 +0,0 @@ -class SymbolTable: - def __init__(self): - self.symbols = {} - return - - def put(self, token, id = None): - try: - id = int(id) - except TypeError: - try: - id = max(self.symbols.values()) + 1 - except ValueError: - id = 0 - except ValueError: - raise ValueError("only digits as id possible. '%s' is not a number" % id) - - self.symbols[token] = id - return id - - def get(self, token): - try: - return self.symbols[token] - except KeyError: - return None - - def getOrPut(self, token): - try: - return self.symbols[token] - except KeyError: - return self.put(token) -- cgit v1.2.3