Mercurial > lcfOS
annotate python/c3/builder.py @ 254:bd26dc13f270
Added logger
author | Windel Bouwman |
---|---|
date | Wed, 31 Jul 2013 21:20:58 +0200 |
parents | 6ed3d3a82a63 |
children | 7416c923a02a |
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): | |
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 self.packages = {} |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
20 |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
21 def getPackage(self, pname): |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
22 """ package provider for use when analyzing """ |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
23 if pname in self.packages: |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
24 return self.packages[pname] |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
25 else: |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
26 # Try to lookup package from file |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
27 fns = glob.glob('./*/{}.c3'.format(pname)) |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
28 if fns: |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
29 with open(fns[0]) as f: |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
30 src = f.read() |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
31 self.build(src) |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
32 if pname in self.packages: |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
33 return self.packages[pname] |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
34 |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
35 def parse(self, src): |
226 | 36 pkg = self.parser.parseSource(src) |
37 if not pkg: | |
186 | 38 return |
254 | 39 logging.getLogger('c3').info('Source parsed') |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
40 |
226 | 41 # TODO: merge the two below? |
42 #AstPrinter().printAst(pkg) | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
43 if not self.al.analyzePackage(pkg, self): |
186 | 44 return |
226 | 45 if not self.tc.checkPackage(pkg): |
186 | 46 return |
194 | 47 |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
48 # Store for later use: |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
49 self.packages[pkg.name] = pkg |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
50 return pkg |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
51 |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
52 def build(self, src): |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
53 """ Create IR-code from sources """ |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
54 pkg = self.parse(src) |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
55 |
226 | 56 # Only return ircode when everything is OK |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
57 if pkg: |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
58 return self.cg.gencode(pkg) |
165 | 59 |