summaryrefslogblamecommitdiffstats
path: root/src/front/symbols.py
blob: 0f98d831c1856f86c3a0828217435d5ff0d62c29 (plain) (tree)
1
2
3
4
5
6
7
8
9
10

                       
                         

              




                                    
                                                   




                                                                                     

                                

                         
            
                                      

                        

                              



                                      
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)