comparison python/ppci/frontends/ks/symboltable.py @ 100:fe145e42259d

Fixes after movage
author windel
date Mon, 24 Dec 2012 16:35:22 +0100
parents python/ppci/symboltable.py@a350055d6119
children
comparison
equal deleted inserted replaced
99:6efbeb903777 100:fe145e42259d
1 from .nodes import *
2 from ...core.errors import Error
3
4 class SymbolTable:
5 """
6 Symbol table for a current scope.
7 It has functions:
8 - hasname for checking for a name in current scope or above
9 - addSymbol to add an object
10 """
11 def __init__(self, parent=None):
12 self.parent = parent
13 self.syms = {}
14
15 def __repr__(self):
16 return 'Symboltable with {0} symbols\n'.format(len(self.syms))
17
18 def printTable(self, indent=0):
19 for name in self.syms:
20 print(self.syms[name])
21
22 def getAllLocal(self, cls):
23 """ Get all local objects of a specific type """
24 r = []
25 for key in self.syms.keys():
26 sym = self.syms[key]
27 if issubclass(type(sym), cls):
28 r.append(sym)
29 return r
30
31 def getLocal(self, cls, name):
32 if name in self.syms.keys():
33 sym = self.syms[name]
34 if isinstance(sym, cls):
35 return sym
36 else:
37 Error('Wrong type found')
38 else:
39 Error('Symbol not found')
40
41 # Retrieving of specific classes of items:
42 def get(self, cls, name):
43 if self.hasSymbol(name):
44 sym = self.getSymbol(name)
45 if issubclass(type(sym), cls):
46 return sym
47 raise SymbolException('type {0} undefined'.format(typename))
48
49 def has(self, cls, name):
50 if self.hasSymbol(name):
51 sym = self.getSymbol(name)
52 if issubclass(type(sym), cls):
53 return True
54 return False
55
56 # Adding and retrieving of symbols in general:
57 def addSymbol(self, sym):
58 if sym.name in self.syms.keys():
59 raise Exception('Symbol "{0}" redefined'.format(sym.name))
60 else:
61 self.syms[sym.name] = sym
62
63 def getSymbol(self, name):
64 if name in self.syms.keys():
65 return self.syms[name]
66 else:
67 if self.parent:
68 return self.parent.getSymbol(name)
69 else:
70 Error('Symbol "{0}" undeclared!'.format(name))
71
72 def hasSymbol(self, name):
73 if name in self.syms.keys():
74 return True
75 else:
76 if self.parent:
77 return self.parent.hasSymbol(name)
78 else:
79 return False
80