comparison 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
comparison
equal deleted inserted replaced
149:74241ca312cc 150:4ae0e02599de
1 import c3.parser, c3.semantics 1 import c3
2 from ppci.errors import printError, Diagnostics 2 from ppci.errors import printError, Diagnostics
3 import time 3 import time
4 4
5 testsrc = """ 5 testsrc = """
6 package test; 6 package test;
7 7
8 var u32 a ;
9 var u32 c, d; 8 var u32 c, d;
10 9
11 function void test1() 10 function void test1()
12 { 11 {
13 var u32 b; 12 var u32 b;
14 var int a = 10; 13 var int a = 10;
15 b = 20; 14 b = 20;
16 var int buf; 15 var int88 buf;
17 var int i; 16 var int i;
18 i = 2; 17 i = 2;
18 zero = i - 2;
19 if (i > 1) 19 if (i > 1)
20 { 20 {
21 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 21 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - one
22 } 22 }
23 } 23 }
24 24
25 function int t2(u32 a, u32 b) 25 function int t2(u32 a, u32 b)
26 { 26 {
27 return a + b; 27 return a + b;
28 a = 2 28 a = 2
29 } 29 }
30 30
31 var u8 hahaa = 23 * 2;
32
31 function int t2(u32 a, u32 b) 33 function int t2(u32 a, u32 b)
32 { 34 {
33 return a + b; 35 return a + b;
34 a = 2 + 33 - 1; 36 a = 2 + 33 * 1 - 3;
35 a = a - 2 37 a = a - 2
36 } 38 }
37 39
38 """ 40 """
39 41
40 def printAst(ast, indent=''): 42 def printAst(ast, indent=''):
41 print(indent + str(ast)) 43 print(indent + str(ast))
42 if isinstance(ast, c3.astnodes.Package): 44 if isinstance(ast, c3.astnodes.Package):
43 for s in ast.scope: 45 for s in ast.scope:
44 printAst(s, indent + ' ') 46 printAst(s, indent + ' ')
45 if isinstance(ast, c3.astnodes.Procedure): 47 if isinstance(ast, c3.astnodes.Function):
46 for s in ast.scope: 48 for s in ast.scope:
47 printAst(s, indent + ' ') 49 printAst(s, indent + ' ')
48 for c in ast.getChildren(): 50 for c in ast.getChildren():
49 printAst(c, indent + ' ') 51 printAst(c, indent + ' ')
50 52
51 def do(): 53 def do():
52 print('[0] source:') 54 print('[0] source:')
53 print(testsrc) 55 print(testsrc)
54 print('[1] parsing') 56 print('[1] parsing')
55 diag = Diagnostics() 57 diag = Diagnostics()
56 sema = c3.semantics.Semantics(diag) 58 sema = c3.Semantics(diag)
57 p = c3.parser.Parser(sema, diag) 59 p = c3.Parser(sema, diag)
60 tc = c3.TypeChecker(diag)
61 al = c3.Analyzer(diag)
58 t1 = time.time() 62 t1 = time.time()
59 p.parseSource(testsrc) 63 p.parseSource(testsrc)
60 t2 = time.time() 64 t2 = time.time()
61 print('parsetime: {0} [s]'.format(t2 - t1)) 65 print('parsetime: {0} [s]'.format(t2 - t1))
66 t2 = time.time()
67 tc.checkPackage(sema.mod)
68 t3 = time.time()
69 print('checktime: {0} [s]'.format(t3 - t2))
70 print('{0} errors'.format(len(diag.diags)))
62 71
63 for d in diag.diags: 72 for d in diag.diags:
64 print('ERROR:', d) 73 print('ERROR:')
65 printError(testsrc, d) 74 printError(testsrc, d)
66 print('[2] ast:') 75 print('[2] ast:')
67 printAst(sema.mod) 76 printAst(sema.mod)
68 77
69 do() 78 do()