diff options
-rw-r--r-- | src/front/__init__.py | 3 | ||||
-rw-r--r-- | src/front/symbols.py | 34 |
2 files changed, 20 insertions, 17 deletions
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) |