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;
|
150
|
13 var int88 buf;
|
148
|
14 var int i;
|
|
15 i = 2;
|
150
|
16 zero = i - 2;
|
148
|
17 if (i > 1)
|
|
18 {
|
150
|
19 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - one
|
148
|
20 }
|
|
21 }
|
|
22
|
149
|
23 function int t2(u32 a, u32 b)
|
148
|
24 {
|
|
25 return a + b;
|
|
26 a = 2
|
|
27 }
|
|
28
|
150
|
29 var u8 hahaa = 23 * 2;
|
|
30
|
149
|
31 function int t2(u32 a, u32 b)
|
|
32 {
|
|
33 return a + b;
|
150
|
34 a = 2 + 33 * 1 - 3;
|
149
|
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 + ' ')
|
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:')
|
151
|
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:')
|
|
74 printAst(sema.mod)
|
|
75
|
151
|
76 ok = len(diag.diags) == 0
|
|
77 if ok:
|
|
78 cg.gencode(sema.mod)
|
|
79 else:
|
|
80 print('Not generating code')
|
|
81
|
|
82 def do():
|
152
|
83 diag = ppci.DiagnosticsManager()
|
|
84 c3compile(testsrc, diag)
|
151
|
85
|
148
|
86 do()
|
|
87
|