view python/c3/analyse.py @ 205:d77cb5962cc5

Added some handcoded arm code generation
author Windel Bouwman
date Sun, 23 Jun 2013 18:23:18 +0200
parents 46d62dadd61b
children c1ccb1cb4cef
line wrap: on
line source

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.ok = True
        self.visitor.visit(pkg)
        return self.ok
   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:
         self.ok = False
         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) in [Variable, Constant]:
         sym.typ = self.resolveDesignator(sym.typ, sym)
      elif type(sym) is Function:
         pass
      elif type(sym) is VariableUse:
         sym.target = self.resolveDesignator(sym.target, sym)
      elif type(sym) is FunctionCall:
         sym.proc = self.resolveDesignator(sym.proc, sym)
      elif type(sym) is FunctionType:
         sym.returntype = self.resolveDesignator(sym.returntype)
         sym.parametertypes = [self.resolveDesignator(pt) for pt in sym.parametertypes]