Mercurial > lcfOS
annotate python/testc3.py @ 211:99164160fb0b
Added another missing file
author | Windel Bouwman |
---|---|
date | Sat, 29 Jun 2013 10:10:45 +0200 |
parents | d77cb5962cc5 |
children | 003c8a976fff |
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 | |
170 | 59 |
152 | 60 def c3compile(src, diag): |
163 | 61 # Structures: |
165 | 62 builder = c3.Builder(diag) |
63 ir = builder.build(src) | |
64 # optional optimize here | |
182
e9b27f7193e3
Replace clear function by function also supported in python 3.2
Windel Bouwman
parents:
180
diff
changeset
|
65 x86gen = x86.X86CodeGenSimple(diag) |
162 | 66 ok = len(diag.diags) == 0 |
67 if not ok: | |
68 return | |
165 | 69 print('generating x86 code') |
70 x86gen.genBin(ir) | |
71 with open('dummydummy.asm', 'w') as f: | |
72 f.write('bits 64\n') | |
73 for a in x86gen.asm: | |
74 print(a) | |
75 f.write(str(a) + '\n') | |
151 | 76 |
77 def do(): | |
152 | 78 diag = ppci.DiagnosticsManager() |
79 c3compile(testsrc, diag) | |
151 | 80 |
204 | 81 class testLexer(unittest.TestCase): |
205 | 82 def testUnexpectedCharacter(self): |
83 snippet = """ var s \u6c34 """ | |
84 with self.assertRaises(ppci.CompilerError): | |
85 list(c3.lexer.tokenize(snippet)) | |
86 | |
204 | 87 def testBlockComment(self): |
88 snippet = """ | |
89 /* Demo */ | |
90 var int x = 0; | |
91 """ | |
92 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] | |
93 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks) | |
205 | 94 |
204 | 95 def testBlockCommentMultiLine(self): |
96 snippet = """ | |
97 /* Demo | |
98 bla1 | |
99 bla2 | |
100 */ | |
101 var int x = 0; | |
102 """ | |
103 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] | |
104 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks) | |
105 | |
106 class testBuilder(unittest.TestCase): | |
167 | 107 def setUp(self): |
108 self.diag = ppci.DiagnosticsManager() | |
109 self.builder = c3.Builder(self.diag) | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
110 def testSrc(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
111 self.builder.build(testsrc) |
167 | 112 def testFunctArgs(self): |
113 snippet = """ | |
114 package testargs; | |
115 function void t2(int a, double b) | |
116 { | |
117 t2(2, 2); | |
118 t2(2); | |
119 t2(1, 1.2); | |
120 } | |
121 """ | |
122 self.diag.clear() | |
123 ir = self.builder.build(snippet) | |
168 | 124 assert len(self.diag.diags) == 2 |
205 | 125 self.assertEqual(5, self.diag.diags[0].loc.row) |
126 self.assertEqual(6, self.diag.diags[1].loc.row) | |
148 | 127 |
167 | 128 def testExpressions(self): |
129 snippet = """ | |
130 package test; | |
131 function void t(int a, double b) | |
132 { | |
133 var int a2; | |
134 var bool c; | |
135 | |
136 a2 = b * a; | |
137 c = a; | |
138 c = b > 1; | |
139 } | |
140 """ | |
141 self.diag.clear() | |
194 | 142 ircode = self.builder.build(snippet) |
187 | 143 self.assertEqual(len(self.diag.diags), 3) |
186 | 144 self.assertEqual(self.diag.diags[0].loc.row, 8) |
145 self.assertEqual(self.diag.diags[1].loc.row, 9) | |
146 self.assertEqual(self.diag.diags[2].loc.row, 10) | |
194 | 147 self.assertFalse(ircode) |
205 | 148 |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
149 def testEmpty(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
150 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
151 package A |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
152 """ |
194 | 153 ircode = self.builder.build(snippet) |
154 self.assertFalse(ircode) | |
205 | 155 |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
156 def testEmpty2(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
157 snippet = "" |
194 | 158 self.diag.clear() |
159 ircode = self.builder.build(snippet) | |
160 self.assertFalse(ircode) | |
205 | 161 |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
162 def testRedefine(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
163 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
164 package test; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
165 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
166 var int b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
167 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
168 """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
169 self.diag.clear() |
194 | 170 ircode = self.builder.build(snippet) |
171 self.assertFalse(ircode) | |
186 | 172 self.assertEqual(len(self.diag.diags), 1) |
173 self.assertEqual(self.diag.diags[0].loc.row, 5) | |
205 | 174 |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
175 def testWhile(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
176 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
177 package tstwhile; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
178 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
179 function void t() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
180 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
181 var int i = 0; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
182 while (i < 1054) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
183 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
184 i = i + 3; |
186 | 185 a = a + i; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
186 } |
205 | 187 |
188 while(true) | |
189 { | |
190 } | |
191 | |
192 while(false) | |
193 { | |
194 } | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
195 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
196 """ |
187 | 197 ircode = self.builder.build(snippet) |
198 if not ircode: | |
186 | 199 self.diag.printErrors(snippet) |
187 | 200 self.assertTrue(ircode) |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
201 def testIf(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
202 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
203 package tstIFF; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
204 function void t(int b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
205 { |
180 | 206 var int a; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
207 a = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
208 if (a > b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
209 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
210 if (a > 1337) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
211 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
212 b = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
213 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
214 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
215 else |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
216 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
217 b = 1; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
218 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
219 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
220 return b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
221 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
222 """ |
191 | 223 ircode = self.builder.build(snippet) |
224 if not ircode: | |
225 self.diag.printErrors(snippet) | |
226 self.assertTrue(ircode) | |
205 | 227 |
170 | 228 def test2(self): |
205 | 229 # testsrc2 is valid code: |
230 testsrc2 = """ | |
231 package test2; | |
232 | |
233 function void tst() | |
234 { | |
235 var int a, b; | |
236 a = 2 * 33 - 12; | |
237 b = a * 2 + 13; | |
238 a = b + a; | |
239 if (a > b and b == 3) | |
240 { | |
241 var int x = a; | |
242 x = b * 2 - a; | |
243 a = x*x; | |
244 } | |
245 else | |
246 { | |
247 a = b + a; | |
248 } | |
249 } | |
250 | |
251 """ | |
252 self.diag.clear() | |
253 ir = self.builder.build(testsrc2) | |
254 self.assertTrue(ir) | |
167 | 255 |
256 if __name__ == '__main__': | |
257 do() | |
258 unittest.main() | |
259 | |
260 |