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