comparison python/testc3.py @ 205:d77cb5962cc5

Added some handcoded arm code generation
author Windel Bouwman
date Sun, 23 Jun 2013 18:23:18 +0200
parents de3a68f677a5
children 003c8a976fff
comparison
equal deleted inserted replaced
204:de3a68f677a5 205:d77cb5962cc5
54 var int hahaa = 23 * 2; 54 var int hahaa = 23 * 2;
55 55
56 56
57 """ 57 """
58 58
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 59
82 def c3compile(src, diag): 60 def c3compile(src, diag):
83 # Structures: 61 # Structures:
84 builder = c3.Builder(diag) 62 builder = c3.Builder(diag)
85 ir = builder.build(src) 63 ir = builder.build(src)
99 def do(): 77 def do():
100 diag = ppci.DiagnosticsManager() 78 diag = ppci.DiagnosticsManager()
101 c3compile(testsrc, diag) 79 c3compile(testsrc, diag)
102 80
103 class testLexer(unittest.TestCase): 81 class testLexer(unittest.TestCase):
82 def testUnexpectedCharacter(self):
83 snippet = """ var s \u6c34 """
84 with self.assertRaises(ppci.CompilerError):
85 list(c3.lexer.tokenize(snippet))
86
104 def testBlockComment(self): 87 def testBlockComment(self):
105 snippet = """ 88 snippet = """
106 /* Demo */ 89 /* Demo */
107 var int x = 0; 90 var int x = 0;
108 """ 91 """
109 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END'] 92 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END']
110 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks) 93 self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks)
94
111 def testBlockCommentMultiLine(self): 95 def testBlockCommentMultiLine(self):
112 snippet = """ 96 snippet = """
113 /* Demo 97 /* Demo
114 bla1 98 bla1
115 bla2 99 bla2
136 } 120 }
137 """ 121 """
138 self.diag.clear() 122 self.diag.clear()
139 ir = self.builder.build(snippet) 123 ir = self.builder.build(snippet)
140 assert len(self.diag.diags) == 2 124 assert len(self.diag.diags) == 2
141 assert self.diag.diags[0].loc.row == 5 125 self.assertEqual(5, self.diag.diags[0].loc.row)
142 assert self.diag.diags[1].loc.row == 6 126 self.assertEqual(6, self.diag.diags[1].loc.row)
143 127
144 def testExpressions(self): 128 def testExpressions(self):
145 snippet = """ 129 snippet = """
146 package test; 130 package test;
147 function void t(int a, double b) 131 function void t(int a, double b)
159 self.assertEqual(len(self.diag.diags), 3) 143 self.assertEqual(len(self.diag.diags), 3)
160 self.assertEqual(self.diag.diags[0].loc.row, 8) 144 self.assertEqual(self.diag.diags[0].loc.row, 8)
161 self.assertEqual(self.diag.diags[1].loc.row, 9) 145 self.assertEqual(self.diag.diags[1].loc.row, 9)
162 self.assertEqual(self.diag.diags[2].loc.row, 10) 146 self.assertEqual(self.diag.diags[2].loc.row, 10)
163 self.assertFalse(ircode) 147 self.assertFalse(ircode)
148
164 def testEmpty(self): 149 def testEmpty(self):
165 snippet = """ 150 snippet = """
166 package A 151 package A
167 """ 152 """
168 ircode = self.builder.build(snippet) 153 ircode = self.builder.build(snippet)
169 self.assertFalse(ircode) 154 self.assertFalse(ircode)
155
170 def testEmpty2(self): 156 def testEmpty2(self):
171 snippet = "" 157 snippet = ""
172 self.diag.clear() 158 self.diag.clear()
173 ircode = self.builder.build(snippet) 159 ircode = self.builder.build(snippet)
174 self.assertFalse(ircode) 160 self.assertFalse(ircode)
161
175 def testRedefine(self): 162 def testRedefine(self):
176 snippet = """ 163 snippet = """
177 package test; 164 package test;
178 var int a; 165 var int a;
179 var int b; 166 var int b;
182 self.diag.clear() 169 self.diag.clear()
183 ircode = self.builder.build(snippet) 170 ircode = self.builder.build(snippet)
184 self.assertFalse(ircode) 171 self.assertFalse(ircode)
185 self.assertEqual(len(self.diag.diags), 1) 172 self.assertEqual(len(self.diag.diags), 1)
186 self.assertEqual(self.diag.diags[0].loc.row, 5) 173 self.assertEqual(self.diag.diags[0].loc.row, 5)
174
187 def testWhile(self): 175 def testWhile(self):
188 snippet = """ 176 snippet = """
189 package tstwhile; 177 package tstwhile;
190 var int a; 178 var int a;
191 function void t() 179 function void t()
194 while (i < 1054) 182 while (i < 1054)
195 { 183 {
196 i = i + 3; 184 i = i + 3;
197 a = a + i; 185 a = a + i;
198 } 186 }
187
188 while(true)
189 {
190 }
191
192 while(false)
193 {
194 }
199 } 195 }
200 """ 196 """
201 ircode = self.builder.build(snippet) 197 ircode = self.builder.build(snippet)
202 if not ircode: 198 if not ircode:
203 self.diag.printErrors(snippet) 199 self.diag.printErrors(snippet)
226 """ 222 """
227 ircode = self.builder.build(snippet) 223 ircode = self.builder.build(snippet)
228 if not ircode: 224 if not ircode:
229 self.diag.printErrors(snippet) 225 self.diag.printErrors(snippet)
230 self.assertTrue(ircode) 226 self.assertTrue(ircode)
227
231 def test2(self): 228 def test2(self):
232 # testsrc2 is valid code: 229 # testsrc2 is valid code:
233 self.diag.clear() 230 testsrc2 = """
234 ir = self.builder.build(testsrc2) 231 package test2;
235 self.assertTrue(ir) 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)
236 255
237 if __name__ == '__main__': 256 if __name__ == '__main__':
238 do() 257 do()
239 unittest.main() 258 unittest.main()
240 259