annotate python/c3/analyse.py @ 167:0b5b2ee6b435

Added 2 unit tests
author Windel Bouwman
date Fri, 22 Mar 2013 17:40:13 +0100
parents da0087b82fbe
children 46d62dadd61b
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)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
9 def analyzePackage(self, pkg):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
10 self.visitor.visit(pkg)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
11 def resolveDesignator(self, d, referee=None):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
12 assert type(d) is Designator
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
13 if d.scope.hasSymbol(d.tname):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
14 s = d.scope.getSymbol(d.tname)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
15 if hasattr(s, 'addRef'):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
16 # TODO: make this nicer
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
17 s.addRef(referee)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
18 return s
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
19 else:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
20 msg = 'Cannot resolve name {0}'.format(d.tname)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
21 self.diag.error(msg, d.loc)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
22 def a1(self, sym):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
23 pass
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
24 def analyze(self, sym):
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 163
diff changeset
25 if type(sym) in [Variable, Constant]:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
26 sym.typ = self.resolveDesignator(sym.typ, sym)
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
27 elif type(sym) is Function:
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
28 pass
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
29 elif type(sym) is VariableUse:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
30 sym.target = self.resolveDesignator(sym.target, sym)
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
31 elif type(sym) is FunctionCall:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
32 sym.proc = self.resolveDesignator(sym.proc, sym)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
33 elif type(sym) is FunctionType:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
34 sym.returntype = self.resolveDesignator(sym.returntype)
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
35 sym.parametertypes = [self.resolveDesignator(pt) for pt in sym.parametertypes]
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
36