annotate python/ppci/c3/builder.py @ 301:6753763d3bec

merge codegen into ppci package
author Windel Bouwman
date Thu, 05 Dec 2013 17:02:38 +0100
parents 158068af716c
children 0615b5308710
rev   line source
254
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
1 import logging
300
Windel Bouwman
parents: 295
diff changeset
2 from .parser import Parser
Windel Bouwman
parents: 295
diff changeset
3 from .analyse import TypeChecker, Analyzer
Windel Bouwman
parents: 295
diff changeset
4 from .codegenerator import CodeGenerator
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
5 from .analyse import AddScope
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
6
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
7
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
8 class Builder:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
9 """
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 168
diff changeset
10 Generates IR-code from c3 source.
300
Windel Bouwman
parents: 295
diff changeset
11 Reports errors to the diagnostics system.
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 168
diff changeset
12 """
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
13 def __init__(self, diag, target):
254
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
14 self.logger = logging.getLogger('c3')
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
15 self.diag = diag
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
16 self.parser = Parser(diag)
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
17 self.tc = TypeChecker(diag)
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
18 self.al = Analyzer(diag)
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
19 self.cg = CodeGenerator()
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 226
diff changeset
20
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
21 def checkSource(self, srcs, imps=[]):
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
22 """ Performs syntax and type check. """
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
23 iter(srcs)
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
24 iter(imps)
300
Windel Bouwman
parents: 295
diff changeset
25
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
26 def doParse(srcs):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
27 for src in srcs:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
28 pkg = self.parser.parseSource(src)
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
29 if pkg:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
30 yield pkg
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
31 else:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
32 self.ok = False
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
33 s_pkgs = set(doParse(srcs))
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
34 i_pkgs = set(doParse(imps))
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
35 all_pkgs = s_pkgs | i_pkgs
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
36 # Fix scopes:
300
Windel Bouwman
parents: 295
diff changeset
37
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
38 def doF(f, pkgs):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
39 for pkg in pkgs:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
40 if f(pkg):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
41 yield pkg
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
42 else:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
43 self.ok = False
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
44 all_pkgs = set(doF(AddScope(self.diag).addScope, all_pkgs))
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
45 # TODO: fix error handling better
300
Windel Bouwman
parents: 295
diff changeset
46
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
47 def doA(pkgs):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
48 packages = {pkg.name: pkg for pkg in pkgs}
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
49 for pkg in pkgs:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
50 if self.al.analyzePackage(pkg, packages):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
51 yield pkg
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
52 else:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
53 self.ok = False
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
54 all_pkgs = set(doA(all_pkgs))
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
55 all_pkgs = set(doF(self.tc.checkPackage, all_pkgs))
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
56 return all_pkgs & s_pkgs
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 186
diff changeset
57
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
58 def build(self, srcs, imps=[]):
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 226
diff changeset
59 """ Create IR-code from sources """
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
60 self.logger.info('Starting build with {} source files'.format(len(srcs)))
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
61 self.ok = True
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
62 for pkg in self.checkSource(srcs, imps):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
63 # Only return ircode when everything is OK
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
64 if self.ok:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
65 yield self.cg.gencode(pkg)