annotate python/c3/builder.py @ 165:598d3888a11c

Added front class and fided AST view
author Windel Bouwman
date Fri, 22 Mar 2013 15:12:38 +0100
parents
children da0087b82fbe
rev   line source
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
1 import ppci
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
2 from . import Parser, Semantics, TypeChecker, Analyzer, CodeGenerator, AstPrinter
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
3
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
4 class Builder:
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
5 def __init__(self, diag):
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
6 self.diag = diag
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
7 self.sema = Semantics(diag)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
8 self.p = Parser(self.sema, diag)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
9 self.tc = TypeChecker(diag)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
10 self.al = Analyzer(diag)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
11 self.cg = CodeGenerator()
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
12 self.ap = AstPrinter()
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
13 def build(self, src):
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
14 # Store src for later:
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
15 self.src = src
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
16 print('[0] source:')
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
17 print(src)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
18 print('[1] parsing')
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
19 self.p.parseSource(src)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
20 ok = len(self.diag.diags) == 0
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
21 if not ok:
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
22 return
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
23 pkg = self.sema.mod
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
24 self.pkg = pkg
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
25 self.al.analyzePackage(pkg)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
26 self.tc.checkPackage(pkg)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
27 print('{0} errors'.format(len(self.diag.diags)))
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
28
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
29 for d in self.diag.diags:
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
30 print('ERROR:')
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
31 ppci.printError(src, d)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
32 print('[2] ast:')
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
33 self.ap.printAst(pkg)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
34
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
35 #printAst(sema.mod)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
36
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
37 ok = len(self.diag.diags) == 0
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
38 if ok:
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
39 print('Generating ir-code')
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
40 i = cg.gencode(pkg)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
41 return i
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
42