annotate test/testc3.py @ 300:158068af716c

yafm
author Windel Bouwman
date Tue, 03 Dec 2013 18:00:22 +0100
parents 917eab04b8b7
children 6753763d3bec
rev   line source
300
Windel Bouwman
parents: 295
diff changeset
1 from ppci.c3 import Builder, Lexer
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
2 import ppci
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
3 import unittest
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
4 import io
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
5
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
6
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
7 class testLexer(unittest.TestCase):
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
8 def setUp(self):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
9 diag = ppci.DiagnosticsManager()
300
Windel Bouwman
parents: 295
diff changeset
10 self.l = Lexer(diag)
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
11
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
12 def testUnexpectedCharacter(self):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
13 snippet = io.StringIO(""" var s \u6c34 """)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
14 with self.assertRaises(ppci.CompilerError):
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
15 list(self.l.tokenize(snippet))
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
16
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
17 def check(self, snippet, toks):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
18 toks2 = list(tok.typ for tok in self.l.tokenize(io.StringIO(snippet)))
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
19 self.assertSequenceEqual(toks, toks2)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
20
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
21 def testBlockComment(self):
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
22 snippet = """
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
23 /* Demo */
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
24 var int x = 0;
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
25 """
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
26 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END']
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
27 self.check(snippet, toks)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
28
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
29 def testBlockCommentMultiLine(self):
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
30 snippet = """
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
31 /* Demo
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
32 bla1
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
33 bla2
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
34 */
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
35 var int x = 0;
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
36 """
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
37 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END']
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
38 self.check(snippet, toks)
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
39
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
40
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
41 class testBuilder(unittest.TestCase):
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
42 def setUp(self):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
43 self.diag = ppci.DiagnosticsManager()
300
Windel Bouwman
parents: 295
diff changeset
44 self.builder = Builder(self.diag)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
45 self.diag.clear()
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
46
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
47 def expectErrors(self, snippet, rows):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
48 """ Helper to test for expected errors on rows """
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
49 ircode = list(self.builder.build([io.StringIO(snippet)]))
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
50 actualErrors = [err.row for err in self.diag.diags]
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
51 if rows != actualErrors:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
52 self.diag.printErrors(snippet)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
53 self.assertSequenceEqual(rows, actualErrors)
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
54 # self.assertFalse(all(ircode))
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
55
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
56 def expectOK(self, snippet):
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
57 if type(snippet) is list:
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
58 ircode = self.builder.build(snippet)
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
59 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
60 ircode = self.builder.build([io.StringIO(snippet)])
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
61 if not ircode:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
62 self.diag.printErrors(snippet)
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
63 self.assertTrue(all(ircode))
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 215
diff changeset
64 return ircode
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
65
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
66 def testPackage(self):
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
67 p1 = """module p1;
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
68 type int A;
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
69 """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
70 p2 = """module p2;
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
71 import p1;
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
72 var A b;
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
73 """
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
74 self.expectOK([io.StringIO(s) for s in (p1, p2)])
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
75
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
76 def testPackageMutual(self):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
77 p1 = """module p1;
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
78 import p2;
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
79 type int A;
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
80 var p2.B b;
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
81 """
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
82 p2 = """module p2;
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
83 import p1;
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
84 var p1.A a;
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
85 """
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
86 self.expectOK([io.StringIO(s) for s in (p1, p2)])
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
87
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
88 def testPackageNotExists(self):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
89 p1 = """module p1;
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
90 import p23;
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
91 """
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
92 self.expectOK([io.StringIO(p1)])
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
93
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
94 def testFunctArgs(self):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
95 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
96 module testargs;
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
97 function void t2(int a, double b)
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
98 {
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
99 t2(2, 2);
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
100 t2(2);
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
101 t2(1, 1.2);
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
102 }
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
103 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
104 self.expectErrors(snippet, [5, 6])
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
105
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
106 def testExpressions(self):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
107 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
108 module test;
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
109 function void t(int a, double b)
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
110 {
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
111 var int a2;
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
112 var bool c;
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
113
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
114 a2 = b * a;
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
115 c = a;
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
116 c = b > 1;
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
117 }
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
118 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
119 self.expectErrors(snippet, [8, 9, 10])
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
120
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
121 def testExpression1(self):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
122 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
123 module testexpr1;
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
124 function void t()
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
125 {
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
126 var int a, b, c;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
127 a = 1;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
128 b = a * 2 + a * a;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
129 c = b * a - 3;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
130 }
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
131 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
132 self.expectOK(snippet)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
133
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
134 def testEmpty(self):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
135 snippet = """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
136 module A
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
137 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
138 self.expectErrors(snippet, [3])
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
139
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
140 def testEmpty2(self):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
141 snippet = ""
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
142 self.expectErrors(snippet, [1])
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
143
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
144 def testRedefine(self):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
145 snippet = """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
146 module test;
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
147 var int a;
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
148 var int b;
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
149 var int a;
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
150 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
151 self.expectErrors(snippet, [5])
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
152
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
153 def testWhile(self):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
154 snippet = """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
155 module tstwhile;
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
156 var int a;
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
157 function void t()
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
158 {
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
159 var int i = 0;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
160 while (i < 1054)
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 i = i + 3;
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 182
diff changeset
163 a = a + i;
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
164 }
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
165
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
166 while(true)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
167 {
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
168 }
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
169
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
170 while(false)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
171 {
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
172 }
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
173 }
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
174 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
175 self.expectOK(snippet)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
176
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
177 def testIf(self):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
178 snippet = """
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
179 module tstIFF;
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
180 function void t(int b)
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
181 {
180
25a0753da4cf Re-organized files
Windel Bouwman
parents: 170
diff changeset
182 var int a;
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
183 a = 2;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
184 if (a > b)
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
185 {
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
186 if (a > 1337)
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
187 {
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
188 b = 2;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
189 }
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 else
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 = 1;
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 return b;
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
197 }
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
198 """
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
199 self.expectOK(snippet)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
200
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
201 def testTypeDef(self):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
202 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
203 module testtypedef;
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
204 type int my_int;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
205 function void t()
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
206 {
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
207 var my_int a;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
208 var int b;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
209 a = 2;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
210 b = a + 2;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
211 }
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
212 """
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
213 self.expectOK(snippet)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
214
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
215 def testLocalVariable(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
216 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
217 module testlocalvar;
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
218 function void t()
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
219 {
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
220 var int a, b;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
221 a = 2;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
222 b = a + 2;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
223 }
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
224 """
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 252
diff changeset
225 self.expectOK(snippet)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
226
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
227 def testUnknownType(self):
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
228 snippet = """module testlocalvar;
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
229 function void t()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
230 {
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
231 var int2 a;
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
232 }
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
233 """
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
234 self.expectErrors(snippet, [4])
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
235
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
236 def testStruct1(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
237 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
238 module teststruct1;
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
239 function void t()
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
240 {
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
241 var struct {int x, y;} a;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
242 a.x = 2;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
243 a.y = a.x + 2;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
244 }
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
245 """
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 252
diff changeset
246 self.expectOK(snippet)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
247
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
248 def testStructCall(self):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
249 snippet = """
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
250 module teststruct1;
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
251 function void t()
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
252 {
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
253 var struct {int x, y;} a;
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
254 a.b.c(9);
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
255 }
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
256 """
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
257 self.expectOK(snippet)
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
258
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
259 def testPointerType1(self):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
260 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
261 module testpointer1;
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
262 var int* pa;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
263 function void t()
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
264 {
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
265 var int a;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
266 pa = &a;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
267 *pa = 22;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
268 }
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
269 """
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
270 self.expectOK(snippet)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
271
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
272 def testPointerType(self):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
273 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
274 module testpointer;
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
275 var int* pa, pb;
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
276 function void t(int a, double b)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
277 {
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
278 var int a2;
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
279 a2 = a; // parameters cannot be escaped for now..
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
280 pa = &a2;
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
281 pb = pa;
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
282 *pa = 22;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
283 }
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
284 """
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
285 self.expectOK(snippet)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
286
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
287 def testPointerTypeInCorrect(self):
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
288 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
289 module testpointerincorrect;
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
290 var int* pa;
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
291 function void t(int a, double b)
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
292 {
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
293 pa = 2; // type conflict
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
294 pa = &a;
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
295 pa = &2;
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
296 &a = pa; // No valid lvalue
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
297 **pa = 22; // Cannot deref int
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
298 }
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
299 """
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
300 self.expectErrors(snippet, [6, 9, 10])
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
301
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
302 def testPointerTypeIr(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
303 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
304 module testptr_ir;
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
305 function void t()
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
306 {
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
307 var int* a;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
308 a = cast<int*>(40);
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
309 *a = 2;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
310 }
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
311 """
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 252
diff changeset
312 self.expectOK(snippet)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
313
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
314 def testPointerTypeIr2(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
315 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
316 module testptr_ir;
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
317 type struct {int x,y;}* gpio;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
318 function void t()
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
319 {
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
320 var gpio a;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
321 a = cast<gpio>(40);
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
322 a->x = 2;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
323 a->y = a->x - 14;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
324 }
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
325 """
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 252
diff changeset
326 self.expectOK(snippet)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
327
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
328 def testWrongCast(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
329 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
330 module testptr_ir;
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
331 type struct {int x,y;}* gpio;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
332 function void t()
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
333 {
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
334 var gpio a;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
335 *cast<gpio>(*a);
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
336 }
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
337 """
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
338 self.expectErrors(snippet, [7, 7])
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
339
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
340 def testComplexType(self):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
341 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
342 module testpointer;
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
343 type int my_int;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
344
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
345 type struct {
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
346 int x, y;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
347 } point;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
348
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
349 type struct {
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
350 int mem1;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
351 int memb2;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
352 point P1;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
353 } my_struct;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
354
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
355 type my_struct* my_sptr;
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
356 var int* pa;
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
357
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 225
diff changeset
358 function void t(int a, int b, my_sptr x)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
359 {
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
360 var my_struct *msp;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
361
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
362 var my_struct u, v;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
363 var point *pt;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
364
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
365 pt = &msp->P1;
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
366 msp = x;
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
367 *pa = 22 + u.mem1 * v.memb2 - u.P1.x;
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
368 x->memb2 = *pa + a * b;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
369
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
370 msp->P1.x = a * x->P1.y;
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
371 }
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
372 """
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
373 self.expectOK(snippet)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
374
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
375
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
376 if __name__ == '__main__':
243
ef683881c64e Remove various files
Windel Bouwman
parents: 231
diff changeset
377 unittest.main()