PageRenderTime 99ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/DICK.B1/IronPython/Compiler/Ast/DictionaryExpression.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 102 lines | 68 code | 14 blank | 20 comment | 17 complexity | add0c1a07c6cf303ea3865c6d3c55baf MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. #if !CLR2
  16. using MSAst = System.Linq.Expressions;
  17. #else
  18. using MSAst = Microsoft.Scripting.Ast;
  19. #endif
  20. using System;
  21. using System.Collections.Generic;
  22. using IronPython.Runtime;
  23. using IronPython.Runtime.Operations;
  24. using AstUtils = Microsoft.Scripting.Ast.Utils;
  25. namespace IronPython.Compiler.Ast {
  26. using Ast = MSAst.Expression;
  27. public class DictionaryExpression : Expression {
  28. private readonly SliceExpression[] _items;
  29. public DictionaryExpression(params SliceExpression[] items) {
  30. _items = items;
  31. }
  32. public IList<SliceExpression> Items {
  33. get { return _items; }
  34. }
  35. public override MSAst.Expression Reduce() {
  36. // create keys & values into array and then call helper function
  37. // which creates the dictionary
  38. if (_items.Length != 0) {
  39. MSAst.Expression[] parts = new MSAst.Expression[_items.Length * 2];
  40. Type t = null;
  41. bool heterogeneous = false;
  42. for (int index = 0; index < _items.Length; index++) {
  43. SliceExpression slice = _items[index];
  44. // Eval order should be:
  45. // { 2 : 1, 4 : 3, 6 :5 }
  46. // This is backwards from parameter list eval, so create temporaries to swap ordering.
  47. parts[index * 2] = TransformOrConstantNull(slice.SliceStop, typeof(object));
  48. MSAst.Expression key = parts[index * 2 + 1] = TransformOrConstantNull(slice.SliceStart, typeof(object));
  49. Type newType;
  50. if (key.NodeType == MSAst.ExpressionType.Convert) {
  51. newType = ((MSAst.UnaryExpression)key).Operand.Type;
  52. } else {
  53. newType = key.Type;
  54. }
  55. if (t == null) {
  56. t = newType;
  57. } else if (newType == typeof(object)) {
  58. heterogeneous = true;
  59. } else if (newType != t) {
  60. heterogeneous = true;
  61. }
  62. }
  63. return Ast.Call(
  64. heterogeneous ? AstMethods.MakeDictFromItems : AstMethods.MakeHomogeneousDictFromItems,
  65. Ast.NewArrayInit(
  66. typeof(object),
  67. parts
  68. )
  69. );
  70. }
  71. // empty dictionary
  72. return Ast.Call(
  73. AstMethods.MakeDict,
  74. AstUtils.Constant(0)
  75. );
  76. }
  77. public override void Walk(PythonWalker walker) {
  78. if (walker.Walk(this)) {
  79. if (_items != null) {
  80. foreach (SliceExpression s in _items) {
  81. s.Walk(walker);
  82. }
  83. }
  84. }
  85. walker.PostWalk(this);
  86. }
  87. }
  88. }