| 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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 static const String SET_MUTABLE_VARIABLE = "SetMutableVariable"; | 138 static const String SET_MUTABLE_VARIABLE = "SetMutableVariable"; |
| 139 static const String TYPE_OPERATOR = "TypeOperator"; | 139 static const String TYPE_OPERATOR = "TypeOperator"; |
| 140 | 140 |
| 141 // Primitives | 141 // Primitives |
| 142 static const String CONSTANT = "Constant"; | 142 static const String CONSTANT = "Constant"; |
| 143 static const String CREATE_FUNCTION = "CreateFunction"; | 143 static const String CREATE_FUNCTION = "CreateFunction"; |
| 144 static const String GET_MUTABLE_VARIABLE = "GetMutableVariable"; | 144 static const String GET_MUTABLE_VARIABLE = "GetMutableVariable"; |
| 145 static const String LITERAL_LIST = "LiteralList"; | 145 static const String LITERAL_LIST = "LiteralList"; |
| 146 static const String LITERAL_MAP = "LiteralMap"; | 146 static const String LITERAL_MAP = "LiteralMap"; |
| 147 static const String REIFY_TYPE_VAR = "ReifyTypeVar"; | 147 static const String REIFY_TYPE_VAR = "ReifyTypeVar"; |
| 148 static const String THIS = "This"; | |
| 149 | 148 |
| 150 // Other | 149 // Other |
| 151 static const String FUNCTION_DEFINITION = "FunctionDefinition"; | 150 static const String FUNCTION_DEFINITION = "FunctionDefinition"; |
| 152 static const String IS_TRUE = "IsTrue"; | 151 static const String IS_TRUE = "IsTrue"; |
| 153 | 152 |
| 154 // Constants | 153 // Constants |
| 155 static const String BOOL = "Bool"; | 154 static const String BOOL = "Bool"; |
| 156 static const String DOUBLE = "Double"; | 155 static const String DOUBLE = "Double"; |
| 157 static const String INT = "Int"; | 156 static const String INT = "Int"; |
| 158 static const String NULL = "Null"; | 157 static const String NULL = "Null"; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 183 /// Returns a new named dummy selector with a roughly appropriate kind. | 182 /// Returns a new named dummy selector with a roughly appropriate kind. |
| 184 Selector dummySelector(String name, int argumentCount) { | 183 Selector dummySelector(String name, int argumentCount) { |
| 185 SelectorKind kind; | 184 SelectorKind kind; |
| 186 if (name == "[]") { | 185 if (name == "[]") { |
| 187 kind = SelectorKind.INDEX; | 186 kind = SelectorKind.INDEX; |
| 188 } else if (Elements.isOperatorName(name)) { | 187 } else if (Elements.isOperatorName(name)) { |
| 189 kind = SelectorKind.OPERATOR; | 188 kind = SelectorKind.OPERATOR; |
| 190 } else { | 189 } else { |
| 191 kind = SelectorKind.CALL; | 190 kind = SelectorKind.CALL; |
| 192 } | 191 } |
| 193 return new Selector(kind, new PublicName(name), | 192 return new Selector(kind, new PublicName(name), |
| 194 new CallStructure.unnamed(argumentCount)); | 193 new CallStructure.unnamed(argumentCount)); |
| 195 } | 194 } |
| 196 | 195 |
| 197 /// Returns the tokens in s. Note that string literals are not necessarily | 196 /// Returns the tokens in s. Note that string literals are not necessarily |
| 198 /// preserved; for instance, "(literalString)" is transformed to | 197 /// preserved; for instance, "(literalString)" is transformed to |
| 199 /// " ( literalString ) ". | 198 /// " ( literalString ) ". |
| 200 Tokens tokenize(String s) => | 199 Tokens tokenize(String s) => |
| 201 new Tokens( | 200 new Tokens( |
| 202 s.replaceAll("(", " ( ") | 201 s.replaceAll("(", " ( ") |
| 203 .replaceAll(")", " ) ") | 202 .replaceAll(")", " ) ") |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 /// (ConcatenateStrings (args) cont) | 348 /// (ConcatenateStrings (args) cont) |
| 350 ConcatenateStrings parseConcatenateStrings() { | 349 ConcatenateStrings parseConcatenateStrings() { |
| 351 tokens.consumeStart(CONCATENATE_STRINGS); | 350 tokens.consumeStart(CONCATENATE_STRINGS); |
| 352 | 351 |
| 353 List<Primitive> args = parsePrimitiveList(); | 352 List<Primitive> args = parsePrimitiveList(); |
| 354 | 353 |
| 355 Continuation cont = name2variable[tokens.read()]; | 354 Continuation cont = name2variable[tokens.read()]; |
| 356 assert(cont != null); | 355 assert(cont != null); |
| 357 | 356 |
| 358 tokens.consumeEnd(); | 357 tokens.consumeEnd(); |
| 359 return new ConcatenateStrings(cont, args); | 358 return new ConcatenateStrings(args, cont); |
| 360 } | 359 } |
| 361 | 360 |
| 362 /// (DeclareFunction name = function in body) | 361 /// (DeclareFunction name = function in body) |
| 363 DeclareFunction parseDeclareFunction() { | 362 DeclareFunction parseDeclareFunction() { |
| 364 tokens.consumeStart(DECLARE_FUNCTION); | 363 tokens.consumeStart(DECLARE_FUNCTION); |
| 365 | 364 |
| 366 // name = | 365 // name = |
| 367 MutableVariable local = addMutableVariable(tokens.read()); | 366 MutableVariable local = addMutableVariable(tokens.read()); |
| 368 tokens.read("="); | 367 tokens.read("="); |
| 369 | 368 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 389 dart_types.DartType type = new DummyNamedType(split[0]); | 388 dart_types.DartType type = new DummyNamedType(split[0]); |
| 390 Element element = new DummyElement((split.length == 1) ? "" : split[1]); | 389 Element element = new DummyElement((split.length == 1) ? "" : split[1]); |
| 391 | 390 |
| 392 List<Primitive> args = parsePrimitiveList(); | 391 List<Primitive> args = parsePrimitiveList(); |
| 393 | 392 |
| 394 Continuation cont = name2variable[tokens.read()]; | 393 Continuation cont = name2variable[tokens.read()]; |
| 395 assert(cont != null); | 394 assert(cont != null); |
| 396 | 395 |
| 397 tokens.consumeEnd(); | 396 tokens.consumeEnd(); |
| 398 Selector selector = dummySelector(constructorName, args.length); | 397 Selector selector = dummySelector(constructorName, args.length); |
| 399 return new InvokeConstructor(type, element, selector, cont, args); | 398 return new InvokeConstructor(type, element, selector, args, cont); |
| 400 } | 399 } |
| 401 | 400 |
| 402 /// (InvokeContinuation rec? name (args)) | 401 /// (InvokeContinuation rec? name (args)) |
| 403 InvokeContinuation parseInvokeContinuation() { | 402 InvokeContinuation parseInvokeContinuation() { |
| 404 tokens.consumeStart(INVOKE_CONTINUATION); | 403 tokens.consumeStart(INVOKE_CONTINUATION); |
| 405 String name = tokens.read(); | 404 String name = tokens.read(); |
| 406 bool isRecursive = name == "rec"; | 405 bool isRecursive = name == "rec"; |
| 407 if (isRecursive) name = tokens.read(); | 406 if (isRecursive) name = tokens.read(); |
| 408 | 407 |
| 409 Continuation cont = name2variable[name]; | 408 Continuation cont = name2variable[name]; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 424 | 423 |
| 425 String methodName = tokens.read(); | 424 String methodName = tokens.read(); |
| 426 | 425 |
| 427 List<Primitive> args = parsePrimitiveList(); | 426 List<Primitive> args = parsePrimitiveList(); |
| 428 | 427 |
| 429 Continuation cont = name2variable[tokens.read()]; | 428 Continuation cont = name2variable[tokens.read()]; |
| 430 assert(cont != null); | 429 assert(cont != null); |
| 431 | 430 |
| 432 tokens.consumeEnd(); | 431 tokens.consumeEnd(); |
| 433 Selector selector = dummySelector(methodName, args.length); | 432 Selector selector = dummySelector(methodName, args.length); |
| 434 return new InvokeMethod(receiver, selector, cont, args); | 433 return new InvokeMethod(receiver, selector, args, cont); |
| 435 } | 434 } |
| 436 | 435 |
| 437 /// (InvokeStatic method (args) cont) | 436 /// (InvokeStatic method (args) cont) |
| 438 InvokeStatic parseInvokeStatic() { | 437 InvokeStatic parseInvokeStatic() { |
| 439 tokens.consumeStart(INVOKE_STATIC); | 438 tokens.consumeStart(INVOKE_STATIC); |
| 440 | 439 |
| 441 String methodName = tokens.read(); | 440 String methodName = tokens.read(); |
| 442 | 441 |
| 443 List<Primitive> args = parsePrimitiveList(); | 442 List<Primitive> args = parsePrimitiveList(); |
| 444 | 443 |
| 445 Continuation cont = name2variable[tokens.read()]; | 444 Continuation cont = name2variable[tokens.read()]; |
| 446 assert(cont != null); | 445 assert(cont != null); |
| 447 | 446 |
| 448 Entity entity = new DummyEntity(methodName); | 447 Entity entity = new DummyEntity(methodName); |
| 449 Selector selector = dummySelector(methodName, args.length); | 448 Selector selector = dummySelector(methodName, args.length); |
| 450 | 449 |
| 451 tokens.consumeEnd(); | 450 tokens.consumeEnd(); |
| 452 return new InvokeStatic(entity, selector, cont, args, null); | 451 return new InvokeStatic(entity, selector, args, cont, null); |
| 453 } | 452 } |
| 454 | 453 |
| 455 /// (InvokeMethodDirectly receiver method (args) cont) | 454 /// (InvokeMethodDirectly receiver method (args) cont) |
| 456 InvokeMethodDirectly parseInvokeMethodDirectly() { | 455 InvokeMethodDirectly parseInvokeMethodDirectly() { |
| 457 tokens.consumeStart(INVOKE_METHOD_DIRECTLY); | 456 tokens.consumeStart(INVOKE_METHOD_DIRECTLY); |
| 458 | 457 |
| 459 Definition receiver = name2variable[tokens.read()]; | 458 Definition receiver = name2variable[tokens.read()]; |
| 460 assert(receiver != null); | 459 assert(receiver != null); |
| 461 | 460 |
| 462 String methodName = tokens.read(); | 461 String methodName = tokens.read(); |
| 463 | 462 |
| 464 List<Primitive> args = parsePrimitiveList(); | 463 List<Primitive> args = parsePrimitiveList(); |
| 465 | 464 |
| 466 Continuation cont = name2variable[tokens.read()]; | 465 Continuation cont = name2variable[tokens.read()]; |
| 467 assert(cont != null); | 466 assert(cont != null); |
| 468 | 467 |
| 469 tokens.consumeEnd(); | 468 tokens.consumeEnd(); |
| 470 Element element = new DummyElement(methodName); | 469 Element element = new DummyElement(methodName); |
| 471 Selector selector = dummySelector(methodName, args.length); | 470 Selector selector = dummySelector(methodName, args.length); |
| 472 return new InvokeMethodDirectly(receiver, element, selector, cont, args); | 471 return new InvokeMethodDirectly(receiver, element, selector, args, cont); |
| 473 } | 472 } |
| 474 | 473 |
| 475 // (rec? name (args) body) | 474 // (rec? name (args) body) |
| 476 Continuation parseContinuation() { | 475 Continuation parseContinuation() { |
| 477 // (rec? name | 476 // (rec? name |
| 478 tokens.consumeStart(); | 477 tokens.consumeStart(); |
| 479 String name = tokens.read(); | 478 String name = tokens.read(); |
| 480 bool isRecursive = name == "rec"; | 479 bool isRecursive = name == "rec"; |
| 481 if (isRecursive) name = tokens.read(); | 480 if (isRecursive) name = tokens.read(); |
| 482 | 481 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 595 case CREATE_FUNCTION: | 594 case CREATE_FUNCTION: |
| 596 return parseCreateFunction(); | 595 return parseCreateFunction(); |
| 597 case GET_MUTABLE_VARIABLE: | 596 case GET_MUTABLE_VARIABLE: |
| 598 return parseGetMutableVariable(); | 597 return parseGetMutableVariable(); |
| 599 case LITERAL_LIST: | 598 case LITERAL_LIST: |
| 600 return parseLiteralList(); | 599 return parseLiteralList(); |
| 601 case LITERAL_MAP: | 600 case LITERAL_MAP: |
| 602 return parseLiteralMap(); | 601 return parseLiteralMap(); |
| 603 case REIFY_TYPE_VAR: | 602 case REIFY_TYPE_VAR: |
| 604 return parseReifyTypeVar(); | 603 return parseReifyTypeVar(); |
| 605 case THIS: | |
| 606 return parseThis(); | |
| 607 default: | 604 default: |
| 608 assert(false); | 605 assert(false); |
| 609 } | 606 } |
| 610 | 607 |
| 611 return null; | 608 return null; |
| 612 } | 609 } |
| 613 | 610 |
| 614 /// (Constant (constant)) | 611 /// (Constant (constant)) |
| 615 Constant parseConstant() { | 612 Constant parseConstant() { |
| 616 tokens.consumeStart(CONSTANT); | 613 tokens.consumeStart(CONSTANT); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 | 719 |
| 723 /// (ReifyTypeVar type) | 720 /// (ReifyTypeVar type) |
| 724 ReifyTypeVar parseReifyTypeVar() { | 721 ReifyTypeVar parseReifyTypeVar() { |
| 725 tokens.consumeStart(REIFY_TYPE_VAR); | 722 tokens.consumeStart(REIFY_TYPE_VAR); |
| 726 | 723 |
| 727 TypeVariableElement type = new DummyElement(tokens.read()); | 724 TypeVariableElement type = new DummyElement(tokens.read()); |
| 728 | 725 |
| 729 tokens.consumeEnd(); | 726 tokens.consumeEnd(); |
| 730 return new ReifyTypeVar(type); | 727 return new ReifyTypeVar(type); |
| 731 } | 728 } |
| 732 | |
| 733 /// (This) | |
| 734 This parseThis() { | |
| 735 tokens.consumeStart(THIS); | |
| 736 tokens.consumeEnd(); | |
| 737 return new This(); | |
| 738 } | |
| 739 } | 729 } |
| OLD | NEW |