150
|
1 import c3
|
148
|
2 from ppci.errors import printError, Diagnostics
|
149
|
3 import time
|
148
|
4
|
|
5 testsrc = """
|
|
6 package test;
|
|
7
|
149
|
8 var u32 c, d;
|
|
9
|
|
10 function void test1()
|
148
|
11 {
|
|
12 var u32 b;
|
|
13 var int a = 10;
|
|
14 b = 20;
|
150
|
15 var int88 buf;
|
148
|
16 var int i;
|
|
17 i = 2;
|
150
|
18 zero = i - 2;
|
148
|
19 if (i > 1)
|
|
20 {
|
150
|
21 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - one
|
148
|
22 }
|
|
23 }
|
|
24
|
149
|
25 function int t2(u32 a, u32 b)
|
148
|
26 {
|
|
27 return a + b;
|
|
28 a = 2
|
|
29 }
|
|
30
|
150
|
31 var u8 hahaa = 23 * 2;
|
|
32
|
149
|
33 function int t2(u32 a, u32 b)
|
|
34 {
|
|
35 return a + b;
|
150
|
36 a = 2 + 33 * 1 - 3;
|
149
|
37 a = a - 2
|
|
38 }
|
|
39
|
148
|
40 """
|
|
41
|
149
|
42 def printAst(ast, indent=''):
|
|
43 print(indent + str(ast))
|
|
44 if isinstance(ast, c3.astnodes.Package):
|
|
45 for s in ast.scope:
|
|
46 printAst(s, indent + ' ')
|
150
|
47 if isinstance(ast, c3.astnodes.Function):
|
149
|
48 for s in ast.scope:
|
|
49 printAst(s, indent + ' ')
|
148
|
50 for c in ast.getChildren():
|
149
|
51 printAst(c, indent + ' ')
|
148
|
52
|
|
53 def do():
|
|
54 print('[0] source:')
|
|
55 print(testsrc)
|
|
56 print('[1] parsing')
|
|
57 diag = Diagnostics()
|
150
|
58 sema = c3.Semantics(diag)
|
|
59 p = c3.Parser(sema, diag)
|
|
60 tc = c3.TypeChecker(diag)
|
|
61 al = c3.Analyzer(diag)
|
149
|
62 t1 = time.time()
|
148
|
63 p.parseSource(testsrc)
|
149
|
64 t2 = time.time()
|
|
65 print('parsetime: {0} [s]'.format(t2 - t1))
|
150
|
66 t2 = time.time()
|
|
67 tc.checkPackage(sema.mod)
|
|
68 t3 = time.time()
|
|
69 print('checktime: {0} [s]'.format(t3 - t2))
|
|
70 print('{0} errors'.format(len(diag.diags)))
|
148
|
71
|
|
72 for d in diag.diags:
|
150
|
73 print('ERROR:')
|
148
|
74 printError(testsrc, d)
|
|
75 print('[2] ast:')
|
|
76 printAst(sema.mod)
|
|
77
|
|
78 do()
|
|
79
|