annotate python/c3/scope.py @ 217:8b2e5f3cd579

Removed some stale python source files
author Windel Bouwman
date Fri, 05 Jul 2013 14:13:59 +0200
parents e023d3ce1d63
children 521567d17388
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):
164
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
10 # Iterate in a deterministic manner:
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
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):
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 164
diff changeset
24 return [s for s in self.Syms if type(s) is astnodes.Variable]
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):
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
31 if name in self.symbols:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
32 return self.symbols[name]
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
33 # Look for symbol:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
34 if self.parent:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
35 return self.parent.getSymbol(name)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
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):
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
39 if name in self.symbols:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
40 return True
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
41 if self.parent:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
42 return self.parent.hasSymbol(name)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
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):
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
46 self.symbols[sym.name] = sym
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
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
51 # buildin types:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
52 intType = astnodes.BaseType('int')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
53 doubleType = astnodes.BaseType('double')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
54 voidType = astnodes.BaseType('void')
164
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
55 boolType = astnodes.BaseType('bool')
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
56 stringType = astnodes.BaseType('string')
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
57 byteType = astnodes.BaseType('byte')
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
58
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
59 def createBuiltins(scope):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
60 for tn in ['u64', 'u32', 'u16', 'u8']:
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
61 scope.addSymbol(astnodes.BaseType(tn))
164
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
62 for t in [intType, doubleType, voidType, boolType, stringType, byteType]:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
63 scope.addSymbol(t)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
64
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
65 topScope = Scope()
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
66 createBuiltins(topScope)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
67