From fb9e6a20ac7393b6cc27949ad2d2af7a305c96ba Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 20 May 2009 04:19:52 +0200 Subject: implemented lexer (with tokens and symbolTable) todo: beautify code, implement token classes for parser implemented test function with testcode moved token class to single file (token.py) --- src/front/symbols.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'src/front/symbols.py') diff --git a/src/front/symbols.py b/src/front/symbols.py index f4ab40e..a868d77 100644 --- a/src/front/symbols.py +++ b/src/front/symbols.py @@ -1,9 +1,30 @@ class SymbolTable: def __init__(self): + self.symbols = {} return - def put(token, id): - 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) - def get(token): - return + if id == None: + if len(self.symbols) <= 0: + id = 0 + else: + id = max(self.symbols.values()) + 1 + + self.symbols[token] = id + return id + + + def get(self, token): + if token in self.symbols: + return self.symbols[token] + + return None + + def getOrPut(self, token): + if self.get(token): + return self.get(token) + + return self.put(token) -- cgit v1.2.3