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 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 return new Selector(kind, name, null, argumentCount); | 177 return new Selector(kind, name, null, argumentCount); |
178 } | 178 } |
179 | 179 |
180 /// Returns the tokens in s. Note that string literals are not necessarily | 180 /// Returns the tokens in s. Note that string literals are not necessarily |
181 /// preserved; for instance, "(literalString)" is transformed to | 181 /// preserved; for instance, "(literalString)" is transformed to |
182 /// " ( literalString ) ". | 182 /// " ( literalString ) ". |
183 Tokens tokenize(String s) => | 183 Tokens tokenize(String s) => |
184 new Tokens( | 184 new Tokens( |
185 s.replaceAll("(", " ( ") | 185 s.replaceAll("(", " ( ") |
186 .replaceAll(")", " ) ") | 186 .replaceAll(")", " ) ") |
| 187 .replaceAll("{", " { ") |
| 188 .replaceAll("}", " } ") |
187 .replaceAll(new RegExp(r"[ \t\n]+"), " ") | 189 .replaceAll(new RegExp(r"[ \t\n]+"), " ") |
188 .trim() | 190 .trim() |
189 .split(" ") | 191 .split(" ") |
190 .map(canonicalizeOperators) | 192 .map(canonicalizeOperators) |
191 .toList()); | 193 .toList()); |
192 | 194 |
193 /// Canonicalizes strings containing operator names. | 195 /// Canonicalizes strings containing operator names. |
194 String canonicalizeOperators(String token) { | 196 String canonicalizeOperators(String token) { |
195 String opname = OPERATORS.lookup(token); | 197 String opname = OPERATORS.lookup(token); |
196 if (opname != null) { | 198 if (opname != null) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 tokens.consumeEnd(); | 264 tokens.consumeEnd(); |
263 return prims; | 265 return prims; |
264 } | 266 } |
265 | 267 |
266 /// (FunctionDefinition name (args cont) body) | 268 /// (FunctionDefinition name (args cont) body) |
267 FunctionDefinition parseFunctionDefinition() { | 269 FunctionDefinition parseFunctionDefinition() { |
268 tokens.consumeStart(FUNCTION_DEFINITION); | 270 tokens.consumeStart(FUNCTION_DEFINITION); |
269 | 271 |
270 // name | 272 // name |
271 Element element = new DummyElement(""); | 273 Element element = new DummyElement(""); |
272 if (tokens.current != "(") { | 274 if (tokens.current != '(' && tokens.current != '{') { |
273 // This is a named function. | 275 // This is a named function. |
274 element = new DummyElement(tokens.read()); | 276 element = new DummyElement(tokens.read()); |
275 } | 277 } |
276 | 278 |
| 279 // [closure-vars] |
| 280 List<ClosureVariable> closureVariables = <ClosureVariable>[]; |
| 281 if (tokens.current == '{') { |
| 282 tokens.read('{'); |
| 283 while (tokens.current != '}') { |
| 284 String varName = tokens.read(); |
| 285 ClosureVariable variable = |
| 286 new ClosureVariable(element, new DummyElement(varName)); |
| 287 closureVariables.add(variable); |
| 288 name2variable[varName] = variable; |
| 289 } |
| 290 tokens.read('}'); |
| 291 } |
| 292 |
277 // (args cont) | 293 // (args cont) |
278 List<Parameter> parameters = <Parameter>[]; | 294 List<Definition> parameters = <Definition>[]; |
279 tokens.consumeStart(); | 295 tokens.consumeStart(); |
280 while (tokens.next != ")") { | 296 while (tokens.next != ")") { |
281 String paramName = tokens.read(); | 297 String paramName = tokens.read(); |
282 Parameter param = new Parameter(new DummyElement(paramName)); | 298 if (name2variable.containsKey(paramName)) { |
283 name2variable[paramName] = param; | 299 parameters.add(name2variable[paramName]); |
284 parameters.add(param); | 300 } else { |
| 301 Parameter param = new Parameter(new DummyElement(paramName)); |
| 302 name2variable[paramName] = param; |
| 303 parameters.add(param); |
| 304 } |
285 } | 305 } |
286 | 306 |
287 String contName = tokens.read("return"); | 307 String contName = tokens.read("return"); |
288 Continuation cont = name2variable[contName]; | 308 Continuation cont = name2variable[contName]; |
289 assert(cont != null); | 309 assert(cont != null); |
290 tokens.consumeEnd(); | 310 tokens.consumeEnd(); |
291 | 311 |
292 // body | 312 // body |
293 Expression body = parseExpression(); | 313 Expression body = parseExpression(); |
294 | 314 |
295 tokens.consumeEnd(); | 315 tokens.consumeEnd(); |
296 return new FunctionDefinition(element, cont, parameters, body, null, null); | 316 return new FunctionDefinition(element, cont, parameters, body, null, null, |
| 317 closureVariables); |
297 } | 318 } |
298 | 319 |
299 /// (IsTrue arg) | 320 /// (IsTrue arg) |
300 Condition parseCondition() { | 321 Condition parseCondition() { |
301 // Handles IsTrue only for now. | 322 // Handles IsTrue only for now. |
302 tokens.consumeStart(IS_TRUE); | 323 tokens.consumeStart(IS_TRUE); |
303 | 324 |
304 Definition value = name2variable[tokens.read()]; | 325 Definition value = name2variable[tokens.read()]; |
305 assert(value != null); | 326 assert(value != null); |
306 | 327 |
(...skipping 25 matching lines...) Expand all Loading... |
332 | 353 |
333 tokens.consumeEnd(); | 354 tokens.consumeEnd(); |
334 return new ConcatenateStrings(cont, args); | 355 return new ConcatenateStrings(cont, args); |
335 } | 356 } |
336 | 357 |
337 /// (DeclareFunction name = function in body) | 358 /// (DeclareFunction name = function in body) |
338 DeclareFunction parseDeclareFunction() { | 359 DeclareFunction parseDeclareFunction() { |
339 tokens.consumeStart(DECLARE_FUNCTION); | 360 tokens.consumeStart(DECLARE_FUNCTION); |
340 | 361 |
341 // name = | 362 // name = |
342 Local local = new DummyLocal(tokens.read()); | 363 ClosureVariable local = name2variable[tokens.read()]; |
343 tokens.read("="); | 364 tokens.read("="); |
344 | 365 |
345 // function in | 366 // function in |
346 FunctionDefinition def = parseFunctionDefinition(); | 367 FunctionDefinition def = parseFunctionDefinition(); |
347 tokens.read("in"); | 368 tokens.read("in"); |
348 | 369 |
349 // body | 370 // body |
350 Expression body = parseExpression(); | 371 Expression body = parseExpression(); |
351 | 372 |
352 tokens.consumeEnd(); | 373 tokens.consumeEnd(); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 // body | 494 // body |
474 Expression body = parseExpression(); | 495 Expression body = parseExpression(); |
475 | 496 |
476 return new LetCont(cont, body); | 497 return new LetCont(cont, body); |
477 } | 498 } |
478 | 499 |
479 /// (SetClosureVariable name value body) | 500 /// (SetClosureVariable name value body) |
480 SetClosureVariable parseSetClosureVariable() { | 501 SetClosureVariable parseSetClosureVariable() { |
481 tokens.consumeStart(SET_CLOSURE_VARIABLE); | 502 tokens.consumeStart(SET_CLOSURE_VARIABLE); |
482 | 503 |
483 Local local = new DummyLocal(tokens.read()); | 504 ClosureVariable local = name2variable[tokens.read()]; |
484 Primitive value = name2variable[tokens.read()]; | 505 Primitive value = name2variable[tokens.read()]; |
485 assert(value != null); | 506 assert(value != null); |
486 | 507 |
487 Expression body = parseExpression(); | 508 Expression body = parseExpression(); |
488 | 509 |
489 tokens.consumeEnd(); | 510 tokens.consumeEnd(); |
490 return new SetClosureVariable(local, value) | 511 return new SetClosureVariable(local, value) |
491 ..plug(body); | 512 ..plug(body); |
492 } | 513 } |
493 | 514 |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 tokens.consumeStart(CREATE_FUNCTION); | 656 tokens.consumeStart(CREATE_FUNCTION); |
636 FunctionDefinition def = parseFunctionDefinition(); | 657 FunctionDefinition def = parseFunctionDefinition(); |
637 tokens.consumeEnd(); | 658 tokens.consumeEnd(); |
638 return new CreateFunction(def); | 659 return new CreateFunction(def); |
639 } | 660 } |
640 | 661 |
641 /// (GetClosureVariable name) | 662 /// (GetClosureVariable name) |
642 GetClosureVariable parseGetClosureVariable() { | 663 GetClosureVariable parseGetClosureVariable() { |
643 tokens.consumeStart(GET_CLOSURE_VARIABLE); | 664 tokens.consumeStart(GET_CLOSURE_VARIABLE); |
644 | 665 |
645 Local local = new DummyLocal(tokens.read()); | 666 ClosureVariable local = name2variable[tokens.read()]; |
646 tokens.consumeEnd(); | 667 tokens.consumeEnd(); |
647 | 668 |
648 return new GetClosureVariable(local); | 669 return new GetClosureVariable(local); |
649 } | 670 } |
650 | 671 |
651 /// (LiteralList values) | 672 /// (LiteralList values) |
652 LiteralList parseLiteralList() { | 673 LiteralList parseLiteralList() { |
653 tokens.consumeStart(LITERAL_LIST); | 674 tokens.consumeStart(LITERAL_LIST); |
654 List<Primitive> values = parsePrimitiveList(); | 675 List<Primitive> values = parsePrimitiveList(); |
655 tokens.consumeEnd(); | 676 tokens.consumeEnd(); |
(...skipping 26 matching lines...) Expand all Loading... |
682 return new ReifyTypeVar(type); | 703 return new ReifyTypeVar(type); |
683 } | 704 } |
684 | 705 |
685 /// (This) | 706 /// (This) |
686 This parseThis() { | 707 This parseThis() { |
687 tokens.consumeStart(THIS); | 708 tokens.consumeStart(THIS); |
688 tokens.consumeEnd(); | 709 tokens.consumeEnd(); |
689 return new This(); | 710 return new This(); |
690 } | 711 } |
691 } | 712 } |
OLD | NEW |