comparison 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
comparison
equal deleted inserted replaced
287:1c7c1e619be8 288:a747a45dcd78
1 import logging 1 import logging
2 import ppci 2 import ppci
3 from . import Parser, TypeChecker, Analyzer, CodeGenerator 3 from . import Parser, TypeChecker, Analyzer, CodeGenerator
4 from . astprinter import AstPrinter
5 import glob
6 4
7 5
8 class Builder: 6 class Builder:
9 """ 7 """
10 Generates IR-code from c3 source. 8 Generates IR-code from c3 source.
11 Reports errors to the diagnostics system 9 Reports errors to the diagnostics system
12 """ 10 """
13 def __init__(self, diag): 11 def __init__(self, diag):
14 self.logger = logging.getLogger('c3') 12 self.logger = logging.getLogger('c3')
15 self.diag = diag 13 self.diag = diag
16 self.parser = Parser(diag) 14 self.parser = Parser(diag)
17 self.tc = TypeChecker(diag) 15 self.tc = TypeChecker(diag)
18 self.al = Analyzer(diag) 16 self.al = Analyzer(diag)
19 self.cg = CodeGenerator() 17 self.cg = CodeGenerator()
20 self.packages = {}
21
22 def getPackage(self, pname):
23 """ package provider for use when analyzing """
24 if pname in self.packages:
25 return self.packages[pname]
26 18
27 def checkSource(self, srcs, imps=[]): 19 def checkSource(self, srcs, imps=[]):
28 """ Performs syntax and type check. """ 20 """ Performs syntax and type check. """
29 # Parse source: 21 packages = {}
22 s_pkgs = []
30 for src in srcs: 23 for src in srcs:
31 pkg = self.parser.parseSource(src) 24 pkg = self.parser.parseSource(src)
32 src.close()
33 if not pkg: 25 if not pkg:
34 return 26 self.ok = False
27 continue
35 # Store for later use: 28 # Store for later use:
36 self.packages[pkg.name] = pkg 29 packages[pkg.name] = pkg
37 30 s_pkgs.append(pkg)
38 for pkg in self.packages.values(): 31 if imps:
39 # Only return ircode when everything is OK 32 for src in imps:
33 pkg = self.parser.parseSource(src)
34 if not pkg:
35 self.ok = False
36 continue
37 # Store for later use:
38 packages[pkg.name] = pkg
39 for pkg in packages.values():
40 # TODO: merge the two below? 40 # TODO: merge the two below?
41 if not self.al.analyzePackage(pkg, self): 41 if not self.al.analyzePackage(pkg, packages):
42 return 42 self.ok = False
43 continue
43 if not self.tc.checkPackage(pkg): 44 if not self.tc.checkPackage(pkg):
44 return 45 self.ok = False
46 continue
47 for pkg in s_pkgs:
45 yield pkg 48 yield pkg
46 49
47 def build(self, srcs, imps=[]): 50 def build(self, srcs, imps=[]):
48 """ Create IR-code from sources """ 51 """ Create IR-code from sources """
52 self.ok = True
49 for pkg in self.checkSource(srcs, imps): 53 for pkg in self.checkSource(srcs, imps):
50 yield self.cg.gencode(pkg) 54 # Only return ircode when everything is OK
51 55 if self.ok:
56 yield self.cg.gencode(pkg)