annotate python/c3/builder.py @ 288:a747a45dcd78

Various styling work
author Windel Bouwman
date Thu, 21 Nov 2013 14:26:13 +0100
parents 1c7c1e619be8
children bd2593de3ff8
rev   line source
254
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
1 import logging
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
2 import ppci
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
3 from . import Parser, TypeChecker, Analyzer, CodeGenerator
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
4
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
5
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
6 class Builder:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
7 """
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 168
diff changeset
8 Generates IR-code from c3 source.
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 168
diff changeset
9 Reports errors to the diagnostics system
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 168
diff changeset
10 """
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 168
diff changeset
11 def __init__(self, diag):
254
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
12 self.logger = logging.getLogger('c3')
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
13 self.diag = diag
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
14 self.parser = Parser(diag)
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
15 self.tc = TypeChecker(diag)
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
16 self.al = Analyzer(diag)
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
17 self.cg = CodeGenerator()
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 226
diff changeset
18
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
19 def checkSource(self, srcs, imps=[]):
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
20 """ Performs syntax and type check. """
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
21 packages = {}
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
22 s_pkgs = []
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
23 for src in srcs:
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
24 pkg = self.parser.parseSource(src)
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
25 if not pkg:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
26 self.ok = False
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
27 continue
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
28 # Store for later use:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
29 packages[pkg.name] = pkg
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
30 s_pkgs.append(pkg)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
31 if imps:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
32 for src in imps:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
33 pkg = self.parser.parseSource(src)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
34 if not pkg:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
35 self.ok = False
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
36 continue
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
37 # Store for later use:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
38 packages[pkg.name] = pkg
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
39 for pkg in packages.values():
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
40 # TODO: merge the two below?
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
41 if not self.al.analyzePackage(pkg, packages):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
42 self.ok = False
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
43 continue
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
44 if not self.tc.checkPackage(pkg):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
45 self.ok = False
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
46 continue
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
47 for pkg in s_pkgs:
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
48 yield pkg
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 186
diff changeset
49
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
50 def build(self, srcs, imps=[]):
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 226
diff changeset
51 """ Create IR-code from sources """
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
52 self.ok = True
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
53 for pkg in self.checkSource(srcs, imps):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
54 # Only return ircode when everything is OK
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
55 if self.ok:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
56 yield self.cg.gencode(pkg)