annotate python/c3/scope.py @ 288:a747a45dcd78

Various styling work
author Windel Bouwman
date Thu, 21 Nov 2013 14:26:13 +0100
parents e64bae57cda8
children
rev   line source
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
1 from . import astnodes
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:
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
5 """ A scope contains all symbols in a scope """
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
6 def __init__(self, parent=None):
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
7 self.symbols = {}
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
8 self.parent = parent
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
9
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
10 def __iter__(self):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
11 # Iterate in a deterministic manner:
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
12 return iter(self.Constants + self.Variables + self.Functions)
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
13
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
14 @property
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
15 def Syms(self):
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
16 syms = self.symbols.values()
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
17 return sorted(syms, key=lambda v: v.name)
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
18
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
19 @property
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
20 def Constants(self):
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
21 return [s for s in self.Syms if type(s) is astnodes.Constant]
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
22
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
23 @property
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
24 def Variables(self):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
25 return [s for s in self.Syms if isinstance(s, astnodes.Variable)]
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
26
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
27 @property
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
28 def Functions(self):
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
29 return [s for s in self.Syms if type(s) is astnodes.Function]
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
30
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
31 def getSymbol(self, name):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
32 if name in self.symbols:
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
33 return self.symbols[name]
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
34 # Look for symbol:
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
35 if self.parent:
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
36 return self.parent.getSymbol(name)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
37 raise CompilerException("Symbol {0} not found".format(name), name.loc)
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
38
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
39 def hasSymbol(self, name):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
40 if name in self.symbols:
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
41 return True
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
42 if self.parent:
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
43 return self.parent.hasSymbol(name)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
44 return False
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 addSymbol(self, sym):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
47 self.symbols[sym.name] = sym
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
48
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
49 def __repr__(self):
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
50 return 'Scope with {} symbols'.format(len(self.symbols))
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
51
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 231
diff changeset
52
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
53 def createBuiltins(scope):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
54 for tn in ['u64', 'u32', 'u16', 'u8']:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
55 scope.addSymbol(astnodes.BaseType(tn))
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
56 for t in [intType, doubleType, voidType, boolType, stringType, byteType]:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
57 scope.addSymbol(t)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
58
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
59 # buildin types:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
60 intType = astnodes.BaseType('int')
231
521567d17388 simplify blink.c3
Windel Bouwman
parents: 217
diff changeset
61 intType.bytesize = 4
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
62 doubleType = astnodes.BaseType('double')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
63 voidType = astnodes.BaseType('void')
164
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
64 boolType = astnodes.BaseType('bool')
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
65 stringType = astnodes.BaseType('string')
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
66 byteType = astnodes.BaseType('byte')
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
67
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
68 # Create top level scope:
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
69 topScope = Scope()
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
70 createBuiltins(topScope)