annotate test/testc3.py @ 299:674789d9ff37

Added a doc
author Windel Bouwman
date Sun, 01 Dec 2013 18:37:23 +0100
parents 917eab04b8b7
children 158068af716c
rev   line source
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
1 import c3
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
2 import time
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
3 import ppci
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
4 import ir
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
5 import unittest
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
6 import glob
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
7 import io
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
8
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
9 testsrc = """module test;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
10
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
11 /*
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
12 demo of the source that is correct :)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
13 */
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
14 var int c, d;
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
15 var double e;
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
16 var int f;
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 162
diff changeset
17
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
18 const int A = 1337;
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
19
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
20 function void test1()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
21 {
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
22 var int bdd;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
23 var int a = 10;
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
24 bdd = 20;
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
25 var int buf;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
26 var int i;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
27 i = 2;
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
28 var int zero = i - 2;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
29 if (i > 1)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
30 {
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
31 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - 1;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
32 }
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
33 else
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
34 {
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
35 ;;;
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
36 }
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
37
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
38 t2(2, 3);
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
39 }
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
40
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
41 function int t2(int a, int b)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
42 {
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
43 if (a > 0)
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
44 {
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
45 a = 2 + t2(a - 1, 10);
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
46 }
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
47
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
48 return a + b;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
49 }
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
50
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
51 var int a, b;
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
52
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
53 function int t3(int aap, int blah)
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
54 {
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
55 if (a > blah and blah < 45 + 33 or 33 > aap or 6 > 2 and true)
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
56 {
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
57 a = 2 + t2(a - 1, 0);
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
58 }
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
59
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
60 return a + b;
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
61 }
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
62
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
63 var int hahaa = 23 * 2;
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
64
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
65
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
66 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
67
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
68 class testLexer(unittest.TestCase):
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
69 def setUp(self):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
70 diag = ppci.DiagnosticsManager()
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
71 self.l = c3.Lexer(diag)
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
72
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
73 def testUnexpectedCharacter(self):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
74 snippet = io.StringIO(""" var s \u6c34 """)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
75 with self.assertRaises(ppci.CompilerError):
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
76 list(self.l.tokenize(snippet))
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
77
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
78 def check(self, snippet, toks):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
79 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
80 self.assertSequenceEqual(toks, toks2)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
81
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
82 def testBlockComment(self):
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
83 snippet = """
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
84 /* Demo */
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
85 var int x = 0;
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
86 """
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
87 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END']
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
88 self.check(snippet, toks)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
89
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
90 def testBlockCommentMultiLine(self):
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
91 snippet = """
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
92 /* Demo
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
93 bla1
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
94 bla2
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
95 */
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
96 var int x = 0;
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
97 """
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
98 toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END']
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
99 self.check(snippet, toks)
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
100
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
101
204
de3a68f677a5 Added long comment to c3 parser
Windel Bouwman
parents: 194
diff changeset
102 class testBuilder(unittest.TestCase):
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
103 def setUp(self):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
104 self.diag = ppci.DiagnosticsManager()
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
105 self.builder = c3.Builder(self.diag)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
106 self.diag.clear()
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
107
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
108 def testSrc(self):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
109 self.expectOK(testsrc)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
110
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
111 def expectErrors(self, snippet, rows):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
112 """ Helper to test for expected errors on rows """
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
113 ircode = list(self.builder.build([io.StringIO(snippet)]))
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
114 actualErrors = [err.row for err in self.diag.diags]
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
115 if rows != actualErrors:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
116 self.diag.printErrors(snippet)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
117 self.assertSequenceEqual(rows, actualErrors)
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
118 # self.assertFalse(all(ircode))
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
119
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
120 def expectOK(self, snippet):
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
121 if type(snippet) is list:
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
122 ircode = self.builder.build(snippet)
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
123 else:
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
124 ircode = self.builder.build([io.StringIO(snippet)])
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
125 if not ircode:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
126 self.diag.printErrors(snippet)
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
127 self.assertTrue(all(ircode))
217
8b2e5f3cd579 Removed some stale python source files
Windel Bouwman
parents: 215
diff changeset
128 return ircode
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
129
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
130 def testPackage(self):
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
131 p1 = """module p1;
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
132 type int A;
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
133 """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
134 p2 = """module p2;
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
135 import p1;
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
136 var A b;
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 249
diff changeset
137 """
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
138 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
139
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
140 def testPackageMutual(self):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
141 p1 = """module p1;
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
142 import p2;
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
143 type int A;
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
144 var p2.B b;
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
145 """
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
146 p2 = """module p2;
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
147 import p1;
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
148 var p1.A a;
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
149 """
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
150 self.expectOK([io.StringIO(s) for s in (p1, p2)])
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
151
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
152 def testPackageNotExists(self):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
153 p1 = """module p1;
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
154 import p23;
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
155 """
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
156 self.expectOK([io.StringIO(p1)])
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
157
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
158 def testFunctArgs(self):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
159 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
160 module testargs;
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
161 function void t2(int a, double b)
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
162 {
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
163 t2(2, 2);
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
164 t2(2);
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
165 t2(1, 1.2);
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
166 }
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
167 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
168 self.expectErrors(snippet, [5, 6])
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
169
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
170 def testExpressions(self):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
171 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
172 module test;
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
173 function void t(int a, double b)
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
174 {
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
175 var int a2;
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
176 var bool c;
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
177
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
178 a2 = b * a;
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
179 c = a;
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
180 c = b > 1;
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
181 }
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
182 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
183 self.expectErrors(snippet, [8, 9, 10])
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
184
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
185 def testExpression1(self):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
186 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
187 module testexpr1;
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
188 function void t()
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
189 {
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
190 var int a, b, c;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
191 a = 1;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
192 b = a * 2 + a * a;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
193 c = b * a - 3;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
194 }
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
195 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
196 self.expectOK(snippet)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
197
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
198 def testEmpty(self):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
199 snippet = """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
200 module A
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
201 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
202 self.expectErrors(snippet, [3])
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
203
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
204 def testEmpty2(self):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
205 snippet = ""
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
206 self.expectErrors(snippet, [1])
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
207
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
208 def testRedefine(self):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
209 snippet = """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
210 module test;
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
211 var int a;
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
212 var int b;
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
213 var int a;
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
214 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
215 self.expectErrors(snippet, [5])
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
216
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
217 def testWhile(self):
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
218 snippet = """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
219 module tstwhile;
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
220 var int a;
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
221 function void t()
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
222 {
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
223 var int i = 0;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
224 while (i < 1054)
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 i = i + 3;
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 182
diff changeset
227 a = a + i;
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
228 }
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
229
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
230 while(true)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
231 {
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
232 }
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
233
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
234 while(false)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
235 {
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
236 }
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
237 }
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
238 """
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
239 self.expectOK(snippet)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
240
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
241 def testIf(self):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
242 snippet = """
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
243 module tstIFF;
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
244 function void t(int b)
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
245 {
180
25a0753da4cf Re-organized files
Windel Bouwman
parents: 170
diff changeset
246 var int a;
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
247 a = 2;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
248 if (a > b)
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
249 {
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
250 if (a > 1337)
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
251 {
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
252 b = 2;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
253 }
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
254 }
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
255 else
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
256 {
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
257 b = 1;
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
258 }
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
259
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 168
diff changeset
260 return b;
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
261 }
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
262 """
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
263 self.expectOK(snippet)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
264
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
265 def testTypeDef(self):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
266 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
267 module testtypedef;
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
268 type int my_int;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
269 function void t()
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
270 {
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
271 var my_int a;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
272 var int b;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
273 a = 2;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
274 b = a + 2;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
275 }
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
276 """
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
277 self.expectOK(snippet)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
278
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
279 def testLocalVariable(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
280 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
281 module testlocalvar;
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
282 function void t()
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
283 {
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
284 var int a, b;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
285 a = 2;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
286 b = a + 2;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
287 }
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
288 """
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 252
diff changeset
289 self.expectOK(snippet)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
290
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
291 def testUnknownType(self):
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
292 snippet = """module testlocalvar;
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
293 function void t()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
294 {
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
295 var int2 a;
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
296 }
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
297 """
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
298 self.expectErrors(snippet, [4])
e41e4109addd Added current position arrow
Windel Bouwman
parents: 243
diff changeset
299
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
300 def testStruct1(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
301 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
302 module teststruct1;
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
303 function void t()
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
304 {
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
305 var struct {int x, y;} a;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
306 a.x = 2;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
307 a.y = a.x + 2;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
308 }
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
309 """
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 252
diff changeset
310 self.expectOK(snippet)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
311
295
917eab04b8b7 Added disasm
Windel Bouwman
parents: 293
diff changeset
312 @unittest.skip('Too strange to handle')
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
313 def testStructCall(self):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
314 snippet = """
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
315 module teststruct1;
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
316 function void t()
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
317 {
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
318 var struct {int x, y;} a;
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
319 a.b.c(9);
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
320 }
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
321 """
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
322 self.expectOK(snippet)
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 292
diff changeset
323
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
324 def testPointerType1(self):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
325 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
326 module testpointer1;
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
327 var int* pa;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
328 function void t()
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
329 {
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
330 var int a;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
331 pa = &a;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
332 *pa = 22;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
333 }
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
334 """
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
335 self.expectOK(snippet)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
336
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
337 def testPointerType(self):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
338 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
339 module testpointer;
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
340 var int* pa, pb;
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
341 function void t(int a, double b)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
342 {
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
343 var int a2;
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
344 a2 = a; // parameters cannot be escaped for now..
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
345 pa = &a2;
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
346 pb = pa;
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
347 *pa = 22;
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 """
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
350 self.expectOK(snippet)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
351
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
352 def testPointerTypeInCorrect(self):
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
353 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
354 module testpointerincorrect;
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
355 var int* pa;
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
356 function void t(int a, double b)
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
357 {
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
358 pa = 2; // type conflict
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
359 pa = &a;
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
360 pa = &2;
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
361 &a = pa; // No valid lvalue
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
362 **pa = 22; // Cannot deref int
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
363 }
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
364 """
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
365 self.expectErrors(snippet, [6, 9, 10])
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
366
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
367 def testPointerTypeIr(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
368 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
369 module testptr_ir;
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
370 function void t()
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
371 {
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
372 var int* a;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
373 a = cast<int*>(40);
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
374 *a = 2;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
375 }
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
376 """
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 252
diff changeset
377 self.expectOK(snippet)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
378
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
379 def testPointerTypeIr2(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
380 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
381 module testptr_ir;
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
382 type struct {int x,y;}* gpio;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
383 function void t()
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
384 {
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
385 var gpio a;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
386 a = cast<gpio>(40);
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
387 a->x = 2;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
388 a->y = a->x - 14;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
389 }
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
390 """
268
5ec7580976d9 Op naar tree-IR
Windel Bouwman
parents: 252
diff changeset
391 self.expectOK(snippet)
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
392
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
393 def testWrongCast(self):
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
394 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
395 module testptr_ir;
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
396 type struct {int x,y;}* gpio;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
397 function void t()
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
398 {
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
399 var gpio a;
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
400 *cast<gpio>(*a);
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
401 }
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
402 """
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
403 self.expectErrors(snippet, [7, 7])
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
404
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
405 def testComplexType(self):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
406 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
407 module testpointer;
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
408 type int my_int;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
409
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
410 type struct {
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
411 int x, y;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
412 } point;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
413
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
414 type struct {
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
415 int mem1;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
416 int memb2;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
417 point P1;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
418 } my_struct;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
419
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
420 type my_struct* my_sptr;
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
421 var int* pa;
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
422
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 225
diff changeset
423 function void t(int a, int b, my_sptr x)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
424 {
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
425 var my_struct *msp;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
426
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
427 var my_struct u, v;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
428 var point *pt;
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
429
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
430 pt = &msp->P1;
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
431 msp = x;
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
432 *pa = 22 + u.mem1 * v.memb2 - u.P1.x;
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
433 x->memb2 = *pa + a * b;
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
434
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 221
diff changeset
435 msp->P1.x = a * x->P1.y;
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
436 }
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 205
diff changeset
437 """
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
438 self.expectOK(snippet)
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
439
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
440 @unittest.skip('To be rewritten')
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
441 def testExamples(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
442 """ Test all examples in the c3/examples directory """
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
443 example_filenames = glob.glob('./c3examples/*.c3')
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
444 for filename in example_filenames:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
445 with open(filename, 'r') as f:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
446 self.expectOK(f)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 217
diff changeset
447
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
448 def test2(self):
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
449 # testsrc2 is valid code:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
450 snippet = """
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
451 module test2;
205
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
452
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
453 function void tst()
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
454 {
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
455 var int a, b;
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
456 a = 2 * 33 - 12;
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
457 b = a * 2 + 13;
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
458 a = b + a;
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
459 if (a > b and b == 3)
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
460 {
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
461 var int x = a;
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
462 x = b * 2 - a;
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
463 a = x*x;
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
464 }
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
465 else
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
466 {
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
467 a = b + a;
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
468 }
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
469 }
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
470
d77cb5962cc5 Added some handcoded arm code generation
Windel Bouwman
parents: 204
diff changeset
471 """
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
472 self.expectOK(snippet)
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
473
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
474 if __name__ == '__main__':
243
ef683881c64e Remove various files
Windel Bouwman
parents: 231
diff changeset
475 unittest.main()
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
476
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
477