148
|
1 import c3.parser, c3.semantics
|
|
2 from ppci.errors import printError, Diagnostics
|
149
|
3 import time
|
148
|
4
|
|
5 testsrc = """
|
|
6 package test;
|
|
7
|
149
|
8 var u32 a ;
|
|
9 var u32 c, d;
|
|
10
|
|
11 function void test1()
|
148
|
12 {
|
|
13 var u32 b;
|
|
14 var int a = 10;
|
|
15 b = 20;
|
|
16 var int buf;
|
|
17 var int i;
|
|
18 i = 2;
|
|
19 if (i > 1)
|
|
20 {
|
|
21 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44
|
|
22 }
|
|
23 }
|
|
24
|
149
|
25 function int t2(u32 a, u32 b)
|
148
|
26 {
|
|
27 return a + b;
|
|
28 a = 2
|
|
29 }
|
|
30
|
149
|
31 function int t2(u32 a, u32 b)
|
|
32 {
|
|
33 return a + b;
|
|
34 a = 2 + 33 - 1;
|
|
35 a = a - 2
|
|
36 }
|
|
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 + ' ')
|
|
45 if isinstance(ast, c3.astnodes.Procedure):
|
|
46 for s in ast.scope:
|
|
47 printAst(s, indent + ' ')
|
148
|
48 for c in ast.getChildren():
|
149
|
49 printAst(c, indent + ' ')
|
148
|
50
|
|
51 def do():
|
|
52 print('[0] source:')
|
|
53 print(testsrc)
|
|
54 print('[1] parsing')
|
|
55 diag = Diagnostics()
|
|
56 sema = c3.semantics.Semantics(diag)
|
|
57 p = c3.parser.Parser(sema, diag)
|
149
|
58 t1 = time.time()
|
148
|
59 p.parseSource(testsrc)
|
149
|
60 t2 = time.time()
|
|
61 print('parsetime: {0} [s]'.format(t2 - t1))
|
148
|
62
|
|
63 for d in diag.diags:
|
|
64 print('ERROR:', d)
|
|
65 printError(testsrc, d)
|
|
66 print('[2] ast:')
|
|
67 printAst(sema.mod)
|
|
68
|
|
69 do()
|
|
70
|