annotate python/testc3.py @ 150:4ae0e02599de

Added type check start and analyze phase
author Windel Bouwman
date Fri, 01 Mar 2013 16:53:22 +0100
parents 74241ca312cc
children afc8c0207984
rev   line source
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
1 import c3
148
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 c, d;
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
9
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
10 function void test1()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
11 {
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
12 var u32 b;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
13 var int a = 10;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
14 b = 20;
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
15 var int88 buf;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
16 var int i;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
17 i = 2;
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
18 zero = i - 2;
148
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 {
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
21 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - one
148
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
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
31 var u8 hahaa = 23 * 2;
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
32
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
33 function int t2(u32 a, u32 b)
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
34 {
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
35 return a + b;
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
36 a = 2 + 33 * 1 - 3;
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
37 a = a - 2
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
38 }
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
39
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
40 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
41
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
42 def printAst(ast, indent=''):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
43 print(indent + str(ast))
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
44 if isinstance(ast, c3.astnodes.Package):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
45 for s in ast.scope:
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
46 printAst(s, indent + ' ')
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
47 if isinstance(ast, c3.astnodes.Function):
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
48 for s in ast.scope:
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
49 printAst(s, indent + ' ')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
50 for c in ast.getChildren():
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
51 printAst(c, indent + ' ')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
52
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
53 def do():
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
54 print('[0] source:')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
55 print(testsrc)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
56 print('[1] parsing')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
57 diag = Diagnostics()
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
58 sema = c3.Semantics(diag)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
59 p = c3.Parser(sema, diag)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
60 tc = c3.TypeChecker(diag)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
61 al = c3.Analyzer(diag)
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
62 t1 = time.time()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
63 p.parseSource(testsrc)
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
64 t2 = time.time()
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
65 print('parsetime: {0} [s]'.format(t2 - t1))
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
66 t2 = time.time()
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
67 tc.checkPackage(sema.mod)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
68 t3 = time.time()
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
69 print('checktime: {0} [s]'.format(t3 - t2))
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
70 print('{0} errors'.format(len(diag.diags)))
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
71
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
72 for d in diag.diags:
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
73 print('ERROR:')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
74 printError(testsrc, d)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
75 print('[2] ast:')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
76 printAst(sema.mod)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
77
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
78 do()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
79