150
|
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):
|
155
|
27 for tn in ['int', 'u32', 'u16', 'double', 'void']:
|
150
|
28 scope.addSymbol(astnodes.BaseType(tn))
|
|
29
|
|
30 topScope = Scope()
|
|
31 createBuiltins(topScope)
|
|
32
|