annotate python/ks/symboltable.py @ 191:6b2bec5653f1

Added assembler testset
author Windel Bouwman
date Sun, 26 May 2013 15:28:07 +0200
parents e9b27f7193e3
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
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
3 class SymbolTable:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
4 """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
5 Symbol table for a current scope.
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
6 It has functions:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
7 - hasname for checking for a name in current scope or above
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
8 - addSymbol to add an object
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
9 """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
10 def __init__(self, parent=None):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
11 self.parent = parent
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
12 self.syms = {}
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
13
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
14 def __repr__(self):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
15 return 'Symboltable with {0} symbols\n'.format(len(self.syms))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
16
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
17 def printTable(self, indent=0):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
18 for name in self.syms:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
19 print(self.syms[name])
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
20
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
21 def getAllLocal(self, cls):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
22 """ Get all local objects of a specific type """
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
23 r = []
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
24 for key in self.syms.keys():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
25 sym = self.syms[key]
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
26 if issubclass(type(sym), cls):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
27 r.append(sym)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
28 return r
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
29
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
30 def getLocal(self, cls, name):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
31 if name in self.syms.keys():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
32 sym = self.syms[name]
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
33 if isinstance(sym, cls):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
34 return sym
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
35 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
36 Error('Wrong type found')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
37 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
38 Error('Symbol not found')
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
39
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
40 # Retrieving of specific classes of items:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
41 def get(self, cls, name):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
42 if self.hasSymbol(name):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
43 sym = self.getSymbol(name)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
44 if issubclass(type(sym), cls):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
45 return sym
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
46 raise SymbolException('type {0} undefined'.format(typename))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
47
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
48 def has(self, cls, name):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
49 if self.hasSymbol(name):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
50 sym = self.getSymbol(name)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
51 if issubclass(type(sym), cls):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
52 return True
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
53 return False
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
54
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
55 # Adding and retrieving of symbols in general:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
56 def addSymbol(self, sym):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
57 if sym.name in self.syms.keys():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
58 raise Exception('Symbol "{0}" redefined'.format(sym.name))
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
59 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
60 self.syms[sym.name] = sym
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
61
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
62 def getSymbol(self, name):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
63 if name in self.syms.keys():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
64 return self.syms[name]
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
65 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
66 if self.parent:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
67 return self.parent.getSymbol(name)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
68 else:
182
e9b27f7193e3 Replace clear function by function also supported in python 3.2
Windel Bouwman
parents: 146
diff changeset
69 raise Exception('Symbol "{0}" undeclared!'.format(name))
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
70
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
71 def hasSymbol(self, name):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
72 if name in self.syms.keys():
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
73 return True
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
74 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
75 if self.parent:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
76 return self.parent.hasSymbol(name)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
77 else:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
78 return False
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
79