annotate python/ppci/c3/scope.py @ 354:5477e499b039

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