Mercurial > lcfOS
annotate python/ppci/c3/builder.py @ 307:e609d5296ee9
Massive rewrite of codegenerator
author | Windel Bouwman |
---|---|
date | Thu, 12 Dec 2013 20:42:56 +0100 |
parents | b145f8e6050b |
children | 2e7f55319858 |
rev | line source |
---|---|
254 | 1 import logging |
306 | 2 from .lexer import Lexer |
300 | 3 from .parser import Parser |
4 from .codegenerator import CodeGenerator | |
307 | 5 from .analyse import ScopeFiller |
306 | 6 from .scope import createTopScope |
7 from .visitor import AstPrinter | |
165 | 8 |
287 | 9 |
165 | 10 class Builder: |
288 | 11 """ |
186 | 12 Generates IR-code from c3 source. |
300 | 13 Reports errors to the diagnostics system. |
186 | 14 """ |
301 | 15 def __init__(self, diag, target): |
254 | 16 self.logger = logging.getLogger('c3') |
226 | 17 self.diag = diag |
306 | 18 self.lexer = Lexer(diag) |
226 | 19 self.parser = Parser(diag) |
307 | 20 self.cg = CodeGenerator(diag) |
306 | 21 self.topScope = createTopScope(target) # Scope with built in types |
194 | 22 |
287 | 23 def build(self, srcs, imps=[]): |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
226
diff
changeset
|
24 """ Create IR-code from sources """ |
305 | 25 self.logger.info('Building {} source files'.format(len(srcs))) |
306 | 26 iter(srcs) # Check if srcs are iterable |
27 iter(imps) | |
288 | 28 self.ok = True |
306 | 29 self.pkgs = {} |
30 | |
307 | 31 # Parsing stage (phase 1) |
306 | 32 def doParse(src): |
33 tokens = self.lexer.lex(src) | |
34 return self.parser.parseSource(tokens) | |
35 s_pkgs = set(map(doParse, srcs)) | |
36 i_pkgs = set(map(doParse, imps)) | |
37 all_pkgs = s_pkgs | i_pkgs | |
38 if not all(all_pkgs): | |
39 self.ok = False | |
40 return | |
41 | |
307 | 42 # Fix scopes and package refs (phase 1.5) |
306 | 43 packages = {pkg.name: pkg for pkg in all_pkgs} |
44 self.pkgs = packages | |
307 | 45 |
46 scopeFiller = ScopeFiller(self.diag, self.topScope, packages) | |
306 | 47 # Fix scopes: |
48 for pkg in all_pkgs: | |
307 | 49 scopeFiller.addScope(pkg) |
306 | 50 if not all(pkg.ok for pkg in all_pkgs): |
51 self.ok = False | |
52 return | |
53 | |
307 | 54 # Generate intermediate code (phase 2) |
55 # Only return ircode when everything is OK | |
56 for pkg in all_pkgs & s_pkgs: | |
57 yield self.cg.gencode(pkg) | |
306 | 58 if not all(pkg.ok for pkg in all_pkgs): |
59 self.ok = False | |
60 return |