Mercurial > lcfOS
comparison 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 |
comparison
equal
deleted
inserted
replaced
149:74241ca312cc | 150:4ae0e02599de |
---|---|
1 from . import astnodes | |
2 | |
3 class Scope: | |
4 """ A scope contains all symbols in a scope """ | |
5 def __init__(self, parent=None): | |
6 self.symbols = {} | |
7 self.parent = parent | |
8 def __iter__(self): | |
9 return iter(self.symbols.values()) | |
10 def getSymbol(self, name): | |
11 if name in self.symbols: | |
12 return self.symbols[name] | |
13 # Look for symbol: | |
14 if self.parent: | |
15 return self.parent.getSymbol(name) | |
16 raise CompilerException("Symbol {0} not found".format(name), name.loc) | |
17 def hasSymbol(self, name): | |
18 if name in self.symbols: | |
19 return True | |
20 if self.parent: | |
21 return self.parent.hasSymbol(name) | |
22 return False | |
23 def addSymbol(self, sym): | |
24 self.symbols[sym.name] = sym | |
25 | |
26 def createBuiltins(scope): | |
27 for tn in ['int', 'u32', 'u16', 'double']: | |
28 scope.addSymbol(astnodes.BaseType(tn)) | |
29 | |
30 topScope = Scope() | |
31 | |
32 createBuiltins(topScope) | |
33 |