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