163
|
1 from .visitor import Visitor
|
|
2 from .astnodes import *
|
150
|
3
|
|
4 class Analyzer:
|
163
|
5 """ This class checks names and references """
|
150
|
6 def __init__(self, diag):
|
|
7 self.diag = diag
|
163
|
8 self.visitor = Visitor(self.a1, self.analyze)
|
|
9 def analyzePackage(self, pkg):
|
|
10 self.visitor.visit(pkg)
|
|
11 def resolveDesignator(self, d, referee=None):
|
|
12 assert type(d) is Designator
|
|
13 if d.scope.hasSymbol(d.tname):
|
|
14 s = d.scope.getSymbol(d.tname)
|
|
15 if hasattr(s, 'addRef'):
|
|
16 # TODO: make this nicer
|
|
17 s.addRef(referee)
|
|
18 return s
|
|
19 else:
|
|
20 msg = 'Cannot resolve name {0}'.format(d.tname)
|
|
21 self.diag.error(msg, d.loc)
|
|
22 def a1(self, sym):
|
|
23 pass
|
|
24 def analyze(self, sym):
|
|
25 if type(sym) is Variable:
|
|
26 sym.typ = self.resolveDesignator(sym.typ, sym)
|
|
27 elif type(sym) is VariableUse:
|
|
28 sym.target = self.resolveDesignator(sym.target, sym)
|
|
29 elif type(sym) is ProcedureCall:
|
|
30 sym.proc = self.resolveDesignator(sym.proc, sym)
|
|
31 elif type(sym) is FunctionType:
|
|
32 sym.returntype = self.resolveDesignator(sym.returntype)
|
|
33 elif type(sym) is Constant:
|
|
34 print(sym)
|
150
|
35
|