示例#1
0
        public static bool Compare(ScriptValue a, ScriptValue b)
        {
            if (a == null && b == null)
            {
                return(true);
            }
            if (a == null || b == null)
            {
                return(false);
            }
            if (a.GetValueType() != b.GetValueType())
            {
                return(false);
            }
            var type = a.GetValueType();

            switch (type)
            {
            case ScriptValueType.Null:
                return(true);

            case ScriptValueType.Number:
                return(System.Math.Abs((double)a.GetValue() - (double)b.GetValue()) < double.Epsilon);

            case ScriptValueType.String:
                return((string)a.GetValue() == (string)b.GetValue());

            case ScriptValueType.Bool:
                return((bool)a.GetValue() == (bool)b.GetValue());
            }
            return(false);
        }
示例#2
0
        public static bool MoreEqual(ScriptValue a, ScriptValue b)
        {
            if (a == null || b == null)
            {
                return(false);
            }
            if (a.GetValueType() != b.GetValueType())
            {
                return(false);
            }
            var type = a.GetValueType();

            switch (type)
            {
            case ScriptValueType.Number:
                return((double)a.GetValue() >= (double)b.GetValue());
            }
            return(false);
        }
示例#3
0
        public static bool Execute(string src, ScriptMethod space, out ScriptValue result)
        {
            result = ScriptValue.NULL;

            var tempSrc = src.Trim();

            // value
            if (ScriptValue.TryParse(tempSrc, space, out result))
            {
                return(true);
            }

            // check validity
            var fpbPos = tempSrc.IndexOf(Grammar.FPB);
            var fpePos = tool.GrammarTool.ReadPairSignPos(tempSrc, fpbPos + 1, Grammar.FPB, Grammar.FPE);

            if (fpbPos == -1 && fpePos != -1)
            {
                return(false);
            }
            if (fpbPos != -1 && fpbPos >= fpePos)
            {
                return(false);
            }

            // object
            var findObj = space.FindObject(tempSrc);

            if (findObj != null)
            {
                result = findObj.GetValue();
                return(true);
            }

            // logic expression
            do
            {
                var srcCondition = tempSrc;

                int    comparePos  = -1;
                string compareSign = "";
                foreach (var tempCmpSign in Grammar.COMPARE_SIGNS)
                {
                    comparePos = srcCondition.IndexOf(tempCmpSign);
                    if (comparePos != -1)
                    {
                        compareSign = tempCmpSign;
                        break;
                    }
                }

                if (comparePos == -1)
                {
                    break;
                }

                var left  = srcCondition.Substring(0, comparePos).Trim();
                var right = srcCondition.Substring(comparePos + compareSign.Length, srcCondition.Length - comparePos - compareSign.Length).Trim();
                if (string.IsNullOrEmpty(left) || string.IsNullOrEmpty(right))
                {
                    return(false);
                }

                ScriptValue leftValue  = null;
                ScriptValue rightValue = null;
                if (!Execute(left, space, out leftValue))
                {
                    return(false);
                }
                if (!Execute(right, space, out rightValue))
                {
                    return(false);
                }
                if (leftValue.GetValueType() != rightValue.GetValueType())
                {
                    return(false);
                }
                bool bCondition = _Compare(leftValue, rightValue, compareSign);
                result = ScriptValue.Create(bCondition);
                return(true);
            } while (false);

            // math expression
            do
            {
                var calcSrc         = tempSrc;
                var mathSign        = new char[] { '+', '-', '*', '/', '(', ')', '%', '^' };
                var highSign        = new char[] { '*', '/', '%', '^' };
                var lowSign         = new char[] { '+', '-' };
                var findMathSignPos = calcSrc.IndexOfAny(mathSign);
                var findSSSignPos   = calcSrc.IndexOf(Grammar.SS);
                if (findSSSignPos != -1 && findSSSignPos < findMathSignPos)
                {
                    var findSSSignPosEnd = calcSrc.IndexOf(Grammar.SS, findSSSignPos + 1);
                    if (findSSSignPosEnd != -1 && findSSSignPosEnd > findMathSignPos)
                    {
                        break;
                    }
                }
                if (findMathSignPos == -1)
                {
                    break;
                }
                while (true)
                {
                    var leftBracket = calcSrc.IndexOf('(');
                    if (leftBracket == -1)
                    {
                        break;
                    }
                    var rightBracket = tool.GrammarTool.ReadPairSignPos(calcSrc, leftBracket + 1, '(', ')');
                    if (rightBracket == -1)
                    {
                        return(false);
                    }
                    var         srcBracket   = calcSrc.Substring(leftBracket + 1, rightBracket - leftBracket - 1);
                    ScriptValue bracketValue = null;
                    if (!Execute(srcBracket, space, out bracketValue))
                    {
                        return(false);
                    }
                    if (bracketValue.GetValueType() != ScriptValueType.Number)
                    {
                        return(false);
                    }
                    calcSrc = calcSrc.Insert(rightBracket + 1, bracketValue.ToString());
                    calcSrc = calcSrc.Remove(leftBracket, rightBracket - leftBracket + 1);
                }
                while (true)
                {
                    var highSignPos = calcSrc.IndexOfAny(highSign);
                    if (highSignPos == -1)
                    {
                        break;
                    }
                    if (highSignPos == 0)
                    {
                        return(false);
                    }
                    char   tempSign   = calcSrc[highSignPos];
                    var    findOL     = calcSrc.LastIndexOfAny(mathSign, highSignPos - 1);
                    int    startPosOL = 0;
                    int    endPosOR   = 0;
                    string srcOL      = "";
                    if (findOL == -1)
                    {
                        srcOL      = calcSrc.Substring(0, highSignPos);
                        startPosOL = 0;
                    }
                    else
                    {
                        if (calcSrc[findOL] == '-')
                        {
                            if (findOL == 0)
                            {
                                --findOL;
                            }
                            else
                            {
                                var checkSignPos = calcSrc.IndexOfAny(mathSign, 0, findOL);
                                if (checkSignPos != -1)
                                {
                                    var checkStr = calcSrc.Substring(checkSignPos + 1, findOL).Trim();
                                    if (string.IsNullOrEmpty(checkStr))
                                    {
                                        findOL = checkSignPos;
                                    }
                                }
                            }
                        }
                        srcOL      = calcSrc.Substring(findOL + 1, highSignPos - findOL - 1);
                        startPosOL = findOL + 1;
                    }
                    ScriptValue valueOL = null;
                    if (!Execute(srcOL, space, out valueOL))
                    {
                        return(false);
                    }
                    if (valueOL.GetValueType() != ScriptValueType.Number)
                    {
                        return(false);
                    }

                    var    findOR = calcSrc.IndexOfAny(mathSign, highSignPos + 1);
                    string srcOR  = "";
                    if (findOR == -1)
                    {
                        srcOR    = calcSrc.Substring(highSignPos + 1);
                        endPosOR = calcSrc.Length;
                    }
                    else
                    {
                        srcOR = calcSrc.Substring(highSignPos + 1, findOR - highSignPos - 1);
                        if (string.IsNullOrEmpty(srcOR.Trim()))
                        {
                            if (calcSrc[findOR] == '-')
                            {
                                var checkSignPos = calcSrc.IndexOfAny(mathSign, findOR + 1);
                                if (checkSignPos != -1)
                                {
                                    findOR = checkSignPos;
                                }
                                else
                                {
                                    findOR = calcSrc.Length;
                                }
                                srcOR = calcSrc.Substring(highSignPos + 1, findOR - highSignPos - 1);
                            }
                        }
                        endPosOR = findOR;
                    }
                    ScriptValue valueOR = null;
                    if (!Execute(srcOR, space, out valueOR))
                    {
                        return(false);
                    }
                    if (valueOR.GetValueType() != ScriptValueType.Number)
                    {
                        return(false);
                    }
                    var valueResult = _Calc((double)valueOL.GetValue(), (double)valueOR.GetValue(), tempSign);
                    calcSrc = calcSrc.Insert(endPosOR, valueResult.ToString());
                    calcSrc = calcSrc.Remove(startPosOL, endPosOR - startPosOL);
                }
                while (true)
                {
                    var lowSignPos = calcSrc.IndexOfAny(lowSign);
                    if (lowSignPos == -1)
                    {
                        break;
                    }
                    if (lowSignPos == 0)
                    {
                        if (calcSrc[lowSignPos] == '-')
                        {
                            lowSignPos = calcSrc.IndexOfAny(lowSign, lowSignPos + 1);
                            if (lowSignPos == -1)
                            {
                                break;
                            }
                        }
                        else
                        {
                            return(false);
                        }
                    }

                    char tempSign = calcSrc[lowSignPos];

                    var findOL      = calcSrc.LastIndexOfAny(mathSign, lowSignPos - 1);
                    var findOLSSEnd = calcSrc.LastIndexOf(Grammar.SS, lowSignPos - 1);
                    if (findOLSSEnd != -1 && findOLSSEnd > findOL)
                    {
                        var findOLSSBegin = calcSrc.LastIndexOf(Grammar.SS, findOLSSEnd - 1);
                        if (findOLSSBegin != -1 && findOLSSBegin < findOL)
                        {
                            findOL = -1;
                        }
                    }

                    int    startPosOL = 0;
                    int    endPosOR   = 0;
                    string srcOL      = "";
                    if (findOL == -1)
                    {
                        srcOL      = calcSrc.Substring(0, lowSignPos);
                        startPosOL = 0;
                    }
                    else
                    {
                        if (calcSrc[findOL] == '-')
                        {
                            if (findOL == 0)
                            {
                                --findOL;
                            }
                            else
                            {
                                var checkSignPos = calcSrc.IndexOfAny(mathSign, 0, findOL);
                                if (checkSignPos != -1)
                                {
                                    var checkStr = calcSrc.Substring(checkSignPos + 1, findOL).Trim();
                                    if (string.IsNullOrEmpty(checkStr))
                                    {
                                        findOL = checkSignPos;
                                    }
                                }
                            }
                        }
                        srcOL      = calcSrc.Substring(findOL + 1, lowSignPos - findOL - 1);
                        startPosOL = findOL + 1;
                    }
                    ScriptValue valueOL = null;
                    if (!Execute(srcOL, space, out valueOL))
                    {
                        return(false);
                    }

                    var    findOR = calcSrc.IndexOfAny(mathSign, lowSignPos + 1);
                    string srcOR  = "";
                    if (findOR == -1)
                    {
                        srcOR    = calcSrc.Substring(lowSignPos + 1);
                        endPosOR = calcSrc.Length;
                    }
                    else
                    {
                        srcOR = calcSrc.Substring(lowSignPos + 1, findOR - lowSignPos - 1);
                        if (string.IsNullOrEmpty(srcOR.Trim()))
                        {
                            if (calcSrc[findOR] == '-')
                            {
                                var checkSignPos = calcSrc.IndexOfAny(mathSign, findOR + 1);
                                if (checkSignPos != -1)
                                {
                                    findOR = checkSignPos;
                                }
                                else
                                {
                                    findOR = calcSrc.Length;
                                }
                                srcOR = calcSrc.Substring(lowSignPos + 1, findOR - lowSignPos - 1);
                            }
                        }
                        endPosOR = findOR;
                        endPosOR = findOR;
                    }
                    ScriptValue valueOR = null;
                    if (!Execute(srcOR, space, out valueOR))
                    {
                        return(false);
                    }
                    object valueResult = null;
                    do
                    {
                        if (tempSign == '+')
                        {
                            if ((valueOL.GetValueType() == ScriptValueType.String) || (valueOR.GetValueType() == ScriptValueType.String))
                            {
                                valueResult = "\"" + valueOL.ToString() + valueOR.ToString() + "\"";
                                break;
                            }
                        }
                        if (valueOL.GetValueType() != ScriptValueType.Number)
                        {
                            return(false);
                        }
                        if (valueOR.GetValueType() != ScriptValueType.Number)
                        {
                            return(false);
                        }
                        valueResult = _Calc((double)valueOL.GetValue(), (double)valueOR.GetValue(), tempSign);
                    } while (false);
                    calcSrc = calcSrc.Insert(endPosOR, valueResult.ToString());
                    calcSrc = calcSrc.Remove(startPosOL, endPosOR - startPosOL);
                }
                if (!Execute(calcSrc, space, out result))
                {
                    return(false);
                }
                return(true);
            } while (false);

            return(false);
        }