PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/Ruby/Ruby/Compiler/Ast/Expressions/ConditionalJumpExpression.cs

#
C# | 92 lines | 62 code | 12 blank | 18 comment | 4 complexity | 7fdfa4b2e61926fb98920361856e54ec 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 * ironruby@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 MSA = System.Linq.Expressions;
18#else
19using MSA = Microsoft.Scripting.Ast;
20#endif
21
22using Microsoft.Scripting;
23using Microsoft.Scripting.Utils;
24using AstUtils = Microsoft.Scripting.Ast.Utils;
25
26namespace IronRuby.Compiler.Ast {
27    using Ast = MSA.Expression;
28
29    /// <summary>
30    /// Represents {condition} {and/or/&&/||} {jump-statement}, 
31    /// or {condition} ? {jump-statement} : {value}.
32    /// </summary>
33    public partial class ConditionalJumpExpression : Expression {
34        private readonly bool _negateCondition;
35        private readonly Expression/*!*/ _condition;
36        private readonly Expression _value;
37        private readonly JumpStatement/*!*/ _jumpStatement;
38
39        public bool NegateCondition {
40            get { return _negateCondition; }
41        }
42
43        public bool IsBooleanExpression {
44            get { return _value == null; }
45        }
46
47        public Expression/*!*/ Condition {
48            get { return _condition; }
49        }
50
51        public Expression Value {
52            get { return _value; }
53        }
54
55        public JumpStatement/*!*/ JumpStatement {
56            get { return _jumpStatement; }
57        }
58
59        public ConditionalJumpExpression(Expression/*!*/ condition, JumpStatement/*!*/ jumpStatement, bool negateCondition, Expression value, SourceSpan location)
60            : base(location) {
61            ContractUtils.RequiresNotNull(condition, "condition");
62            ContractUtils.RequiresNotNull(jumpStatement, "jumpStatement");
63
64            _condition = condition;
65            _jumpStatement = jumpStatement;
66            _negateCondition = negateCondition;
67            _value = value;
68        }
69
70        internal override MSA.Expression/*!*/ TransformRead(AstGenerator/*!*/ gen) {
71            if (_value != null) {
72                return Ast.Block(
73                    AstUtils.IfThen(
74                        _condition.TransformReadBoolean(gen, !_negateCondition),
75                        _jumpStatement.Transform(gen)
76                    ),
77                    _value.TransformRead(gen)
78                );
79            } else {
80                MSA.Expression tmpVariable = gen.CurrentScope.DefineHiddenVariable("#tmp_cond", typeof(object));
81                return Ast.Block(
82                    Ast.Assign(tmpVariable, AstUtils.Box(_condition.TransformRead(gen))),
83                    AstUtils.IfThen(
84                        (_negateCondition ? Methods.IsFalse : Methods.IsTrue).OpCall(tmpVariable),
85                        _jumpStatement.Transform(gen)
86                    ),
87                    tmpVariable
88                );
89            }
90        }
91    }
92}