annotate python/testc3.py @ 166:da0087b82fbe

Improved type checking
author Windel Bouwman
date Fri, 22 Mar 2013 16:15:31 +0100
parents 598d3888a11c
children 0b5b2ee6b435
rev   line source
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
1 import c3
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
2 import time, ppci, x86, ir
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
3
162
d8c735dc31f9 Used new editor in ide
Windel Bouwman
parents: 160
diff changeset
4 testsrc = """package test;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
5
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
6 var u32 c, d;
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
7 var double e;
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
8 var int f;
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
9
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
10 const int A = 1337.;
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
11
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
12 function void test1()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
13 {
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
14 var u32 bdd;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
15 var int a = 10;
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
16 bd = 20;
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
17 var int buf;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
18 var int i;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
19 i = 2;
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
20 var int zero = i - 2;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
21 if (i > 1)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
22 {
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
23 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - 1;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
24 }
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
25 else
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
26 {
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
27 ;;;
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
28 }
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
29
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
30 t2(2, 3);
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
31 }
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
32
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
33 function int t2(u32 a, u32 b)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
34 {
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
35 if (a > 0)
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
36 {
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
37 a = 2 + t2(a - 1);
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
38 }
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
39
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
40 return a + b;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
41 }
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
42
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
43 function int t3(int aap, int blah)
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
44 {
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
45 if (a > blah and blah < 45 + 33 or 33 > aap or 6 and true)
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
46 {
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
47 a = 2 + t2(a - 1);
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
48 }
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
49
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
50 return a + b;
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
51 }
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
52
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
53 var int hahaa = 23 * 2;
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
54
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
55
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
56 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
57
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 151
diff changeset
58 def c3compile(src, diag):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
59 # Structures:
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 163
diff changeset
60 builder = c3.Builder(diag)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 163
diff changeset
61 ir = builder.build(src)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 163
diff changeset
62 # optional optimize here
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
63 x86gen = x86.X86CodeGen(diag)
162
d8c735dc31f9 Used new editor in ide
Windel Bouwman
parents: 160
diff changeset
64 ok = len(diag.diags) == 0
d8c735dc31f9 Used new editor in ide
Windel Bouwman
parents: 160
diff changeset
65 if not ok:
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 163
diff changeset
66 print('Not generating code')
162
d8c735dc31f9 Used new editor in ide
Windel Bouwman
parents: 160
diff changeset
67 return
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 163
diff changeset
68 print('generating x86 code')
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 163
diff changeset
69 x86gen.genBin(ir)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 163
diff changeset
70 with open('dummydummy.asm', 'w') as f:
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 163
diff changeset
71 f.write('bits 64\n')
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 163
diff changeset
72 for a in x86gen.asm:
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 163
diff changeset
73 print(a)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 163
diff changeset
74 f.write(str(a) + '\n')
151
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents: 150
diff changeset
75
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents: 150
diff changeset
76 def do():
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 151
diff changeset
77 diag = ppci.DiagnosticsManager()
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 151
diff changeset
78 c3compile(testsrc, diag)
151
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents: 150
diff changeset
79
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
80 do()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
81