PageRenderTime 44ms CodeModel.GetById 10ms app.highlight 24ms RepoModel.GetById 2ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/IronPython/Runtime/Types/OperatorMapping.cs

#
C# | 143 lines | 93 code | 18 blank | 32 comment | 6 complexity | a015c8f20b4a2c10c7d27705317ca5a5 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using IronPython.Runtime.Binding;
  5
  6namespace IronPython.Runtime.Types {
  7    /// <summary>
  8    /// OperatorMapping provides a mapping from DLR operators to their associated .NET methods.
  9    /// </summary>
 10    class OperatorMapping {
 11        private static readonly OperatorMapping[] _infos = MakeOperatorTable(); // table of Operators, names, and alt names for looking up methods.
 12
 13        private readonly PythonOperationKind _operator;
 14        private readonly string _name;
 15        private readonly string _altName;
 16        private readonly Type _altExpectedType;
 17
 18        private OperatorMapping(PythonOperationKind op, string name, string altName) {
 19            _operator = op;
 20            _name = name;
 21            _altName = altName;
 22        }
 23
 24        private OperatorMapping(PythonOperationKind op, string name, string altName, Type alternateExpectedType) {
 25            _operator = op;
 26            _name = name;
 27            _altName = altName;
 28            _altExpectedType = alternateExpectedType;
 29        }
 30
 31        /// <summary>
 32        /// Given an operator returns the OperatorMapping associated with the operator or null
 33        /// </summary>
 34        public static OperatorMapping GetOperatorMapping(PythonOperationKind op) {
 35            foreach (OperatorMapping info in _infos) {
 36                if (info._operator == op) return info;
 37            }
 38            return null;
 39        }
 40
 41        public static OperatorMapping GetOperatorMapping(string name) {
 42            foreach (OperatorMapping info in _infos) {
 43                if (info.Name == name || info.AlternateName == name) {
 44                    return info;
 45                }
 46            }
 47            return null;
 48        }
 49
 50        /// <summary>
 51        /// The operator the OperatorMapping provides info for.
 52        /// </summary>
 53        public PythonOperationKind Operator {
 54            get { return _operator; }
 55        }
 56
 57        /// <summary>
 58        /// The primary method name associated with the method.  This method name is
 59        /// usally in the form of op_Operator (e.g. op_Addition).
 60        /// </summary>
 61        public string Name {
 62            get { return _name; }
 63        }
 64
 65        /// <summary>
 66        /// The secondary method name associated with the method.  This method name is
 67        /// usually a standard .NET method name with pascal casing (e.g. Add).
 68        /// </summary>
 69        public string AlternateName {
 70            get { return _altName; }
 71        }
 72
 73        /// <summary>
 74        /// The return type that must match for the alternate operator to be valid.
 75        /// 
 76        /// This is available alternate operators don't have special names and therefore
 77        /// could be confused for a normal method which isn't fulfilling the contract.
 78        /// </summary>
 79        public Type AlternateExpectedType {
 80            get {
 81                return _altExpectedType;
 82            }
 83        }
 84
 85        private static OperatorMapping[] MakeOperatorTable() {
 86            List<OperatorMapping> res = new List<OperatorMapping>();
 87
 88            // alternate names from: http://msdn2.microsoft.com/en-us/library/2sk3x8a7(vs.71).aspx
 89            //   different in:
 90            //    comparisons all support alternative names, Xor is "ExclusiveOr" not "Xor"
 91
 92            // unary operators as defined in Partition I Architecture 9.3.1:
 93            res.Add(new OperatorMapping(PythonOperationKind.Negate, "op_UnaryNegation", "Negate"));         // - (unary)
 94            res.Add(new OperatorMapping(PythonOperationKind.Positive, "op_UnaryPlus", "Plus"));           // + (unary)
 95            res.Add(new OperatorMapping(PythonOperationKind.Not, "op_LogicalNot", null));             // !
 96            //res.Add(new OperatorMapping(PythonOperationKind.AddressOf,           "op_AddressOf",                 null));             // & (unary)
 97            res.Add(new OperatorMapping(PythonOperationKind.OnesComplement, "op_OnesComplement", "OnesComplement")); // ~
 98            //res.Add(new OperatorMapping(PythonOperationKind.PointerDereference,  "op_PointerDereference",        null));             // * (unary)
 99
100            // binary operators as defined in Partition I Architecture 9.3.2:
101            res.Add(new OperatorMapping(PythonOperationKind.Add, "op_Addition", "Add"));            // +
102            res.Add(new OperatorMapping(PythonOperationKind.Subtract, "op_Subtraction", "Subtract"));       // -
103            res.Add(new OperatorMapping(PythonOperationKind.Multiply, "op_Multiply", "Multiply"));       // *
104            res.Add(new OperatorMapping(PythonOperationKind.Divide, "op_Division", "Divide"));         // /
105            res.Add(new OperatorMapping(PythonOperationKind.Mod, "op_Modulus", "Mod"));            // %
106            res.Add(new OperatorMapping(PythonOperationKind.ExclusiveOr, "op_ExclusiveOr", "ExclusiveOr"));    // ^
107            res.Add(new OperatorMapping(PythonOperationKind.BitwiseAnd, "op_BitwiseAnd", "BitwiseAnd"));     // &
108            res.Add(new OperatorMapping(PythonOperationKind.BitwiseOr, "op_BitwiseOr", "BitwiseOr"));      // |
109            res.Add(new OperatorMapping(PythonOperationKind.LeftShift, "op_LeftShift", "LeftShift"));      // <<
110            res.Add(new OperatorMapping(PythonOperationKind.RightShift, "op_RightShift", "RightShift"));     // >>
111            res.Add(new OperatorMapping(PythonOperationKind.Equal, "op_Equality", "Equals"));         // ==   
112            res.Add(new OperatorMapping(PythonOperationKind.GreaterThan, "op_GreaterThan", "GreaterThan"));    // >
113            res.Add(new OperatorMapping(PythonOperationKind.LessThan, "op_LessThan", "LessThan"));       // <
114            res.Add(new OperatorMapping(PythonOperationKind.NotEqual, "op_Inequality", "NotEquals"));      // != 
115            res.Add(new OperatorMapping(PythonOperationKind.GreaterThanOrEqual, "op_GreaterThanOrEqual", "GreaterThanOrEqual"));        // >=
116            res.Add(new OperatorMapping(PythonOperationKind.LessThanOrEqual, "op_LessThanOrEqual", "LessThanOrEqual"));        // <=
117            res.Add(new OperatorMapping(PythonOperationKind.InPlaceMultiply, "op_MultiplicationAssignment", "InPlaceMultiply"));       // *=
118            res.Add(new OperatorMapping(PythonOperationKind.InPlaceSubtract, "op_SubtractionAssignment", "InPlaceSubtract"));       // -=
119            res.Add(new OperatorMapping(PythonOperationKind.InPlaceExclusiveOr, "op_ExclusiveOrAssignment", "InPlaceExclusiveOr"));            // ^=
120            res.Add(new OperatorMapping(PythonOperationKind.InPlaceLeftShift, "op_LeftShiftAssignment", "InPlaceLeftShift"));      // <<=
121            res.Add(new OperatorMapping(PythonOperationKind.InPlaceRightShift, "op_RightShiftAssignment", "InPlaceRightShift"));     // >>=
122            res.Add(new OperatorMapping(PythonOperationKind.InPlaceMod, "op_ModulusAssignment", "InPlaceMod"));            // %=
123            res.Add(new OperatorMapping(PythonOperationKind.InPlaceAdd, "op_AdditionAssignment", "InPlaceAdd"));            // += 
124            res.Add(new OperatorMapping(PythonOperationKind.InPlaceBitwiseAnd, "op_BitwiseAndAssignment", "InPlaceBitwiseAnd"));     // &=
125            res.Add(new OperatorMapping(PythonOperationKind.InPlaceBitwiseOr, "op_BitwiseOrAssignment", "InPlaceBitwiseOr"));      // |=
126            res.Add(new OperatorMapping(PythonOperationKind.InPlaceDivide, "op_DivisionAssignment", "InPlaceDivide"));         // /=
127            
128            // these exist just for TypeInfo to map by name
129            res.Add(new OperatorMapping(PythonOperationKind.GetItem, "get_Item", "GetItem"));        // not defined
130            res.Add(new OperatorMapping(PythonOperationKind.SetItem, "set_Item", "SetItem"));        // not defined
131            res.Add(new OperatorMapping(PythonOperationKind.DeleteItem, "del_Item", "DeleteItem"));        // not defined
132
133            // DLR Extended operators:
134            res.Add(new OperatorMapping(PythonOperationKind.Compare, "op_Compare", "Compare", typeof(int)));        // not defined
135
136            res.Add(new OperatorMapping(PythonOperationKind.CallSignatures, "GetCallSignatures", null));
137            res.Add(new OperatorMapping(PythonOperationKind.Documentation, "GetDocumentation", null));
138            res.Add(new OperatorMapping(PythonOperationKind.IsCallable, "IsCallable", null));
139
140            return res.ToArray();
141        }
142    }
143}