annotate python/c3/analyse.py @ 163:8104fc8b5e90

Added visitor to c3
author Windel Bouwman
date Mon, 18 Mar 2013 20:13:57 +0100
parents 4ae0e02599de
children da0087b82fbe
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):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
25 if type(sym) is Variable:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
26 sym.typ = self.resolveDesignator(sym.typ, sym)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
27 elif type(sym) is VariableUse:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
28 sym.target = self.resolveDesignator(sym.target, sym)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
29 elif type(sym) is ProcedureCall:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
30 sym.proc = self.resolveDesignator(sym.proc, sym)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
31 elif type(sym) is FunctionType:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
32 sym.returntype = self.resolveDesignator(sym.returntype)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
33 elif type(sym) is Constant:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 150
diff changeset
34 print(sym)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
35