annotate python/ppci/c3/builder.py @ 306:b145f8e6050b

Start on c3 rewrite
author Windel Bouwman
date Mon, 09 Dec 2013 19:00:21 +0100
parents 0615b5308710
children e609d5296ee9
rev   line source
254
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
1 import logging
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
2 from .lexer import Lexer
300
Windel Bouwman
parents: 295
diff changeset
3 from .parser import Parser
Windel Bouwman
parents: 295
diff changeset
4 from .analyse import TypeChecker, Analyzer
Windel Bouwman
parents: 295
diff changeset
5 from .codegenerator import CodeGenerator
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
6 from .analyse import AddScope
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
7 from .scope import createTopScope
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
8 from .visitor import AstPrinter
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
9
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
10
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
11 class Builder:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
12 """
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 168
diff changeset
13 Generates IR-code from c3 source.
300
Windel Bouwman
parents: 295
diff changeset
14 Reports errors to the diagnostics system.
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 168
diff changeset
15 """
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
16 def __init__(self, diag, target):
254
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
17 self.logger = logging.getLogger('c3')
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
18 self.diag = diag
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
19 self.lexer = Lexer(diag)
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
20 self.parser = Parser(diag)
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
21 self.tc = TypeChecker(diag)
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
22 self.al = Analyzer(diag)
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
23 self.cg = CodeGenerator()
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
24 self.topScope = createTopScope(target) # Scope with built in types
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 186
diff changeset
25
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
26 def build(self, srcs, imps=[]):
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 226
diff changeset
27 """ Create IR-code from sources """
305
0615b5308710 Updated docs
Windel Bouwman
parents: 301
diff changeset
28 self.logger.info('Building {} source files'.format(len(srcs)))
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
29 iter(srcs) # Check if srcs are iterable
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
30 iter(imps)
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
31 self.ok = True
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
32 self.pkgs = {}
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
33
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
34 def doParse(src):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
35 tokens = self.lexer.lex(src)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
36 return self.parser.parseSource(tokens)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
37 s_pkgs = set(map(doParse, srcs))
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
38 i_pkgs = set(map(doParse, imps))
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
39 all_pkgs = s_pkgs | i_pkgs
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
40 if not all(all_pkgs):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
41 self.ok = False
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
42 return
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
43
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
44 #for pkg in all_pkgs:
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
45 # AstPrinter().printAst(pkg)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
46
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
47 packages = {pkg.name: pkg for pkg in all_pkgs}
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
48 self.pkgs = packages
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
49 # Fix scopes:
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
50 for pkg in all_pkgs:
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
51 AddScope(self.diag, self.topScope).addScope(pkg)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
52 if not all(pkg.ok for pkg in all_pkgs):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
53 self.ok = False
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
54 return
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
55
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
56 for pkg in all_pkgs:
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
57 self.al.analyzePackage(pkg, packages)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
58 if not all(pkg.ok for pkg in all_pkgs):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
59 self.ok = False
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
60 return
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
61
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
62 for pkg in all_pkgs:
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
63 self.tc.checkPackage(pkg)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
64 if not all(pkg.ok for pkg in all_pkgs):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
65 self.ok = False
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
66 return
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
67
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
68 # Only return ircode when everything is OK
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
69 for pkg in all_pkgs & s_pkgs:
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
70 yield self.cg.gencode(pkg)