Mercurial > lcfOS
comparison 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 |
comparison
equal
deleted
inserted
replaced
299:674789d9ff37 | 300:158068af716c |
---|---|
1 import logging | |
2 import ppci | |
3 from .parser import Parser | |
4 from .analyse import TypeChecker, Analyzer | |
5 from .codegenerator import CodeGenerator | |
6 from .analyse import AddScope | |
7 | |
8 | |
9 class Builder: | |
10 """ | |
11 Generates IR-code from c3 source. | |
12 Reports errors to the diagnostics system. | |
13 """ | |
14 def __init__(self, diag): | |
15 self.logger = logging.getLogger('c3') | |
16 self.diag = diag | |
17 self.parser = Parser(diag) | |
18 self.tc = TypeChecker(diag) | |
19 self.al = Analyzer(diag) | |
20 self.cg = CodeGenerator() | |
21 | |
22 def checkSource(self, srcs, imps=[]): | |
23 """ Performs syntax and type check. """ | |
24 iter(srcs) | |
25 iter(imps) | |
26 | |
27 def doParse(srcs): | |
28 for src in srcs: | |
29 pkg = self.parser.parseSource(src) | |
30 if pkg: | |
31 yield pkg | |
32 else: | |
33 self.ok = False | |
34 s_pkgs = set(doParse(srcs)) | |
35 i_pkgs = set(doParse(imps)) | |
36 all_pkgs = s_pkgs | i_pkgs | |
37 # Fix scopes: | |
38 | |
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)) | |
46 # TODO: fix error handling better | |
47 | |
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 | |
58 | |
59 def build(self, srcs, imps=[]): | |
60 """ Create IR-code from sources """ | |
61 self.logger.info('Starting build with {}'.format(srcs)) | |
62 self.ok = True | |
63 for pkg in self.checkSource(srcs, imps): | |
64 # Only return ircode when everything is OK | |
65 if self.ok: | |
66 yield self.cg.gencode(pkg) |