annotate python/c3/scope.py @ 163:8104fc8b5e90

Added visitor to c3
author Windel Bouwman
date Mon, 18 Mar 2013 20:13:57 +0100
parents b28a11c01dbe
children e023d3ce1d63
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
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
26 # buildin types:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
27 intType = astnodes.BaseType('int')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
28 doubleType = astnodes.BaseType('double')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
29 voidType = astnodes.BaseType('void')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
30 boolType = astnodes.BaseType('void')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
31
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
32 def createBuiltins(scope):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
33 for tn in ['u64', 'u32', 'u16', 'u8']:
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
34 scope.addSymbol(astnodes.BaseType(tn))
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
35 for t in [intType, doubleType, voidType, boolType]:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
36 scope.addSymbol(t)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
37
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
38 topScope = Scope()
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
39 createBuiltins(topScope)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
40