PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/DICK.B1/IronPython/Runtime/Binding/Binders.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 161 lines | 110 code | 22 blank | 29 comment | 6 complexity | 3ede6b09c07779bd02e82ba44d928a39 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. * ***************************************************************************/
  15. #if !CLR2
  16. using System.Linq.Expressions;
  17. #else
  18. using Microsoft.Scripting.Ast;
  19. #endif
  20. using System;
  21. using System.Dynamic;
  22. using Microsoft.Scripting.Actions;
  23. namespace IronPython.Runtime.Binding {
  24. using Ast = Expression;
  25. static class Binders {
  26. /// <summary>
  27. /// Backwards compatible Convert for the old sites that need to flow CodeContext
  28. /// </summary>
  29. public static Expression/*!*/ Convert(Expression/*!*/ codeContext, PythonContext/*!*/ binder, Type/*!*/ type, ConversionResultKind resultKind, Expression/*!*/ target) {
  30. return Ast.Dynamic(
  31. binder.Convert(type, resultKind),
  32. type,
  33. target
  34. );
  35. }
  36. public static Expression/*!*/ Get(Expression/*!*/ codeContext, PythonContext/*!*/ binder, Type/*!*/ resultType, string/*!*/ name, Expression/*!*/ target) {
  37. return Ast.Dynamic(
  38. binder.GetMember(name),
  39. resultType,
  40. target,
  41. codeContext
  42. );
  43. }
  44. public static Expression/*!*/ TryGet(Expression/*!*/ codeContext, PythonContext/*!*/ binder, Type/*!*/ resultType, string/*!*/ name, Expression/*!*/ target) {
  45. return Ast.Dynamic(
  46. binder.GetMember(
  47. name,
  48. true
  49. ),
  50. resultType,
  51. target,
  52. codeContext
  53. );
  54. }
  55. public static DynamicMetaObjectBinder UnaryOperationBinder(PythonContext state, PythonOperationKind operatorName) {
  56. ExpressionType? et = GetExpressionTypeFromUnaryOperator(operatorName);
  57. if (et == null) {
  58. return state.Operation(
  59. operatorName
  60. );
  61. }
  62. return state.UnaryOperation(et.Value);
  63. }
  64. private static ExpressionType? GetExpressionTypeFromUnaryOperator(PythonOperationKind operatorName) {
  65. switch (operatorName) {
  66. case PythonOperationKind.Positive: return ExpressionType.UnaryPlus;
  67. case PythonOperationKind.Negate: return ExpressionType.Negate;
  68. case PythonOperationKind.OnesComplement: return ExpressionType.OnesComplement;
  69. case PythonOperationKind.Not: return ExpressionType.Not;
  70. case PythonOperationKind.IsFalse: return ExpressionType.IsFalse;
  71. }
  72. return null;
  73. }
  74. public static DynamicMetaObjectBinder BinaryOperationBinder(PythonContext state, PythonOperationKind operatorName) {
  75. ExpressionType? et = GetExpressionTypeFromBinaryOperator(operatorName);
  76. if (et == null) {
  77. return state.Operation(
  78. operatorName
  79. );
  80. }
  81. return state.BinaryOperation(et.Value);
  82. }
  83. private static ExpressionType? GetExpressionTypeFromBinaryOperator(PythonOperationKind operatorName) {
  84. switch (operatorName) {
  85. case PythonOperationKind.Add: return ExpressionType.Add;
  86. case PythonOperationKind.BitwiseAnd: return ExpressionType.And;
  87. case PythonOperationKind.Divide: return ExpressionType.Divide;
  88. case PythonOperationKind.ExclusiveOr: return ExpressionType.ExclusiveOr;
  89. case PythonOperationKind.Mod: return ExpressionType.Modulo;
  90. case PythonOperationKind.Multiply: return ExpressionType.Multiply;
  91. case PythonOperationKind.BitwiseOr: return ExpressionType.Or;
  92. case PythonOperationKind.Power: return ExpressionType.Power;
  93. case PythonOperationKind.RightShift: return ExpressionType.RightShift;
  94. case PythonOperationKind.LeftShift: return ExpressionType.LeftShift;
  95. case PythonOperationKind.Subtract: return ExpressionType.Subtract;
  96. case PythonOperationKind.InPlaceAdd: return ExpressionType.AddAssign;
  97. case PythonOperationKind.InPlaceBitwiseAnd: return ExpressionType.AndAssign;
  98. case PythonOperationKind.InPlaceDivide: return ExpressionType.DivideAssign;
  99. case PythonOperationKind.InPlaceExclusiveOr: return ExpressionType.ExclusiveOrAssign;
  100. case PythonOperationKind.InPlaceMod: return ExpressionType.ModuloAssign;
  101. case PythonOperationKind.InPlaceMultiply: return ExpressionType.MultiplyAssign;
  102. case PythonOperationKind.InPlaceBitwiseOr: return ExpressionType.OrAssign;
  103. case PythonOperationKind.InPlacePower: return ExpressionType.PowerAssign;
  104. case PythonOperationKind.InPlaceRightShift: return ExpressionType.RightShiftAssign;
  105. case PythonOperationKind.InPlaceLeftShift: return ExpressionType.LeftShiftAssign;
  106. case PythonOperationKind.InPlaceSubtract: return ExpressionType.SubtractAssign;
  107. case PythonOperationKind.Equal: return ExpressionType.Equal;
  108. case PythonOperationKind.GreaterThan: return ExpressionType.GreaterThan;
  109. case PythonOperationKind.GreaterThanOrEqual: return ExpressionType.GreaterThanOrEqual;
  110. case PythonOperationKind.LessThan: return ExpressionType.LessThan;
  111. case PythonOperationKind.LessThanOrEqual: return ExpressionType.LessThanOrEqual;
  112. case PythonOperationKind.NotEqual: return ExpressionType.NotEqual;
  113. }
  114. return null;
  115. }
  116. /// <summary>
  117. /// Creates a new InvokeBinder which will call with positional splatting.
  118. ///
  119. /// The signature of the target site should be object(function), object[], retType
  120. /// </summary>
  121. /// <param name="state"></param>
  122. /// <returns></returns>
  123. public static PythonInvokeBinder/*!*/ InvokeSplat(PythonContext/*!*/ state) {
  124. return state.Invoke(
  125. new CallSignature(new Argument(ArgumentType.List))
  126. );
  127. }
  128. /// <summary>
  129. /// Creates a new InvokeBinder which will call with positional and keyword splatting.
  130. ///
  131. /// The signature of the target site should be object(function), object[], dictionary, retType
  132. /// </summary>
  133. public static PythonInvokeBinder/*!*/ InvokeKeywords(PythonContext/*!*/ state) {
  134. return state.Invoke(
  135. new CallSignature(new Argument(ArgumentType.List), new Argument(ArgumentType.Dictionary))
  136. );
  137. }
  138. }
  139. }