Mercurial > lcfOS
annotate python/testc3.py @ 188:1113da536872
Used markdown
author | Windel Bouwman |
---|---|
date | Sat, 25 May 2013 14:46:12 +0200 |
parents | bf5ab358f43a |
children | 6b2bec5653f1 |
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 |
167 | 103 class testA(unittest.TestCase): |
104 def setUp(self): | |
105 self.diag = ppci.DiagnosticsManager() | |
106 self.builder = c3.Builder(self.diag) | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
107 def testSrc(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
108 self.builder.build(testsrc) |
167 | 109 def testFunctArgs(self): |
110 snippet = """ | |
111 package testargs; | |
112 function void t2(int a, double b) | |
113 { | |
114 t2(2, 2); | |
115 t2(2); | |
116 t2(1, 1.2); | |
117 } | |
118 """ | |
119 self.diag.clear() | |
120 ir = self.builder.build(snippet) | |
168 | 121 assert len(self.diag.diags) == 2 |
122 assert self.diag.diags[0].loc.row == 5 | |
123 assert self.diag.diags[1].loc.row == 6 | |
148 | 124 |
167 | 125 def testExpressions(self): |
126 snippet = """ | |
127 package test; | |
128 function void t(int a, double b) | |
129 { | |
130 var int a2; | |
131 var bool c; | |
132 | |
133 a2 = b * a; | |
134 c = a; | |
135 c = b > 1; | |
136 } | |
137 """ | |
138 self.diag.clear() | |
139 ir = self.builder.build(snippet) | |
187 | 140 self.assertEqual(len(self.diag.diags), 3) |
186 | 141 self.assertEqual(self.diag.diags[0].loc.row, 8) |
142 self.assertEqual(self.diag.diags[1].loc.row, 9) | |
143 self.assertEqual(self.diag.diags[2].loc.row, 10) | |
144 self.assertFalse(ir) | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
145 def testEmpty(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
146 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
147 package A |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
148 """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
149 self.builder.build(snippet) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
150 def testEmpty2(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
151 snippet = "" |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
152 self.builder.build(snippet) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
153 def testRedefine(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 test; |
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 var int b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
158 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
159 """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
160 self.diag.clear() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
161 self.builder.build(snippet) |
186 | 162 self.assertEqual(len(self.diag.diags), 1) |
163 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
|
164 def testWhile(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 tstwhile; |
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 function void t() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
169 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
170 var int i = 0; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
171 while (i < 1054) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
172 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
173 i = i + 3; |
186 | 174 a = a + i; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
175 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
176 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
177 """ |
187 | 178 ircode = self.builder.build(snippet) |
179 if not ircode: | |
186 | 180 self.diag.printErrors(snippet) |
187 | 181 self.assertTrue(ircode) |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
182 def testIf(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
183 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
184 package tstIFF; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
185 function void t(int b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
186 { |
180 | 187 var int a; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
188 a = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
189 if (a > b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
190 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
191 if (a > 1337) |
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 b = 2; |
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 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
196 else |
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 b = 1; |
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 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
201 return b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
202 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
203 """ |
186 | 204 ir = self.builder.build(snippet) |
205 self.assertTrue(ir) | |
170 | 206 def test2(self): |
207 # testsrc2 is valid code: | |
208 self.diag.clear() | |
209 ir = self.builder.build(testsrc2) | |
187 | 210 self.assertTrue(ir) |
167 | 211 |
212 if __name__ == '__main__': | |
213 do() | |
214 unittest.main() | |
215 | |
216 |