annotate python/c3/scope.py @ 150:4ae0e02599de

Added type check start and analyze phase
author Windel Bouwman
date Fri, 01 Mar 2013 16:53:22 +0100
parents
children b28a11c01dbe
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):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
9 return iter(self.symbols.values())
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
10 def getSymbol(self, name):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
11 if name in self.symbols:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
12 return self.symbols[name]
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
13 # Look for symbol:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
14 if self.parent:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
15 return self.parent.getSymbol(name)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
16 raise CompilerException("Symbol {0} not found".format(name), name.loc)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
17 def hasSymbol(self, name):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
18 if name in self.symbols:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
19 return True
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
20 if self.parent:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
21 return self.parent.hasSymbol(name)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
22 return False
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
23 def addSymbol(self, sym):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
24 self.symbols[sym.name] = sym
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
25
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
26 def createBuiltins(scope):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
27 for tn in ['int', 'u32', 'u16', 'double']:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
28 scope.addSymbol(astnodes.BaseType(tn))
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
29
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
30 topScope = Scope()
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
31
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
32 createBuiltins(topScope)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
33