Ejemplo n.º 1
0
        public static object MatchIndex(RubyScope /*!*/ scope, RubyRegex /*!*/ self, [DefaultProtocol] MutableString /*!*/ str)
        {
            MatchData match = RubyRegex.SetCurrentMatchData(scope, self, str);

            return((match != null) ? ScriptingRuntimeHelpers.Int32ToObject(match.Index) : null);
        }
Ejemplo n.º 2
0
 protected override object Calculate(object l, object r)
 {
     return(ScriptingRuntimeHelpers.Int32ToObject(unchecked ((Int32)l % (Int32)r)));
 }
Ejemplo n.º 3
0
        private static object FastNew(CodeContext /*!*/ context, object o, int @base = 10)
        {
            Extensible <BigInteger> el;

            if (o is string)
            {
                return(__new__(null, (string)o, @base));
            }
            if (o is double)
            {
                return(DoubleOps.__int__((double)o));
            }
            if (o is int)
            {
                return(o);
            }
            if (o is bool)
            {
                return(((bool)o) ? 1 : 0);
            }
            if (o is BigInteger)
            {
                int res;
                if (((BigInteger)o).AsInt32(out res))
                {
                    return(ScriptingRuntimeHelpers.Int32ToObject(res));
                }
                return(o);
            }

            if ((el = o as Extensible <BigInteger>) != null)
            {
                int res;
                if (el.Value.AsInt32(out res))
                {
                    return(ScriptingRuntimeHelpers.Int32ToObject(res));
                }
                return(el.Value);
            }

            if (o is float)
            {
                return(DoubleOps.__int__((double)(float)o));
            }

            if (o is Complex)
            {
                throw PythonOps.TypeError("can't convert complex to int; use int(abs(z))");
            }

            if (o is Int64)
            {
                Int64 val = (Int64)o;
                if (Int32.MinValue <= val && val <= Int32.MaxValue)
                {
                    return((Int32)val);
                }
                else
                {
                    return((BigInteger)val);
                }
            }
            else if (o is UInt32)
            {
                UInt32 val = (UInt32)o;
                if (val <= Int32.MaxValue)
                {
                    return((Int32)val);
                }
                else
                {
                    return((BigInteger)val);
                }
            }
            else if (o is UInt64)
            {
                UInt64 val = (UInt64)o;
                if (val <= Int32.MaxValue)
                {
                    return((Int32)val);
                }
                else
                {
                    return((BigInteger)val);
                }
            }
            else if (o is Decimal)
            {
                Decimal val = (Decimal)o;
                if (Int32.MinValue <= val && val <= Int32.MaxValue)
                {
                    return((Int32)val);
                }
                else
                {
                    return((BigInteger)val);
                }
            }
            else if (o is Enum)
            {
                return(((IConvertible)o).ToInt32(null));
            }

            if (o is Extensible <string> es)
            {
                // __int__ takes precedence, call it if it's available...
                object value;
                if (PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, es, "__int__", out value))
                {
                    return(value);
                }

                // otherwise call __new__ on the string value
                return(__new__(null, es.Value, @base));
            }

            object     result;
            int        intRes;
            BigInteger bigintRes;

            if (PythonTypeOps.TryInvokeUnaryOperator(context, o, "__int__", out result) &&
                !Object.ReferenceEquals(result, NotImplementedType.Value))
            {
                if (result is int || result is BigInteger ||
                    result is Extensible <int> || result is Extensible <BigInteger> )
                {
                    return(result);
                }
                else
                {
                    throw PythonOps.TypeError("__int__ returned non-Integral (type {0})", PythonTypeOps.GetName(result));
                }
            }
            else if (PythonOps.TryGetBoundAttr(context, o, "__trunc__", out result))
            {
                result = PythonOps.CallWithContext(context, result);
                if (result is int || result is BigInteger ||
                    result is Extensible <int> || result is Extensible <BigInteger> )
                {
                    return(result);
                }
                else if (Converter.TryConvertToInt32(result, out intRes))
                {
                    return(intRes);
                }
                else if (Converter.TryConvertToBigInteger(result, out bigintRes))
                {
                    return(bigintRes);
                }
                else
                {
                    throw PythonOps.TypeError("__trunc__ returned non-Integral (type {0})", PythonTypeOps.GetName(result));
                }
            }

            throw PythonOps.TypeError("int() argument must be a string or a number, not '{0}'", PythonTypeOps.GetName(o));
        }
Ejemplo n.º 4
0
 public void Push(int value)
 {
     Data[StackIndex++] = ScriptingRuntimeHelpers.Int32ToObject(value);
 }
Ejemplo n.º 5
0
 public static object End(MatchData /*!*/ self, [DefaultProtocol] int groupIndex)
 {
     self.RequireExistingGroup(groupIndex);
     return(self.GroupSuccess(groupIndex) ? ScriptingRuntimeHelpers.Int32ToObject(self.GetGroupEnd(groupIndex)) : null);
 }
Ejemplo n.º 6
0
 private static object GetIntReturn(int value)
 {
     return(ScriptingRuntimeHelpers.Int32ToObject((int)value));
 }
Ejemplo n.º 7
0
 public Enumerate(object iter)
 {
     _iter  = PythonOps.GetEnumerator(iter);
     _index = ScriptingRuntimeHelpers.Int32ToObject(-1);
 }
Ejemplo n.º 8
0
        public Enumerate(CodeContext context, object iter, object start)
        {
            object index;

            if (!Converter.TryConvertToIndex(start, out index))
            {
                throw PythonOps.TypeErrorForUnIndexableObject(start);
            }

            _iter  = PythonOps.GetEnumerator(iter);
            _index = context.LanguageContext.Operation(Binding.PythonOperationKind.Subtract, index, ScriptingRuntimeHelpers.Int32ToObject(1));
        }
Ejemplo n.º 9
0
 public object /*!*/ ToObject()
 {
     return((object)_bignum ?? ScriptingRuntimeHelpers.Int32ToObject(_fixnum));
 }
Ejemplo n.º 10
0
        public static object End(MatchData /*!*/ self, [DefaultProtocol] int groupIndex)
        {
            var group = self.GetExistingGroup(groupIndex);

            return(group.Success ? ScriptingRuntimeHelpers.Int32ToObject(group.Index + group.Length) : null);
        }
Ejemplo n.º 11
0
 public static Literal /*!*/ Integer(int value, SourceSpan location)
 {
     return(new Literal(ScriptingRuntimeHelpers.Int32ToObject(value), location));
 }
Ejemplo n.º 12
0
        public static object Getc(RubyIO /*!*/ self)
        {
            int c = self.ReadByteNormalizeEoln();

            return((c != -1) ? ScriptingRuntimeHelpers.Int32ToObject(c) : null);
        }
Ejemplo n.º 13
0
        private static object CharmapDecodeWorker(string input, string errors, IDictionary <object, object> map, bool isDecode)
        {
            if (input.Length == 0)
            {
                return(PythonTuple.MakeTuple(String.Empty, 0));
            }

            StringBuilder res = new StringBuilder();

            for (int i = 0; i < input.Length; i++)
            {
                object val;

                if (map == null)
                {
                    res.Append(input[i]);
                    continue;
                }

                object charObj = ScriptingRuntimeHelpers.Int32ToObject((int)input[i]);

                if (!map.TryGetValue(charObj, out val))
                {
                    if (errors == "strict" && isDecode)
                    {
                        throw PythonOps.UnicodeDecodeError("failed to find key in mapping");
                    }
                    else if (!isDecode)
                    {
                        throw PythonOps.UnicodeEncodeError("failed to find key in mapping");
                    }
                    res.Append("\ufffd");
                }
                else if (val == null)
                {
                    if (errors == "strict" && isDecode)
                    {
                        throw PythonOps.UnicodeDecodeError("'charmap' codec can't decode characters at index {0} because charmap maps to None", i);
                    }
                    else if (!isDecode)
                    {
                        throw PythonOps.UnicodeEncodeError("charmap", input[i], i,
                                                           "'charmap' codec can't encode characters at index {0} because charmap maps to None", i);
                    }
                    res.Append("\ufffd");
                }
                else if (val is string)
                {
                    res.Append((string)val);
                }
                else if (val is int)
                {
                    res.Append((char)(int)val);
                }
                else
                {
                    throw PythonOps.TypeError("charmap must be an int, str, or None");
                }
            }
            return(PythonTuple.MakeTuple(res.ToString(), res.Length));
        }