annotate python/c3/scope.py @ 168:49f1ab80d040

Added awesome icons
author Windel Bouwman
date Fri, 22 Mar 2013 19:09:38 +0100
parents e023d3ce1d63
children 8b2e5f3cd579
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:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
4 """ A scope contains all symbols in a scope """
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
5 def __init__(self, parent=None):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
6 self.symbols = {}
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
7 self.parent = parent
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
8 def __iter__(self):
164
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
9 # Iterate in a deterministic manner:
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
10 return iter(self.Constants + self.Variables + self.Functions)
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
11 @property
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
12 def Syms(self):
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
13 syms = self.symbols.values()
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
14 return sorted(syms, key=lambda v: v.name)
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
15 @property
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
16 def Constants(self):
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
17 return [s for s in self.Syms if type(s) is astnodes.Constant]
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
18 @property
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
19 def Variables(self):
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
20 return [s for s in self.Syms if type(s) is astnodes.Variable]
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
21 @property
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
22 def Functions(self):
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
23 return [s for s in self.Syms if type(s) is astnodes.Function]
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
24 def getSymbol(self, name):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
25 if name in self.symbols:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
26 return self.symbols[name]
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
27 # Look for symbol:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
28 if self.parent:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
29 return self.parent.getSymbol(name)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
30 raise CompilerException("Symbol {0} not found".format(name), name.loc)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
31 def hasSymbol(self, name):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
32 if name in self.symbols:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
33 return True
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.hasSymbol(name)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
36 return False
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
37 def addSymbol(self, sym):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
38 self.symbols[sym.name] = sym
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
39
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
40 # buildin types:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
41 intType = astnodes.BaseType('int')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
42 doubleType = astnodes.BaseType('double')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
43 voidType = astnodes.BaseType('void')
164
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
44 boolType = astnodes.BaseType('bool')
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
45 stringType = astnodes.BaseType('string')
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
46 byteType = astnodes.BaseType('byte')
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
47
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
48 def createBuiltins(scope):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
49 for tn in ['u64', 'u32', 'u16', 'u8']:
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
50 scope.addSymbol(astnodes.BaseType(tn))
164
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
51 for t in [intType, doubleType, voidType, boolType, stringType, byteType]:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
52 scope.addSymbol(t)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
53
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
54 topScope = Scope()
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
55 createBuiltins(topScope)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
56