comparison python/c3/analyse.py @ 251:6ed3d3a82a63

Added another c3 example. First import attempt
author Windel Bouwman
date Mon, 29 Jul 2013 20:23:13 +0200
parents e41e4109addd
children 7416c923a02a
comparison
equal deleted inserted replaced
250:f5fba5b554d7 251:6ed3d3a82a63
10 This class checks names and references 10 This class checks names and references
11 """ 11 """
12 def __init__(self, diag): 12 def __init__(self, diag):
13 self.diag = diag 13 self.diag = diag
14 14
15 def analyzePackage(self, pkg): 15 def analyzePackage(self, pkg, packageProvider):
16 self.ok = True 16 self.ok = True
17 visitor = Visitor() 17 visitor = Visitor()
18 # Prepare top level scope: 18 # Prepare top level scope:
19 self.scopeStack = [topScope] 19 self.scopeStack = [topScope]
20 modScope = Scope(self.CurrentScope)
21 self.scopeStack.append(modScope)
20 visitor.visit(pkg, self.enterScope, self.quitScope) 22 visitor.visit(pkg, self.enterScope, self.quitScope)
21 del self.scopeStack 23 del self.scopeStack
24
25 # Handle imports:
26 for i in pkg.imports:
27 ip = packageProvider.getPackage(i)
28 if not ip:
29 self.error('Cannot import {}'.format(i))
30 continue
31 for x in ip.declarations:
32 modScope.addSymbol(x)
22 visitor.visit(pkg, self.findRefs) 33 visitor.visit(pkg, self.findRefs)
23 visitor.visit(pkg, self.sanity) 34 visitor.visit(pkg, self.sanity)
24 return self.ok 35 return self.ok
25 36
26 def error(self, msg, loc=None): 37 def error(self, msg, loc=None):
27 self.ok = False 38 self.ok = False
28 self.diag.error(msg, loc) 39 self.diag.error(msg, loc)
29 40
30 @property 41 @property
31 def currentScope(self): 42 def CurrentScope(self):
32 return self.scopeStack[-1] 43 return self.scopeStack[-1]
33 44
34 # Scope creation: 45 # Scope creation:
35 def addSymbol(self, sym): 46 def addSymbol(self, sym):
36 if self.currentScope.hasSymbol(sym.name): 47 if self.CurrentScope.hasSymbol(sym.name):
37 self.error('Redefinition of {0}'.format(sym.name), sym.loc) 48 self.error('Redefinition of {0}'.format(sym.name), sym.loc)
38 else: 49 else:
39 self.currentScope.addSymbol(sym) 50 self.CurrentScope.addSymbol(sym)
40 51
41 def enterScope(self, sym): 52 def enterScope(self, sym):
42 # Distribute the scope: 53 # Distribute the scope:
43 sym.scope = self.currentScope 54 sym.scope = self.CurrentScope
44 55
45 # Add symbols to current scope: 56 # Add symbols to current scope:
46 if isinstance(sym, Symbol): 57 if isinstance(sym, Symbol) or isinstance(sym, DefinedType):
47 self.addSymbol(sym)
48 if isinstance(sym, DefinedType):
49 self.addSymbol(sym) 58 self.addSymbol(sym)
50 59
51 # Create subscope: 60 # Create subscope:
52 if type(sym) in [Package, Function]: 61 if type(sym) in [Package, Function]:
53 newScope = Scope(self.currentScope) 62 newScope = Scope(self.CurrentScope)
54 self.scopeStack.append(newScope) 63 self.scopeStack.append(newScope)
55 sym.innerScope = self.currentScope 64 sym.innerScope = self.CurrentScope
56 65
57 def quitScope(self, sym): 66 def quitScope(self, sym):
58 # Pop out of scope: 67 # Pop out of scope:
59 if type(sym) in [Package, Function]: 68 if type(sym) in [Package, Function]:
60 self.scopeStack.pop(-1) 69 self.scopeStack.pop(-1)