Mercurial > lcfOS
comparison 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 |
comparison
equal
deleted
inserted
replaced
162:d8c735dc31f9 | 163:8104fc8b5e90 |
---|---|
1 from .visitor import Visitor | |
2 from .astnodes import * | |
1 | 3 |
2 class Analyzer: | 4 class Analyzer: |
5 """ This class checks names and references """ | |
3 def __init__(self, diag): | 6 def __init__(self, diag): |
4 self.diag = diag | 7 self.diag = diag |
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) | |
5 | 35 |