annotate python/c3/analyse.py @ 186:46d62dadd61b

Improved testsuite
author Windel Bouwman
date Sat, 25 May 2013 14:26:25 +0200
parents 0b5b2ee6b435
children c1ccb1cb4cef
rev   line source
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
1 from .visitor import Visitor
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
2 from .astnodes import *
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
3
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
4 class Analyzer:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
5 """ This class checks names and references """
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
6 def __init__(self, diag):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
7 self.diag = diag
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
8 self.visitor = Visitor(self.a1, self.analyze)
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 167
diff changeset
9
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
10 def analyzePackage(self, pkg):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 167
diff changeset
11 self.ok = True
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 167
diff changeset
12 self.visitor.visit(pkg)
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 167
diff changeset
13 return self.ok
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
14 def resolveDesignator(self, d, referee=None):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
15 assert type(d) is Designator
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
16 if d.scope.hasSymbol(d.tname):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
17 s = d.scope.getSymbol(d.tname)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
18 if hasattr(s, 'addRef'):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
19 # TODO: make this nicer
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
20 s.addRef(referee)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
21 return s
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
22 else:
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 167
diff changeset
23 self.ok = False
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
24 msg = 'Cannot resolve name {0}'.format(d.tname)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
25 self.diag.error(msg, d.loc)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
26 def a1(self, sym):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
27 pass
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
28 def analyze(self, sym):
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 163
diff changeset
29 if type(sym) in [Variable, Constant]:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
30 sym.typ = self.resolveDesignator(sym.typ, sym)
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
31 elif type(sym) is Function:
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
32 pass
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
33 elif type(sym) is VariableUse:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
34 sym.target = self.resolveDesignator(sym.target, sym)
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
35 elif type(sym) is FunctionCall:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
36 sym.proc = self.resolveDesignator(sym.proc, sym)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
37 elif type(sym) is FunctionType:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
38 sym.returntype = self.resolveDesignator(sym.returntype)
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
39 sym.parametertypes = [self.resolveDesignator(pt) for pt in sym.parametertypes]
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
40