PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 5ms RepoModel.GetById 1ms 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
 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
16#if !CLR2
17using System.Linq.Expressions;
18#else
19using Microsoft.Scripting.Ast;
20#endif
21
22using System;
23using System.Collections.Generic;
24using System.Dynamic;
25using System.Collections.ObjectModel;
26using Microsoft.Scripting.Utils;
27
28namespace Microsoft.Scripting.Ast {
29    public sealed class BlockBuilder : ExpressionCollectionBuilder<Expression> {
30        public BlockBuilder() {
31        }
32
33        /// <summary>
34        /// Returns <c>null</c> if no expression was added into the builder.
35        /// If only a single expression was added returns it.
36        /// Otherwise returns a <see cref="BlockExpression"/> containing the expressions added to the builder.
37        /// </summary>
38        public Expression ToExpression() {
39            switch (Count) {
40                case 0: return null;
41                case 1: return Expression0;
42                case 2: return Expression.Block(Expression0, Expression1);
43                case 3: return Expression.Block(Expression0, Expression1, Expression2);
44                case 4: return Expression.Block(Expression0, Expression1, Expression2, Expression3);
45                default: return Expression.Block(Expressions);
46            }
47        }
48
49        public static implicit operator Expression(BlockBuilder/*!*/ block) {
50            return block.ToExpression();
51        }
52    }
53}