diff python/ppci/c3/builder.py @ 306:b145f8e6050b

Start on c3 rewrite
author Windel Bouwman
date Mon, 09 Dec 2013 19:00:21 +0100
parents 0615b5308710
children e609d5296ee9
line wrap: on
line diff
--- a/python/ppci/c3/builder.py	Fri Dec 06 13:50:38 2013 +0100
+++ b/python/ppci/c3/builder.py	Mon Dec 09 19:00:21 2013 +0100
@@ -1,8 +1,11 @@
 import logging
+from .lexer import Lexer
 from .parser import Parser
 from .analyse import TypeChecker, Analyzer
 from .codegenerator import CodeGenerator
 from .analyse import AddScope
+from .scope import createTopScope
+from .visitor import AstPrinter
 
 
 class Builder:
@@ -13,53 +16,55 @@
     def __init__(self, diag, target):
         self.logger = logging.getLogger('c3')
         self.diag = diag
+        self.lexer = Lexer(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. """
-        iter(srcs)
-        iter(imps)
-
-        def doParse(srcs):
-            for src in srcs:
-                pkg = self.parser.parseSource(src)
-                if pkg:
-                    yield pkg
-                else:
-                    self.ok = False
-        s_pkgs = set(doParse(srcs))
-        i_pkgs = set(doParse(imps))
-        all_pkgs = s_pkgs | i_pkgs
-        # Fix scopes:
-
-        def doF(f, pkgs):
-            for pkg in pkgs:
-                if f(pkg):
-                    yield pkg
-                else:
-                    self.ok = False
-        all_pkgs = set(doF(AddScope(self.diag).addScope, all_pkgs))
-        # TODO: fix error handling better
-
-        def doA(pkgs):
-            packages = {pkg.name: pkg for pkg in pkgs}
-            for pkg in pkgs:
-                if self.al.analyzePackage(pkg, packages):
-                    yield pkg
-                else:
-                    self.ok = False
-        all_pkgs = set(doA(all_pkgs))
-        all_pkgs = set(doF(self.tc.checkPackage, all_pkgs))
-        return all_pkgs & s_pkgs
+        self.topScope = createTopScope(target)  # Scope with built in types
 
     def build(self, srcs, imps=[]):
         """ Create IR-code from sources """
         self.logger.info('Building {} source files'.format(len(srcs)))
+        iter(srcs)  # Check if srcs are iterable
+        iter(imps)
         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)
+        self.pkgs = {}
+
+        def doParse(src):
+            tokens = self.lexer.lex(src)
+            return self.parser.parseSource(tokens)
+        s_pkgs = set(map(doParse, srcs))
+        i_pkgs = set(map(doParse, imps))
+        all_pkgs = s_pkgs | i_pkgs
+        if not all(all_pkgs):
+            self.ok = False
+            return
+
+        #for pkg in all_pkgs:
+        #    AstPrinter().printAst(pkg)
+            
+        packages = {pkg.name: pkg for pkg in all_pkgs}
+        self.pkgs = packages
+        # Fix scopes:
+        for pkg in all_pkgs:
+            AddScope(self.diag, self.topScope).addScope(pkg)
+        if not all(pkg.ok for pkg in all_pkgs):
+            self.ok = False
+            return
+
+        for pkg in all_pkgs:
+            self.al.analyzePackage(pkg, packages)
+        if not all(pkg.ok for pkg in all_pkgs):
+            self.ok = False
+            return
+
+        for pkg in all_pkgs:
+            self.tc.checkPackage(pkg)
+        if not all(pkg.ok for pkg in all_pkgs):
+            self.ok = False
+            return
+
+        # Only return ircode when everything is OK
+        for pkg in all_pkgs & s_pkgs:
+            yield self.cg.gencode(pkg)