Пример #1
0
        public ScriptVar MathsOp(ScriptVar b, ScriptLex.LexTypes op)
        {
            ScriptVar a = this;

            char opc = (char)op;

            if (op == ScriptLex.LexTypes.TypeEqual || op == ScriptLex.LexTypes.NTypeEqual)
            {
                bool equal = ((a._flags & Flags.VarTypeMask) == (b._flags & Flags.VarTypeMask));

                if (equal)
                {
                    ScriptVar contents = a.MathsOp(b, ScriptLex.LexTypes.Equal);
                    if (!contents.GetBool())
                    {
                        equal = false;
                    }
                }

                if (op == ScriptLex.LexTypes.TypeEqual)
                {
                    return(new ScriptVar(equal));
                }

                return(new ScriptVar(!equal));
            }

            if (a.IsUndefined && b.IsUndefined)
            {
                if (op == ScriptLex.LexTypes.Equal)
                {
                    return(new ScriptVar(true));
                }
                if (op == ScriptLex.LexTypes.NEqual)
                {
                    return(new ScriptVar(false));
                }

                return(new ScriptVar());
            }

            if ((a.IsNumeric || a.IsUndefined) && (b.IsNumeric || b.IsUndefined))
            {
                if (!a.IsDouble && !b.IsDouble)
                {
                    //ints
                    int da = a.GetInt();
                    int db = b.GetInt();

                    switch (opc)
                    {
                    case '+': return(new ScriptVar(da + db));

                    case '-': return(new ScriptVar(da - db));

                    case '*': return(new ScriptVar(da * db));

                    case '/': return(new ScriptVar(da / db));

                    case '&': return(new ScriptVar(da & db));

                    case '|': return(new ScriptVar(da | db));

                    case '^': return(new ScriptVar(da ^ db));

                    case '%': return(new ScriptVar(da % db));

                    case (char)ScriptLex.LexTypes.Equal: return(new ScriptVar(da == db));

                    case (char)ScriptLex.LexTypes.NEqual: return(new ScriptVar(da != db));

                    case '<': return(new ScriptVar(da < db));

                    case (char)ScriptLex.LexTypes.LEqual: return(new ScriptVar(da <= db));

                    case '>': return(new ScriptVar(da > db));

                    case (char)ScriptLex.LexTypes.GEqual: return(new ScriptVar(da >= db));

                    default: throw new ScriptException("Operation not supported on the Int datatype");
                    }
                }
                else
                {
                    //doubles
                    double da = a.GetDouble();
                    double db = b.GetDouble();

                    switch (opc)
                    {
                    case '+': return(new ScriptVar(da + db));

                    case '-': return(new ScriptVar(da - db));

                    case '*': return(new ScriptVar(da * db));

                    case '/': return(new ScriptVar(da / db));

                    case (char)ScriptLex.LexTypes.Equal: return(new ScriptVar(da == db));

                    case (char)ScriptLex.LexTypes.NEqual: return(new ScriptVar(da != db));

                    case '<': return(new ScriptVar(da < db));

                    case (char)ScriptLex.LexTypes.LEqual: return(new ScriptVar(da <= db));

                    case '>': return(new ScriptVar(da > db));

                    case (char)ScriptLex.LexTypes.GEqual: return(new ScriptVar(da >= db));

                    default: throw new ScriptException("Operation not supported on the Int datatype");
                    }
                }
            }

            if (a.IsArray)
            {
                switch (op)
                {
                case ScriptLex.LexTypes.Equal: return(new ScriptVar(a == b));

                case ScriptLex.LexTypes.NEqual: return(new ScriptVar(a != b));

                default: throw new ScriptException("Operation not supported on the Array datatype");
                }
            }

            if (a.IsObject)
            {
                switch (op)
                {
                case ScriptLex.LexTypes.Equal: return(new ScriptVar(a == b));

                case ScriptLex.LexTypes.NEqual: return(new ScriptVar(a != b));

                default: throw new ScriptException("Operation not supported on the Object datatype");
                }
            }

            string sda = a.GetString();
            string sdb = b.GetString();

            switch (opc)
            {
            case '+': return(new ScriptVar(sda + sdb, Flags.String));

            case (char)ScriptLex.LexTypes.Equal: return(new ScriptVar(sda == sdb));

            case (char)ScriptLex.LexTypes.NEqual: return(new ScriptVar(sda != sdb));

            case '<': return(new ScriptVar(String.CompareOrdinal(sda, sdb) < 0));

            case (char)ScriptLex.LexTypes.LEqual: return(new ScriptVar((String.CompareOrdinal(sda, sdb) < 0) || sda == sdb));

            case '>': return(new ScriptVar(String.CompareOrdinal(sda, sdb) > 0));

            case (char)ScriptLex.LexTypes.GEqual: return(new ScriptVar((String.CompareOrdinal(sda, sdb) > 0) || sda == sdb));

            default: throw new ScriptException("Operation not supported on the String datatype");
            }
        }