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