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
|
151
|
53 def c3compile(src):
|
148
|
54 print('[0] source:')
|
151
|
55 print(src)
|
148
|
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)
|
151
|
62 cg = c3.CodeGenerator()
|
149
|
63 t1 = time.time()
|
151
|
64 p.parseSource(src)
|
149
|
65 t2 = time.time()
|
|
66 print('parsetime: {0} [s]'.format(t2 - t1))
|
150
|
67 t2 = time.time()
|
|
68 tc.checkPackage(sema.mod)
|
|
69 t3 = time.time()
|
|
70 print('checktime: {0} [s]'.format(t3 - t2))
|
|
71 print('{0} errors'.format(len(diag.diags)))
|
148
|
72
|
|
73 for d in diag.diags:
|
150
|
74 print('ERROR:')
|
148
|
75 printError(testsrc, d)
|
|
76 print('[2] ast:')
|
|
77 printAst(sema.mod)
|
|
78
|
151
|
79 ok = len(diag.diags) == 0
|
|
80 if ok:
|
|
81 cg.gencode(sema.mod)
|
|
82 else:
|
|
83 print('Not generating code')
|
|
84
|
|
85 def do():
|
|
86 c3compile(testsrc)
|
|
87
|
148
|
88 do()
|
|
89
|