Mercurial > lcfOS
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 | 1 import c3 |
2 import time, ppci, x86, ir | |
167 | 3 import unittest |
148 | 4 |
162 | 5 testsrc = """package test; |
148 | 6 |
149 | 7 var u32 c, d; |
163 | 8 var double e; |
9 var int f; | |
10 | |
167 | 11 const int A = 1337; |
149 | 12 |
13 function void test1() | |
148 | 14 { |
163 | 15 var u32 bdd; |
148 | 16 var int a = 10; |
163 | 17 bd = 20; |
155 | 18 var int buf; |
148 | 19 var int i; |
20 i = 2; | |
155 | 21 var int zero = i - 2; |
148 | 22 if (i > 1) |
23 { | |
157 | 24 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - 1; |
148 | 25 } |
155 | 26 else |
27 { | |
28 ;;; | |
157 | 29 } |
155 | 30 |
31 t2(2, 3); | |
148 | 32 } |
33 | |
149 | 34 function int t2(u32 a, u32 b) |
148 | 35 { |
157 | 36 if (a > 0) |
37 { | |
167 | 38 a = 2 + t2(a - 1, 1.0); |
157 | 39 } |
40 | |
148 | 41 return a + b; |
42 } | |
43 | |
166 | 44 function int t3(int aap, int blah) |
45 { | |
46 if (a > blah and blah < 45 + 33 or 33 > aap or 6 and true) | |
47 { | |
48 a = 2 + t2(a - 1); | |
49 } | |
50 | |
51 return a + b; | |
52 } | |
53 | |
155 | 54 var int hahaa = 23 * 2; |
150 | 55 |
149 | 56 |
148 | 57 """ |
58 | |
204 | 59 class testLexer(unittest.TestCase): |
205 | 60 def testUnexpectedCharacter(self): |
61 snippet = """ var s \u6c34 """ | |
62 with self.assertRaises(ppci.CompilerError): | |
63 list(c3.lexer.tokenize(snippet)) | |
64 | |
204 | 65 def testBlockComment(self): |
66 snippet = """ | |
67 /* Demo */ | |
68 var int x = 0; | |
69 """ | |
70 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] | |
71 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks) | |
205 | 72 |
204 | 73 def testBlockCommentMultiLine(self): |
74 snippet = """ | |
75 /* Demo | |
76 bla1 | |
77 bla2 | |
78 */ | |
79 var int x = 0; | |
80 """ | |
81 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] | |
82 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks) | |
83 | |
84 class testBuilder(unittest.TestCase): | |
167 | 85 def setUp(self): |
86 self.diag = ppci.DiagnosticsManager() | |
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 | 90 def testFunctArgs(self): |
91 snippet = """ | |
92 package testargs; | |
93 function void t2(int a, double b) | |
94 { | |
95 t2(2, 2); | |
96 t2(2); | |
97 t2(1, 1.2); | |
98 } | |
99 """ | |
100 self.diag.clear() | |
101 ir = self.builder.build(snippet) | |
168 | 102 assert len(self.diag.diags) == 2 |
205 | 103 self.assertEqual(5, self.diag.diags[0].loc.row) |
104 self.assertEqual(6, self.diag.diags[1].loc.row) | |
148 | 105 |
167 | 106 def testExpressions(self): |
107 snippet = """ | |
108 package test; | |
109 function void t(int a, double b) | |
110 { | |
111 var int a2; | |
112 var bool c; | |
113 | |
114 a2 = b * a; | |
115 c = a; | |
116 c = b > 1; | |
117 } | |
118 """ | |
119 self.diag.clear() | |
194 | 120 ircode = self.builder.build(snippet) |
187 | 121 self.assertEqual(len(self.diag.diags), 3) |
186 | 122 self.assertEqual(self.diag.diags[0].loc.row, 8) |
123 self.assertEqual(self.diag.diags[1].loc.row, 9) | |
124 self.assertEqual(self.diag.diags[2].loc.row, 10) | |
194 | 125 self.assertFalse(ircode) |
205 | 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 | 131 ircode = self.builder.build(snippet) |
132 self.assertFalse(ircode) | |
205 | 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 | 136 self.diag.clear() |
137 ircode = self.builder.build(snippet) | |
138 self.assertFalse(ircode) | |
205 | 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 | 148 ircode = self.builder.build(snippet) |
149 self.assertFalse(ircode) | |
186 | 150 self.assertEqual(len(self.diag.diags), 1) |
151 self.assertEqual(self.diag.diags[0].loc.row, 5) | |
205 | 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 | 163 a = a + i; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
164 } |
205 | 165 |
166 while(true) | |
167 { | |
168 } | |
169 | |
170 while(false) | |
171 { | |
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 | 175 ircode = self.builder.build(snippet) |
176 if not ircode: | |
186 | 177 self.diag.printErrors(snippet) |
187 | 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 | 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 | 201 ircode = self.builder.build(snippet) |
202 if not ircode: | |
203 self.diag.printErrors(snippet) | |
204 self.assertTrue(ircode) | |
213 | 205 |
206 @unittest.skip | |
207 def testPointerType(self): | |
208 snippet = """ | |
209 package testpointer; | |
210 var int* pa; | |
211 function void t(int a, double b) | |
212 { | |
213 *pa = 22; | |
214 } | |
215 """ | |
216 self.diag.clear() | |
217 ircode = self.builder.build(snippet) | |
218 if not ircode: | |
219 self.diag.printErrors(snippet) | |
220 self.assertTrue(ircode) | |
221 | |
222 @unittest.skip | |
223 def testComplexType(self): | |
224 snippet = """ | |
225 package testpointer; | |
226 type int my_int; | |
227 | |
228 type struct { | |
229 int x, y; | |
230 } point; | |
231 | |
232 type struct { | |
233 int mem1; | |
234 int memb2; | |
235 point P1; | |
236 } my_struct; | |
237 | |
238 type my_struct* my_sptr; | |
239 | |
240 function void t(int a, double b, my_sptr x) | |
241 { | |
242 var my_struct *msp; | |
243 | |
244 msp = x; | |
245 *pa = 22; | |
246 x->memb2 = *pa + a * b; | |
247 | |
248 mxp->P1.x = a * x->P1.y; | |
249 } | |
250 """ | |
251 self.diag.clear() | |
252 ircode = self.builder.build(snippet) | |
253 if not ircode: | |
254 self.diag.printErrors(snippet) | |
255 self.assertTrue(ircode) | |
205 | 256 |
170 | 257 def test2(self): |
205 | 258 # testsrc2 is valid code: |
259 testsrc2 = """ | |
260 package test2; | |
261 | |
262 function void tst() | |
263 { | |
264 var int a, b; | |
265 a = 2 * 33 - 12; | |
266 b = a * 2 + 13; | |
267 a = b + a; | |
268 if (a > b and b == 3) | |
269 { | |
270 var int x = a; | |
271 x = b * 2 - a; | |
272 a = x*x; | |
273 } | |
274 else | |
275 { | |
276 a = b + a; | |
277 } | |
278 } | |
279 | |
280 """ | |
281 self.diag.clear() | |
282 ir = self.builder.build(testsrc2) | |
283 self.assertTrue(ir) | |
167 | 284 |
285 if __name__ == '__main__': | |
286 unittest.main() | |
287 | |
288 |