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