annotate python/c3/builder.py @ 289:bd2593de3ff8

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