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