annotate ide/compiler/symboltable.py @ 24:d8627924d40d

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