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