annotate 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
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
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
3
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
4 testsrc = """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
5 package test;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
6
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
7 public function void test1()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
8 {
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
9 var u32 b;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
10 var int a = 10;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
11 b = 20;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
12 var int buf;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
13 var int i;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
14 i = 2;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
15 if (i > 1)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
16 {
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
17 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
18 }
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
19 }
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 public function int t2(u32 a, u32 b)
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 return a + b;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
24 a = 2
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
25 }
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 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
28
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
29 def printAst(ast):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
30 print(ast)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
31 for c in ast.getChildren():
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
32 printAst(c)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
33 if isinstance(ast, c3.astnodes.Package):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
34 print('PACK', ast.scope)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
35
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
36 def do():
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
37 print('[0] source:')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
38 print(testsrc)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
39 print('[1] parsing')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
40 diag = Diagnostics()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
41 sema = c3.semantics.Semantics(diag)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
42 p = c3.parser.Parser(sema, diag)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
43
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
44 p.parseSource(testsrc)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
45
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
46 for d in diag.diags:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
47 print('ERROR:', d)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
48 printError(testsrc, d)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
49 print('[2] ast:')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
50 printAst(sema.mod)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
51
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
52 do()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
53