annotate python/c3/scope.py @ 275:6f2423df0675

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