From 90f4e57ee5c4b2649ab01f96ed6ae886734b3eee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedikt=20B=C3=B6hm?= Date: Wed, 20 May 2009 10:11:19 +0200 Subject: beautify symbol table --- src/front/__init__.py | 3 +++ src/front/symbols.py | 34 +++++++++++++++++----------------- 2 files changed, 20 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/front/__init__.py b/src/front/__init__.py index 426a16e..3f7e5f8 100644 --- a/src/front/__init__.py +++ b/src/front/__init__.py @@ -31,3 +31,6 @@ end''' # parse = Parser(lex) # parse.program() + +if __name__ == "__main__": + main() diff --git a/src/front/symbols.py b/src/front/symbols.py index a868d77..0f98d83 100644 --- a/src/front/symbols.py +++ b/src/front/symbols.py @@ -3,28 +3,28 @@ class SymbolTable: self.symbols = {} return - def put(self, token, id=None): - if id and not id.isdigit(): - raise Exception("Only digits as id possible. '%s' is not a number" % id) - - if id == None: - if len(self.symbols) <= 0: - id = 0 - else: + 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): - if token in self.symbols: + try: return self.symbols[token] - - return None + except KeyError: + return None def getOrPut(self, token): - if self.get(token): - return self.get(token) - - return self.put(token) + try: + return self.symbols[token] + except KeyError: + return self.put(token) -- cgit v1.2.3