annotate python/testc3.py @ 213:003c8a976fff

Merge of semantics and parser again ..
author Windel Bouwman
date Fri, 05 Jul 2013 11:18:48 +0200
parents d77cb5962cc5
children c1ccb1cb4cef
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
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
3 import unittest
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
4
162
d8c735dc31f9 Used new editor in ide
Windel Bouwman
parents: 160
diff changeset
5 testsrc = """package test;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
6
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
7 var u32 c, d;
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
8 var double e;
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
9 var int f;
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
10
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
11 const int A = 1337;
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
12
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
13 function void test1()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
14 {
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
15 var u32 bdd;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
16 var int a = 10;
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
17 bd = 20;
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
18 var int buf;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
19 var int i;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
20 i = 2;
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
21 var int zero = i - 2;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
22 if (i > 1)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
23 {
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
24 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - 1;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
25 }
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
26 else
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
27 {
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
28 ;;;
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
29 }
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
30
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
31 t2(2, 3);
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
32 }
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
33
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
34 function int t2(u32 a, u32 b)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
35 {
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
36 if (a > 0)
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
37 {
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
38 a = 2 + t2(a - 1, 1.0);
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
39 }
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
40
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
41 return a + b;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
42 }
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
43
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
44 function int t3(int aap, int blah)
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
45 {
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
46 if (a > blah and blah < 45 + 33 or 33 > aap or 6 and true)
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
47 {
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
48 a = 2 + t2(a - 1);
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
49 }
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
50
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
51 return a + b;
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
52 }
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
53
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
54 var int hahaa = 23 * 2;
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
55
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
56
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
57 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
58
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
59 class testLexer(unittest.TestCase):
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
60 def testUnexpectedCharacter(self):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
61 snippet = """ var s \u6c34 """
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
62 with self.assertRaises(ppci.CompilerError):
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
63 list(c3.lexer.tokenize(snippet))
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
64
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
65 def testBlockComment(self):
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
66 snippet = """
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
67 /* Demo */
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
68 var int x = 0;
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
69 """
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
70 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END']
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
71 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
72
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
73 def testBlockCommentMultiLine(self):
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
74 snippet = """
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
75 /* Demo
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
76 bla1
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
77 bla2
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
78 */
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
79 var int x = 0;
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
80 """
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
81 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END']
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
82 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks)
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
83
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
84 class testBuilder(unittest.TestCase):
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
85 def setUp(self):
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
86 self.diag = ppci.DiagnosticsManager()
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
87 self.builder = c3.Builder(self.diag)
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
88 def testSrc(self):
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
89 self.builder.build(testsrc)
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
90 def testFunctArgs(self):
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
91 snippet = """
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
92 package testargs;
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
93 function void t2(int a, double b)
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
94 {
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
95 t2(2, 2);
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
96 t2(2);
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
97 t2(1, 1.2);
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
98 }
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
99 """
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
100 self.diag.clear()
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
101 ir = self.builder.build(snippet)
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 167
diff changeset
102 assert len(self.diag.diags) == 2
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
103 self.assertEqual(5, self.diag.diags[0].loc.row)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
104 self.assertEqual(6, self.diag.diags[1].loc.row)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
105
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
106 def testExpressions(self):
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
107 snippet = """
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
108 package test;
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
109 function void t(int a, double b)
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
110 {
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
111 var int a2;
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
112 var bool c;
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
113
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
114 a2 = b * a;
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
115 c = a;
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
116 c = b > 1;
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
117 }
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
118 """
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
119 self.diag.clear()
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
120 ircode = self.builder.build(snippet)
187
bf5ab358f43a Renamed readme and added status badge
Windel Bouwman
parents: 186
diff changeset
121 self.assertEqual(len(self.diag.diags), 3)
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 182
diff changeset
122 self.assertEqual(self.diag.diags[0].loc.row, 8)
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 182
diff changeset
123 self.assertEqual(self.diag.diags[1].loc.row, 9)
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 182
diff changeset
124 self.assertEqual(self.diag.diags[2].loc.row, 10)
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
125 self.assertFalse(ircode)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
126
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
127 def testEmpty(self):
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
128 snippet = """
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
129 package A
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
130 """
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
131 ircode = self.builder.build(snippet)
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
132 self.assertFalse(ircode)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
133
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
134 def testEmpty2(self):
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
135 snippet = ""
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
136 self.diag.clear()
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
137 ircode = self.builder.build(snippet)
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
138 self.assertFalse(ircode)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
139
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
140 def testRedefine(self):
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
141 snippet = """
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
142 package test;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
143 var int a;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
144 var int b;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
145 var int a;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
146 """
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
147 self.diag.clear()
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
148 ircode = self.builder.build(snippet)
b01429a5d695 Fixed test
Windel Bouwman
parents: 191
diff changeset
149 self.assertFalse(ircode)
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 182
diff changeset
150 self.assertEqual(len(self.diag.diags), 1)
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 182
diff changeset
151 self.assertEqual(self.diag.diags[0].loc.row, 5)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
152
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
153 def testWhile(self):
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
154 snippet = """
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
155 package tstwhile;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
156 var int a;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
157 function void t()
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
158 {
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
159 var int i = 0;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
160 while (i < 1054)
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
161 {
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
162 i = i + 3;
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 182
diff changeset
163 a = a + i;
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
164 }
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
165
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
166 while(true)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
167 {
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
168 }
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
169
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
170 while(false)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
171 {
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
172 }
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
173 }
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
174 """
187
bf5ab358f43a Renamed readme and added status badge
Windel Bouwman
parents: 186
diff changeset
175 ircode = self.builder.build(snippet)
bf5ab358f43a Renamed readme and added status badge
Windel Bouwman
parents: 186
diff changeset
176 if not ircode:
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 182
diff changeset
177 self.diag.printErrors(snippet)
187
bf5ab358f43a Renamed readme and added status badge
Windel Bouwman
parents: 186
diff changeset
178 self.assertTrue(ircode)
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
179 def testIf(self):
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
180 snippet = """
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
181 package tstIFF;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
182 function void t(int b)
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
183 {
180
25a0753da4cf Re-organized files
Windel Bouwman
parents: 170
diff changeset
184 var int a;
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
185 a = 2;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
186 if (a > b)
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
187 {
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
188 if (a > 1337)
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
189 {
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
190 b = 2;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
191 }
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
192 }
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
193 else
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
194 {
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
195 b = 1;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
196 }
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
197
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
198 return b;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
199 }
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
200 """
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 187
diff changeset
201 ircode = self.builder.build(snippet)
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 187
diff changeset
202 if not ircode:
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 187
diff changeset
203 self.diag.printErrors(snippet)
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 187
diff changeset
204 self.assertTrue(ircode)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
205
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
206 @unittest.skip
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
207 def testPointerType(self):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
208 snippet = """
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
209 package testpointer;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
210 var int* pa;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
211 function void t(int a, double b)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
212 {
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
213 *pa = 22;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
214 }
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
215 """
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
216 self.diag.clear()
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
217 ircode = self.builder.build(snippet)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
218 if not ircode:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
219 self.diag.printErrors(snippet)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
220 self.assertTrue(ircode)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
221
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
222 @unittest.skip
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
223 def testComplexType(self):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
224 snippet = """
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
225 package testpointer;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
226 type int my_int;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
227
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
228 type struct {
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
229 int x, y;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
230 } point;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
231
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
232 type struct {
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
233 int mem1;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
234 int memb2;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
235 point P1;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
236 } my_struct;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
237
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
238 type my_struct* my_sptr;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
239
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
240 function void t(int a, double b, my_sptr x)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
241 {
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
242 var my_struct *msp;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
243
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
244 msp = x;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
245 *pa = 22;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
246 x->memb2 = *pa + a * b;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
247
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
248 mxp->P1.x = a * x->P1.y;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
249 }
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
250 """
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
251 self.diag.clear()
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
252 ircode = self.builder.build(snippet)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
253 if not ircode:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
254 self.diag.printErrors(snippet)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
255 self.assertTrue(ircode)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
256
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 169
diff changeset
257 def test2(self):
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
258 # testsrc2 is valid code:
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
259 testsrc2 = """
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
260 package test2;
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
261
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
262 function void tst()
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
263 {
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
264 var int a, b;
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
265 a = 2 * 33 - 12;
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
266 b = a * 2 + 13;
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
267 a = b + a;
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
268 if (a > b and b == 3)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
269 {
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
270 var int x = a;
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
271 x = b * 2 - a;
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
272 a = x*x;
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
273 }
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
274 else
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
275 {
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
276 a = b + a;
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
277 }
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
278 }
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
279
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
280 """
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
281 self.diag.clear()
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
282 ir = self.builder.build(testsrc2)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
283 self.assertTrue(ir)
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
284
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
285 if __name__ == '__main__':
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
286 unittest.main()
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
287
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
288