diff options
author | Benedikt Böhm <bb@xnull.de> | 2009-05-25 18:45:16 +0200 |
---|---|---|
committer | Benedikt Böhm <bb@xnull.de> | 2009-05-25 18:45:16 +0200 |
commit | f720e2d49c6292ccb3de8c9668c99a5b799038b2 (patch) | |
tree | 061af5030d7ec6d6becfb1c1a7fba16681233c11 | |
parent | a200c7f8c51ace73295974cc07e9e4a9db38d3e1 (diff) | |
download | swppy-f720e2d49c6292ccb3de8c9668c99a5b799038b2.tar.gz swppy-f720e2d49c6292ccb3de8c9668c99a5b799038b2.tar.xz swppy-f720e2d49c6292ccb3de8c9668c99a5b799038b2.zip |
add scope, and symbol class, obsoletes SymbolTable
-rw-r--r-- | src/front/scope.py | 20 | ||||
-rw-r--r-- | src/front/symbol.py | 34 | ||||
-rw-r--r-- | src/front/symbols.py | 30 |
3 files changed, 54 insertions, 30 deletions
diff --git a/src/front/scope.py b/src/front/scope.py new file mode 100644 index 0000000..2cc3018 --- /dev/null +++ b/src/front/scope.py @@ -0,0 +1,20 @@ +class Scope(object, DictMixin): + def __init__(self, parent): + self.parent = parent + self.__symbols = {} + + def __getitem__(self, key): + try: + return self.__symbols[key] + except KeyError: + return self.parent[key] + + def __setitem__(self, key, item): + self.__symbols[key] = item + + def keys(self): + keys = self.__symbols.keys() + for key in self.parent.keys(): + if key not in keys: + keys.append(key) + return keys diff --git a/src/front/symbol.py b/src/front/symbol.py new file mode 100644 index 0000000..879403f --- /dev/null +++ b/src/front/symbol.py @@ -0,0 +1,34 @@ +class SymbolClass(object): + def __init__(self, name): + self.name = name + + def __str__(self): + return self.name + + def __repr__(self): + return "<SymbolClass: %s>" % self + +SymbolClass.CONST = SymbolClass("CONST") +SymbolClass.VAR = SymbolClass("VAR") +SymbolClass.FUNC = SymbolClass("FUNC") + +class SymbolType(object): + def __init__(self, name): + self.name = name + + def __str__(self): + return self.name + + def __repr__(self): + return "<SymbolType: %s>" % self + +SymbolType.INT = SymbolType("INT") +SymbolType.BOOL = SymbolType("BOOL") + +class Symbol(object): + __slots__ = ['cls', 'type', 'value'] + + def __init__(self, value, type = SymbolType.INT, cls = SymbolClass.VAR): + self.value = value + self.type = type + self.cls = cls diff --git a/src/front/symbols.py b/src/front/symbols.py deleted file mode 100644 index 0f98d83..0000000 --- a/src/front/symbols.py +++ /dev/null @@ -1,30 +0,0 @@ -class SymbolTable: - def __init__(self): - self.symbols = {} - return - - 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): - try: - return self.symbols[token] - except KeyError: - return None - - def getOrPut(self, token): - try: - return self.symbols[token] - except KeyError: - return self.put(token) |