| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 // SExpressionUnstringifier implements the inverse operation to | 5 // SExpressionUnstringifier implements the inverse operation to |
| 6 // [SExpressionStringifier]. | 6 // [SExpressionStringifier]. |
| 7 | 7 |
| 8 library sexpr_unstringifier; | 8 library sexpr_unstringifier; |
| 9 | 9 |
| 10 import 'package:compiler/src/constants/expressions.dart' | 10 import 'package:compiler/src/constants/expressions.dart' |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 return parseSetClosureVariable(); | 240 return parseSetClosureVariable(); |
| 241 case TYPE_OPERATOR: | 241 case TYPE_OPERATOR: |
| 242 return parseTypeOperator(); | 242 return parseTypeOperator(); |
| 243 default: | 243 default: |
| 244 assert(false); | 244 assert(false); |
| 245 } | 245 } |
| 246 | 246 |
| 247 return null; | 247 return null; |
| 248 } | 248 } |
| 249 | 249 |
| 250 /// prim1 prim2 ... primn cont) | |
| 251 /// Note that cont is *not* included in the returned list and not consumed. | |
| 252 List<Primitive> parseArgumentList() { | |
| 253 List<Primitive> args = <Primitive>[]; | |
| 254 while (tokens.next != ")") { | |
| 255 Primitive prim = name2variable[tokens.read()]; | |
| 256 assert(prim != null); | |
| 257 args.add(prim); | |
| 258 } | |
| 259 return args; | |
| 260 } | |
| 261 | |
| 262 /// (prim1 prim2 ... primn) | 250 /// (prim1 prim2 ... primn) |
| 263 List<Primitive> parsePrimitiveList() { | 251 List<Primitive> parsePrimitiveList() { |
| 264 tokens.consumeStart(); | 252 tokens.consumeStart(); |
| 265 List<Primitive> prims = <Primitive>[]; | 253 List<Primitive> prims = <Primitive>[]; |
| 266 while (tokens.current != ")") { | 254 while (tokens.current != ")") { |
| 267 Primitive prim = name2variable[tokens.read()]; | 255 Primitive prim = name2variable[tokens.read()]; |
| 268 assert(prim != null); | 256 assert(prim != null); |
| 269 prims.add(prim); | 257 prims.add(prim); |
| 270 } | 258 } |
| 271 tokens.consumeEnd(); | 259 tokens.consumeEnd(); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 | 330 |
| 343 Condition cond = parseCondition(); | 331 Condition cond = parseCondition(); |
| 344 Continuation trueCont = name2variable[tokens.read()]; | 332 Continuation trueCont = name2variable[tokens.read()]; |
| 345 Continuation falseCont = name2variable[tokens.read()]; | 333 Continuation falseCont = name2variable[tokens.read()]; |
| 346 assert(trueCont != null && falseCont != null); | 334 assert(trueCont != null && falseCont != null); |
| 347 | 335 |
| 348 tokens.consumeEnd(); | 336 tokens.consumeEnd(); |
| 349 return new Branch(cond, trueCont, falseCont); | 337 return new Branch(cond, trueCont, falseCont); |
| 350 } | 338 } |
| 351 | 339 |
| 352 /// (ConcatenateStrings args cont) | 340 /// (ConcatenateStrings (args) cont) |
| 353 ConcatenateStrings parseConcatenateStrings() { | 341 ConcatenateStrings parseConcatenateStrings() { |
| 354 tokens.consumeStart(CONCATENATE_STRINGS); | 342 tokens.consumeStart(CONCATENATE_STRINGS); |
| 355 | 343 |
| 356 List<Primitive> args = parseArgumentList(); | 344 List<Primitive> args = parsePrimitiveList(); |
| 357 | 345 |
| 358 Continuation cont = name2variable[tokens.read()]; | 346 Continuation cont = name2variable[tokens.read()]; |
| 359 assert(cont != null); | 347 assert(cont != null); |
| 360 | 348 |
| 361 tokens.consumeEnd(); | 349 tokens.consumeEnd(); |
| 362 return new ConcatenateStrings(cont, args); | 350 return new ConcatenateStrings(cont, args); |
| 363 } | 351 } |
| 364 | 352 |
| 365 /// (DeclareFunction name = function in body) | 353 /// (DeclareFunction name = function in body) |
| 366 DeclareFunction parseDeclareFunction() { | 354 DeclareFunction parseDeclareFunction() { |
| 367 tokens.consumeStart(DECLARE_FUNCTION); | 355 tokens.consumeStart(DECLARE_FUNCTION); |
| 368 | 356 |
| 369 // name = | 357 // name = |
| 370 ClosureVariable local = name2variable[tokens.read()]; | 358 ClosureVariable local = name2variable[tokens.read()]; |
| 371 tokens.read("="); | 359 tokens.read("="); |
| 372 | 360 |
| 373 // function in | 361 // function in |
| 374 FunctionDefinition def = parseFunctionDefinition(); | 362 FunctionDefinition def = parseFunctionDefinition(); |
| 375 tokens.read("in"); | 363 tokens.read("in"); |
| 376 | 364 |
| 377 // body | 365 // body |
| 378 Expression body = parseExpression(); | 366 Expression body = parseExpression(); |
| 379 | 367 |
| 380 tokens.consumeEnd(); | 368 tokens.consumeEnd(); |
| 381 return new DeclareFunction(local, def)..plug(body); | 369 return new DeclareFunction(local, def)..plug(body); |
| 382 } | 370 } |
| 383 | 371 |
| 384 /// (InvokeConstructor name args cont) | 372 /// (InvokeConstructor name (args) cont) |
| 385 InvokeConstructor parseInvokeConstructor() { | 373 InvokeConstructor parseInvokeConstructor() { |
| 386 tokens.consumeStart(INVOKE_CONSTRUCTOR); | 374 tokens.consumeStart(INVOKE_CONSTRUCTOR); |
| 387 | 375 |
| 388 String constructorName = tokens.read(); | 376 String constructorName = tokens.read(); |
| 389 List<String> split = constructorName.split("."); | 377 List<String> split = constructorName.split("."); |
| 390 assert(split.length < 3); | 378 assert(split.length < 3); |
| 391 | 379 |
| 392 dart_types.DartType type = new DummyNamedType(split[0]); | 380 dart_types.DartType type = new DummyNamedType(split[0]); |
| 393 Element element = new DummyElement((split.length == 1) ? "" : split[1]); | 381 Element element = new DummyElement((split.length == 1) ? "" : split[1]); |
| 394 | 382 |
| 395 List<Primitive> args = parseArgumentList(); | 383 List<Primitive> args = parsePrimitiveList(); |
| 396 | 384 |
| 397 Continuation cont = name2variable[tokens.read()]; | 385 Continuation cont = name2variable[tokens.read()]; |
| 398 assert(cont != null); | 386 assert(cont != null); |
| 399 | 387 |
| 400 tokens.consumeEnd(); | 388 tokens.consumeEnd(); |
| 401 Selector selector = dummySelector(constructorName, args.length); | 389 Selector selector = dummySelector(constructorName, args.length); |
| 402 return new InvokeConstructor(type, element, selector, cont, args); | 390 return new InvokeConstructor(type, element, selector, cont, args); |
| 403 } | 391 } |
| 404 | 392 |
| 405 /// (InvokeContinuation name args) | 393 /// (InvokeContinuation name (args)) |
| 406 InvokeContinuation parseInvokeContinuation(bool recursive) { | 394 InvokeContinuation parseInvokeContinuation(bool recursive) { |
| 407 tokens.consumeStart(recursive | 395 tokens.consumeStart(recursive |
| 408 ? INVOKE_CONTINUATION_RECURSIVE : INVOKE_CONTINUATION); | 396 ? INVOKE_CONTINUATION_RECURSIVE : INVOKE_CONTINUATION); |
| 409 | 397 |
| 410 Continuation cont = name2variable[tokens.read()]; | 398 Continuation cont = name2variable[tokens.read()]; |
| 411 assert(cont != null); | 399 assert(cont != null); |
| 412 | 400 |
| 413 List<Primitive> args = <Primitive>[]; | 401 List<Primitive> args = parsePrimitiveList(); |
| 414 while (tokens.current != ")") { | |
| 415 Primitive arg = name2variable[tokens.read()]; | |
| 416 assert(arg != null); | |
| 417 args.add(arg); | |
| 418 } | |
| 419 | 402 |
| 420 tokens.consumeEnd(); | 403 tokens.consumeEnd(); |
| 421 return new InvokeContinuation(cont, args, recursive: recursive); | 404 return new InvokeContinuation(cont, args, recursive: recursive); |
| 422 } | 405 } |
| 423 | 406 |
| 424 /// (InvokeMethod receiver method args cont) | 407 /// (InvokeMethod receiver method (args) cont) |
| 425 InvokeMethod parseInvokeMethod() { | 408 InvokeMethod parseInvokeMethod() { |
| 426 tokens.consumeStart(INVOKE_METHOD); | 409 tokens.consumeStart(INVOKE_METHOD); |
| 427 | 410 |
| 428 Definition receiver = name2variable[tokens.read()]; | 411 Definition receiver = name2variable[tokens.read()]; |
| 429 assert(receiver != null); | 412 assert(receiver != null); |
| 430 | 413 |
| 431 String methodName = tokens.read(); | 414 String methodName = tokens.read(); |
| 432 | 415 |
| 433 List<Primitive> args = parseArgumentList(); | 416 List<Primitive> args = parsePrimitiveList(); |
| 434 | 417 |
| 435 Continuation cont = name2variable[tokens.read()]; | 418 Continuation cont = name2variable[tokens.read()]; |
| 436 assert(cont != null); | 419 assert(cont != null); |
| 437 | 420 |
| 438 tokens.consumeEnd(); | 421 tokens.consumeEnd(); |
| 439 Selector selector = dummySelector(methodName, args.length); | 422 Selector selector = dummySelector(methodName, args.length); |
| 440 return new InvokeMethod(receiver, selector, cont, args); | 423 return new InvokeMethod(receiver, selector, cont, args); |
| 441 } | 424 } |
| 442 | 425 |
| 443 /// (InvokeStatic method args cont) | 426 /// (InvokeStatic method (args) cont) |
| 444 InvokeStatic parseInvokeStatic() { | 427 InvokeStatic parseInvokeStatic() { |
| 445 tokens.consumeStart(INVOKE_STATIC); | 428 tokens.consumeStart(INVOKE_STATIC); |
| 446 | 429 |
| 447 String methodName = tokens.read(); | 430 String methodName = tokens.read(); |
| 448 | 431 |
| 449 List<Primitive> args = parseArgumentList(); | 432 List<Primitive> args = parsePrimitiveList(); |
| 450 | 433 |
| 451 Continuation cont = name2variable[tokens.read()]; | 434 Continuation cont = name2variable[tokens.read()]; |
| 452 assert(cont != null); | 435 assert(cont != null); |
| 453 | 436 |
| 454 Entity entity = new DummyEntity(methodName); | 437 Entity entity = new DummyEntity(methodName); |
| 455 Selector selector = dummySelector(methodName, args.length); | 438 Selector selector = dummySelector(methodName, args.length); |
| 456 | 439 |
| 457 tokens.consumeEnd(); | 440 tokens.consumeEnd(); |
| 458 return new InvokeStatic(entity, selector, cont, args); | 441 return new InvokeStatic(entity, selector, cont, args); |
| 459 } | 442 } |
| 460 | 443 |
| 461 /// (InvokeSuperMethod method args cont) | 444 /// (InvokeSuperMethod method (args) cont) |
| 462 InvokeSuperMethod parseInvokeSuperMethod() { | 445 InvokeSuperMethod parseInvokeSuperMethod() { |
| 463 tokens.consumeStart(INVOKE_SUPER_METHOD); | 446 tokens.consumeStart(INVOKE_SUPER_METHOD); |
| 464 | 447 |
| 465 String methodName = tokens.read(); | 448 String methodName = tokens.read(); |
| 466 | 449 |
| 467 List<Primitive> args = parseArgumentList(); | 450 List<Primitive> args = parsePrimitiveList(); |
| 468 | 451 |
| 469 Continuation cont = name2variable[tokens.read()]; | 452 Continuation cont = name2variable[tokens.read()]; |
| 470 assert(cont != null); | 453 assert(cont != null); |
| 471 | 454 |
| 472 tokens.consumeEnd(); | 455 tokens.consumeEnd(); |
| 473 Selector selector = dummySelector(methodName, args.length); | 456 Selector selector = dummySelector(methodName, args.length); |
| 474 return new InvokeSuperMethod(selector, cont, args); | 457 return new InvokeSuperMethod(selector, cont, args); |
| 475 } | 458 } |
| 476 | 459 |
| 477 /// (LetCont (cont args) (cont_body)) body | 460 /// (LetCont (name (args) cont_body) body) |
| 478 LetCont parseLetCont(bool recursive) { | 461 LetCont parseLetCont(bool recursive) { |
| 479 tokens.consumeStart(recursive ? LET_CONT_RECURSIVE : LET_CONT); | 462 tokens.consumeStart(recursive ? LET_CONT_RECURSIVE : LET_CONT); |
| 480 | 463 |
| 481 // (name args) (cont_body)) | 464 // (name |
| 482 tokens.consumeStart(); | 465 tokens.consumeStart(); |
| 483 String name = tokens.read(); | 466 String name = tokens.read(); |
| 484 | 467 |
| 468 // (args) |
| 469 tokens.consumeStart(); |
| 485 List<Parameter> params = <Parameter>[]; | 470 List<Parameter> params = <Parameter>[]; |
| 486 while (tokens.current != ")") { | 471 while (tokens.current != ")") { |
| 487 String paramName = tokens.read(); | 472 String paramName = tokens.read(); |
| 488 Parameter param = new Parameter(new DummyElement(paramName)); | 473 Parameter param = new Parameter(new DummyElement(paramName)); |
| 489 name2variable[paramName] = param; | 474 name2variable[paramName] = param; |
| 490 params.add(param); | 475 params.add(param); |
| 491 } | 476 } |
| 492 tokens.consumeEnd(); | 477 tokens.consumeEnd(); |
| 493 | 478 |
| 494 Continuation cont = new Continuation(params); | 479 Continuation cont = new Continuation(params); |
| 495 name2variable[name] = cont; | 480 name2variable[name] = cont; |
| 496 | 481 |
| 497 cont.isRecursive = recursive; | 482 cont.isRecursive = recursive; |
| 483 // cont_body |
| 498 cont.body = parseExpression(); | 484 cont.body = parseExpression(); |
| 499 tokens.consumeEnd(); | 485 tokens.consumeEnd(); |
| 500 | 486 |
| 501 // body | 487 // body) |
| 502 Expression body = parseExpression(); | 488 Expression body = parseExpression(); |
| 489 tokens.consumeEnd(); |
| 503 | 490 |
| 504 return new LetCont(cont, body); | 491 return new LetCont(cont, body); |
| 505 } | 492 } |
| 506 | 493 |
| 507 /// (SetClosureVariable name value body) | 494 /// (SetClosureVariable name value body) |
| 508 SetClosureVariable parseSetClosureVariable() { | 495 SetClosureVariable parseSetClosureVariable() { |
| 509 tokens.consumeStart(SET_CLOSURE_VARIABLE); | 496 tokens.consumeStart(SET_CLOSURE_VARIABLE); |
| 510 | 497 |
| 511 ClosureVariable local = name2variable[tokens.read()]; | 498 ClosureVariable local = name2variable[tokens.read()]; |
| 512 Primitive value = name2variable[tokens.read()]; | 499 Primitive value = name2variable[tokens.read()]; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 530 | 517 |
| 531 dart_types.DartType type = new DummyNamedType(tokens.read()); | 518 dart_types.DartType type = new DummyNamedType(tokens.read()); |
| 532 | 519 |
| 533 Continuation cont = name2variable[tokens.read()]; | 520 Continuation cont = name2variable[tokens.read()]; |
| 534 assert(cont != null); | 521 assert(cont != null); |
| 535 | 522 |
| 536 tokens.consumeEnd(); | 523 tokens.consumeEnd(); |
| 537 return new TypeOperator(recv, type, cont, isTypeTest: operator == 'is'); | 524 return new TypeOperator(recv, type, cont, isTypeTest: operator == 'is'); |
| 538 } | 525 } |
| 539 | 526 |
| 540 /// (LetPrim name (primitive)) body | 527 /// (LetPrim (name primitive) body) |
| 541 LetPrim parseLetPrim() { | 528 LetPrim parseLetPrim() { |
| 542 tokens.consumeStart(LET_PRIM); | 529 tokens.consumeStart(LET_PRIM); |
| 543 | 530 |
| 544 // name | 531 // (name |
| 532 tokens.consumeStart(); |
| 545 String name = tokens.read(); | 533 String name = tokens.read(); |
| 546 | 534 |
| 547 // (primitive) | 535 // primitive) |
| 548 Primitive primitive = parsePrimitive(); | 536 Primitive primitive = parsePrimitive(); |
| 549 name2variable[name] = primitive; | 537 name2variable[name] = primitive; |
| 550 tokens.consumeEnd(); | 538 tokens.consumeEnd(); |
| 551 | 539 |
| 552 // body | 540 // body) |
| 553 Expression body = parseExpression(); | 541 Expression body = parseExpression(); |
| 542 tokens.consumeEnd(); |
| 554 | 543 |
| 555 return new LetPrim(primitive)..plug(body); | 544 return new LetPrim(primitive)..plug(body); |
| 556 } | 545 } |
| 557 | 546 |
| 558 Primitive parsePrimitive() { | 547 Primitive parsePrimitive() { |
| 559 assert(tokens.current == "("); | 548 assert(tokens.current == "("); |
| 560 | 549 |
| 561 switch (tokens.next) { | 550 switch (tokens.next) { |
| 562 case CONSTANT: | 551 case CONSTANT: |
| 563 return parseConstant(); | 552 return parseConstant(); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 651 /// (GetClosureVariable name) | 640 /// (GetClosureVariable name) |
| 652 GetClosureVariable parseGetClosureVariable() { | 641 GetClosureVariable parseGetClosureVariable() { |
| 653 tokens.consumeStart(GET_CLOSURE_VARIABLE); | 642 tokens.consumeStart(GET_CLOSURE_VARIABLE); |
| 654 | 643 |
| 655 ClosureVariable local = name2variable[tokens.read()]; | 644 ClosureVariable local = name2variable[tokens.read()]; |
| 656 tokens.consumeEnd(); | 645 tokens.consumeEnd(); |
| 657 | 646 |
| 658 return new GetClosureVariable(local); | 647 return new GetClosureVariable(local); |
| 659 } | 648 } |
| 660 | 649 |
| 661 /// (LiteralList values) | 650 /// (LiteralList (values)) |
| 662 LiteralList parseLiteralList() { | 651 LiteralList parseLiteralList() { |
| 663 tokens.consumeStart(LITERAL_LIST); | 652 tokens.consumeStart(LITERAL_LIST); |
| 664 List<Primitive> values = parsePrimitiveList(); | 653 List<Primitive> values = parsePrimitiveList(); |
| 665 tokens.consumeEnd(); | 654 tokens.consumeEnd(); |
| 666 return new LiteralList(null, values); | 655 return new LiteralList(null, values); |
| 667 } | 656 } |
| 668 | 657 |
| 669 /// (LiteralMap keys values) | 658 /// (LiteralMap (keys) (values)) |
| 670 LiteralMap parseLiteralMap() { | 659 LiteralMap parseLiteralMap() { |
| 671 tokens.consumeStart(LITERAL_MAP); | 660 tokens.consumeStart(LITERAL_MAP); |
| 672 | 661 |
| 673 List<Primitive> keys = parsePrimitiveList(); | 662 List<Primitive> keys = parsePrimitiveList(); |
| 674 List<Primitive> values = parsePrimitiveList(); | 663 List<Primitive> values = parsePrimitiveList(); |
| 675 | 664 |
| 676 List<LiteralMapEntry> entries = <LiteralMapEntry>[]; | 665 List<LiteralMapEntry> entries = <LiteralMapEntry>[]; |
| 677 for (int i = 0; i < keys.length; i++) { | 666 for (int i = 0; i < keys.length; i++) { |
| 678 entries.add(new LiteralMapEntry(keys[i], values[i])); | 667 entries.add(new LiteralMapEntry(keys[i], values[i])); |
| 679 } | 668 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 692 return new ReifyTypeVar(type); | 681 return new ReifyTypeVar(type); |
| 693 } | 682 } |
| 694 | 683 |
| 695 /// (This) | 684 /// (This) |
| 696 This parseThis() { | 685 This parseThis() { |
| 697 tokens.consumeStart(THIS); | 686 tokens.consumeStart(THIS); |
| 698 tokens.consumeEnd(); | 687 tokens.consumeEnd(); |
| 699 return new This(); | 688 return new This(); |
| 700 } | 689 } |
| 701 } | 690 } |
| OLD | NEW |