view python/c3/builder.py @ 288:a747a45dcd78

Various styling work
author Windel Bouwman
date Thu, 21 Nov 2013 14:26:13 +0100
parents 1c7c1e619be8
children bd2593de3ff8
line wrap: on
line source

import logging
import ppci
from . import Parser, TypeChecker, Analyzer, CodeGenerator


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()

    def checkSource(self, srcs, imps=[]):
        """ Performs syntax and type check. """
        packages = {}
        s_pkgs = []
        for src in srcs:
            pkg = self.parser.parseSource(src)
            if not pkg:
                self.ok = False
                continue
            # Store for later use:
            packages[pkg.name] = pkg
            s_pkgs.append(pkg)
        if imps:
            for src in imps:
                pkg = self.parser.parseSource(src)
                if not pkg:
                    self.ok = False
                    continue
                # Store for later use:
                packages[pkg.name] = pkg
        for pkg in packages.values():
            # TODO: merge the two below?
            if not self.al.analyzePackage(pkg, packages):
                self.ok = False
                continue
            if not self.tc.checkPackage(pkg):
                self.ok = False
                continue
        for pkg in s_pkgs:
            yield pkg

    def build(self, srcs, imps=[]):
        """ Create IR-code from sources """
        self.ok = True
        for pkg in self.checkSource(srcs, imps):
            # Only return ircode when everything is OK
            if self.ok:
                yield self.cg.gencode(pkg)