summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBenedikt Böhm <bb@xnull.de>2009-05-20 10:11:19 +0200
committerBenedikt Böhm <bb@xnull.de>2009-05-20 10:11:19 +0200
commit90f4e57ee5c4b2649ab01f96ed6ae886734b3eee (patch)
tree4748399236d548142370d486ff64af34c0f1e91b /src
parentfb9e6a20ac7393b6cc27949ad2d2af7a305c96ba (diff)
downloadswppy-90f4e57ee5c4b2649ab01f96ed6ae886734b3eee.tar.gz
swppy-90f4e57ee5c4b2649ab01f96ed6ae886734b3eee.tar.xz
swppy-90f4e57ee5c4b2649ab01f96ed6ae886734b3eee.zip
beautify symbol table
Diffstat (limited to 'src')
-rw-r--r--src/front/__init__.py3
-rw-r--r--src/front/symbols.py34
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)