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