PageRenderTime 60ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/williamybs/uidipythontool
C# | 131 lines | 98 code | 17 blank | 16 comment | 21 complexity | 546f5ab831d942cb2ff5c5deec246757 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.Collections.Generic;
  21. using System.Runtime.CompilerServices;
  22. namespace IronPython.Compiler.Ast {
  23. using Ast = MSAst.Expression;
  24. using AstUtils = Microsoft.Scripting.Ast.Utils;
  25. public class PrintStatement : Statement {
  26. private readonly Expression _dest;
  27. private readonly Expression[] _expressions;
  28. private readonly bool _trailingComma;
  29. public PrintStatement(Expression destination, Expression[] expressions, bool trailingComma) {
  30. _dest = destination;
  31. _expressions = expressions;
  32. _trailingComma = trailingComma;
  33. }
  34. public Expression Destination {
  35. get { return _dest; }
  36. }
  37. public IList<Expression> Expressions {
  38. get { return _expressions; }
  39. }
  40. public bool TrailingComma {
  41. get { return _trailingComma; }
  42. }
  43. public override MSAst.Expression Reduce() {
  44. MSAst.Expression destination = _dest;
  45. if (_expressions.Length == 0) {
  46. MSAst.Expression result;
  47. if (destination != null) {
  48. result = Ast.Call(
  49. AstMethods.PrintNewlineWithDest,
  50. Parent.LocalContext,
  51. destination
  52. );
  53. } else {
  54. result = Ast.Call(
  55. AstMethods.PrintNewline,
  56. Parent.LocalContext
  57. );
  58. }
  59. return GlobalParent.AddDebugInfo(result, Span);
  60. } else {
  61. // Create list for the individual statements
  62. ReadOnlyCollectionBuilder<MSAst.Expression> statements = new ReadOnlyCollectionBuilder<MSAst.Expression>();
  63. // Store destination in a temp, if we have one
  64. MSAst.ParameterExpression temp = null;
  65. if (destination != null) {
  66. temp = Ast.Variable(typeof(object), "destination");
  67. statements.Add(MakeAssignment(temp, destination));
  68. destination = temp;
  69. }
  70. for (int i = 0; i < _expressions.Length; i++) {
  71. bool withComma = (i < _expressions.Length - 1 || _trailingComma);// ? "PrintComma" : "Print";
  72. Expression current = _expressions[i];
  73. MSAst.MethodCallExpression mce;
  74. if (destination != null) {
  75. mce = Ast.Call(
  76. withComma ? AstMethods.PrintCommaWithDest : AstMethods.PrintWithDest,
  77. Parent.LocalContext,
  78. destination,
  79. AstUtils.Convert(current, typeof(object))
  80. );
  81. } else {
  82. mce = Ast.Call(
  83. withComma ? AstMethods.PrintComma : AstMethods.Print,
  84. Parent.LocalContext,
  85. AstUtils.Convert(current, typeof(object))
  86. );
  87. }
  88. statements.Add(mce);
  89. }
  90. statements.Add(AstUtils.Empty());
  91. MSAst.Expression res;
  92. if (temp != null) {
  93. res = Ast.Block(new[] { temp }, statements.ToReadOnlyCollection());
  94. } else {
  95. res = Ast.Block(statements.ToReadOnlyCollection());
  96. }
  97. return GlobalParent.AddDebugInfo(res, Span);
  98. }
  99. }
  100. public override void Walk(PythonWalker walker) {
  101. if (walker.Walk(this)) {
  102. if (_dest != null) {
  103. _dest.Walk(walker);
  104. }
  105. if (_expressions != null) {
  106. foreach (Expression expression in _expressions) {
  107. expression.Walk(walker);
  108. }
  109. }
  110. }
  111. walker.PostWalk(this);
  112. }
  113. }
  114. }