annotate python/testc3.py @ 239:63bb40758066

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