annotate python/testc3.py @ 149:74241ca312cc

Fixes on parser and semantics
author Windel Bouwman
date Fri, 01 Mar 2013 11:43:52 +0100
parents e5263f74b287
children 4ae0e02599de
rev   line source
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
1 import c3.parser, c3.semantics
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
2 from ppci.errors import printError, Diagnostics
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
3 import time
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
4
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
5 testsrc = """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
6 package test;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
7
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
8 var u32 a ;
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
9 var u32 c, d;
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
10
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
11 function void test1()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
12 {
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
13 var u32 b;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
14 var int a = 10;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
15 b = 20;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
16 var int buf;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
17 var int i;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
18 i = 2;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
19 if (i > 1)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
20 {
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
21 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
22 }
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
23 }
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
24
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
25 function int t2(u32 a, u32 b)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
26 {
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
27 return a + b;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
28 a = 2
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
29 }
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
30
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
31 function int t2(u32 a, u32 b)
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
32 {
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
33 return a + b;
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
34 a = 2 + 33 - 1;
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
35 a = a - 2
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
36 }
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
37
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
38 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
39
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
40 def printAst(ast, indent=''):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
41 print(indent + str(ast))
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
42 if isinstance(ast, c3.astnodes.Package):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
43 for s in ast.scope:
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
44 printAst(s, indent + ' ')
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
45 if isinstance(ast, c3.astnodes.Procedure):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
46 for s in ast.scope:
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
47 printAst(s, indent + ' ')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
48 for c in ast.getChildren():
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
49 printAst(c, indent + ' ')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
50
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
51 def do():
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
52 print('[0] source:')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
53 print(testsrc)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
54 print('[1] parsing')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
55 diag = Diagnostics()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
56 sema = c3.semantics.Semantics(diag)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
57 p = c3.parser.Parser(sema, diag)
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
58 t1 = time.time()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
59 p.parseSource(testsrc)
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
60 t2 = time.time()
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
61 print('parsetime: {0} [s]'.format(t2 - t1))
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
62
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
63 for d in diag.diags:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
64 print('ERROR:', d)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
65 printError(testsrc, d)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
66 print('[2] ast:')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
67 printAst(sema.mod)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
68
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
69 do()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
70