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