Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: dart/frog/tests/leg/src/ResolverTest.dart

Issue 9634008: Introduce element categories and move resolver towards being more compositional. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #import("../../../leg/leg.dart"); 5 #import("../../../leg/leg.dart");
6 #import("../../../leg/elements/elements.dart"); 6 #import("../../../leg/elements/elements.dart");
7 #import("../../../leg/tree/tree.dart"); 7 #import("../../../leg/tree/tree.dart");
8 #import("../../../leg/util/util.dart"); 8 #import("../../../leg/util/util.dart");
9 #import("mock_compiler.dart"); 9 #import("mock_compiler.dart");
10 #import("parser_helper.dart"); 10 #import("parser_helper.dart");
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 } 166 }
167 167
168 testLocalsThree() { 168 testLocalsThree() {
169 MockCompiler compiler = new MockCompiler(); 169 MockCompiler compiler = new MockCompiler();
170 ResolverVisitor visitor = compiler.resolverVisitor(); 170 ResolverVisitor visitor = compiler.resolverVisitor();
171 Node tree = parseStatement("{ var a = 1; if (true) { a; } }"); 171 Node tree = parseStatement("{ var a = 1; if (true) { a; } }");
172 Element element = visitor.visit(tree); 172 Element element = visitor.visit(tree);
173 Expect.equals(null, element); 173 Expect.equals(null, element);
174 BlockScope scope = visitor.context; 174 BlockScope scope = visitor.context;
175 Expect.equals(0, scope.elements.length); 175 Expect.equals(0, scope.elements.length);
176 Expect.equals(2, map(visitor).length); 176 Expect.equals(3, map(visitor).length);
177 List<Element> elements = map(visitor).getValues(); 177 List<Element> elements = map(visitor).getValues();
178 Expect.equals(elements[0], elements[1]); 178 Expect.equals(elements[0], elements[1]);
179 } 179 }
180 180
181 testLocalsFour() { 181 testLocalsFour() {
182 MockCompiler compiler = new MockCompiler(); 182 MockCompiler compiler = new MockCompiler();
183 ResolverVisitor visitor = compiler.resolverVisitor(); 183 ResolverVisitor visitor = compiler.resolverVisitor();
184 Node tree = parseStatement("{ var a = 1; if (true) { var a = 1; } }"); 184 Node tree = parseStatement("{ var a = 1; if (true) { var a = 1; } }");
185 Element element = visitor.visit(tree); 185 Element element = visitor.visit(tree);
186 Expect.equals(null, element); 186 Expect.equals(null, element);
187 BlockScope scope = visitor.context; 187 BlockScope scope = visitor.context;
188 Expect.equals(0, scope.elements.length); 188 Expect.equals(0, scope.elements.length);
189 Expect.equals(2, map(visitor).length); 189 Expect.equals(2, map(visitor).length);
190 List<Element> elements = map(visitor).getValues(); 190 List<Element> elements = map(visitor).getValues();
191 Expect.notEquals(elements[0], elements[1]); 191 Expect.notEquals(elements[0], elements[1]);
192 } 192 }
193 193
194 testLocalsFive() { 194 testLocalsFive() {
195 MockCompiler compiler = new MockCompiler(); 195 MockCompiler compiler = new MockCompiler();
196 ResolverVisitor visitor = compiler.resolverVisitor(); 196 ResolverVisitor visitor = compiler.resolverVisitor();
197 If tree = parseStatement("if (true) { var a = 1; a; } else { var a = 2; a;}"); 197 If tree = parseStatement("if (true) { var a = 1; a; } else { var a = 2; a;}");
198 Element element = visitor.visit(tree); 198 Element element = visitor.visit(tree);
199 Expect.equals(null, element); 199 Expect.equals(null, element);
200 BlockScope scope = visitor.context; 200 BlockScope scope = visitor.context;
201 Expect.equals(0, scope.elements.length); 201 Expect.equals(0, scope.elements.length);
202 Expect.equals(4, map(visitor).length); 202 Expect.equals(6, map(visitor).length);
203 203
204 Block thenPart = tree.thenPart; 204 Block thenPart = tree.thenPart;
205 List statements1 = thenPart.statements.nodes.toList(); 205 List statements1 = thenPart.statements.nodes.toList();
206 Node def1 = statements1[0].definitions.nodes.head; 206 Node def1 = statements1[0].definitions.nodes.head;
207 Node id1 = statements1[1].expression; 207 Node id1 = statements1[1].expression;
208 Expect.equals(visitor.mapping[def1], visitor.mapping[id1]); 208 Expect.equals(visitor.mapping[def1], visitor.mapping[id1]);
209 209
210 Block elsePart = tree.elsePart; 210 Block elsePart = tree.elsePart;
211 List statements2 = elsePart.statements.nodes.toList(); 211 List statements2 = elsePart.statements.nodes.toList();
212 Node def2 = statements2[0].definitions.nodes.head; 212 Node def2 = statements2[0].definitions.nodes.head;
213 Node id2 = statements2[1].expression; 213 Node id2 = statements2[1].expression;
214 Expect.equals(visitor.mapping[def2], visitor.mapping[id2]); 214 Expect.equals(visitor.mapping[def2], visitor.mapping[id2]);
215 215
216 Expect.notEquals(visitor.mapping[def1], visitor.mapping[def2]); 216 Expect.notEquals(visitor.mapping[def1], visitor.mapping[def2]);
217 Expect.notEquals(visitor.mapping[id1], visitor.mapping[id2]); 217 Expect.notEquals(visitor.mapping[id1], visitor.mapping[id2]);
218 } 218 }
219 219
220 testParametersOne() { 220 testParametersOne() {
221 MockCompiler compiler = new MockCompiler(); 221 MockCompiler compiler = new MockCompiler();
222 ResolverVisitor visitor = compiler.resolverVisitor(); 222 ResolverVisitor visitor = compiler.resolverVisitor();
223 FunctionExpression tree = 223 FunctionExpression tree =
224 parseFunction("void foo(int a) { return a; }", compiler); 224 parseFunction("void foo(int a) { return a; }", compiler);
225 Element element = visitor.visit(tree); 225 visitor.visit(tree);
226 Expect.equals(ElementKind.FUNCTION, element.kind);
227 226
228 // Check that an element has been created for the parameter. 227 // Check that an element has been created for the parameter.
229 VariableDefinitions vardef = tree.parameters.nodes.head; 228 VariableDefinitions vardef = tree.parameters.nodes.head;
230 Node param = vardef.definitions.nodes.head; 229 Node param = vardef.definitions.nodes.head;
231 Expect.equals(ElementKind.PARAMETER, visitor.mapping[param].kind); 230 Expect.equals(ElementKind.PARAMETER, visitor.mapping[param].kind);
232 231
233 // Check that 'a' in 'return a' is resolved to the parameter. 232 // Check that 'a' in 'return a' is resolved to the parameter.
234 Block body = tree.body; 233 Block body = tree.body;
235 Return ret = body.statements.nodes.head; 234 Return ret = body.statements.nodes.head;
236 Send use = ret.expression; 235 Send use = ret.expression;
237 Expect.equals(ElementKind.PARAMETER, visitor.mapping[use].kind); 236 Expect.equals(ElementKind.PARAMETER, visitor.mapping[use].kind);
238 Expect.equals(visitor.mapping[param], visitor.mapping[use]); 237 Expect.equals(visitor.mapping[param], visitor.mapping[use]);
239 } 238 }
240 239
241 testFor() { 240 testFor() {
242 MockCompiler compiler = new MockCompiler(); 241 MockCompiler compiler = new MockCompiler();
243 ResolverVisitor visitor = compiler.resolverVisitor(); 242 ResolverVisitor visitor = compiler.resolverVisitor();
244 For tree = parseStatement("for (int i = 0; i < 10; i = i + 1) { i = 5; }"); 243 For tree = parseStatement("for (int i = 0; i < 10; i = i + 1) { i = 5; }");
245 visitor.visit(tree); 244 visitor.visit(tree);
246 245
247 BlockScope scope = visitor.context; 246 BlockScope scope = visitor.context;
248 Expect.equals(0, scope.elements.length); 247 Expect.equals(0, scope.elements.length);
249 Expect.equals(6, map(visitor).length); 248 Expect.equals(10, map(visitor).length);
250 249
251 VariableDefinitions initializer = tree.initializer; 250 VariableDefinitions initializer = tree.initializer;
252 Node iNode = initializer.definitions.nodes.head; 251 Node iNode = initializer.definitions.nodes.head;
253 Element iElement = visitor.mapping[iNode]; 252 Element iElement = visitor.mapping[iNode];
254 253
255 // Check that we have the expected nodes. This test relies on the mapping 254 // Check that we have the expected nodes. This test relies on the mapping
256 // field to be a linked hash map (preserving insertion order). 255 // field to be a linked hash map (preserving insertion order).
257 Expect.isTrue(map(visitor) is LinkedHashMap); 256 Expect.isTrue(map(visitor) is LinkedHashMap);
258 List<Node> nodes = map(visitor).getKeys(); 257 List<Node> nodes = map(visitor).getKeys();
259 List<Element> elements = map(visitor).getValues(); 258 List<Element> elements = map(visitor).getValues();
260 259
261 Expect.isTrue(nodes[0] is TypeAnnotation); // int
262 260
263 Expect.isTrue(nodes[1] is SendSet); // i = 0 261 // for (int i = 0; i < 10; i = i + 1) { i = 5; };
264 Expect.equals(elements[1], iElement); 262 // ^^^
263 Expect.isTrue(nodes[0] is TypeAnnotation);
265 264
266 Expect.isTrue(nodes[2] is Send); // i (in i < 10) 265 // for (int i = 0; i < 10; i = i + 1) { i = 5; };
267 Expect.isTrue(nodes[2] is !SendSet); 266 // ^^^^^
268 Expect.equals(elements[2], iElement); 267 checkSendSet(iElement, nodes[1], elements[1]);
269 268
270 Expect.isTrue(nodes[3] is Send); // i (in i + 1) 269 // for (int i = 0; i < 10; i = i + 1) { i = 5; };
271 Expect.isTrue(nodes[3] is !SendSet); 270 // ^
272 Expect.equals(elements[3], iElement); 271 checkIdentifier(iElement, nodes[2], elements[2]);
273 272
274 Expect.isTrue(nodes[4] is SendSet); // i = i + 1 273 // for (int i = 0; i < 10; i = i + 1) { i = 5; };
275 Expect.equals(elements[4], iElement); 274 // ^
275 checkSend(iElement, nodes[3], elements[3]);
276 276
277 Expect.isTrue(nodes[5] is SendSet); // i = 5 277 // for (int i = 0; i < 10; i = i + 1) { i = 5; };
278 Expect.equals(elements[5], iElement); 278 // ^
279 checkIdentifier(iElement, nodes[4], elements[4]);
280
281 // for (int i = 0; i < 10; i = i + 1) { i = 5; };
282 // ^
283 checkIdentifier(iElement, nodes[5], elements[5]);
284
285 // for (int i = 0; i < 10; i = i + 1) { i = 5; };
286 // ^
287 checkSend(iElement, nodes[6], elements[6]);
288
289 // for (int i = 0; i < 10; i = i + 1) { i = 5; };
290 // ^^^^^^^^^
291 checkSendSet(iElement, nodes[7], elements[7]);
292
293 // for (int i = 0; i < 10; i = i + 1) { i = 5; };
294 // ^
295 checkIdentifier(iElement, nodes[8], elements[8]);
296
297 // for (int i = 0; i < 10; i = i + 1) { i = 5; };
298 // ^^^^^
299 checkSendSet(iElement, nodes[9], elements[9]);
300 }
301
302 checkIdentifier(Element expected, Node node, Element actual) {
303 Expect.isTrue(node is Identifier, node.toDebugString());
304 Expect.equals(expected, actual);
305 }
306
307 checkSend(Element expected, Node node, Element actual) {
308 Expect.isTrue(node is Send, node.toDebugString());
309 Expect.isTrue(node is !SendSet, node.toDebugString());
310 Expect.equals(expected, actual);
311 }
312
313 checkSendSet(Element expected, Node node, Element actual) {
314 Expect.isTrue(node is SendSet, node.toDebugString());
315 Expect.equals(expected, actual);
279 } 316 }
280 317
281 testTypeAnnotation() { 318 testTypeAnnotation() {
282 MockCompiler compiler = new MockCompiler(); 319 MockCompiler compiler = new MockCompiler();
283 String statement = "Foo bar;"; 320 String statement = "Foo bar;";
284 321
285 // Test that we get a warning when Foo is not defined. 322 // Test that we get a warning when Foo is not defined.
286 Map mapping = compiler.resolveStatement(statement).map; 323 Map mapping = compiler.resolveStatement(statement).map;
287 324
288 Expect.equals(1, mapping.length); // bar has an element. 325 Expect.equals(1, mapping.length); // bar has an element.
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 A() : this.foo = 1; 572 A() : this.foo = 1;
536 }"""; 573 }""";
537 resolveConstructor(script, "A a = new A();", "A", "A", 0, 574 resolveConstructor(script, "A a = new A();", "A", "A", 0,
538 [], [MessageKind.CANNOT_RESOLVE]); 575 [], [MessageKind.CANNOT_RESOLVE]);
539 576
540 script = """class A { 577 script = """class A {
541 int foo; 578 int foo;
542 int bar; 579 int bar;
543 A() : this.foo = bar; 580 A() : this.foo = bar;
544 }"""; 581 }""";
545 resolveConstructor(script, "A a = new A();", "A", "A", 2, 582 resolveConstructor(script, "A a = new A();", "A", "A", 3,
546 [], [MessageKind.NO_INSTANCE_AVAILABLE]); 583 [], [MessageKind.NO_INSTANCE_AVAILABLE]);
547 584
548 script = """class A { 585 script = """class A {
549 int foo() => 42; 586 int foo() => 42;
550 A() : foo(); 587 A() : foo();
551 }"""; 588 }""";
552 resolveConstructor(script, "A a = new A();", "A", "A", 0, 589 resolveConstructor(script, "A a = new A();", "A", "A", 0,
553 [], [MessageKind.CONSTRUCTOR_CALL_EXPECTED]); 590 [], [MessageKind.CONSTRUCTOR_CALL_EXPECTED]);
554 591
555 script = """class A { 592 script = """class A {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
602 length(Link link) => link.isEmpty() ? 0 : length(link.tail) + 1; 639 length(Link link) => link.isEmpty() ? 0 : length(link.tail) + 1;
603 640
604 at(Link link, int index) => (index == 0) ? link.head : at(link.tail, index - 1); 641 at(Link link, int index) => (index == 0) ? link.head : at(link.tail, index - 1);
605 642
606 List<String> asSortedStrings(Link link) { 643 List<String> asSortedStrings(Link link) {
607 List<String> result = <String>[]; 644 List<String> result = <String>[];
608 for (; !link.isEmpty(); link = link.tail) result.add(link.head.toString()); 645 for (; !link.isEmpty(); link = link.tail) result.add(link.head.toString());
609 result.sort((s1, s2) => s1.compareTo(s2)); 646 result.sort((s1, s2) => s1.compareTo(s2));
610 return result; 647 return result;
611 } 648 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698