Mercurial > lcfOS
annotate python/testc3.py @ 169:ee0d30533dae
Added more tests and improved the diagnostic update
author | Windel Bouwman |
---|---|
date | Sat, 23 Mar 2013 18:34:41 +0100 |
parents | 49f1ab80d040 |
children | 4348da5ca307 |
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 | |
152 | 59 def c3compile(src, diag): |
163 | 60 # Structures: |
165 | 61 builder = c3.Builder(diag) |
62 ir = builder.build(src) | |
63 # optional optimize here | |
157 | 64 x86gen = x86.X86CodeGen(diag) |
162 | 65 ok = len(diag.diags) == 0 |
66 if not ok: | |
67 return | |
165 | 68 print('generating x86 code') |
69 x86gen.genBin(ir) | |
70 with open('dummydummy.asm', 'w') as f: | |
71 f.write('bits 64\n') | |
72 for a in x86gen.asm: | |
73 print(a) | |
74 f.write(str(a) + '\n') | |
151 | 75 |
76 def do(): | |
152 | 77 diag = ppci.DiagnosticsManager() |
78 c3compile(testsrc, diag) | |
151 | 79 |
167 | 80 class testA(unittest.TestCase): |
81 def setUp(self): | |
82 self.diag = ppci.DiagnosticsManager() | |
83 self.builder = c3.Builder(self.diag) | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
84 def testSrc(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
85 self.builder.build(testsrc) |
167 | 86 def testFunctArgs(self): |
87 snippet = """ | |
88 package testargs; | |
89 function void t2(int a, double b) | |
90 { | |
91 t2(2, 2); | |
92 t2(2); | |
93 t2(1, 1.2); | |
94 } | |
95 """ | |
96 self.diag.clear() | |
97 ir = self.builder.build(snippet) | |
168 | 98 assert len(self.diag.diags) == 2 |
99 assert self.diag.diags[0].loc.row == 5 | |
100 assert self.diag.diags[1].loc.row == 6 | |
148 | 101 |
167 | 102 def testExpressions(self): |
103 snippet = """ | |
104 package test; | |
105 function void t(int a, double b) | |
106 { | |
107 var int a2; | |
108 var bool c; | |
109 | |
110 a2 = b * a; | |
111 c = a; | |
112 c = b > 1; | |
113 } | |
114 """ | |
115 self.diag.clear() | |
116 ir = self.builder.build(snippet) | |
168 | 117 assert len(self.diag.diags) == 3 |
118 assert self.diag.diags[0].loc.row == 8 | |
119 assert self.diag.diags[1].loc.row == 9 | |
120 assert self.diag.diags[2].loc.row == 10 | |
121 assert ir == None | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
122 def testEmpty(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
123 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
124 package A |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
125 """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
126 self.builder.build(snippet) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
127 def testEmpty2(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 self.builder.build(snippet) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
130 def testRedefine(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
131 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
132 package test; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
133 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
134 var int b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
135 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
136 """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
137 self.diag.clear() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
138 self.builder.build(snippet) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
139 assert len(self.diag.diags) == 1 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
140 assert self.diag.diags[0].loc.row == 5 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
141 def testWhile(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
142 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
143 package tstwhile; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
144 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
145 function void t() |
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 var int i = 0; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
148 while (i < 1054) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
149 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
150 i = i + 3; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
151 a = a + i |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
152 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
153 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
154 """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
155 self.builder.build(snippet) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
156 def testIf(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
157 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
158 package tstIFF; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
159 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
160 function void t(int b) |
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 a = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
163 if (a > b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
164 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
165 if (a > 1337) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
166 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
167 b = 2; |
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 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
170 else |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
171 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
172 b = 1; |
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 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
175 return b; |
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 """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
178 self.builder.build(snippet) |
167 | 179 |
180 if __name__ == '__main__': | |
181 do() | |
182 unittest.main() | |
183 | |
184 |