Mercurial > lcfOS
annotate python/c3/builder.py @ 289:bd2593de3ff8
Semifix burn2
author | Windel Bouwman |
---|---|
date | Thu, 21 Nov 2013 15:46:50 +0100 |
parents | a747a45dcd78 |
children | 6aa721e7b10b |
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. """ | |
288 | 22 packages = {} |
23 s_pkgs = [] | |
287 | 24 for src in srcs: |
25 pkg = self.parser.parseSource(src) | |
26 if not pkg: | |
288 | 27 self.ok = False |
28 continue | |
287 | 29 # Store for later use: |
288 | 30 packages[pkg.name] = pkg |
31 s_pkgs.append(pkg) | |
32 if imps: | |
33 for src in imps: | |
34 pkg = self.parser.parseSource(src) | |
35 if not pkg: | |
36 self.ok = False | |
37 continue | |
38 # Store for later use: | |
39 packages[pkg.name] = pkg | |
289 | 40 # Fix scopes: |
288 | 41 for pkg in packages.values(): |
289 | 42 if not AddScope(self.diag).addScope(pkg): |
43 self.ok = False | |
44 # TODO: fix error handling better | |
45 for pkg in packages.values(): | |
288 | 46 if not self.al.analyzePackage(pkg, packages): |
47 self.ok = False | |
48 continue | |
289 | 49 for pkg in packages.values(): |
287 | 50 if not self.tc.checkPackage(pkg): |
288 | 51 self.ok = False |
52 continue | |
53 for pkg in s_pkgs: | |
287 | 54 yield pkg |
194 | 55 |
287 | 56 def build(self, srcs, imps=[]): |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
57 """ Create IR-code from sources """ |
288 | 58 self.ok = True |
287 | 59 for pkg in self.checkSource(srcs, imps): |
288 | 60 # Only return ircode when everything is OK |
61 if self.ok: | |
62 yield self.cg.gencode(pkg) |