Mercurial > lcfOS
diff 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 |
line wrap: on
line diff
--- a/python/c3/analyse.py Sun Mar 10 11:36:55 2013 +0100 +++ b/python/c3/analyse.py Mon Mar 18 20:13:57 2013 +0100 @@ -1,5 +1,35 @@ +from .visitor import Visitor +from .astnodes import * class Analyzer: + """ This class checks names and references """ def __init__(self, diag): self.diag = diag + self.visitor = Visitor(self.a1, self.analyze) + def analyzePackage(self, pkg): + self.visitor.visit(pkg) + def resolveDesignator(self, d, referee=None): + assert type(d) is Designator + if d.scope.hasSymbol(d.tname): + s = d.scope.getSymbol(d.tname) + if hasattr(s, 'addRef'): + # TODO: make this nicer + s.addRef(referee) + return s + else: + msg = 'Cannot resolve name {0}'.format(d.tname) + self.diag.error(msg, d.loc) + def a1(self, sym): + pass + def analyze(self, sym): + if type(sym) is Variable: + sym.typ = self.resolveDesignator(sym.typ, sym) + elif type(sym) is VariableUse: + sym.target = self.resolveDesignator(sym.target, sym) + elif type(sym) is ProcedureCall: + sym.proc = self.resolveDesignator(sym.proc, sym) + elif type(sym) is FunctionType: + sym.returntype = self.resolveDesignator(sym.returntype) + elif type(sym) is Constant: + print(sym)