comparison python/testc3.py @ 148:e5263f74b287

Added c3 language frontend initial parser
author Windel Bouwman
date Fri, 01 Mar 2013 10:24:01 +0100
parents
children 74241ca312cc
comparison
equal deleted inserted replaced
147:4e79484a9d47 148:e5263f74b287
1 import c3.parser, c3.semantics
2 from ppci.errors import printError, Diagnostics
3
4 testsrc = """
5 package test;
6
7 public function void test1()
8 {
9 var u32 b;
10 var int a = 10;
11 b = 20;
12 var int buf;
13 var int i;
14 i = 2;
15 if (i > 1)
16 {
17 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44
18 }
19 }
20
21 public function int t2(u32 a, u32 b)
22 {
23 return a + b;
24 a = 2
25 }
26
27 """
28
29 def printAst(ast):
30 print(ast)
31 for c in ast.getChildren():
32 printAst(c)
33 if isinstance(ast, c3.astnodes.Package):
34 print('PACK', ast.scope)
35
36 def do():
37 print('[0] source:')
38 print(testsrc)
39 print('[1] parsing')
40 diag = Diagnostics()
41 sema = c3.semantics.Semantics(diag)
42 p = c3.parser.Parser(sema, diag)
43
44 p.parseSource(testsrc)
45
46 for d in diag.diags:
47 print('ERROR:', d)
48 printError(testsrc, d)
49 print('[2] ast:')
50 printAst(sema.mod)
51
52 do()
53