Exemple #1
0
        static bool UpdateIfTernary(ParsingScript script, string token, char ch, List <Variable> listInput, Action <List <Variable> > listToMerge)
        {
            if (listInput.Count < 1 || ch != Constants.TERNARY_OPERATOR || token.Length > 0)
            {
                return(false);
            }

            Variable result;
            Variable arg1 = MergeList(listInput, script);

            script.MoveForwardIf(Constants.TERNARY_OPERATOR);
            double condition = arg1.AsDouble();

            if (condition != 0)
            {
                result = script.Execute(Constants.TERNARY_SEPARATOR);
                script.MoveForwardIf(Constants.TERNARY_SEPARATOR);
                Utils.SkipRestExpr(script, Constants.END_STATEMENT);
            }
            else
            {
                Utils.SkipRestExpr(script, Constants.TERNARY_SEPARATOR[0]);
                script.MoveForwardIf(Constants.TERNARY_SEPARATOR);
                result = script.Execute(Constants.NEXT_OR_END_ARRAY);
            }

            listInput.Clear();
            listInput.Add(result);
            listToMerge(listInput);

            return(true);
        }
Exemple #2
0
        public static object SqlDbTypeToVariable(SqlDbType dbType, Variable var)
        {
            switch (dbType)
            {
            case SqlDbType.SmallInt:
            case SqlDbType.Int:
            case SqlDbType.BigInt: return(var.AsInt());

            case SqlDbType.NChar:
            case SqlDbType.NText:
            case SqlDbType.NVarChar: return(var.AsString());

            case SqlDbType.Real:
            case SqlDbType.Decimal:
            case SqlDbType.SmallMoney:
            case SqlDbType.Money:
            case SqlDbType.Float: return(var.AsDouble());

            case SqlDbType.Binary:
            case SqlDbType.Bit: return(var.AsBool());

            case SqlDbType.SmallDateTime:
            case SqlDbType.Date:
            case SqlDbType.Time:
            case SqlDbType.DateTime: return(var.AsDateTime());
            }
            return(var.AsString());
        }
Exemple #3
0
        static bool UpdateIfTernary(ParsingScript script, string token, char ch, ref List <Variable> listToMerge)
        {
            if (listToMerge.Count < 1 || ch != Constants.TERNARY_OPERATOR || token.Length > 0)
            {
                return(false);
            }

            Variable arg1 = MergeList(listToMerge);

            script.MoveForwardIf(Constants.TERNARY_OPERATOR);
            Variable arg2 = script.Execute(Constants.TERNARY_SEPARATOR);

            script.MoveForwardIf(Constants.TERNARY_SEPARATOR);
            Variable arg3 = script.Execute(Constants.NEXT_OR_END_ARRAY);

            script.MoveForwardIf(Constants.NEXT_OR_END_ARRAY);

            double   condition = arg1.AsDouble();
            Variable result    = condition != 0 ? arg2 : arg3;

            listToMerge.Clear();
            listToMerge.Add(result);

            return(true);
        }
Exemple #4
0
        static async Task <bool> UpdateIfTernaryAsync(ParsingScript script, string token, char ch, List <Variable> listInput, Action <List <Variable> > listToMerge)
        {
            if (listInput.Count < 1 || ch != Constants.TERNARY_OPERATOR || token.Length > 0)
            {
                return(false);
            }

            Variable arg1 = MergeList(listInput);

            script.MoveForwardIf(Constants.TERNARY_OPERATOR);
            Variable arg2 = await script.ExecuteAsync(Constants.TERNARY_SEPARATOR);

            script.MoveForwardIf(Constants.TERNARY_SEPARATOR);
            Variable arg3 = await script.ExecuteAsync(Constants.NEXT_OR_END_ARRAY);

            script.MoveForwardIf(Constants.NEXT_OR_END_ARRAY);

            double   condition = arg1.AsDouble();
            Variable result    = condition != 0 ? arg2 : arg3;

            listInput.Clear();
            listInput.Add(result);
            listToMerge(listInput);

            return(true);
        }
Exemple #5
0
        public static double GetSafeDouble(List <Variable> args, int index, double defaultValue = 0.0)
        {
            if (args.Count <= index)
            {
                return(defaultValue);
            }

            Variable numberVar = args[index];

            if (numberVar.Type != Variable.VarType.NUMBER)
            {
                double num;
                if (!CanConvertToDouble(numberVar.String, out num))
                {
                    throw new ArgumentException("Expected a double instead of [" + numberVar.AsString() + "]");
                }
                return(num);
            }
            return(numberVar.AsDouble());
        }
Exemple #6
0
        public static object SqlDbTypeToType(DbType dbType, Variable var)
        {
            switch (dbType)
            {
            case DbType.Int16:
            case DbType.Int32:
            case DbType.Int64:    return(var.AsInt());

            case DbType.Double:
            case DbType.Single:   return(var.AsDouble());

            case DbType.String:   return(var.AsString());

            case DbType.Byte:
            case DbType.Boolean:  return(var.AsBool());

            case DbType.DateTime: return(var.AsDateTime());
            }
            return(var.AsString());
        }
Exemple #7
0
        public void Sort()
        {
            if (Tuple == null || Tuple.Count <= 1)
            {
                return;
            }

            List <double> numbers = new List <double>();
            List <string> strings = new List <string>();

            for (int i = 0; i < Tuple.Count; i++)
            {
                Variable arg = Tuple[i];
                if (arg.Tuple != null)
                {
                    arg.Sort();
                }
                else if (arg.Type == VarType.NUMBER)
                {
                    numbers.Add(arg.AsDouble());
                }
                else
                {
                    strings.Add(arg.AsString());
                }
            }
            List <Variable> newTuple = new List <Variable>(Tuple.Count);

            numbers.Sort();
            strings.Sort();

            for (int i = 0; i < numbers.Count; i++)
            {
                newTuple.Add(new Variable(numbers[i]));
            }
            for (int i = 0; i < strings.Count; i++)
            {
                newTuple.Add(new Variable(strings[i]));
            }
            Tuple = newTuple;
        }
Exemple #8
0
        public static double GetSafeDouble(List <Variable> args, int index, double defaultValue = 0.0)
        {
            if (args.Count <= index)
            {
                return(defaultValue);
            }

            Variable numberVar = args[index];

            if (numberVar.Type != Variable.VarType.NUMBER)
            {
                double num;
                if (!Double.TryParse(numberVar.String, NumberStyles.Number |
                                     NumberStyles.AllowExponent |
                                     NumberStyles.Float,
                                     CultureInfo.InvariantCulture, out num))
                {
                    throw new ArgumentException("Expected a double instead of [" + numberVar.AsString() + "]");
                }
                return(num);
            }
            return(numberVar.AsDouble());
        }
Exemple #9
0
        public static double GetDouble(string paramName, ParsingScript script = null)
        {
            Variable result = GetVar(paramName, script);

            return(result.AsDouble());
        }
Exemple #10
0
        public static async Task <double> GetDouble(string paramName, ParsingScript script = null)
        {
            Variable result = await GetVar(paramName, script);

            return(result.AsDouble());
        }
Exemple #11
0
        Variable GetCoreProperty(string propName, ParsingScript script = null)
        {
            Variable result = Variable.EmptyInstance;

            if (m_propertyMap.TryGetValue(propName, out result) ||
                m_propertyMap.TryGetValue(GetRealName(propName), out result))
            {
                return(result);
            }
            else if (propName.Equals(Constants.OBJECT_PROPERTIES, StringComparison.OrdinalIgnoreCase))
            {
                return(new Variable(GetProperties()));
            }
            else if (propName.Equals(Constants.OBJECT_TYPE, StringComparison.OrdinalIgnoreCase))
            {
                return(new Variable((int)Type));
            }
            else if (propName.Equals(Constants.SIZE, StringComparison.OrdinalIgnoreCase))
            {
                return(new Variable(GetSize()));
            }
            else if (propName.Equals(Constants.UPPER, StringComparison.OrdinalIgnoreCase))
            {
                return(new Variable(AsString().ToUpper()));
            }
            else if (propName.Equals(Constants.LOWER, StringComparison.OrdinalIgnoreCase))
            {
                return(new Variable(AsString().ToLower()));
            }
            else if (propName.Equals(Constants.STRING, StringComparison.OrdinalIgnoreCase))
            {
                return(new Variable(AsString()));
            }
            else if (propName.Equals(Constants.FIRST, StringComparison.OrdinalIgnoreCase))
            {
                if (Tuple != null && Tuple.Count > 0)
                {
                    return(Tuple[0]);
                }
                return(AsString().Length > 0 ? new Variable("" + AsString()[0]) : Variable.EmptyInstance);
            }
            else if (propName.Equals(Constants.LAST, StringComparison.OrdinalIgnoreCase))
            {
                if (Tuple != null && Tuple.Count > 0)
                {
                    return(Tuple.Last <Variable>());
                }
                return(AsString().Length > 0 ? new Variable("" + AsString().Last <char>()) : Variable.EmptyInstance);
            }
            else if (script != null && propName.Equals(Constants.INDEX_OF, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 1, propName);

                string           search    = Utils.GetSafeString(args, 0);
                int              startFrom = Utils.GetSafeInt(args, 1, 0);
                string           param     = Utils.GetSafeString(args, 2, "no_case");
                StringComparison comp      = param.Equals("case", StringComparison.OrdinalIgnoreCase) ?
                                             StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;

                return(new Variable(AsString().IndexOf(search, startFrom, comp)));
            }
            else if (script != null && propName.Equals(Constants.SUBSTRING, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 1, propName);

                int startFrom = Utils.GetSafeInt(args, 0, 0);
                int length    = Utils.GetSafeInt(args, 1, AsString().Length);
                length = Math.Min(length, AsString().Length - startFrom);

                return(new Variable(AsString().Substring(startFrom, length)));
            }
            else if (script != null && propName.Equals(Constants.REVERSE, StringComparison.OrdinalIgnoreCase))
            {
                script.GetFunctionArgs();
                if (Tuple != null)
                {
                    Tuple.Reverse();
                }
                else if (Type == VarType.STRING)
                {
                    char[] charArray = AsString().ToCharArray();
                    Array.Reverse(charArray);
                    String = new string(charArray);
                }

                return(this);
            }
            else if (script != null && propName.Equals(Constants.SORT, StringComparison.OrdinalIgnoreCase))
            {
                script.GetFunctionArgs();
                Sort();

                return(this);
            }
            else if (script != null && propName.Equals(Constants.SPLIT, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args   = script.GetFunctionArgs();
                string          sep    = Utils.GetSafeString(args, 0, " ");
                var             option = Utils.GetSafeString(args, 1);

                return(TokenizeFunction.Tokenize(AsString(), sep, option));
            }
            else if (script != null && propName.Equals(Constants.JOIN, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                string          sep  = Utils.GetSafeString(args, 0, " ");
                if (Tuple == null)
                {
                    return(new Variable(AsString()));
                }

                var join = string.Join(sep, Tuple);
                return(new Variable(join));
            }
            else if (script != null && propName.Equals(Constants.ADD, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 1, propName);
                Variable var = Utils.GetSafeVariable(args, 0);
                if (Tuple != null)
                {
                    Tuple.Add(var);
                }
                else if (Type == VarType.NUMBER)
                {
                    Value += var.AsDouble();
                }
                else
                {
                    String += var.AsString();
                }
                return(var);
            }
            else if (script != null && propName.Equals(Constants.AT, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 1, propName);
                int at = Utils.GetSafeInt(args, 0);

                if (Tuple != null && Tuple.Count > 0)
                {
                    return(Tuple.Count > at ? Tuple[at] : Variable.EmptyInstance);
                }
                string str = AsString();
                return(str.Length > at ? new Variable("" + str[at]) : Variable.EmptyInstance);
            }
            else if (script != null && propName.Equals(Constants.REPLACE, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 2, propName);
                string oldVal = Utils.GetSafeString(args, 0);
                string newVal = Utils.GetSafeString(args, 1);

                return(new Variable(AsString().Replace(oldVal, newVal)));
            }
            else if (propName.Equals(Constants.EMPTY_WHITE, StringComparison.OrdinalIgnoreCase))
            {
                bool isEmpty = string.IsNullOrWhiteSpace(AsString());
                return(new Variable(isEmpty));
            }
            else if (script != null && propName.Equals(Constants.REPLACE_TRIM, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 2, propName);
                string currentValue = AsString();

                for (int i = 0; i < args.Count; i += 2)
                {
                    string oldVal = Utils.GetSafeString(args, i);
                    string newVal = Utils.GetSafeString(args, i + 1);
                    currentValue = currentValue.Replace(oldVal, newVal);
                }

                return(new Variable(currentValue.Trim()));
            }
            else if (script != null && propName.Equals(Constants.CONTAINS, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 1, propName);
                string           val   = Utils.GetSafeString(args, 0);
                string           param = Utils.GetSafeString(args, 1, "no_case");
                StringComparison comp  = param.Equals("case", StringComparison.OrdinalIgnoreCase) ?
                                         StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;

                bool contains = false;
                if (Type == Variable.VarType.ARRAY)
                {
                    string lower = val.ToLower();
                    contains = m_dictionary != null && m_dictionary.ContainsKey(lower);
                    if (!contains && Tuple != null)
                    {
                        foreach (var item in Tuple)
                        {
                            if (item.AsString().Equals(val, comp))
                            {
                                contains = true;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    contains = val != "" && AsString().IndexOf(val, comp) >= 0;
                }
                return(new Variable(contains));
            }
            else if (script != null && propName.Equals(Constants.EQUALS, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 1, propName);
                string           val   = Utils.GetSafeString(args, 0);
                string           param = Utils.GetSafeString(args, 1, "no_case");
                StringComparison comp  = param.Equals("case", StringComparison.OrdinalIgnoreCase) ?
                                         StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;

                return(new Variable(AsString().Equals(val, comp)));
            }
            else if (script != null && propName.Equals(Constants.STARTS_WITH, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 1, propName);
                string           val   = Utils.GetSafeString(args, 0);
                string           param = Utils.GetSafeString(args, 1, "no_case");
                StringComparison comp  = param.Equals("case", StringComparison.OrdinalIgnoreCase) ?
                                         StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;

                return(new Variable(AsString().StartsWith(val, comp)));
            }
            else if (script != null && propName.Equals(Constants.ENDS_WITH, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> args = script.GetFunctionArgs();
                Utils.CheckArgs(args.Count, 1, propName);
                string           val   = Utils.GetSafeString(args, 0);
                string           param = Utils.GetSafeString(args, 1, "no_case");
                StringComparison comp  = param.Equals("case", StringComparison.OrdinalIgnoreCase) ?
                                         StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;

                return(new Variable(AsString().EndsWith(val, comp)));
            }
            else if (script != null && propName.Equals(Constants.TRIM, StringComparison.OrdinalIgnoreCase))
            {
                script.GetFunctionArgs();
                return(new Variable(AsString().Trim()));
            }
            else if (propName.Equals(Constants.KEYS, StringComparison.OrdinalIgnoreCase))
            {
                List <Variable> results = GetAllKeys();
                return(new Variable(results));
            }

            return(result);
        }
Exemple #12
0
        private static void MergeNumbers(Variable leftCell, Variable rightCell, ParsingScript script)
        {
            if (rightCell.Type != Variable.VarType.NUMBER)
            {
                rightCell.Value = rightCell.AsDouble();
            }
            switch (leftCell.Action)
            {
            case "%":
                leftCell.Value %= rightCell.Value;
                break;

            case "*":
                leftCell.Value *= rightCell.Value;
                break;

            case "/":
                if (rightCell.Value == 0.0)
                {
                    throw new ArgumentException("Division by zero");
                }
                leftCell.Value /= rightCell.Value;
                break;

            case "+":
                if (rightCell.Type != Variable.VarType.NUMBER)
                {
                    leftCell.String = leftCell.AsString() + rightCell.String;
                }
                else
                {
                    leftCell.Value += rightCell.Value;
                }
                break;

            case "-":
                leftCell.Value -= rightCell.Value;
                break;

            case "<":
                leftCell.Value = Convert.ToDouble(leftCell.Value < rightCell.Value);
                break;

            case ">":
                leftCell.Value = Convert.ToDouble(leftCell.Value > rightCell.Value);
                break;

            case "<=":
                leftCell.Value = Convert.ToDouble(leftCell.Value <= rightCell.Value);
                break;

            case ">=":
                leftCell.Value = Convert.ToDouble(leftCell.Value >= rightCell.Value);
                break;

            case "==":
                leftCell.Value = Convert.ToDouble(leftCell.Value == rightCell.Value);
                break;

            case "!=":
                leftCell.Value = Convert.ToDouble(leftCell.Value != rightCell.Value);
                break;

            case "&":
                leftCell.Value = (int)leftCell.Value & (int)rightCell.Value;
                break;

            case "^":
                leftCell.Value = (int)leftCell.Value ^ (int)rightCell.Value;
                break;

            case "|":
                leftCell.Value = (int)leftCell.Value | (int)rightCell.Value;
                break;

            case "&&":
                leftCell.Value = Convert.ToDouble(
                    Convert.ToBoolean(leftCell.Value) && Convert.ToBoolean(rightCell.Value));
                break;

            case "||":
                leftCell.Value = Convert.ToDouble(
                    Convert.ToBoolean(leftCell.Value) || Convert.ToBoolean(rightCell.Value));
                break;

            case "**":
                leftCell.Value = Math.Pow(leftCell.Value, rightCell.Value);
                break;

            case ")":
                Utils.ThrowErrorMsg("Can't process last token [" + rightCell.Value + "] in the expression.",
                                    script, script.Current.ToString());
                break;

            default:
                Utils.ThrowErrorMsg("Can't process operation [" + leftCell.Action + "] in the expression.",
                                    script, leftCell.Action);
                break;
            }
        }
Exemple #13
0
        private static void MergeNumbers(Variable leftCell, Variable rightCell)
        {
            if (rightCell.Type != Variable.VarType.NUMBER)
            {
                rightCell.Value = rightCell.AsDouble();
            }
            switch (leftCell.Action)
            {
            case "%":
                leftCell.Value %= rightCell.Value;
                break;

            case "*":
                leftCell.Value *= rightCell.Value;
                break;

            case "/":
                if (rightCell.Value == 0.0)
                {
                    throw new ArgumentException("Division by zero");
                }
                leftCell.Value /= rightCell.Value;
                break;

            case "+":
                if (rightCell.Type != Variable.VarType.NUMBER)
                {
                    leftCell.String = leftCell.AsString() + rightCell.String;
                }
                else
                {
                    leftCell.Value += rightCell.Value;
                }
                break;

            case "-":
                leftCell.Value -= rightCell.Value;
                break;

            case "<":
                leftCell.Value = Convert.ToDouble(leftCell.Value < rightCell.Value);
                break;

            case ">":
                leftCell.Value = Convert.ToDouble(leftCell.Value > rightCell.Value);
                break;

            case "<=":
                leftCell.Value = Convert.ToDouble(leftCell.Value <= rightCell.Value);
                break;

            case ">=":
                leftCell.Value = Convert.ToDouble(leftCell.Value >= rightCell.Value);
                break;

            case "==":
                leftCell.Value = Convert.ToDouble(leftCell.Value == rightCell.Value);
                break;

            case "!=":
                leftCell.Value = Convert.ToDouble(leftCell.Value != rightCell.Value);
                break;

            case "&":
                leftCell.Value = (int)leftCell.Value & (int)rightCell.Value;
                break;

            case "^":
                leftCell.Value = (int)leftCell.Value ^ (int)rightCell.Value;
                break;

            case "|":
                leftCell.Value = (int)leftCell.Value | (int)rightCell.Value;
                break;

            case "&&":
                leftCell.Value = Convert.ToDouble(
                    Convert.ToBoolean(leftCell.Value) && Convert.ToBoolean(rightCell.Value));
                break;

            case "||":
                leftCell.Value = Convert.ToDouble(
                    Convert.ToBoolean(leftCell.Value) || Convert.ToBoolean(rightCell.Value));
                break;
            }
        }