PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_2_0/Src/Microsoft.Scripting.Core/Ast/DoStatement.cs

#
C# | 75 lines | 48 code | 11 blank | 16 comment | 7 complexity | 62be2451045efd02fc1a10df5e89ff8c 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 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. using System; using Microsoft;
  16. using Microsoft.Scripting.Utils;
  17. namespace Microsoft.Linq.Expressions {
  18. // TODO: rename to DoWhileExpression
  19. public sealed class DoStatement : Expression {
  20. private readonly Expression _test;
  21. private readonly Expression _body;
  22. private readonly LabelTarget _break;
  23. private readonly LabelTarget _continue;
  24. internal DoStatement(Expression body, Expression test, LabelTarget @break, LabelTarget @continue, Annotations annotations)
  25. : base(ExpressionType.DoStatement, typeof(void), annotations) {
  26. _test = test;
  27. _body = body;
  28. _break = @break;
  29. _continue = @continue;
  30. }
  31. public Expression Test {
  32. get { return _test; }
  33. }
  34. public Expression Body {
  35. get { return _body; }
  36. }
  37. public LabelTarget BreakLabel {
  38. get { return _break; }
  39. }
  40. public LabelTarget ContinueLabel {
  41. get { return _continue; }
  42. }
  43. internal override Expression Accept(ExpressionTreeVisitor visitor) {
  44. return visitor.VisitDoWhile(this);
  45. }
  46. }
  47. public partial class Expression {
  48. public static DoStatement DoWhile(Expression body, Expression test) {
  49. return DoWhile(body, test, null, null, null);
  50. }
  51. public static DoStatement DoWhile(Expression body, Expression test, LabelTarget @break, LabelTarget @continue) {
  52. return DoWhile(body, test, @break, @continue, null);
  53. }
  54. public static DoStatement DoWhile(Expression body, Expression test, LabelTarget @break, LabelTarget @continue, Annotations annotations) {
  55. RequiresCanRead(body, "body");
  56. RequiresCanRead(test, "test");
  57. ContractUtils.Requires(test.Type == typeof(bool), "test", Strings.ConditionMustBeBoolean);
  58. // TODO: lift the restriction on break, and allow loops to have non-void type
  59. ContractUtils.Requires(@break == null || @break.Type == typeof(void), "break", Strings.LabelTypeMustBeVoid);
  60. ContractUtils.Requires(@continue == null || @continue.Type == typeof(void), "continue", Strings.LabelTypeMustBeVoid);
  61. return new DoStatement(body, test, @break, @continue, annotations);
  62. }
  63. }
  64. }