157
|
1 import c3, time, ppci, x86
|
148
|
2
|
|
3 testsrc = """
|
|
4 package test;
|
|
5
|
149
|
6 var u32 c, d;
|
|
7
|
|
8 function void test1()
|
148
|
9 {
|
|
10 var u32 b;
|
|
11 var int a = 10;
|
|
12 b = 20;
|
155
|
13 var int buf;
|
148
|
14 var int i;
|
|
15 i = 2;
|
155
|
16 var int zero = i - 2;
|
148
|
17 if (i > 1)
|
|
18 {
|
157
|
19 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - 1;
|
148
|
20 }
|
155
|
21 else
|
|
22 {
|
|
23 ;;;
|
157
|
24 }
|
155
|
25
|
|
26 t2(2, 3);
|
148
|
27 }
|
|
28
|
149
|
29 function int t2(u32 a, u32 b)
|
148
|
30 {
|
157
|
31 if (a > 0)
|
|
32 {
|
|
33 a = 2 + t2(a - 1);
|
|
34 }
|
|
35
|
148
|
36 return a + b;
|
|
37 }
|
|
38
|
155
|
39 var int hahaa = 23 * 2;
|
150
|
40
|
149
|
41
|
148
|
42 """
|
|
43
|
149
|
44 def printAst(ast, indent=''):
|
|
45 print(indent + str(ast))
|
|
46 if isinstance(ast, c3.astnodes.Package):
|
|
47 for s in ast.scope:
|
|
48 printAst(s, indent + ' ')
|
150
|
49 if isinstance(ast, c3.astnodes.Function):
|
149
|
50 for s in ast.scope:
|
|
51 printAst(s, indent + ' ')
|
148
|
52 for c in ast.getChildren():
|
149
|
53 printAst(c, indent + ' ')
|
148
|
54
|
152
|
55 def c3compile(src, diag):
|
148
|
56 print('[0] source:')
|
155
|
57 #print(src)
|
148
|
58 print('[1] parsing')
|
150
|
59 sema = c3.Semantics(diag)
|
|
60 p = c3.Parser(sema, diag)
|
|
61 tc = c3.TypeChecker(diag)
|
|
62 al = c3.Analyzer(diag)
|
151
|
63 cg = c3.CodeGenerator()
|
157
|
64 x86gen = x86.X86CodeGen(diag)
|
149
|
65 t1 = time.time()
|
151
|
66 p.parseSource(src)
|
149
|
67 t2 = time.time()
|
|
68 print('parsetime: {0} [s]'.format(t2 - t1))
|
150
|
69 t2 = time.time()
|
|
70 tc.checkPackage(sema.mod)
|
|
71 t3 = time.time()
|
|
72 print('checktime: {0} [s]'.format(t3 - t2))
|
|
73 print('{0} errors'.format(len(diag.diags)))
|
148
|
74
|
|
75 for d in diag.diags:
|
150
|
76 print('ERROR:')
|
152
|
77 ppci.printError(testsrc, d)
|
148
|
78 print('[2] ast:')
|
155
|
79 #printAst(sema.mod)
|
148
|
80
|
151
|
81 ok = len(diag.diags) == 0
|
|
82 if ok:
|
155
|
83 print('Generating code')
|
|
84 i = cg.gencode(sema.mod)
|
157
|
85
|
155
|
86 print(i)
|
|
87 print(i.Globals)
|
157
|
88 x86gen.genBin(i)
|
151
|
89 else:
|
|
90 print('Not generating code')
|
|
91
|
|
92 def do():
|
152
|
93 diag = ppci.DiagnosticsManager()
|
|
94 c3compile(testsrc, diag)
|
151
|
95
|
148
|
96 do()
|
|
97
|