Mercurial > lcfOS
annotate python/ppci/c3/builder.py @ 300:158068af716c
yafm
author | Windel Bouwman |
---|---|
date | Tue, 03 Dec 2013 18:00:22 +0100 |
parents | python/c3/builder.py@917eab04b8b7 |
children | 6753763d3bec |
rev | line source |
---|---|
254 | 1 import logging |
165 | 2 import ppci |
300 | 3 from .parser import Parser |
4 from .analyse import TypeChecker, Analyzer | |
5 from .codegenerator import CodeGenerator | |
289 | 6 from .analyse import AddScope |
165 | 7 |
287 | 8 |
165 | 9 class Builder: |
288 | 10 """ |
186 | 11 Generates IR-code from c3 source. |
300 | 12 Reports errors to the diagnostics system. |
186 | 13 """ |
14 def __init__(self, diag): | |
254 | 15 self.logger = logging.getLogger('c3') |
226 | 16 self.diag = diag |
17 self.parser = Parser(diag) | |
18 self.tc = TypeChecker(diag) | |
19 self.al = Analyzer(diag) | |
20 self.cg = CodeGenerator() | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
21 |
287 | 22 def checkSource(self, srcs, imps=[]): |
23 """ Performs syntax and type check. """ | |
293 | 24 iter(srcs) |
25 iter(imps) | |
300 | 26 |
293 | 27 def doParse(srcs): |
28 for src in srcs: | |
288 | 29 pkg = self.parser.parseSource(src) |
293 | 30 if pkg: |
31 yield pkg | |
32 else: | |
288 | 33 self.ok = False |
293 | 34 s_pkgs = set(doParse(srcs)) |
35 i_pkgs = set(doParse(imps)) | |
36 all_pkgs = s_pkgs | i_pkgs | |
289 | 37 # Fix scopes: |
300 | 38 |
293 | 39 def doF(f, pkgs): |
40 for pkg in pkgs: | |
41 if f(pkg): | |
42 yield pkg | |
43 else: | |
44 self.ok = False | |
45 all_pkgs = set(doF(AddScope(self.diag).addScope, all_pkgs)) | |
289 | 46 # TODO: fix error handling better |
300 | 47 |
293 | 48 def doA(pkgs): |
49 packages = {pkg.name: pkg for pkg in pkgs} | |
50 for pkg in pkgs: | |
51 if self.al.analyzePackage(pkg, packages): | |
52 yield pkg | |
53 else: | |
54 self.ok = False | |
55 all_pkgs = set(doA(all_pkgs)) | |
56 all_pkgs = set(doF(self.tc.checkPackage, all_pkgs)) | |
57 return all_pkgs & s_pkgs | |
194 | 58 |
287 | 59 def build(self, srcs, imps=[]): |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
60 """ Create IR-code from sources """ |
300 | 61 self.logger.info('Starting build with {}'.format(srcs)) |
288 | 62 self.ok = True |
287 | 63 for pkg in self.checkSource(srcs, imps): |
288 | 64 # Only return ircode when everything is OK |
65 if self.ok: | |
66 yield self.cg.gencode(pkg) |