diff options
author | Alexander Sulfrian <alexander@sulfrian.net> | 2009-05-20 04:19:52 +0200 |
---|---|---|
committer | Alexander Sulfrian <alexander@sulfrian.net> | 2009-05-20 04:19:52 +0200 |
commit | fb9e6a20ac7393b6cc27949ad2d2af7a305c96ba (patch) | |
tree | 2be8d1ea500651ae7415deb9ba718473969468e0 /src/front/symbols.py | |
parent | b649933b98691a43e7e3adf158109ced285e802c (diff) | |
download | swppy-fb9e6a20ac7393b6cc27949ad2d2af7a305c96ba.tar.gz swppy-fb9e6a20ac7393b6cc27949ad2d2af7a305c96ba.tar.xz swppy-fb9e6a20ac7393b6cc27949ad2d2af7a305c96ba.zip |
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)
Diffstat (limited to '')
-rw-r--r-- | src/front/symbols.py | 29 |
1 files changed, 25 insertions, 4 deletions
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) |