Mercurial > lcfOS
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 | 1 import logging |
306 | 2 from .lexer import Lexer |
300 | 3 from .parser import Parser |
4 from .analyse import TypeChecker, Analyzer | |
5 from .codegenerator import CodeGenerator | |
289 | 6 from .analyse import AddScope |
306 | 7 from .scope import createTopScope |
8 from .visitor import AstPrinter | |
165 | 9 |
287 | 10 |
165 | 11 class Builder: |
288 | 12 """ |
186 | 13 Generates IR-code from c3 source. |
300 | 14 Reports errors to the diagnostics system. |
186 | 15 """ |
301 | 16 def __init__(self, diag, target): |
254 | 17 self.logger = logging.getLogger('c3') |
226 | 18 self.diag = diag |
306 | 19 self.lexer = Lexer(diag) |
226 | 20 self.parser = Parser(diag) |
21 self.tc = TypeChecker(diag) | |
22 self.al = Analyzer(diag) | |
23 self.cg = CodeGenerator() | |
306 | 24 self.topScope = createTopScope(target) # Scope with built in types |
194 | 25 |
287 | 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 | 28 self.logger.info('Building {} source files'.format(len(srcs))) |
306 | 29 iter(srcs) # Check if srcs are iterable |
30 iter(imps) | |
288 | 31 self.ok = True |
306 | 32 self.pkgs = {} |
33 | |
34 def doParse(src): | |
35 tokens = self.lexer.lex(src) | |
36 return self.parser.parseSource(tokens) | |
37 s_pkgs = set(map(doParse, srcs)) | |
38 i_pkgs = set(map(doParse, imps)) | |
39 all_pkgs = s_pkgs | i_pkgs | |
40 if not all(all_pkgs): | |
41 self.ok = False | |
42 return | |
43 | |
44 #for pkg in all_pkgs: | |
45 # AstPrinter().printAst(pkg) | |
46 | |
47 packages = {pkg.name: pkg for pkg in all_pkgs} | |
48 self.pkgs = packages | |
49 # Fix scopes: | |
50 for pkg in all_pkgs: | |
51 AddScope(self.diag, self.topScope).addScope(pkg) | |
52 if not all(pkg.ok for pkg in all_pkgs): | |
53 self.ok = False | |
54 return | |
55 | |
56 for pkg in all_pkgs: | |
57 self.al.analyzePackage(pkg, packages) | |
58 if not all(pkg.ok for pkg in all_pkgs): | |
59 self.ok = False | |
60 return | |
61 | |
62 for pkg in all_pkgs: | |
63 self.tc.checkPackage(pkg) | |
64 if not all(pkg.ok for pkg in all_pkgs): | |
65 self.ok = False | |
66 return | |
67 | |
68 # Only return ircode when everything is OK | |
69 for pkg in all_pkgs & s_pkgs: | |
70 yield self.cg.gencode(pkg) |