summaryrefslogtreecommitdiffstats
path: root/src/front/symbols.py
blob: 0f98d831c1856f86c3a0828217435d5ff0d62c29 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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)