Mercurial > lcfOS
annotate python/c3/builder.py @ 281:4496cae24d7f
Improved logview
author | Windel Bouwman |
---|---|
date | Sat, 02 Nov 2013 11:11:40 +0100 |
parents | e64bae57cda8 |
children | 1c7c1e619be8 |
rev | line source |
---|---|
254 | 1 import logging |
165 | 2 import ppci |
215 | 3 from . import Parser, TypeChecker, Analyzer, CodeGenerator |
225 | 4 from . astprinter import AstPrinter |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
5 import glob |
165 | 6 |
7 class Builder: | |
186 | 8 """ |
9 Generates IR-code from c3 source. | |
10 Reports errors to the diagnostics system | |
11 """ | |
12 def __init__(self, diag): | |
272 | 13 self.pack_dir = None |
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 self.packages = {} |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
21 |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
22 def getPackage(self, pname): |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
23 """ package provider for use when analyzing """ |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
24 if pname in self.packages: |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
25 return self.packages[pname] |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
26 else: |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
27 # Try to lookup package from file |
272 | 28 fns = glob.glob('./**/{}.c3'.format(pname)) |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
29 if fns: |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
30 with open(fns[0]) as f: |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
31 src = f.read() |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
32 self.build(src) |
272 | 33 if self.pack_dir: |
34 fns = glob.glob('{}/{}.c3'.format(self.pack_dir, pname)) | |
35 if fns: | |
36 with open(fns[0]) as f: | |
37 src = f.read() | |
38 self.build(src) | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
39 if pname in self.packages: |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
40 return self.packages[pname] |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
41 |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
42 def parse(self, src): |
226 | 43 pkg = self.parser.parseSource(src) |
44 if not pkg: | |
186 | 45 return |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
46 |
226 | 47 # TODO: merge the two below? |
48 #AstPrinter().printAst(pkg) | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
49 if not self.al.analyzePackage(pkg, self): |
186 | 50 return |
226 | 51 if not self.tc.checkPackage(pkg): |
186 | 52 return |
194 | 53 |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
54 # Store for later use: |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
55 self.packages[pkg.name] = pkg |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
56 return pkg |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
57 |
272 | 58 def build(self, src, pack_dir=None): |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
59 """ Create IR-code from sources """ |
272 | 60 self.pack_dir = pack_dir |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
61 pkg = self.parse(src) |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
62 |
226 | 63 # Only return ircode when everything is OK |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
64 if pkg: |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
65 return self.cg.gencode(pkg) |
165 | 66 |