diff 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 diff
--- a/python/c3/builder.py	Fri Nov 15 13:52:32 2013 +0100
+++ b/python/c3/builder.py	Thu Nov 21 11:57:27 2013 +0100
@@ -4,13 +4,13 @@
 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.pack_dir = None
         self.logger = logging.getLogger('c3')
         self.diag = diag
         self.parser = Parser(diag)
@@ -23,44 +23,29 @@
         """ package provider for use when analyzing """
         if pname in self.packages:
             return self.packages[pname]
-        else:
-            # Try to lookup package from file
-            fns = glob.glob('./**/{}.c3'.format(pname))
-            if fns:
-                with open(fns[0]) as f:
-                    src = f.read()
-                self.build(src)
-            if self.pack_dir:
-                fns = glob.glob('{}/{}.c3'.format(self.pack_dir, pname))
-                if fns:
-                    with open(fns[0]) as f:
-                        src = f.read()
-                    self.build(src)
-            if pname in self.packages:
-                return self.packages[pname]
 
-    def parse(self, src):
-        pkg = self.parser.parseSource(src)
-        if not pkg:
-            return
+    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
 
-        # TODO: merge the two below?
-        #AstPrinter().printAst(pkg)
-        if not self.al.analyzePackage(pkg, self):
-            return
-        if not self.tc.checkPackage(pkg):
-            return
+        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
 
-        # Store for later use:
-        self.packages[pkg.name] = pkg
-        return pkg
-
-    def build(self, src, pack_dir=None):
+    def build(self, srcs, imps=[]):
         """ Create IR-code from sources """
-        self.pack_dir = pack_dir
-        pkg = self.parse(src)
+        for pkg in self.checkSource(srcs, imps):
+            yield self.cg.gencode(pkg)
 
-        # Only return ircode when everything is OK
-        if pkg:
-            return self.cg.gencode(pkg)
-