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
|
163
|
26 # buildin types:
|
|
27 intType = astnodes.BaseType('int')
|
|
28 doubleType = astnodes.BaseType('double')
|
|
29 voidType = astnodes.BaseType('void')
|
|
30 boolType = astnodes.BaseType('void')
|
|
31
|
150
|
32 def createBuiltins(scope):
|
163
|
33 for tn in ['u64', 'u32', 'u16', 'u8']:
|
150
|
34 scope.addSymbol(astnodes.BaseType(tn))
|
163
|
35 for t in [intType, doubleType, voidType, boolType]:
|
|
36 scope.addSymbol(t)
|
150
|
37
|
|
38 topScope = Scope()
|
|
39 createBuiltins(topScope)
|
|
40
|