comparison python/c3/scope.py @ 217:8b2e5f3cd579

Removed some stale python source files
author Windel Bouwman
date Fri, 05 Jul 2013 14:13:59 +0200
parents e023d3ce1d63
children 521567d17388
comparison
equal deleted inserted replaced
216:57c032c5e753 217:8b2e5f3cd579
1 from . import astnodes 1 from . import astnodes
2 2
3 class Scope: 3 class Scope:
4 """ A scope contains all symbols in a scope """ 4 """ A scope contains all symbols in a scope """
5 def __init__(self, parent=None): 5 def __init__(self, parent=None):
6 self.symbols = {} 6 self.symbols = {}
7 self.parent = parent 7 self.parent = parent
8 def __iter__(self): 8
9 def __iter__(self):
9 # Iterate in a deterministic manner: 10 # Iterate in a deterministic manner:
10 return iter(self.Constants + self.Variables + self.Functions) 11 return iter(self.Constants + self.Variables + self.Functions)
11 @property 12
12 def Syms(self): 13 @property
13 syms = self.symbols.values() 14 def Syms(self):
14 return sorted(syms, key=lambda v: v.name) 15 syms = self.symbols.values()
15 @property 16 return sorted(syms, key=lambda v: v.name)
16 def Constants(self): 17
17 return [s for s in self.Syms if type(s) is astnodes.Constant] 18 @property
18 @property 19 def Constants(self):
19 def Variables(self): 20 return [s for s in self.Syms if type(s) is astnodes.Constant]
20 return [s for s in self.Syms if type(s) is astnodes.Variable] 21
21 @property 22 @property
22 def Functions(self): 23 def Variables(self):
23 return [s for s in self.Syms if type(s) is astnodes.Function] 24 return [s for s in self.Syms if type(s) is astnodes.Variable]
24 def getSymbol(self, name): 25
26 @property
27 def Functions(self):
28 return [s for s in self.Syms if type(s) is astnodes.Function]
29
30 def getSymbol(self, name):
25 if name in self.symbols: 31 if name in self.symbols:
26 return self.symbols[name] 32 return self.symbols[name]
27 # Look for symbol: 33 # Look for symbol:
28 if self.parent: 34 if self.parent:
29 return self.parent.getSymbol(name) 35 return self.parent.getSymbol(name)
30 raise CompilerException("Symbol {0} not found".format(name), name.loc) 36 raise CompilerException("Symbol {0} not found".format(name), name.loc)
31 def hasSymbol(self, name): 37
38 def hasSymbol(self, name):
32 if name in self.symbols: 39 if name in self.symbols:
33 return True 40 return True
34 if self.parent: 41 if self.parent:
35 return self.parent.hasSymbol(name) 42 return self.parent.hasSymbol(name)
36 return False 43 return False
37 def addSymbol(self, sym): 44
45 def addSymbol(self, sym):
38 self.symbols[sym.name] = sym 46 self.symbols[sym.name] = sym
47
48 def __repr__(self):
49 return 'Scope with {} symbols'.format(len(self.symbols))
39 50
40 # buildin types: 51 # buildin types:
41 intType = astnodes.BaseType('int') 52 intType = astnodes.BaseType('int')
42 doubleType = astnodes.BaseType('double') 53 doubleType = astnodes.BaseType('double')
43 voidType = astnodes.BaseType('void') 54 voidType = astnodes.BaseType('void')