PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/IronPython/Compiler/Ast/AndExpression.cs

#
C# | 106 lines | 78 code | 14 blank | 14 comment | 7 complexity | f112d9edc0b86e17747be290e1006381 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 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. #if !CLR2
  16. using MSAst = System.Linq.Expressions;
  17. #else
  18. using MSAst = Microsoft.Scripting.Ast;
  19. #endif
  20. using System;
  21. using IronPython.Runtime.Binding;
  22. using Microsoft.Scripting.Actions;
  23. using Microsoft.Scripting.Utils;
  24. using AstUtils = Microsoft.Scripting.Ast.Utils;
  25. namespace IronPython.Compiler.Ast {
  26. using Ast = MSAst.Expression;
  27. public class AndExpression : Expression {
  28. private readonly Expression _left, _right;
  29. public AndExpression(Expression left, Expression right) {
  30. ContractUtils.RequiresNotNull(left, "left");
  31. ContractUtils.RequiresNotNull(right, "right");
  32. _left = left;
  33. _right = right;
  34. StartIndex = left.StartIndex;
  35. EndIndex = right.EndIndex;
  36. }
  37. public Expression Left {
  38. get { return _left; }
  39. }
  40. public Expression Right {
  41. get { return _right; }
  42. }
  43. public override MSAst.Expression Reduce() {
  44. MSAst.Expression left = _left;
  45. MSAst.Expression right = _right;
  46. Type t = Type;
  47. MSAst.ParameterExpression tmp = Ast.Variable(t, "__all__");
  48. return Ast.Block(
  49. new [] { tmp },
  50. Ast.Condition(
  51. GlobalParent.Convert(
  52. typeof(bool),
  53. ConversionResultKind.ExplicitCast,
  54. Ast.Assign(
  55. tmp,
  56. AstUtils.Convert(
  57. left,
  58. t
  59. )
  60. )
  61. ),
  62. AstUtils.Convert(
  63. right,
  64. t
  65. ),
  66. tmp
  67. )
  68. );
  69. }
  70. public override Type Type {
  71. get {
  72. return _left.Type == _right.Type ? _left.Type : typeof(object);
  73. }
  74. }
  75. public override void Walk(PythonWalker walker) {
  76. if (walker.Walk(this)) {
  77. if (_left != null) {
  78. _left.Walk(walker);
  79. }
  80. if (_right != null) {
  81. _right.Walk(walker);
  82. }
  83. }
  84. walker.PostWalk(this);
  85. }
  86. internal override bool CanThrow {
  87. get {
  88. return _left.CanThrow || _right.CanThrow;
  89. }
  90. }
  91. }
  92. }