annotate python/ppci/c3/scope.py @ 346:3bb7dcfe5529

expanded arm target
author Windel Bouwman
date Fri, 07 Mar 2014 17:05:32 +0100
parents e609d5296ee9
children 5477e499b039
rev   line source
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
1 from .astnodes import Constant, Variable, Function, BaseType, Symbol
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
2
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
3
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
4 class Scope:
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
5 """ A scope contains all symbols in a scope. It also has a parent scope,
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
6 when looking for a symbol, also the parent scopes are checked. """
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
7 def __init__(self, parent=None):
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
8 self.symbols = {}
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
9 self.parent = parent
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
10
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
11 def __iter__(self):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
12 # Iterate in a deterministic manner:
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
13 return iter(self.Constants + self.Variables + self.Functions)
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
14
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
15 @property
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
16 def Syms(self):
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
17 syms = self.symbols.values()
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
18 return sorted(syms, key=lambda v: v.name)
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
19
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
20 @property
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
21 def Constants(self):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
22 return [s for s in self.Syms if type(s) is Constant]
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
23
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
24 @property
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
25 def Variables(self):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
26 return [s for s in self.Syms if isinstance(s, Variable)]
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
27
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
28 @property
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
29 def Functions(self):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
30 return [s for s in self.Syms if type(s) is Function]
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
31
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
32 def getSymbol(self, name):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
33 if name in self.symbols:
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
34 return self.symbols[name]
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
35 # Look for symbol:
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
36 elif self.parent:
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
37 return self.parent.getSymbol(name)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
38 else:
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
39 raise KeyError(name)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
40
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
41 def __getitem__(self, key):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
42 return self.getSymbol(key)
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
43
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
44 def hasSymbol(self, name):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
45 if name in self.symbols:
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
46 return True
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
47 elif self.parent:
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
48 return self.parent.hasSymbol(name)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
49 else:
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
50 return False
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
51
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
52 def __contains__(self, name):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
53 return self.hasSymbol(name)
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
54
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
55 def addSymbol(self, sym):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
56 assert sym.name not in self.symbols
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
57 assert isinstance(sym, Symbol)
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
58 self.symbols[sym.name] = sym
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
59
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
60 def __repr__(self):
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
61 return 'Scope with {} symbols'.format(len(self.symbols))
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
62
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
63
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
64 def createTopScope(target):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
65 scope = Scope()
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
66 for tn in ['u64', 'u32', 'u16', 'u8']:
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
67 scope.addSymbol(BaseType(tn))
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
68 # buildin types:
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
69 intType = BaseType('int')
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
70 intType.bytesize = target.byte_sizes['int']
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
71 scope.addSymbol(intType)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
72 scope.addSymbol(BaseType('double'))
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
73 scope.addSymbol(BaseType('void'))
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
74 scope.addSymbol(BaseType('bool'))
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
75 scope.addSymbol(BaseType('string'))
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
76 scope.addSymbol(BaseType('byte'))
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 300
diff changeset
77 return scope