view python/c3/builder.py @ 287:1c7c1e619be8

File movage
author Windel Bouwman
date Thu, 21 Nov 2013 11:57:27 +0100
parents e64bae57cda8
children a747a45dcd78
line wrap: on
line source

import logging
import ppci
from . import Parser, TypeChecker, Analyzer, CodeGenerator
from . astprinter import AstPrinter
import glob


class Builder:
    """ 
        Generates IR-code from c3 source.
        Reports errors to the diagnostics system
    """
    def __init__(self, diag):
        self.logger = logging.getLogger('c3')
        self.diag = diag
        self.parser = Parser(diag)
        self.tc = TypeChecker(diag)
        self.al = Analyzer(diag)
        self.cg = CodeGenerator()
        self.packages = {}

    def getPackage(self, pname):
        """ package provider for use when analyzing """
        if pname in self.packages:
            return self.packages[pname]

    def checkSource(self, srcs, imps=[]):
        """ Performs syntax and type check. """
        # Parse source:
        for src in srcs:
            pkg = self.parser.parseSource(src)
            src.close()
            if not pkg:
                return
            # Store for later use:
            self.packages[pkg.name] = pkg

        for pkg in self.packages.values():
            # Only return ircode when everything is OK
            # TODO: merge the two below?
            if not self.al.analyzePackage(pkg, self):
                return
            if not self.tc.checkPackage(pkg):
                return
            yield pkg

    def build(self, srcs, imps=[]):
        """ Create IR-code from sources """
        for pkg in self.checkSource(srcs, imps):
            yield self.cg.gencode(pkg)