diff python/c3/builder.py @ 251:6ed3d3a82a63

Added another c3 example. First import attempt
author Windel Bouwman
date Mon, 29 Jul 2013 20:23:13 +0200
parents 240111e0456f
children bd26dc13f270
line wrap: on
line diff
--- a/python/c3/builder.py	Sun Jul 28 19:07:51 2013 +0200
+++ b/python/c3/builder.py	Mon Jul 29 20:23:13 2013 +0200
@@ -1,6 +1,7 @@
 import ppci
 from . import Parser, TypeChecker, Analyzer, CodeGenerator
 from . astprinter import AstPrinter
+import glob
 
 class Builder:
     """ 
@@ -13,19 +14,43 @@
         self.tc = TypeChecker(diag)
         self.al = Analyzer(diag)
         self.cg = CodeGenerator()
-    def build(self, src):
-        """ Create IR-code from sources """
+        self.packages = {}
+
+    def getPackage(self, pname):
+        """ 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 pname in self.packages:
+                return self.packages[pname]
+
+    def parse(self, src):
         pkg = self.parser.parseSource(src)
         if not pkg:
             return
-        self.pkg = pkg
+
         # TODO: merge the two below?
         #AstPrinter().printAst(pkg)
-        if not self.al.analyzePackage(pkg):
+        if not self.al.analyzePackage(pkg, self):
             return
         if not self.tc.checkPackage(pkg):
             return
 
+        # Store for later use:
+        self.packages[pkg.name] = pkg
+        return pkg
+
+    def build(self, src):
+        """ Create IR-code from sources """
+        pkg = self.parse(src)
+
         # Only return ircode when everything is OK
-        return self.cg.gencode(pkg)
+        if pkg:
+            return self.cg.gencode(pkg)