PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Runtime/Microsoft.Dynamic/Ast/BlockBuilder.cs

#
C# | 53 lines | 29 code | 5 blank | 19 comment | 1 complexity | cc5f205fef69a0d87a5fd8b2c30c3533 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. 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 Apache License, Version 2.0, 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 Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. #if !CLR2
  16. using System.Linq.Expressions;
  17. #else
  18. using Microsoft.Scripting.Ast;
  19. #endif
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Dynamic;
  23. using System.Collections.ObjectModel;
  24. using Microsoft.Scripting.Utils;
  25. namespace Microsoft.Scripting.Ast {
  26. public sealed class BlockBuilder : ExpressionCollectionBuilder<Expression> {
  27. public BlockBuilder() {
  28. }
  29. /// <summary>
  30. /// Returns <c>null</c> if no expression was added into the builder.
  31. /// If only a single expression was added returns it.
  32. /// Otherwise returns a <see cref="BlockExpression"/> containing the expressions added to the builder.
  33. /// </summary>
  34. public Expression ToExpression() {
  35. switch (Count) {
  36. case 0: return null;
  37. case 1: return Expression0;
  38. case 2: return Expression.Block(Expression0, Expression1);
  39. case 3: return Expression.Block(Expression0, Expression1, Expression2);
  40. case 4: return Expression.Block(Expression0, Expression1, Expression2, Expression3);
  41. default: return Expression.Block(Expressions);
  42. }
  43. }
  44. public static implicit operator Expression(BlockBuilder/*!*/ block) {
  45. return block.ToExpression();
  46. }
  47. }
  48. }