Exemplo n.º 1
0
        public static int Compare(BigInteger x, uint y) {
            uint ix;
            if (x.AsUInt32(out ix)) {
                return ix == y ? 0 : ix > y ? 1 : -1;
            }

            return BigInteger.Compare(x, y);
        }
Exemplo n.º 2
0
        internal static UInt32 ToUInt32(BigInteger value)
        {
            UInt32 result;

            if (value.AsUInt32(out result))
            {
                return(result);
            }
            throw RubyExceptions.CreateRangeError("number too big to convert into System::UInt32");
        }
Exemplo n.º 3
0
        public static object EvalExpression(string code, Dictionary <string, object> localScope)
        {
            var scope = Prepare(localScope);

            try
            {
                ScriptSource source = scope.Engine.CreateScriptSourceFromString(code, SourceCodeKind.Expression);
                object       obj    = source.Execute(scope);

                if (obj != null && obj.GetType() == typeof(BigInteger))
                {
                    BigInteger bint = (BigInteger)obj;

                    int   i32;
                    uint  ui32;
                    long  i64;
                    ulong ui64;

                    if (bint.AsInt32(out i32))
                    {
                        return(i32);
                    }

                    if (bint.AsInt64(out i64))
                    {
                        return(i64);
                    }

                    if (bint.AsUInt32(out ui32))
                    {
                        return(ui32);
                    }

                    if (bint.AsUInt64(out ui64))
                    {
                        return(ui64);
                    }
                }

                return(obj);
            }
            catch (Exception ex)
            {
                throw new PeachException("Error executing expression [" + code + "]: " + ex.ToString(), ex);
            }
            finally
            {
                var names = scope.GetVariableNames().ToList();
                foreach (var name in names)
                {
                    scope.RemoveVariable(name);
                }
            }
        }
Exemplo n.º 4
0
        public uint ToUInt32Unchecked()
        {
            if (IsFixnum)
            {
                return(unchecked ((uint)_fixnum));
            }

            uint u;

            if (_bignum.AsUInt32(out u))
            {
                return(u);
            }
            throw RubyExceptions.CreateRangeError("bignum too big to convert into 32-bit unsigned integer");
        }
Exemplo n.º 5
0
 public static bool AsUInt32(BigInteger self, out uint res)
 {
     return(self.AsUInt32(out res));
 }
Exemplo n.º 6
0
 private void AsUInt32Test(BigInteger i, bool expRet, uint expInt)
 {
     uint v;
     bool b = i.AsUInt32(out v);
     Expect(b, EqualTo(expRet));
     Expect(v, EqualTo(expInt));
 }
Exemplo n.º 7
0
        public static object EvalExpression(string code, Dictionary <string, object> localScope)
        {
            var missing = Imports.Except(Engine.Modules.Keys).ToList();

            foreach (string import in missing)
            {
                Engine.Modules.Add(import, Engine.Instance.ImportModule(import));
            }

            ScriptScope scope = Engine.Instance.CreateScope();

            foreach (var kv in Engine.Modules)
            {
                scope.SetVariable(kv.Key, kv.Value);
            }

            foreach (var kv in GlobalScope)
            {
                scope.SetVariable(kv.Key, kv.Value);
            }

            foreach (var kv in localScope)
            {
                scope.SetVariable(kv.Key, kv.Value);
            }

            try
            {
                ScriptSource source = scope.Engine.CreateScriptSourceFromString(code, SourceCodeKind.Expression);
                object       obj    = source.Execute(scope);

                if (obj != null && obj.GetType() == typeof(BigInteger))
                {
                    BigInteger bint = (BigInteger)obj;

                    int   i32;
                    uint  ui32;
                    long  i64;
                    ulong ui64;

                    if (bint.AsInt32(out i32))
                    {
                        return(i32);
                    }

                    if (bint.AsInt64(out i64))
                    {
                        return(i64);
                    }

                    if (bint.AsUInt32(out ui32))
                    {
                        return(ui32);
                    }

                    if (bint.AsUInt64(out ui64))
                    {
                        return(ui64);
                    }
                }

                return(obj);
            }
            catch (Exception ex)
            {
                throw new PeachException("Error executing expression [" + code + "]: " + ex.ToString(), ex);
            }
            finally
            {
                var names = scope.GetVariableNames().ToList();
                foreach (var name in names)
                {
                    scope.RemoveVariable(name);
                }
            }
        }